diff --git a/packages/strapi/lib/commands/__tests__/generate-template.test.js b/packages/strapi/lib/commands/__tests__/generate-template.test.js index e1e576a173..8200bf630a 100644 --- a/packages/strapi/lib/commands/__tests__/generate-template.test.js +++ b/packages/strapi/lib/commands/__tests__/generate-template.test.js @@ -3,6 +3,7 @@ jest.mock('fs-extra', () => ({ ensureDir: jest.fn(() => Promise.resolve()), copy: jest.fn(() => Promise.resolve()), pathExists: jest.fn(() => Promise.resolve()), + writeJSON: jest.fn(() => Promise.resolve()), })); 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', () => { it('updates directory if confirmed', async () => { fse.pathExists.mockReturnValue(true); @@ -62,6 +74,17 @@ describe('generate:template command', () => { 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 () => { fse.pathExists.mockReturnValue(true); jest.spyOn(console, 'error').mockImplementation(() => {}); diff --git a/packages/strapi/lib/commands/generate-template.js b/packages/strapi/lib/commands/generate-template.js index 2bf63089de..45fc49f495 100644 --- a/packages/strapi/lib/commands/generate-template.js +++ b/packages/strapi/lib/commands/generate-template.js @@ -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) { const dir = directory.startsWith('.') ? directory : `../${directory}`; const templatePath = resolve(dir); await createTemplate(templatePath); await copyContent(templatePath); + + const exists = await configExists(templatePath); + if (!exists) { + await writeTemplateJson(templatePath); + } };