fix(nxdev): verify whether srcFolder passed to generateFiles exists (#7219)

This commit is contained in:
Juri Strumpflohner 2021-10-04 17:30:02 +02:00 committed by GitHub
parent 2898ccd8e2
commit 83a6ffb47a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -66,29 +66,37 @@ export function generateFiles(
substitutions: { [k: string]: any } substitutions: { [k: string]: any }
): void { ): void {
const ejs = require('ejs'); const ejs = require('ejs');
allFilesInDir(srcFolder).forEach((filePath) => {
let newContent: Buffer | string; const files = allFilesInDir(srcFolder);
const computedPath = computePath( if (files.length === 0) {
srcFolder, throw new Error(
target, `generateFiles: No files found in "${srcFolder}". Are you sure you specified the correct path?`
filePath,
substitutions
); );
} else {
files.forEach((filePath) => {
let newContent: Buffer | string;
const computedPath = computePath(
srcFolder,
target,
filePath,
substitutions
);
if (binaryExts.has(path.extname(filePath))) { if (binaryExts.has(path.extname(filePath))) {
newContent = readFileSync(filePath); newContent = readFileSync(filePath);
} else { } else {
const template = readFileSync(filePath, 'utf-8'); const template = readFileSync(filePath, 'utf-8');
try { try {
newContent = ejs.render(template, substitutions, {}); newContent = ejs.render(template, substitutions, {});
} catch (e) { } catch (e) {
logger.error(`Error in ${filePath.replace(`${tree.root}/`, '')}:`); logger.error(`Error in ${filePath.replace(`${tree.root}/`, '')}:`);
throw e; throw e;
}
} }
}
tree.write(computedPath, newContent); tree.write(computedPath, newContent);
}); });
}
} }
function computePath( function computePath(