npm-link-up
Version:
Use this package to link your projects together for local development.
130 lines (129 loc) • 6.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const fs = require("fs");
const async = require("async");
const chalk_1 = require("chalk");
const logging_1 = require("../../logging");
exports.makeFindProjects = function (mainProjectName, ignore, opts, map, toAdd, status) {
let isIgnored = (pth) => {
return ignore.some(r => {
if (r.test(pth)) {
if (opts.verbosity > 2) {
logging_1.default.warning(`Path with value "${pth}" was ignored because it matched the following regex:`);
logging_1.default.warning(`${r}`);
}
return true;
}
});
};
return function findProject(item, cb) {
item = path.normalize(item);
logging_1.default.info('new path being searched:', chalk_1.default.blue(item));
(function getMarkers(dir, cb) {
if (status.searching === false) {
return process.nextTick(cb);
}
if (isIgnored(String(dir + '/'))) {
if (opts.verbosity > 2) {
logging_1.default.warning('path ignored => ', dir);
}
return process.nextTick(cb);
}
fs.readdir(dir, function (err, items) {
if (err) {
logging_1.default.warn('Could not read a directory at path:', dir);
logging_1.default.warn(err.message || err);
if (String(err.message || err).match(/permission denied/)) {
return cb(null);
}
return cb(err);
}
if (status.searching === false) {
return process.nextTick(cb);
}
items = items.map(function (item) {
return path.resolve(dir, item);
});
async.eachLimit(items, 3, function (item, cb) {
if (isIgnored(String(item))) {
if (opts.verbosity > 2) {
logging_1.default.warning('path ignored => ', item);
}
return process.nextTick(cb);
}
fs.lstat(item, function (err, stats) {
if (err) {
opts.verbosity > 2 && logging_1.default.warning('warning => maybe a symlink? => ', err.message || err);
return cb(null);
}
if (status.searching === false) {
return process.nextTick(cb);
}
if (stats.isSymbolicLink()) {
opts.verbosity > 2 && logging_1.default.warning('warning => looks like a symlink => ', item);
return cb();
}
if (stats.isDirectory()) {
if (isIgnored(String(item + '/'))) {
if (opts.verbosity > 2) {
opts.verbosity > 2 && logging_1.default.warning('path ignored by settings/regex => ', item);
}
cb(null);
}
else {
getMarkers(item, cb);
}
return;
}
if (!stats.isFile()) {
opts.verbosity > 2 && logging_1.default.warning('Not a directory or file (maybe a symlink?) => ', item);
return cb(null);
}
let dirname = path.dirname(item);
let filename = path.basename(item);
if (String(filename) !== 'package.json') {
return cb(null);
}
let pkg, nluIsLink1 = true;
try {
pkg = require(item);
nluIsLink1 = !(pkg.nlu && pkg.nlu.linkable === false);
}
catch (err) {
return cb(err);
}
let npmlinkup, nluIsLink2 = true;
try {
npmlinkup = require(path.resolve(dirname + '/.nlu.json'));
nluIsLink2 = npmlinkup['npm-link-up'] !== false;
}
catch (e) {
}
const index = toAdd.indexOf(pkg.name);
const isAdd = index > -1;
if (isAdd && nluIsLink1 && nluIsLink2) {
toAdd.splice(index, 1);
if (toAdd.length < 1) {
status.searching = false;
}
logging_1.default.info('We found a relevant project:', chalk_1.default.blueBright.bold(pkg.name), ', at path:', chalk_1.default.gray.bold(dirname));
const helpLink = 'https://github.com/ORESoftware/npm-link-up/tree/master/docs/easy-duplicate-package-mitigation.md';
if (map[pkg.name]) {
logging_1.default.warn('The following package name exists in multiple package.json files on your fs:', pkg.name);
return cb(new Error(`Multiple packages with name "${pkg.name}", to easily mitigate this problem see: ${chalk_1.default.blueBright(helpLink)}.`));
}
map[pkg.name] = {
name: pkg.name,
isMainProject: pkg.name === mainProjectName,
path: dirname,
deps: npmlinkup && npmlinkup.list || []
};
}
cb(null);
});
}, cb);
});
})(item, cb);
};
};