add template config file to root

This commit is contained in:
Mark Kaylor 2021-05-26 12:15:43 +02:00
parent c2d438ba88
commit a6eb49b75d
2 changed files with 44 additions and 0 deletions

View File

@ -3,6 +3,7 @@ jest.mock('fs-extra', () => ({
ensureDir: jest.fn(() => Promise.resolve()), ensureDir: jest.fn(() => Promise.resolve()),
copy: jest.fn(() => Promise.resolve()), copy: jest.fn(() => Promise.resolve()),
pathExists: jest.fn(() => Promise.resolve()), pathExists: jest.fn(() => Promise.resolve()),
writeJSON: jest.fn(() => Promise.resolve()),
})); }));
const { resolve, join } = require('path'); const { resolve, join } = require('path');
@ -42,6 +43,17 @@ describe('generate:template command', () => {
} }
); );
it('creates a json config file', async () => {
fse.pathExists.mockReturnValue(false);
const directory = '../test-dir';
const templatePath = resolve(directory);
await exportTemplate(directory);
expect(fse.pathExists).toHaveBeenCalledWith(join(templatePath, 'template.json'));
expect(fse.writeJSON).toHaveBeenCalledWith(join(templatePath, 'template.json'), {});
});
describe('handles prompt input', () => { describe('handles prompt input', () => {
it('updates directory if confirmed', async () => { it('updates directory if confirmed', async () => {
fse.pathExists.mockReturnValue(true); fse.pathExists.mockReturnValue(true);
@ -62,6 +74,17 @@ describe('generate:template command', () => {
expect(fse.copy).toHaveBeenCalled(); expect(fse.copy).toHaveBeenCalled();
}); });
it('does not replace existing config file', async () => {
fse.pathExists.mockReturnValue(true);
jest.spyOn(inquirer, 'prompt').mockImplementationOnce(() => ({ confirm: true }));
const directory = '../test-dir';
const templatePath = resolve(directory);
await exportTemplate(directory);
expect(fse.pathExists).toHaveBeenCalledWith(join(templatePath, 'template.json'));
expect(fse.writeJSON).not.toHaveBeenCalled();
});
it('exits if not confirmed', async () => { it('exits if not confirmed', async () => {
fse.pathExists.mockReturnValue(true); fse.pathExists.mockReturnValue(true);
jest.spyOn(console, 'error').mockImplementation(() => {}); jest.spyOn(console, 'error').mockImplementation(() => {});

View File

@ -56,10 +56,31 @@ async function copyContent(templatePath) {
}); });
} }
async function writeTemplateJson(rootPath) {
try {
await fse.writeJSON(join(rootPath, 'template.json'), {});
console.log(`${chalk.green('success')}: create JSON config file`);
} catch (error) {
console.error(`${chalk.red('error')}: ${error.message}`);
}
}
async function configExists(templatePath) {
const jsonConfig = await fse.pathExists(join(templatePath, 'template.json'));
const functionConfig = await fse.pathExists(join(templatePath, 'template.js'));
return jsonConfig || functionConfig;
}
module.exports = async function generateTemplate(directory) { module.exports = async function generateTemplate(directory) {
const dir = directory.startsWith('.') ? directory : `../${directory}`; const dir = directory.startsWith('.') ? directory : `../${directory}`;
const templatePath = resolve(dir); const templatePath = resolve(dir);
await createTemplate(templatePath); await createTemplate(templatePath);
await copyContent(templatePath); await copyContent(templatePath);
const exists = await configExists(templatePath);
if (!exists) {
await writeTemplateJson(templatePath);
}
}; };