UNPKG

nlu

Version:

Use this package to link your projects together for local development.

241 lines (240 loc) 11.6 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); const assert = require("assert"); const path = require("path"); const fs = require("fs"); const async = require("async"); const chalk_1 = require("chalk"); const logging_1 = require("./logging"); const search_queue_1 = require("./search-queue"); const map_paths_with_env_vars_1 = require("./map-paths-with-env-vars"); const utils_1 = require("./utils"); const searchedPaths = {}; exports.makeFindProject = function (mainProjectName, totalList, map, ignore, opts, status) { const isPathSearchableBasic = function (item) { item = path.normalize(item); if (!path.isAbsolute(item)) { throw new Error('Path to be searched is not absolute:' + item); } if (searchedPaths[item]) { opts.verbosity > 2 && logging_1.default.good('already searched this path, not searching again:', chalk_1.default.bold(item)); return false; } return true; }; const isPathSearchable = function (item) { item = path.normalize(item); if (!path.isAbsolute(item)) { throw new Error('Path to be searched is not absolute:' + item); } if (searchedPaths[item]) { opts.verbosity > 2 && logging_1.default.good('already searched this path, not searching again:', chalk_1.default.bold(item)); return false; } let goodPth = ''; const keys = Object.keys(searchedPaths); const match = keys.some(function (pth) { if (String(item).startsWith(pth)) { goodPth = pth; return true; } }); if (match && opts.verbosity > 1) { logging_1.default.good(chalk_1.default.blue('path has already been covered:')); logging_1.default.good('new path:', chalk_1.default.bold(item)); logging_1.default.good('already searched path:', chalk_1.default.bold(goodPth)); } return match; }; const isIgnored = function (pth) { return ignore.some(r => { if (r.test(pth)) { if (opts.verbosity > 3) { 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); if (!isPathSearchableBasic(item)) { return process.nextTick(cb); } searchedPaths[item] = true; logging_1.default.good('new path being searched:', chalk_1.default.blue(item)); (function getMarkers(dir, cb) { if (isIgnored(String(dir + '/'))) { if (opts.verbosity > 2) { logging_1.default.warning('path ignored => ', dir); } return process.nextTick(cb); } if (status.searching === false) { opts.verbosity > 2 && logging_1.default.error('There was an error so we short-circuited search.'); return process.nextTick(cb); } searchedPaths[dir] = true; fs.readdir(dir, function (err, items) { if (err) { logging_1.default.error(err.message || err); if (String(err.message || err).match(/permission denied/)) { return cb(null); } return cb(err); } if (status.searching === false) { opts.verbosity > 2 && logging_1.default.error('There was an error so we short-circuited search.'); 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) { logging_1.default.warning('warning => maybe a symlink? => ', item); return cb(); } if (status.searching === false) { opts.verbosity > 1 && logging_1.default.error('There was an error so we short-circuited search.'); 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 (!isPathSearchableBasic(item)) { return cb(null); } if (isIgnored(String(item + '/'))) { if (opts.verbosity > 2) { logging_1.default.warning('path ignored by settings/regex => ', item); } cb(null); } else { getMarkers(item, cb); } return; } if (!stats.isFile()) { if (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, linkable = null; try { pkg = require(item); } catch (err) { return cb(err); } try { linkable = pkg.nlu.linkable; } catch (err) { } if (linkable === false) { return cb(null); } if (pkg.name === mainProjectName && linkable !== true) { if (opts.verbosity > 1) { logging_1.default.info('Another project on your fs has your main projects package.json name, at path:', chalk_1.default.yellow.bold(dirname)); } return cb(null); } let npmlinkup; try { npmlinkup = require(path.resolve(dirname + '/.nlu.json')); } catch (e) { } if (npmlinkup && npmlinkup.linkable === false) { return cb(null); } let deps, searchRoots; if (npmlinkup && (deps = npmlinkup.list)) { try { assert(Array.isArray(deps), `the 'list' property in an .nlu.json file is not an Array instance for '${filename}'.`); } catch (err) { return cb(err); } deps.forEach(function (item) { totalList.set(item, true); }); } const m = map[pkg.name] = { name: pkg.name, bin: pkg.bin || null, isMainProject: false, linkToItself: Boolean(npmlinkup && npmlinkup.linkToItself), runInstall: Boolean(npmlinkup && npmlinkup.alwaysReinstall), path: dirname, deps: deps || [] }; const nm = path.resolve(dirname + '/node_modules'); const keys = opts.production ? utils_1.getProdKeys(pkg) : utils_1.getDevKeys(pkg); async.autoInject({ reinstall(cb) { if (!totalList.get(pkg.name)) { return process.nextTick(cb); } utils_1.determineIfReinstallIsNeeded(nm, keys, opts, (err, val) => { m.runInstall = val === true; if (val === true) { logging_1.default.debug(chalk_1.default.red('the following project needs reinstall:'), dirname); } cb(err); }); }, addToSearchRoots(cb) { searchRoots = npmlinkup && npmlinkup.searchRoots; if (!searchRoots) { return cb(null); } if (!Array.isArray(searchRoots)) { return cb(new Error('The "searchRoots" property is not an array in .nlu.json file at path: ' + dirname)); } try { assert(Array.isArray(searchRoots), `the 'searchRoots' property in an .nlu.json file is not an Array instance for '${filename}'.`); } catch (err) { return cb(err); } map_paths_with_env_vars_1.mapPaths(searchRoots, function (err, roots) { if (err) { return cb(err); } roots.forEach(function (r) { if (isPathSearchable(r)) { search_queue_1.q.push(function (cb) { findProject(r, cb); }); } }); cb(null); }); } }, cb); }); }, cb); }); })(item, cb); }; };