Skip to content

Commit

Permalink
Issue facebook#11257 - compare files/folders in package/npm to "files…
Browse files Browse the repository at this point in the history
…" in package.json, only copy whitelisted files/folders to build (+1 squashed commit)

Squashed commits:
[d400198] handle directories in files list - e.g. "libs/" (+1 squashed commit)
Squashed commits:
[52a4d3b] Add in file check to make sure all entry points have equivalent files in ./npm folder.
Fail the build if any equivalent missing. (+1 squashed commit)
Squashed commits:
[e2d1516] Update glob to exclude *.fb.js, replace the check in if statement (+1 squashed commit)
Squashed commits:
[b4ddd28] Code refactor as per peer review.
  • Loading branch information
yu-tian113 authored and Yu Tian committed Nov 13, 2017
1 parent 2d23a45 commit 8a509ae
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions scripts/rollup/packaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const join = require('path').join;
const resolve = require('path').resolve;
const Bundles = require('./bundles');
const asyncCopyTo = require('./utils').asyncCopyTo;
const glob = require('glob');

const UMD_DEV = Bundles.bundleTypes.UMD_DEV;
const UMD_PROD = Bundles.bundleTypes.UMD_PROD;
Expand Down Expand Up @@ -165,15 +166,53 @@ function copyNodePackageTemplate(packageName) {
// We already created this package (e.g. due to another entry point).
return Promise.resolve();
}
// TODO: verify that all copied files are either in the "files"
// whitelist or implicitly published by npm.
return asyncCopyTo(npmFrom, to).then(() =>
Promise.all([
fs.mkdirSync(to);
const packageJson = resolve(`${from}/package.json`);
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.
if (!npmRootFiles.includes(entry)) {
console.error(
`Entry point ${entry} in package ${packageName} does not have an equivalent in ./npm`
);
process.exit(1);
}
});
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([
...promisesForForwardingModules,
asyncCopyTo(resolve(`${from}/package.json`), `${to}/package.json`),
asyncCopyTo(resolve(`${from}/README.md`), `${to}/README.md`),
asyncCopyTo(resolve('./LICENSE'), `${to}/LICENSE`),
])
);
]);
} else {
return Promise.resolve();
}
}

function createNodePackage(bundleType, packageName, filename) {
Expand Down

0 comments on commit 8a509ae

Please sign in to comment.