I'm reading an array (of unknown length) of modules package.json
files in order to extract the "style" property and store it in an array of its own:
var gulp = require('gulp')
, path = require('path')
, Promise = require('bluebird')
, fs = Promise.promisifyAll(require('fs-extra'));
let components = [
'comp1',
'comp2',
'comp3'
];
let npmScope = '@myscope';
gulp.task('getstyle', () => {
let componentsBaseLocation = path.join(__dirname, 'node_modules', npmScope);
Promise.map(components, (componentName) => {
return fs.readFileAsync(path.join(componentsBaseLocation, componentName, 'package.json'), 'utf8')
})
.spread((...packageData) => {
return packageData.map((packageDatum, i) => {
packageDatum = JSON.parse(packageDatum);
tempName = packageDatum.name.match(new RegExp(`${NpmScope}\/(.*?)$`))[1]
tempStyle = path.join(componentsBaseLocation, tempName, packageDatum.style);
return {
name: tempName,
style: tempStyle
};
});
})
.then(components => {
console.log(JSON.stringify(components, null, 2));
});
});
This works, but I wonder if there are smarter, better, more appropriate ways to use bluebird or promises in general.
.spread((...packageData) =>
. Can just do.then((packageData) =>
since the argument for aPromise.map(...).then()
handler is already the array you want in your next step. \$\endgroup\$