Skip to content

Commit

Permalink
Code refactor as per peer review.
Browse files Browse the repository at this point in the history
  • Loading branch information
yu-tian113 committed Nov 4, 2017
1 parent 3b4a6d8 commit b4ddd28
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions scripts/rollup/packaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,39 +168,44 @@ 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
const packageRootEntries = glob.sync('!(*.fb).js', {
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`
);
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`),
Expand Down

0 comments on commit b4ddd28

Please sign in to comment.