From b4ddd2891399a2c125c2dc3aa5b926b32dd55717 Mon Sep 17 00:00:00 2001 From: Yu Tian Date: Sat, 4 Nov 2017 13:25:09 +1100 Subject: [PATCH] Code refactor as per peer review. --- scripts/rollup/packaging.js | 41 +++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/scripts/rollup/packaging.js b/scripts/rollup/packaging.js index 8161128610e3e..be641502a1545 100644 --- a/scripts/rollup/packaging.js +++ b/scripts/rollup/packaging.js @@ -168,9 +168,8 @@ function copyNodePackageTemplate(packageName) { } fs.mkdirSync(to); const packageJson = resolve(`${from}/package.json`); - const whitelistedFiles = fs.existsSync(packageJson) - ? require(packageJson).files - : null; + const whitelistedFiles = (fs.existsSync(packageJson) && + require(packageJson).files) || []; const npmRootFiles = fs.readdirSync(npmFrom); // Get all entry points in the package root // Exceptions: *.fb.js @@ -178,7 +177,8 @@ function copyNodePackageTemplate(packageName) { cwd: from, }); packageRootEntries.forEach(entry => { - // Terminate the build if any entry point in the package root does not have an equivalent in ./npm. + // Terminate the build if any entry point in the package root + // does not have an equivalent in ./npm. if (!npmRootFiles.includes(entry)) { console.error( `Entry point ${entry} in package ${packageName} does not have an equivalent in ./npm` @@ -186,21 +186,26 @@ function copyNodePackageTemplate(packageName) { process.exit(1); } }); - if (whitelistedFiles && whitelistedFiles.length > 0) { - let asyncCopyProxies = []; - asyncCopyProxies = npmRootFiles.reduce((proxies, file) => { - if ( - whitelistedFiles.includes(file) || - whitelistedFiles.includes(`${file}/`) - ) { - proxies.push( - asyncCopyTo(resolve(`${npmFrom}/${file}`), `${to}/${file}`) - ); - } - return proxies; - }, asyncCopyProxies); + if (whitelistedFiles.length > 0) { + // Looping through files and folders in ./npm root, + // if whitelisted in package.json, copy to build package root, + // return an array of the promises generated by async copy. + const promisesForForwardingModules = npmRootFiles.reduce( + (promises, file) => { + if ( + whitelistedFiles.includes(file) || + whitelistedFiles.includes(`${file}/`) + ) { + promises.push( + asyncCopyTo(resolve(`${npmFrom}/${file}`), `${to}/${file}`) + ); + } + return promises; + }, + [] + ); return Promise.all([ - ...asyncCopyProxies, + ...promisesForForwardingModules, asyncCopyTo(resolve(`${from}/package.json`), `${to}/package.json`), asyncCopyTo(resolve(`${from}/README.md`), `${to}/README.md`), asyncCopyTo(resolve('./LICENSE'), `${to}/LICENSE`),