copy folders that exist

This commit is contained in:
Mark Kaylor 2021-05-31 16:34:42 +02:00
parent aad0515764
commit eec4aa2e56
2 changed files with 16 additions and 7 deletions

View File

@ -33,12 +33,17 @@ describe('generate:template command', () => {
it.each(['api', 'components', 'config/functions/bootstrap.js', 'data'])(
'copies folder %s',
async item => {
// Mock the empty directory arg
fse.pathExists.mockReturnValueOnce(false);
// Mock the folder exists
fse.pathExists.mockReturnValue(true);
const directory = '../test-dir';
const rootPath = resolve(directory);
const templatePath = join(rootPath, 'template');
await exportTemplate(directory);
expect(fse.pathExists).toHaveBeenCalledWith(join(process.cwd(), item));
expect(fse.copy).toHaveBeenCalledWith(join(process.cwd(), item), join(templatePath, item));
}
);

View File

@ -16,13 +16,17 @@ const TEMPLATE_CONTENT = ['api', 'components', 'config/functions/bootstrap.js',
async function copyContent(templatePath, rootBase) {
for (const item of TEMPLATE_CONTENT) {
try {
await fse.copy(join(process.cwd(), item), join(templatePath, item));
const currentProjectBase = basename(process.cwd());
console.log(
`${chalk.green(
'success'
)}: copy ${currentProjectBase}/${item} => ${rootBase}/template/${item}`
);
const folderExists = await fse.pathExists(join(process.cwd(), item));
if (folderExists) {
await fse.copy(join(process.cwd(), item), join(templatePath, item));
const currentProjectBase = basename(process.cwd());
console.log(
`${chalk.green(
'success'
)}: copy ${currentProjectBase}/${item} => ${rootBase}/template/${item}`
);
}
} catch (error) {
console.error(`${chalk.red('error')}: ${error.message}`);
}