Skip to content

Commit

Permalink
Merge pull request #73 from straker/auditFix
Browse files Browse the repository at this point in the history
fix(utils): update to async/await and ensure callback is called in file order
  • Loading branch information
straker authored May 6, 2019
2 parents 3a67968 + 7ce10e8 commit d75fe49
Show file tree
Hide file tree
Showing 8 changed files with 1,686 additions and 919 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
/coverage
examples/**/*.html
bower_components
npm-debug.log
npm-debug.log
.nyc_output
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- "8.0"
- "6.0"
- "10.0"
- "12.0"
after_script: "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
10 changes: 1 addition & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,7 @@ function livingcss(source, dest, options) {

return Promise.all([
// read the handlebars template
new Promise(function(resolve, reject) {
fs.readFile(options.template, 'utf8', function(err, data) {
if (err) {
reject(err);
}

resolve(data);
});
}),
utils._readFileWithPromise(options.template),

// read all source files and handlebar templates
utils.readFileGlobs(source, function(data, file) {
Expand Down
134 changes: 70 additions & 64 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,44 @@ var svgRegex = /url\("data:image\/svg\+xml;.*?"\)/gi;
var whiteSpaceRegex = /\s/g;
var utils = {};

/**
* Read a file and return a promise.
* @param {string} file - Path to the file
* @returns Promise
*/
utils._readFileWithPromise = function readFileWithPromise(file) {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8', function(err, data) {
if (err) {
reject(err);
}

resolve(data);
});
});
}

/**
* Read a glob string and return a promise.
* @param {string} pattern - Glob file pattern
* @returns Promise
*/
function globWithPromsie(pattern) {
return new Promise((resolve, reject) => {
glob(pattern, function(err, files) {
if (err) {
reject(err);
}

if (files.length === 0) {
console.warn('pattern "' + pattern + '" does not match any file');
}

resolve(files);
});
});
}

/**
* Get an id by hyphen separating the name.
*
Expand Down Expand Up @@ -112,53 +150,33 @@ utils.sortCategoryBy = function sortCategoryBy(categories, sortOrder) {
* @param {function} callback - Callback to execute for each read file.
* @returns {Promise}
*/
utils.readFileGlobs = function readFileGlobs(patterns, callback) {
utils.readFileGlobs = async function readFileGlobs(patterns, callback) {
patterns = (!Array.isArray(patterns) ? [patterns] : patterns);
var promises = [];

patterns.forEach(function(pattern) {

promises.push(new Promise(function(resolve, reject) {
glob(pattern, function(err, files) {
if (err) {
reject(err);
}

if (files.length === 0) {
console.warn('pattern "' + pattern + '" does not match any file');
}

resolve(files);
});
}));

let globPromises = await patterns.map(async (pattern) => {
return globWithPromsie(pattern);
});

return Promise.all(promises).then(function(fileList) {
var readFilePromises = [];
return Promise.all(globPromises)
.then(async (fileList) => {

fileList.forEach(function(files) {
files.forEach(function(file) {
readFilePromises.push(new Promise(function(resolve, reject) {
utils.readFiles(file, function(data, file) {
resolve([data, file]);
});
}));
// fileList is an array of arrays of files
// e.g. [ [fileOne, fileTwo], [fileThree, fileFour], ...]
let readFilePromises = await fileList.map(async (files) => {
return await utils.readFiles(files)
});
});

return Promise.all(readFilePromises).then(function(files) {
files.forEach(function(data) {
callback.apply(null, data);
});
return Promise.all(readFilePromises);
})
.catch(function(err) {
throw err;
.then(readFileList => {

// readFileList is an array of arrays of file data
// e.g. [ [ ['contents fileOne', fileOne], ['contents fileTwo', fileTow] ] ]
readFileList.forEach(readFiles => {
readFiles.forEach(fileData => {
callback.apply(null, fileData);
});
});
});
})
.catch(function(err) {
throw err;
});
};

/**
Expand All @@ -169,37 +187,25 @@ utils.readFileGlobs = function readFileGlobs(patterns, callback) {
* @param {function} callback - Callback to execute for each read file.
* @returns {Promise}
*/
utils.readFiles = function readFiles(files, callback) {
if (!files || files.length === 0) {
return Promise.resolve();
}
utils.readFiles = async function readFiles(files, callback) {
if (!files || (Array.isArray(files) && !files.length)) return Promise.resolve();

files = (!Array.isArray(files) ? [files] : files);
var promises = [];
let promises = await Promise.all(files.map(async (file) => {
let data = await utils._readFileWithPromise(file);
return [data, file];
}));

files.forEach(function(file) {

promises.push(new Promise(function(resolve, reject) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) {
reject(err);
}

resolve([data, file]);
return Promise.all(promises).then(files => {
if (callback) {
files.forEach(function(data) {
callback.apply(null, data);
});
}));

});
}

return Promise.all(promises).then(function(files) {
files.forEach(function(data) {
callback.apply(null, data);
});
return files;
})
.catch(function(err) {
throw err;
});
};
}

/**
* Workaround for polymer issue: /~https://github.com/Polymer/polymer/issues/1276.
Expand Down
Loading

0 comments on commit d75fe49

Please sign in to comment.