-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnpm-install-fetch-cli.js
executable file
·165 lines (151 loc) · 6.68 KB
/
npm-install-fetch-cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env node
/*!
** NPM-Install-Fetch -- Fetch External Resources on NPM Package Installation
** Copyright (c) 2018-2024 Dr. Ralf S. Engelschall <[email protected]>
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* internal requirements */
const fs = require("fs")
const path = require("path")
/* external requirements */
const yargs = require("yargs")
const chalk = require("chalk")
const yaml = require("js-yaml")
const traverse = require("traverse")
/* local requirements */
const fetch = require("./npm-install-fetch-api.js")
;(async () => {
/* load my package information */
const my = require("./package.json")
/* command-line option parsing */
const argv = yargs
/* eslint indent: off */
.usage("Usage: $0 [-h] [-V] [-c <file>] [-a <arch>] [-p <platform>] [-n <name>] [-e] [-f <filter>] [-m <map-from>:<map-to>] [-s <number>] [-o <file>] [<input>]")
.help("h").alias("h", "help").default("h", false)
.describe("h", "show usage help")
.boolean("V").alias("V", "version").default("V", false)
.describe("V", "show program version information")
.string("c").alias("c", "config").default("c", "npm-install-fetch.yaml")
.describe("c", "path to YAML configuration file")
.string("a").alias("a", "arch").default("a", "*")
.describe("a", "ensure system architecture matches")
.string("p").alias("p", "platform").default("p", "*")
.describe("p", "ensure system platform matches")
.string("n").alias("n", "name").default("n", "")
.describe("n", "name of resource")
.boolean("d").alias("d", "decompress").default("d", false)
.describe("d", "decompress resource")
.boolean("e").alias("e", "extract").default("e", false)
.describe("e", "extract archive to individual files")
.string("f").nargs("f", 1).alias("f", "filter")
.describe("f", "filter extracted file(s) [requires extract option]")
.string("m").alias("m", "map")
.describe("m", "map extracted file paths [requires extract option]")
.number("s").alias("s", "strip")
.describe("s", "strip top-level directories from extracted files [requires extract option]")
.string("o").alias("o", "output")
.describe("o", "output directory or file")
.version(false)
.strict()
.showHelpOnFail(true)
.demand(0)
.parse(process.argv.slice(2))
/* short-circuit processing of "-V" command-line option */
if (argv.version) {
process.stderr.write(`${my.name} ${my.version} <${my.homepage}>\n`)
process.stderr.write(`${my.description}\n`)
process.stderr.write(`Copyright (c) 2018-2024 ${my.author.name} <${my.author.url}>\n`)
process.stderr.write(`Licensed under ${my.license} <http://spdx.org/licenses/${my.license}.html>\n`)
process.exit(0)
}
/* start assembling fetching request(s) */
let requests = []
/* take over CLI options */
if (argv._.length === 1) {
const options = {}
if (argv._.length === 1) options.input = argv._[0]
if (argv.arch) options.arch = argv.arch
if (argv.platform) options.platform = argv.platform
if (argv.name) options.name = argv.name
if (argv.decompress) options.decompress = argv.decompress
if (argv.extract) options.extract = argv.extract
if (argv.filter) options.filter = argv.filter
if (argv.strip) options.strip = argv.strip
if (argv.output) options.output = argv.output
if (argv.map) {
let list = argv.map
if (typeof list === "string")
list = [ list ]
options.map = []
list.forEach((mapping) => {
options.map.push(mapping.split(/:/))
})
}
requests.push(options)
}
/* load package.json entry */
let file = path.resolve(process.cwd(), "package.json")
let pkg = {}
if (fs.existsSync(file)) {
pkg = require(file)
if (pkg.name === "npm-install-fetch")
pkg = {}
if (typeof pkg["npm-install-fetch"] === "object") {
if (pkg["npm-install-fetch"] instanceof Array)
requests = requests.concat(pkg["npm-install-fetch"])
else
requests.push(pkg["npm-install-fetch"])
}
}
file = path.resolve(process.cwd(), argv.config)
if (fs.existsSync(file)) {
const obj = yaml.load(fs.readFileSync(file, { encoding: "utf8" }))
if (typeof obj === "object") {
if (obj instanceof Array)
requests = requests.concat(obj)
else
requests.push(obj)
}
}
/* sanity check situation */
if (requests.length === 0)
throw new Error("no resources given")
/* expand variables from "package.json" */
/* eslint array-callback-return: off */
requests = traverse(requests).map(function (val) {
if (typeof val === "string") {
const valNew = val.replace(/%\{(.+?)\}/g, (m, name) => {
let result = m
if (typeof pkg[name] === "string")
result = pkg[name]
return result
})
if (valNew !== val)
this.update(valNew)
}
})
/* pass-through execution to API */
await fetch(requests)
})().catch((err) => {
/* fatal error */
process.stderr.write(`npm-install-fetch: ${chalk.red("ERROR:")} ${err.message}\n`)
process.exit(1)
})