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'])( it.each(['api', 'components', 'config/functions/bootstrap.js', 'data'])(
'copies folder %s', 'copies folder %s',
async item => { async item => {
// Mock the empty directory arg
fse.pathExists.mockReturnValueOnce(false);
// Mock the folder exists
fse.pathExists.mockReturnValue(true);
const directory = '../test-dir'; const directory = '../test-dir';
const rootPath = resolve(directory); const rootPath = resolve(directory);
const templatePath = join(rootPath, 'template'); const templatePath = join(rootPath, 'template');
await exportTemplate(directory); await exportTemplate(directory);
expect(fse.pathExists).toHaveBeenCalledWith(join(process.cwd(), item));
expect(fse.copy).toHaveBeenCalledWith(join(process.cwd(), item), join(templatePath, 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) { async function copyContent(templatePath, rootBase) {
for (const item of TEMPLATE_CONTENT) { for (const item of TEMPLATE_CONTENT) {
try { try {
await fse.copy(join(process.cwd(), item), join(templatePath, item)); const folderExists = await fse.pathExists(join(process.cwd(), item));
const currentProjectBase = basename(process.cwd());
console.log( if (folderExists) {
`${chalk.green( await fse.copy(join(process.cwd(), item), join(templatePath, item));
'success' const currentProjectBase = basename(process.cwd());
)}: copy ${currentProjectBase}/${item} => ${rootBase}/template/${item}` console.log(
); `${chalk.green(
'success'
)}: copy ${currentProjectBase}/${item} => ${rootBase}/template/${item}`
);
}
} catch (error) { } catch (error) {
console.error(`${chalk.red('error')}: ${error.message}`); console.error(`${chalk.red('error')}: ${error.message}`);
} }