5
0
node-binaries/node/node_modules/npm-js-interface/index.js
2025-01-10 23:02:45 +08:00

60 lines
1.4 KiB
JavaScript

function splitLine(line) {
if (line.indexOf(" ") == -1) {
return [line]
}
const args = []
let current = ""
let inQuotes = false
for (let i = 0; i < line.length; i++) {
const c = line[i]
if (c == '"') {
inQuotes = !inQuotes
}
else if (c == ' ' && !inQuotes) {
args.push(current)
current = ""
}
else {
current += c
}
}
args.push(current)
return args
}
module.exports = async (cmdLine) => {
const Npm = require('../npm/lib/npm.js')
const npm = new Npm()
try {
await npm.load()
if (cmdLine.length == 0) {
console.log(npm.usage)
return false
}
const args = splitLine(cmdLine)
if (args[0].indexOf("npm") != -1) {
args.shift()
}
const cmd = args.shift()
if (!cmd) {
console.log(`Unknown command: "${cmd}"\nTo see a list of supported npm commands, run:\n npm help`)
return false
}
await npm.exec(cmd, args)
}
catch (err) {
if (err.code === 'EUNKNOWNCOMMAND') {
console.log(`Bad command.\nTo see a list of supported npm commands, run:\n npm help`)
return false
}
console.log(`Error when executing npm command. ${err.message}\n\n${err.stack}\n`)
return false
}
return true
}