From e4b828e5aa9b2d76a5269c0b2bf7da80cc3604a5 Mon Sep 17 00:00:00 2001 From: Hoang Mirs Date: Fri, 29 Jul 2022 13:11:11 +0700 Subject: [PATCH] [#29] Improve file helper --- src/commands/generate/index.ts | 4 +-- src/helpers/file.test.ts | 55 +++++++++++----------------------- src/helpers/file.ts | 41 +++++++++---------------- 3 files changed, 34 insertions(+), 66 deletions(-) diff --git a/src/commands/generate/index.ts b/src/commands/generate/index.ts index 01b4d661..3ab52a74 100644 --- a/src/commands/generate/index.ts +++ b/src/commands/generate/index.ts @@ -1,7 +1,7 @@ import { Command } from '@oclif/core'; import { prompt } from 'inquirer'; -import { getTargetDir } from '../../helpers/file'; +import { getProjectPath } from '../../helpers/file'; import { detectTerraform, formatCode } from '../../helpers/terraform'; import { generateAwsTemplate } from '../../templates/aws'; @@ -82,7 +82,7 @@ export default class Generator extends Command { private async postProcess(generalOptions: GeneralOptions): Promise { try { if (await detectTerraform()) { - await formatCode(getTargetDir(generalOptions.projectName)); + await formatCode(getProjectPath(generalOptions.projectName)); } } catch (error) { console.error(error); diff --git a/src/helpers/file.test.ts b/src/helpers/file.test.ts index 3c338fce..a2369b56 100644 --- a/src/helpers/file.test.ts +++ b/src/helpers/file.test.ts @@ -7,13 +7,12 @@ import { appendToFile, copy, createFile, - getSourcePath, - getTargetDir, - getTargetPath, + getTemplateFilePath, + getProjectPath, + getProjectFilePath, getTemplatePath, injectToFile, remove, - renameFile, } from './file'; jest.mock('fs-extra'); @@ -23,25 +22,25 @@ describe('File helpers', () => { jest.clearAllMocks(); }); - describe('getTargetDir', () => { + describe('getProjectPath', () => { describe('given projectName', () => { it('returns the correct target directory', () => { const projectName = 'projectName'; - const targetDir = getTargetDir(projectName); + const targetDir = getProjectPath(projectName); expect(targetDir).toBe(path.join(process.cwd(), projectName)); }); }); }); - describe('getTargetPath', () => { + describe('getProjectFilePath', () => { describe('given file name and projectName', () => { it('returns the correct target path', () => { const file = 'file'; const projectName = 'projectName'; - const targetPath = getTargetPath(file, projectName); + const targetPath = getProjectFilePath(file, projectName); expect(targetPath).toBe(path.join(process.cwd(), projectName, file)); }); @@ -90,12 +89,12 @@ describe('File helpers', () => { }); }); - describe('getSourcePath', () => { + describe('getTemplateFilePath', () => { describe('given file name', () => { it('returns the correct source path', () => { const file = 'example.txt'; - const sourcePath = getSourcePath(file); + const sourcePath = getTemplateFilePath(file); expect(sourcePath).toBe(path.join(getTemplatePath(), file)); }); @@ -108,8 +107,8 @@ describe('File helpers', () => { const source = 'sourceDir'; const target = 'targetDir'; const projectName = 'projectName'; - const sourcePath = getSourcePath(source); - const targetPath = getTargetPath(target, projectName); + const sourcePath = getTemplateFilePath(source); + const targetPath = getProjectFilePath(target, projectName); const copySpy = jest.spyOn(fs, 'copySync'); @@ -126,7 +125,7 @@ describe('File helpers', () => { const target = 'targetFile.txt'; const content = 'creating content'; const projectName = 'projectName'; - const targetPath = getTargetPath(target, projectName); + const targetPath = getProjectFilePath(target, projectName); const createSpy = jest.spyOn(fs, 'writeFileSync'); @@ -142,7 +141,7 @@ describe('File helpers', () => { it('removes the target file', () => { const target = 'targetFile.txt'; const projectName = 'projectName'; - const targetPath = getTargetPath(target, projectName); + const targetPath = getProjectFilePath(target, projectName); const removeSpy = jest.spyOn(fs, 'removeSync'); @@ -156,7 +155,7 @@ describe('File helpers', () => { it('removes the target directory', () => { const target = 'targetDir'; const projectName = 'projectName'; - const targetPath = getTargetPath(target, projectName); + const targetPath = getProjectFilePath(target, projectName); const removeSpy = jest.spyOn(fs, 'removeSync'); @@ -167,31 +166,13 @@ describe('File helpers', () => { }); }); - describe('renameFile', () => { - describe('given source and target', () => { - it('renames the source file to the target file', () => { - const source = 'sourceFile.txt'; - const target = 'targetFile.txt'; - const projectName = 'projectName'; - const sourcePath = getTargetPath(source, projectName); - const targetPath = getTargetPath(target, projectName); - - const renameSpy = jest.spyOn(fs, 'renameSync'); - - renameFile(source, target, projectName); - - expect(renameSpy).toHaveBeenCalledWith(sourcePath, targetPath); - }); - }); - }); - describe('appendToFile', () => { describe('given target file and content', () => { it('appends content to the target file', () => { const target = 'targetFile.txt'; const content = 'appending content'; const projectName = 'projectName'; - const targetPath = getTargetDir(projectName); + const targetPath = getProjectPath(projectName); const appendSpy = jest.spyOn(fs, 'appendFileSync'); @@ -213,7 +194,7 @@ describe('File helpers', () => { const initialContent = 'initial content'; const content = 'injecting content'; const projectName = 'projectName'; - const targetPath = getTargetDir(projectName); + const targetPath = getProjectPath(projectName); const injectSpy = jest.spyOn(fs, 'writeFileSync'); @@ -236,7 +217,7 @@ describe('File helpers', () => { const initialContent = 'initial content'; const content = 'injecting content'; const projectName = 'projectName'; - const targetPath = getTargetDir(projectName); + const targetPath = getProjectPath(projectName); const injectSpy = jest.spyOn(fs, 'writeFileSync'); @@ -259,7 +240,7 @@ describe('File helpers', () => { const initialContent = 'initial content'; const content = 'injecting content'; const projectName = 'projectName'; - const targetPath = getTargetDir(projectName); + const targetPath = getProjectPath(projectName); const injectSpy = jest.spyOn(fs, 'writeFileSync'); diff --git a/src/helpers/file.ts b/src/helpers/file.ts index 791caa14..b718be50 100644 --- a/src/helpers/file.ts +++ b/src/helpers/file.ts @@ -6,7 +6,6 @@ import { existsSync, readFileSync, removeSync, - renameSync, writeFileSync, } from 'fs-extra'; @@ -17,12 +16,12 @@ interface InjectToFileOptions { const ROOT_DIR = path.join(__dirname, '..', '..'); -const getTargetDir = (projectName: string): string => { +const getProjectPath = (projectName: string): string => { return path.join(process.cwd(), projectName); }; -const getTargetPath = (file: string, projectName: string): string => { - return path.join(getTargetDir(projectName), file); +const getProjectFilePath = (file: string, projectName: string): string => { + return path.join(getProjectPath(projectName), file); }; const getTemplatePath = (): string => { @@ -31,7 +30,7 @@ const getTemplatePath = (): string => { return path.join(ROOT_DIR, templateDir); }; -const getSourcePath = (file: string): string => { +const getTemplateFilePath = (file: string): string => { return path.join(getTemplatePath(), file); }; @@ -40,14 +39,14 @@ const appendToFile = ( content: string, projectName: string ): void => { - const targetPath = getTargetPath(target, projectName); + const targetPath = getProjectFilePath(target, projectName); appendFileSync(targetPath, content); }; const copy = (source: string, target: string, projectName: string): void => { const sourcePath = path.join(getTemplatePath(), source); - const targetPath = getTargetPath(target, projectName); + const targetPath = getProjectFilePath(target, projectName); copySync(sourcePath, targetPath); }; @@ -57,7 +56,7 @@ const createFile = ( content: string, projectName: string ): void => { - const targetPath = getTargetPath(target, projectName); + const targetPath = getProjectFilePath(target, projectName); const targetExists = existsSync(targetPath); if (!targetExists) { @@ -66,22 +65,11 @@ const createFile = ( }; const remove = (target: string, projectName: string): void => { - const targetPath = getTargetPath(target, projectName); + const targetPath = getProjectFilePath(target, projectName); removeSync(targetPath); }; -const renameFile = ( - source: string, - target: string, - projectName: string -): void => { - const sourcePath = getTargetPath(source, projectName); - const targetPath = getTargetPath(target, projectName); - - renameSync(sourcePath, targetPath); -}; - const injectToFile = ( target: string, content: string, @@ -89,7 +77,7 @@ const injectToFile = ( { insertBefore = '', insertAfter = '' }: InjectToFileOptions = {} ): void => { const targetPath = - projectName !== '' ? getTargetPath(target, projectName) : target; + projectName !== '' ? getProjectFilePath(target, projectName) : target; const data = readFileSync(targetPath, 'utf8'); const lines = data.toString().split('\n'); @@ -113,14 +101,13 @@ const injectToFile = ( }; export { - getTargetDir, - getTargetPath, - getTemplatePath, - getSourcePath, appendToFile, copy, - remove, createFile, + getProjectFilePath, + getProjectPath, + getTemplateFilePath, + getTemplatePath, injectToFile, - renameFile, + remove, };