Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Apr 6, 2022
1 parent ad99360 commit 79fc706
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 119 deletions.
10 changes: 5 additions & 5 deletions node_modules/bin-links/lib/check-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ const checkShim = async ({ target, path }) => {
target + '.cmd',
target + '.ps1',
]
await Promise.all(shims.map(async target => {
const current = await readCmdShim(target)
.catch(er => handleReadCmdShimError({ er, target }))
await Promise.all(shims.map(async shim => {
const current = await readCmdShim(shim)
.catch(er => handleReadCmdShimError({ er, target: shim }))

if (!current) {
return
}

const resolved = resolve(dirname(target), current.replace(/\\/g, '/'))
const resolved = resolve(dirname(shim), current.replace(/\\/g, '/'))

if (resolved.toLowerCase().indexOf(path.toLowerCase()) !== 0) {
return failEEXIST({ target })
return failEEXIST({ target: shim })
}
}))
}
Expand Down
19 changes: 5 additions & 14 deletions node_modules/bin-links/lib/get-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// are present, then we can assume that they're associated.
const binTarget = require('./bin-target.js')
const manTarget = require('./man-target.js')
const { resolve, basename } = require('path')
const { resolve, basename, extname } = require('path')
const isWindows = require('./is-windows.js')
module.exports = ({ path, pkg, global, top }) => {
if (top && !global) {
Expand All @@ -27,23 +27,14 @@ module.exports = ({ path, pkg, global, top }) => {
const manSet = []
if (manTarg && pkg.man && Array.isArray(pkg.man) && pkg.man.length) {
for (const man of pkg.man) {
const parseMan = man.match(/(.*\.([0-9]+)(\.gz)?)$/)
// invalid entries invalidate the entire man set
if (!parseMan) {
if (!/.\.[0-9]+(\.gz)?$/.test(man)) {
return binSet
}

const stem = parseMan[1]
const sxn = parseMan[2]
const base = basename(stem)
const absFrom = resolve(path, man)
const section = extname(basename(man, '.gz')).slice(1)
const base = basename(man)

/* istanbul ignore if - should be impossible */
if (absFrom.indexOf(path) !== 0) {
return binSet
}

manSet.push(resolve(manTarg, 'man' + sxn, base))
manSet.push(resolve(manTarg, 'man' + section, base))
}
}

Expand Down
2 changes: 1 addition & 1 deletion node_modules/bin-links/lib/link-mans.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const linkMans = ({ path, pkg, top, force }) => {
// break any links to c:\\blah or /foo/blah or ../blah
// and filter out duplicates
const set = [...new Set(pkg.man.map(man =>
man ? join('/', man).replace(/\\|:/g, '/').substr(1) : null)
man ? join('/', man).replace(/\\|:/g, '/').slice(1) : null)
.filter(man => typeof man === 'string'))]

return Promise.all(set.map(man => {
Expand Down
27 changes: 15 additions & 12 deletions node_modules/bin-links/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bin-links",
"version": "3.0.0",
"version": "3.0.1",
"description": "JavaScript package binary linker",
"main": "./lib/index.js",
"scripts": {
Expand All @@ -9,14 +9,15 @@
"prepublishOnly": "git push origin --follow-tags",
"snap": "tap",
"test": "tap",
"lint": "eslint '**/*.js'",
"postlint": "npm-template-check",
"lint": "eslint \"**/*.js\"",
"postlint": "template-oss-check",
"lintfix": "npm run lint -- --fix",
"posttest": "npm run lint"
"posttest": "npm run lint",
"template-oss-apply": "template-oss-apply --force"
},
"repository": {
"type": "git",
"url": "git://github.com/npm/bin-links.git"
"url": "https://github.com/npm/bin-links.git"
},
"keywords": [
"npm",
Expand All @@ -25,15 +26,16 @@
],
"license": "ISC",
"dependencies": {
"cmd-shim": "^4.0.1",
"cmd-shim": "^5.0.0",
"mkdirp-infer-owner": "^2.0.0",
"npm-normalize-package-bin": "^1.0.0",
"read-cmd-shim": "^2.0.0",
"read-cmd-shim": "^3.0.0",
"rimraf": "^3.0.0",
"write-file-atomic": "^4.0.0"
},
"devDependencies": {
"@npmcli/template-oss": "^2.5.0",
"@npmcli/eslint-config": "^3.0.1",
"@npmcli/template-oss": "3.2.2",
"mkdirp": "^1.0.3",
"require-inject": "^1.4.4",
"tap": "^15.0.10"
Expand All @@ -43,15 +45,16 @@
"coverage-map": "map.js"
},
"files": [
"bin",
"lib"
"bin/",
"lib/"
],
"engines": {
"node": "^12.13.0 || ^14.15.0 || >=16"
"node": "^12.13.0 || ^14.15.0 || >=16.0.0"
},
"author": "GitHub Inc.",
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
"windowsCI": false,
"version": "2.5.0"
"version": "3.2.2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
// Write a binroot/pkg.bin + ".cmd" file that has this line in it:
// @<prog> <args...> %dp0%<target> %*

const {promisify} = require('util')
const { promisify } = require('util')
const fs = require('fs')
const writeFile = promisify(fs.writeFile)
const readFile = promisify(fs.readFile)
const chmod = promisify(fs.chmod)
const stat = promisify(fs.stat)
const unlink = promisify(fs.unlink)

const {dirname, relative} = require('path')
const { dirname, relative } = require('path')
const mkdir = require('mkdirp-infer-owner')
const toBatchSyntax = require('./lib/to-batch-syntax')
const shebangExpr = /^#\!\s*(?:\/usr\/bin\/env)?\s*([^ \t]+=[^ \t]+\s+)*\s*([^ \t]+)(.*)$/
const toBatchSyntax = require('./to-batch-syntax')
const shebangExpr = /^#!\s*(?:\/usr\/bin\/env\s*((?:[^ \t=]+=[^ \t=]+\s+)*))?([^ \t]+)(.*)$/

const cmdShimIfExists = (from, to) =>
stat(from).then(() => cmdShim(from, to), () => {})
Expand Down Expand Up @@ -47,14 +47,15 @@ const writeShim = (from, to) =>
.then(data => {
const firstLine = data.trim().split(/\r*\n/)[0]
const shebang = firstLine.match(shebangExpr)
if (!shebang) return writeShim_(from, to)
if (!shebang) {
return writeShim_(from, to)
}
const vars = shebang[1] || ''
const prog = shebang[2]
const args = shebang[3] || ''
return writeShim_(from, to, prog, args, vars)
}, er => writeShim_(from, to))


const writeShim_ = (from, to, prog, args, variables) => {
let shTarget = relative(dirname(to), from)
let target = shTarget.split('/').join('\\')
Expand Down Expand Up @@ -94,8 +95,8 @@ const writeShim_ = (from, to, prog, args, variables) => {

let cmd
if (longProg) {
shLongProg = shLongProg.trim();
args = args.trim();
shLongProg = shLongProg.trim()
args = args.trim()
const variablesBatch = toBatchSyntax.convertToSetCommands(variables)
cmd = head
+ variablesBatch
Expand All @@ -110,7 +111,7 @@ const writeShim_ = (from, to, prog, args, variables) => {
// prevent "Terminate Batch Job? (Y/n)" message
// https://github.com/npm/cli/issues/969#issuecomment-737496588
+ 'endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & '
+ `"%_prog%" ${args} ${target} %*\r\n`
+ `"%_prog%" ${args} ${target} %*\r\n`
} else {
cmd = `${head}${prog} ${args} ${target} %*\r\n`
}
Expand All @@ -128,7 +129,7 @@ const writeShim_ = (from, to, prog, args, variables) => {
// exec node "$basedir/node_modules/npm/bin/npm-cli.js" "$@"
// fi

let sh = "#!/bin/sh\n"
let sh = '#!/bin/sh\n'

sh = sh
+ `basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")\n`
Expand Down Expand Up @@ -182,7 +183,7 @@ const writeShim_ = (from, to, prog, args, variables) => {
+ '$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent\n'
+ '\n'
+ '$exe=""\n'
+ 'if ($PSVersionTable.PSVersion -lt \"6.0\" -or $IsWindows) {\n'
+ 'if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {\n'
+ ' # Fix case when both the Windows and Linux builds of Node\n'
+ ' # are installed in the same directory\n'
+ ' $exe=".exe"\n'
Expand Down
78 changes: 38 additions & 40 deletions node_modules/cmd-shim/lib/to-batch-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,48 @@ exports.replaceDollarWithPercentPair = replaceDollarWithPercentPair
exports.convertToSetCommand = convertToSetCommand
exports.convertToSetCommands = convertToSetCommands

function convertToSetCommand(key, value) {
var line = ""
key = key || ""
key = key.trim()
value = value || ""
value = value.trim()
if(key && value && value.length > 0) {
line = "@SET " + key + "=" + replaceDollarWithPercentPair(value) + "\r\n"
}
return line
function convertToSetCommand (key, value) {
var line = ''
key = key || ''
key = key.trim()
value = value || ''
value = value.trim()
if (key && value && value.length > 0) {
line = '@SET ' + key + '=' + replaceDollarWithPercentPair(value) + '\r\n'
}
return line
}

function extractVariableValuePairs(declarations) {
var pairs = {}
declarations.map(function(declaration) {
var split = declaration.split("=")
pairs[split[0]]=split[1]
})
return pairs
function extractVariableValuePairs (declarations) {
var pairs = {}
declarations.map(function (declaration) {
var split = declaration.split('=')
pairs[split[0]] = split[1]
})
return pairs
}

function convertToSetCommands(variableString) {
var variableValuePairs = extractVariableValuePairs(variableString.split(" "))
var variableDeclarationsAsBatch = ""
Object.keys(variableValuePairs).forEach(function (key) {
variableDeclarationsAsBatch += convertToSetCommand(key, variableValuePairs[key])
})
return variableDeclarationsAsBatch
function convertToSetCommands (variableString) {
var variableValuePairs = extractVariableValuePairs(variableString.split(' '))
var variableDeclarationsAsBatch = ''
Object.keys(variableValuePairs).forEach(function (key) {
variableDeclarationsAsBatch += convertToSetCommand(key, variableValuePairs[key])
})
return variableDeclarationsAsBatch
}

function replaceDollarWithPercentPair(value) {
var dollarExpressions = /\$\{?([^\$@#\?\- \t{}:]+)\}?/g
var result = ""
var startIndex = 0
do {
var match = dollarExpressions.exec(value)
if(match) {
var betweenMatches = value.substring(startIndex, match.index) || ""
result += betweenMatches + "%" + match[1] + "%"
startIndex = dollarExpressions.lastIndex
}
} while (dollarExpressions.lastIndex > 0)
result += value.substr(startIndex)
return result
function replaceDollarWithPercentPair (value) {
var dollarExpressions = /\$\{?([^$@#?\- \t{}:]+)\}?/g
var result = ''
var startIndex = 0
do {
var match = dollarExpressions.exec(value)
if (match) {
var betweenMatches = value.substring(startIndex, match.index) || ''
result += betweenMatches + '%' + match[1] + '%'
startIndex = dollarExpressions.lastIndex
}
} while (dollarExpressions.lastIndex > 0)
result += value.slice(startIndex)
return result
}


28 changes: 21 additions & 7 deletions node_modules/cmd-shim/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
{
"name": "cmd-shim",
"version": "4.1.0",
"version": "5.0.0",
"description": "Used in npm for command line application support",
"scripts": {
"test": "tap",
"snap": "tap",
"preversion": "npm test",
"postversion": "npm publish",
"postpublish": "git push origin --follow-tags"
"postpublish": "git push origin --follow-tags",
"lint": "eslint \"**/*.js\"",
"postlint": "template-oss-check",
"template-oss-apply": "template-oss-apply --force",
"lintfix": "npm run lint -- --fix",
"prepublishOnly": "git push origin --follow-tags",
"posttest": "npm run lint"
},
"repository": {
"type": "git",
Expand All @@ -18,19 +24,27 @@
"mkdirp-infer-owner": "^2.0.0"
},
"devDependencies": {
"rimraf": "~2.2.8",
"tap": "^14.10.6"
"@npmcli/eslint-config": "^3.0.1",
"@npmcli/template-oss": "3.2.2",
"rimraf": "^3.0.2",
"tap": "^16.0.1"
},
"files": [
"index.js",
"lib"
"bin/",
"lib/"
],
"main": "lib/index.js",
"tap": {
"before": "test/00-setup.js",
"after": "test/zz-cleanup.js",
"check-coverage": true
},
"engines": {
"node": ">=10"
"node": "^12.13.0 || ^14.15.0 || >=16.0.0"
},
"author": "GitHub Inc.",
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
"version": "3.2.2"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs')
const {promisify} = require('util')
const {readFileSync} = fs
const { promisify } = require('util')
const { readFileSync } = fs
const readFile = promisify(fs.readFile)

const extractPath = (path, cmdshimContents) => {
Expand Down Expand Up @@ -53,15 +53,19 @@ const readCmdShim = path => {
Error.captureStackTrace(er, readCmdShim)
return readFile(path).then(contents => {
const destination = extractPath(path, contents.toString())
if (destination) return destination
if (destination) {
return destination
}
return Promise.reject(notaShim(path, er))
}, readFileEr => Promise.reject(wrapError(readFileEr, er)))
}

const readCmdShimSync = path => {
const contents = readFileSync(path)
const destination = extractPath(path, contents.toString())
if (!destination) throw notaShim(path)
if (!destination) {
throw notaShim(path)
}
return destination
}

Expand Down
Loading

0 comments on commit 79fc706

Please sign in to comment.