Skip to content

Commit b4ddd28

Browse files
committed
Code refactor as per peer review.
1 parent 3b4a6d8 commit b4ddd28

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

‎scripts/rollup/packaging.js‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -168,39 +168,44 @@ function copyNodePackageTemplate(packageName) {
168168
}
169169
fs.mkdirSync(to);
170170
const packageJson = resolve(`${from}/package.json`);
171-
const whitelistedFiles = fs.existsSync(packageJson)
172-
? require(packageJson).files
173-
: null;
171+
const whitelistedFiles = (fs.existsSync(packageJson) &&
172+
require(packageJson).files) || [];
174173
const npmRootFiles = fs.readdirSync(npmFrom);
175174
// Get all entry points in the package root
176175
// Exceptions: *.fb.js
177176
const packageRootEntries = glob.sync('!(*.fb).js', {
178177
cwd: from,
179178
});
180179
packageRootEntries.forEach(entry => {
181-
// Terminate the build if any entry point in the package root does not have an equivalent in ./npm.
180+
// Terminate the build if any entry point in the package root
181+
// does not have an equivalent in ./npm.
182182
if (!npmRootFiles.includes(entry)) {
183183
console.error(
184184
`Entry point ${entry} in package ${packageName} does not have an equivalent in ./npm`
185185
);
186186
process.exit(1);
187187
}
188188
});
189-
if (whitelistedFiles && whitelistedFiles.length > 0) {
190-
let asyncCopyProxies = [];
191-
asyncCopyProxies = npmRootFiles.reduce((proxies, file) => {
192-
if (
193-
whitelistedFiles.includes(file) ||
194-
whitelistedFiles.includes(`${file}/`)
195-
) {
196-
proxies.push(
197-
asyncCopyTo(resolve(`${npmFrom}/${file}`), `${to}/${file}`)
198-
);
199-
}
200-
return proxies;
201-
}, asyncCopyProxies);
189+
if (whitelistedFiles.length > 0) {
190+
// Looping through files and folders in ./npm root,
191+
// if whitelisted in package.json, copy to build package root,
192+
// return an array of the promises generated by async copy.
193+
const promisesForForwardingModules = npmRootFiles.reduce(
194+
(promises, file) => {
195+
if (
196+
whitelistedFiles.includes(file) ||
197+
whitelistedFiles.includes(`${file}/`)
198+
) {
199+
promises.push(
200+
asyncCopyTo(resolve(`${npmFrom}/${file}`), `${to}/${file}`)
201+
);
202+
}
203+
return promises;
204+
},
205+
[]
206+
);
202207
return Promise.all([
203-
...asyncCopyProxies,
208+
...promisesForForwardingModules,
204209
asyncCopyTo(resolve(`${from}/package.json`), `${to}/package.json`),
205210
asyncCopyTo(resolve(`${from}/README.md`), `${to}/README.md`),
206211
asyncCopyTo(resolve('./LICENSE'), `${to}/LICENSE`),

0 commit comments

Comments
 (0)