Skip to content

Commit

Permalink
[#29] Improve file helper
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangmirs committed Jul 29, 2022
1 parent b0c23e2 commit e4b828e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 66 deletions.
4 changes: 2 additions & 2 deletions src/commands/generate/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -82,7 +82,7 @@ export default class Generator extends Command {
private async postProcess(generalOptions: GeneralOptions): Promise<void> {
try {
if (await detectTerraform()) {
await formatCode(getTargetDir(generalOptions.projectName));
await formatCode(getProjectPath(generalOptions.projectName));
}
} catch (error) {
console.error(error);
Expand Down
55 changes: 18 additions & 37 deletions src/helpers/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import {
appendToFile,
copy,
createFile,
getSourcePath,
getTargetDir,
getTargetPath,
getTemplateFilePath,
getProjectPath,
getProjectFilePath,
getTemplatePath,
injectToFile,
remove,
renameFile,
} from './file';

jest.mock('fs-extra');
Expand All @@ -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));
});
Expand Down Expand Up @@ -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));
});
Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand Down
41 changes: 14 additions & 27 deletions src/helpers/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
existsSync,
readFileSync,
removeSync,
renameSync,
writeFileSync,
} from 'fs-extra';

Expand All @@ -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 => {
Expand All @@ -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);
};

Expand All @@ -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);
};
Expand All @@ -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) {
Expand All @@ -66,30 +65,19 @@ 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,
projectName: string,
{ 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');
Expand All @@ -113,14 +101,13 @@ const injectToFile = (
};

export {
getTargetDir,
getTargetPath,
getTemplatePath,
getSourcePath,
appendToFile,
copy,
remove,
createFile,
getProjectFilePath,
getProjectPath,
getTemplateFilePath,
getTemplatePath,
injectToFile,
renameFile,
remove,
};

0 comments on commit e4b828e

Please sign in to comment.