@pnpm/link-bins
Advanced tools
Comparing version
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -15,38 +7,34 @@ const flatten = require("arr-flatten"); | ||
const path = require("path"); | ||
function default_1(modules, warn) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const dirs = yield getDirectories(modules, warn); | ||
const subdirs = yield Promise.all(dirs.map((dir) => { | ||
return isScopedPkgsDir(dir) ? getDirectories(dir, warn) : Promise.resolve([dir]); | ||
})); | ||
return flatten(subdirs); | ||
}); | ||
async function default_1(modules, warn) { | ||
const dirs = await getDirectories(modules, warn); | ||
const subdirs = await Promise.all(dirs.map((dir) => { | ||
return isScopedPkgsDir(dir) ? getDirectories(dir, warn) : Promise.resolve([dir]); | ||
})); | ||
return flatten(subdirs); | ||
} | ||
exports.default = default_1; | ||
function getDirectories(srcPath, warn) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let dirs; | ||
async function getDirectories(srcPath, warn) { | ||
let dirs; | ||
try { | ||
dirs = await fs.readdir(srcPath); | ||
} | ||
catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
throw err; | ||
} | ||
dirs = []; | ||
} | ||
return pFilter(dirs | ||
.filter((relativePath) => relativePath[0] !== '.') // ignore directories like .bin, .store, etc | ||
.map((relativePath) => path.join(srcPath, relativePath)), async (absolutePath) => { | ||
try { | ||
dirs = yield fs.readdir(srcPath); | ||
const stats = await fs.stat(absolutePath); | ||
return stats.isDirectory(); | ||
} | ||
catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
if (err.code !== 'ENOENT') | ||
throw err; | ||
} | ||
dirs = []; | ||
warn(`Cannot find file at ${absolutePath} although it was listed by readdir`); | ||
return false; | ||
} | ||
return pFilter(dirs | ||
.filter((relativePath) => relativePath[0] !== '.') // ignore directories like .bin, .store, etc | ||
.map((relativePath) => path.join(srcPath, relativePath)), (absolutePath) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const stats = yield fs.stat(absolutePath); | ||
return stats.isDirectory(); | ||
} | ||
catch (err) { | ||
if (err.code !== 'ENOENT') | ||
throw err; | ||
warn(`Cannot find file at ${absolutePath} although it was listed by readdir`); | ||
return false; | ||
} | ||
})); | ||
}); | ||
@@ -53,0 +41,0 @@ } |
134
lib/index.js
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -25,5 +17,5 @@ const package_bins_1 = require("@pnpm/package-bins"); | ||
const POWER_SHELL_IS_SUPPORTED = IS_WINDOWS; | ||
exports.default = (modules, binPath, opts) => __awaiter(this, void 0, void 0, function* () { | ||
const pkgDirs = yield getPkgDirs_1.default(modules, opts.warn); | ||
const allCmds = R.unnest((yield Promise.all(pkgDirs | ||
exports.default = async (modules, binPath, opts) => { | ||
const pkgDirs = await getPkgDirs_1.default(modules, opts.warn); | ||
const allCmds = R.unnest((await Promise.all(pkgDirs | ||
.map(normalizePath) | ||
@@ -33,80 +25,66 @@ .map((target) => getPackageBins(target, opts.warn)))) | ||
return linkBins(allCmds, binPath, opts); | ||
}); | ||
function linkBinsOfPackages(pkgs, binsTarget, opts) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!pkgs.length) | ||
return; | ||
const allCmds = R.unnest((yield Promise.all(pkgs | ||
.map((pkg) => getPackageBinsFromPackageJson(pkg.manifest, pkg.location)))) | ||
.filter((cmds) => cmds.length)); | ||
return linkBins(allCmds, binsTarget, opts); | ||
}); | ||
}; | ||
async function linkBinsOfPackages(pkgs, binsTarget, opts) { | ||
if (!pkgs.length) | ||
return; | ||
const allCmds = R.unnest((await Promise.all(pkgs | ||
.map((pkg) => getPackageBinsFromPackageJson(pkg.manifest, pkg.location)))) | ||
.filter((cmds) => cmds.length)); | ||
return linkBins(allCmds, binsTarget, opts); | ||
} | ||
exports.linkBinsOfPackages = linkBinsOfPackages; | ||
function linkBins(allCmds, binPath, opts) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!allCmds.length) | ||
async function linkBins(allCmds, binPath, opts) { | ||
if (!allCmds.length) | ||
return; | ||
await mkdirp(binPath); | ||
const [cmdsWithOwnName, cmdsWithOtherNames] = R.partition((cmd) => cmd.ownName, allCmds); | ||
await Promise.all(cmdsWithOwnName.map((cmd) => linkBin(cmd, binPath))); | ||
const usedNames = R.fromPairs(cmdsWithOwnName.map((cmd) => [cmd.name, cmd.name])); | ||
await Promise.all(cmdsWithOtherNames.map((cmd) => { | ||
if (usedNames[cmd.name]) { | ||
opts.warn(`Cannot link bin "${cmd.name}" of "${cmd.pkgName}" to "${binPath}". A package called "${usedNames[cmd.name]}" already has its bin linked.`); | ||
return; | ||
yield mkdirp(binPath); | ||
const [cmdsWithOwnName, cmdsWithOtherNames] = R.partition((cmd) => cmd.ownName, allCmds); | ||
yield Promise.all(cmdsWithOwnName.map((cmd) => linkBin(cmd, binPath))); | ||
const usedNames = R.fromPairs(cmdsWithOwnName.map((cmd) => [cmd.name, cmd.name])); | ||
yield Promise.all(cmdsWithOtherNames.map((cmd) => { | ||
if (usedNames[cmd.name]) { | ||
opts.warn(`Cannot link bin "${cmd.name}" of "${cmd.pkgName}" to "${binPath}". A package called "${usedNames[cmd.name]}" already has its bin linked.`); | ||
return; | ||
} | ||
usedNames[cmd.name] = cmd.pkgName; | ||
return linkBin(cmd, binPath); | ||
})); | ||
}); | ||
} | ||
function getPackageBins(target, warn) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const pkg = yield safeReadPkg(target); | ||
if (!pkg) { | ||
warn(`There's a directory in node_modules without package.json: ${target}`); | ||
return []; | ||
} | ||
return getPackageBinsFromPackageJson(pkg, target); | ||
}); | ||
usedNames[cmd.name] = cmd.pkgName; | ||
return linkBin(cmd, binPath); | ||
})); | ||
} | ||
function getPackageBinsFromPackageJson(pkgJson, pkgPath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cmds = yield package_bins_1.default(pkgJson, pkgPath); | ||
return cmds.map((cmd) => (Object.assign({}, cmd, { ownName: cmd.name === pkgJson.name, pkgName: pkgJson.name }))); | ||
}); | ||
async function getPackageBins(target, warn) { | ||
const pkg = await safeReadPkg(target); | ||
if (!pkg) { | ||
warn(`There's a directory in node_modules without package.json: ${target}`); | ||
return []; | ||
} | ||
return getPackageBinsFromPackageJson(pkg, target); | ||
} | ||
function linkBin(cmd, binPath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const externalBinPath = path.join(binPath, cmd.name); | ||
if (EXECUTABLE_SHEBANG_SUPPORTED) { | ||
yield fs.chmod(cmd.path, 0o755); | ||
} | ||
const nodePath = yield getBinNodePaths(cmd.path); | ||
return cmdShim(cmd.path, externalBinPath, { | ||
createPwshFile: POWER_SHELL_IS_SUPPORTED, | ||
nodePath, | ||
}); | ||
}); | ||
async function getPackageBinsFromPackageJson(pkgJson, pkgPath) { | ||
const cmds = await package_bins_1.default(pkgJson, pkgPath); | ||
return cmds.map((cmd) => (Object.assign({}, cmd, { ownName: cmd.name === pkgJson.name, pkgName: pkgJson.name }))); | ||
} | ||
function getBinNodePaths(target) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const targetRealPath = yield fs.realpath(target); | ||
return R.union(Module._nodeModulePaths(targetRealPath), Module._nodeModulePaths(target)); | ||
async function linkBin(cmd, binPath) { | ||
const externalBinPath = path.join(binPath, cmd.name); | ||
if (EXECUTABLE_SHEBANG_SUPPORTED) { | ||
await fs.chmod(cmd.path, 0o755); | ||
} | ||
const nodePath = await getBinNodePaths(cmd.path); | ||
return cmdShim(cmd.path, externalBinPath, { | ||
createPwshFile: POWER_SHELL_IS_SUPPORTED, | ||
nodePath, | ||
}); | ||
} | ||
function safeReadPkg(pkgPath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
return yield read_package_json_1.fromDir(pkgPath); | ||
async function getBinNodePaths(target) { | ||
const targetRealPath = await fs.realpath(target); | ||
return R.union(Module._nodeModulePaths(targetRealPath), Module._nodeModulePaths(target)); | ||
} | ||
async function safeReadPkg(pkgPath) { | ||
try { | ||
return await read_package_json_1.fromDir(pkgPath); | ||
} | ||
catch (err) { | ||
if (err.code === 'ENOENT') { | ||
return null; | ||
} | ||
catch (err) { | ||
if (err.code === 'ENOENT') { | ||
return null; | ||
} | ||
throw err; | ||
} | ||
}); | ||
throw err; | ||
} | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@pnpm/link-bins", | ||
"version": "3.0.3", | ||
"version": "4.0.0", | ||
"description": "Link bins to node_modules/.bin", | ||
@@ -11,3 +11,3 @@ "main": "lib/index.js", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
@@ -37,5 +37,5 @@ "scripts": { | ||
"dependencies": { | ||
"@pnpm/package-bins": "^2.0.0", | ||
"@pnpm/read-package-json": "^1.0.0", | ||
"@pnpm/types": "^2.0.0", | ||
"@pnpm/package-bins": "^3.0.0", | ||
"@pnpm/read-package-json": "^2.0.0", | ||
"@pnpm/types": "^3.0.0", | ||
"@types/mz": "^0.0.32", | ||
@@ -67,3 +67,3 @@ "@types/node": "^9.6.5 || 10", | ||
"tempy": "^0.2.1", | ||
"ts-node": "^7.0.0", | ||
"ts-node": "^8.0.0", | ||
"tslint": "^5.8.0", | ||
@@ -70,0 +70,0 @@ "typescript": "^3.0.0" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
14207
-13.22%142
-19.32%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated