Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(deps): skipping dependencies via CLI #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ var yargs = require('yargs')
baseDir: {
alias: 'b',
default: process.cwd(),
},
skippedDependencies: {
alias: 's',
default: '',
}
})
.array('baseDir')
.example('$0 -o ./tpn', 'run the tool and output text and backing json to ${projectRoot}/tpn directory.')
.example('$0 -s fsevents,angular,everything', 'skip the given dependencies (comma-seprated) for checking')
.example('$0 -b ./some/path/to/projectDir', 'run the tool for Bower/NPM projects in another directory.')
.example('$0 -o tpn -b ./some/path/to/projectDir', 'run the tool in some other directory and dump the output in a directory called "tpn" there.');

Expand Down Expand Up @@ -109,42 +114,51 @@ function getNpmLicenses() {
}
return merged;
}).then((result) => {

// we want to exclude the top-level project from being included
var dir = result[Object.keys(result)[0]]['dir'];
var topLevelProjectInfo = jetpack.read(path.join(dir, 'package.json'), 'json');

var keys = Object.getOwnPropertyNames(result).filter((k) => {
return k !== `${topLevelProjectInfo.name}@${topLevelProjectInfo.version}`;
});

var skippedDeps = !yargs.argv.skippedDependencies.trim()
? []
: yargs.argv.skippedDependencies.split(',');
if (skippedDeps.length > 0) {
console.log("Skipping dependencies", yargs.argv.skippedDependencies);
keys = keys.filter((k) => skippedDeps.map((s) => k.startsWith(s)).filter(s => s).length < 1);
}

return bluebird.map(keys, (key) => {
console.log('processing', key);

var package = result[key];
var defaultPackagePath = `${package['dir']}/node_modules/${package.name}/package.json`;

var itemAtPath = jetpack.exists(defaultPackagePath);
var packagePath = [defaultPackagePath];

if (itemAtPath !== 'file') {
packagePath = jetpack.find(package['dir'], {
matching: `**/node_modules/${package.name}/package.json`
});
}

var packageJson = "";

if (packagePath && packagePath[0]) {
packageJson = jetpack.read(packagePath[0], 'json');
} else {

return Promise.reject(`${package.name}: unable to locate package.json`);
}

console.log('processing', packageJson.name, 'for authors and licenseText');

var props = {};

props.authors =
(packageJson.author && getAttributionForAuthor(packageJson.author)) ||
(packageJson.contributors && packageJson.contributors
Expand All @@ -157,9 +171,9 @@ function getNpmLicenses() {

return getAttributionForAuthor(m);
}).join(', '));

var licenseFile = package.licenseFile;

try {
if (licenseFile && jetpack.exists(licenseFile) && path.basename(licenseFile).match(/license/i)) {
props.licenseText = jetpack.read(licenseFile);
Expand All @@ -169,12 +183,12 @@ function getNpmLicenses() {
} catch (e) {
console.warn(e);

return {
return {
authors: '',
licenseText: ''
};
}

return {
ignore: false,
name: package.name,
Expand Down Expand Up @@ -233,21 +247,21 @@ function getBowerLicenses() {
// npm license check didn't work
// try to get the license and package info from .bower.json first
// because it has more metadata than the plain bower.json

var package = '';

try {
package = jetpack.read(path.join(absPath, '.bower.json'), 'json');
} catch (e) {
package = jetpack.read(path.join(absPath, 'bower.json'), 'json');
}

console.log('processing', package.name);
// assumptions here based on https://github.com/bower/spec/blob/master/json.md
// extract necessary properties as described in TL;DR above
var url = package["_source"] || (package.repository && package.repository.url) ||
package.url || package.homepage;

var authors = '';

if (package.authors) {
Expand All @@ -264,7 +278,7 @@ function getBowerLicenses() {
.replace(/\//g, '');
}
}

// normalize the license object
package.license = package.license || package.licenses;

Expand All @@ -273,7 +287,7 @@ function getBowerLicenses() {
: _.isArray(package.license)
? package.license.join(',')
: package.licenses;

// find the license file if it exists
var licensePath = _.find(component.children, c => {
return /licen[cs]e/i.test(c.name);
Expand All @@ -284,7 +298,7 @@ function getBowerLicenses() {
if (licensePath) {
licenseText = jetpack.read(path.join(bowerComponentsDir, licensePath.relativePath));
}

return {
ignore: false,
name: package.name,
Expand Down Expand Up @@ -360,7 +374,7 @@ taim('Total Processing', bluebird.all([
var attribution = attributionSequence.join(`${os.EOL}${os.EOL}******************************${os.EOL}${os.EOL}`);

var headerPath = path.join(options.outputDir, 'header.txt');

if (jetpack.exists(headerPath)) {
var template = jetpack.read(headerPath);
console.log('using template', template);
Expand Down