npm-link-up
Version:
Use this package to link your projects together for local development.
70 lines (69 loc) • 2.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cp = require("child_process");
const logging_1 = require("./logging");
const path = require("path");
const envstr = require("@oresoftware/envstr");
exports.mapPaths = (searchRoots, dirname, cb) => {
const mappedRoots = searchRoots
.map(v => String(v || '').trim())
.filter(Boolean);
envstr.getEnvStrings(mappedRoots, (err, results) => {
if (err) {
return cb(err);
}
const pths = [];
results.map(d => String(d || '').trim())
.filter(Boolean)
.sort((a, b) => (a.length - b.length))
.forEach(v => {
const s = !pths.some(p => {
return p.startsWith(v + '/');
});
if (s) {
pths.push(v);
}
});
cb(null, pths.map(p => path.isAbsolute(p) ? p : path.resolve(dirname + '/' + p)));
});
};
exports.mapPathsOriginal = (searchRoots, dirname, cb) => {
const mappedRoots = searchRoots
.map(v => String(v || '').trim())
.filter(Boolean)
.map(function (v) {
return `echo "${v}"`;
});
const k = cp.spawn('bash');
k.stdin.end(mappedRoots.join(';') + '\n');
const results = [];
k.stdout.setEncoding('utf8');
k.stderr.setEncoding('utf8');
k.stderr.pipe(process.stderr);
k.stdout.on('data', d => {
String(d || '').split('\n')
.map(v => String(v || '').trim())
.filter(Boolean).forEach(v => results.push(v));
});
k.once('error', e => {
e && logging_1.default.error(e);
});
k.once('exit', code => {
if (code > 0) {
return cb({ code: code });
}
const pths = [];
results.map(d => String(d || '').trim())
.filter(Boolean)
.sort((a, b) => (a.length - b.length))
.forEach(v => {
const s = !pths.some(p => {
return p.startsWith(v + '/');
});
if (s) {
pths.push(v);
}
});
cb(null, pths.map(p => path.isAbsolute(p) ? p : path.resolve(dirname + '/' + p)));
});
};