Last active
December 21, 2015 03:58
-
-
Save dannycoates/6245521 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To start a promise chain from a sync function wrap it with P(). | |
// This is short-hand for creating a deferred and immediately resolving it. | |
function getFiles(pth) { | |
return P(shell.find(pth)) | |
} | |
// It's ok to put sync function in a promise chain | |
function filterFolders(data) { | |
return data.filter( | |
function (file) { | |
return !file.match(/\/(node_modules|tests?|automation-tests|example|js\/lib)\//i) | |
} | |
) | |
} | |
function filterJS(data) { | |
return data.filter( | |
function (file) { | |
return file.match(/\.js$/i); | |
} | |
) | |
} | |
// here we take advantage of promises by using the async fs.readFile | |
function hasCopyright(files) { | |
var promises = files.map( | |
function (file) { | |
var d = P.defer() | |
fs.readFile(file, 'utf8', function (err, data) { | |
if (err || data.indexOf(COPYRIGHT_HEADER) === -1) { | |
return d.reject(err) | |
} | |
d.resolve(data) | |
}) | |
return d.promise | |
} | |
) | |
return P.allSettled(promises) // allSettled will wait for all promises to finish, then "return" an array of results | |
.then( | |
function (results) { // allSettled returns both rejected and resolved results, so filter out rejected | |
return results.filter(function (r) { return r.state === "fulfilled" }).map(function (r) { return r.value }) | |
} | |
) | |
} | |
function logResults(data) { | |
data.forEach(function (file) { | |
console.log(file) | |
}) | |
} | |
// chain it all together | |
getFiles('browserid') | |
.then(filterFolders) | |
.then(filterJS) | |
.then(hasCopyright) | |
.done(logResults) // done ensured that any "uncaught" rejects or errors gets rethrown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var fs = require("fs"); | |
var p = require("p-promise"), | |
shell = require("shelljs"); | |
var COPYRIGHT_HEADER = "* This Source Code Form is subject to the terms of the Mozilla Public\n"+ | |
" * License, v. 2.0. If a copy of the MPL was not distributed with this\n"+ | |
" * file, You can obtain one at http://mozilla.org/MPL/2.0/."; | |
/** | |
shell.find("browserid").filter(function (file) { | |
// We only care about .JS files, presumably. | |
return file.match(/\.js$/i); | |
}).filter(function (file) { | |
// Ignore if it's a node module, a test file, or an example file. | |
return !file.match(/\/(node_modules|tests?|automation-tests|example)\//i); | |
}).forEach(function (file) { | |
var data = fs.readFileSync(file, "utf8"); | |
if (data.indexOf(COPYRIGHT_HEADER) === -1) { | |
console.log("No copyright found in: %s", file); | |
} | |
}); | |
*/ | |
getFiles("browserid").then(filterFolders).then(filterJS).then(hasCopyright).then(logResults); | |
function getFiles(pth) { | |
var files = shell.find(pth); | |
var deferred = p.defer(); | |
deferred.resolve(files); | |
return deferred.promise; | |
} | |
// We only want .js files... | |
function filterJS(data) { | |
var files = data.filter(function (file) { | |
return file.match(/\.js$/i); | |
}); | |
var deferred = p.defer(); | |
deferred.resolve(files); | |
return deferred.promise; | |
} | |
// But we don't care about the /node_modules/, /test/, /tests/, or /js/lib/ directories. | |
function filterFolders(data) { | |
var files = data.filter(function (file) { | |
return !file.match(/\/(node_modules|tests?|automation-tests|example|js\/lib)\//i); | |
}); | |
var deferred = p.defer(); | |
deferred.resolve(files); | |
return deferred.promise; | |
} | |
// Sync-load the file and check for the copyright header. | |
function hasCopyright(data) { | |
var files = data.filter(function (file) { | |
var contents = fs.readFileSync(file, "utf8"); | |
return (contents.indexOf(COPYRIGHT_HEADER) === -1); | |
}); | |
var deferred = p.defer(); | |
deferred.resolve(files); | |
return deferred.promise; | |
} | |
function logResults(data) { | |
data.forEach(function (file) { | |
console.log(file); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment