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.
  • Loading branch information
yu-tian113 authored and Yu Tian committed Nov 2, 2017
1 parent 1298e15 commit 9a0189b
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 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,47 @@ 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
: null;
const npmRootFiles = fs.readdirSync(npmFrom);
const packageRootEntries = glob.sync('*.js', {
cwd: from,
});
packageRootEntries.forEach(entry => {
// Terminate the build if any entry point in the package root does not have an equivalent in ./npm.
// Exceptions: *.fb.js
if (!npmRootFiles.includes(entry) && !/.fb.js$/.test(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);
return Promise.all([
...asyncCopyProxies,
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 9a0189b

Please sign in to comment.