mirror of
https://github.com/strapi/strapi.git
synced 2025-08-31 12:23:05 +00:00
add template config file to root
This commit is contained in:
parent
c2d438ba88
commit
a6eb49b75d
@ -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(() => {});
|
||||
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user