1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getConfig = exports.getNpmExecPath = exports.endPrintVerbose = exports.printVerbose = exports.execFileAsync = exports.readJSONFile = exports.isPkgData = exports.relativePath = exports.isObject = void 0;
|
4 | const command_join_1 = require("command-join");
|
5 | const fs = require("fs");
|
6 | const path = require("path");
|
7 | const util_1 = require("util");
|
8 | const crossSpawn = require("cross-spawn");
|
9 | const readFileAsync = util_1.promisify(fs.readFile);
|
10 | function isObject(value) {
|
11 | return typeof value === 'object' && value !== null;
|
12 | }
|
13 | exports.isObject = isObject;
|
14 | function relativePath(pathStr) {
|
15 | const relativePathStr = path.relative(process.cwd(), pathStr);
|
16 | return path.isAbsolute(relativePathStr) || relativePathStr.startsWith('.')
|
17 | ? relativePathStr
|
18 | : `.${path.sep}${relativePathStr}`;
|
19 | }
|
20 | exports.relativePath = relativePath;
|
21 | function isPkgData(value) {
|
22 | if (isObject(value)) {
|
23 | return typeof value.version === 'string';
|
24 | }
|
25 | return false;
|
26 | }
|
27 | exports.isPkgData = isPkgData;
|
28 | async function readJSONFile(filepath) {
|
29 | try {
|
30 | const dataText = await readFileAsync(filepath, 'utf8');
|
31 | try {
|
32 | return JSON.parse(dataText);
|
33 | }
|
34 | catch (error) {
|
35 | throw new Error(`Invalid JSON: ${relativePath(filepath)}`);
|
36 | }
|
37 | }
|
38 | catch (error) {
|
39 | throw new Error(`Could not read file: ${relativePath(filepath)}`);
|
40 | }
|
41 | }
|
42 | exports.readJSONFile = readJSONFile;
|
43 |
|
44 |
|
45 |
|
46 | function execExithandler({ command, args = [], stdoutList, stderrList, resolve, reject, }) {
|
47 | return (code, signal) => {
|
48 | const stdout = stdoutList.join('');
|
49 | const stderr = stderrList.join('');
|
50 | if (code === 0 && signal === null) {
|
51 | resolve({ stdout, stderr });
|
52 | return;
|
53 | }
|
54 | let cmd = command;
|
55 | if (args.length > 0) {
|
56 | cmd += ` ${command_join_1.commandJoin(args)}`;
|
57 | }
|
58 | const error = new Error(`Command failed: ${cmd}\n${stderr}`);
|
59 | reject(error);
|
60 | };
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 | function execErrorhandler({ process, reject, }) {
|
66 | return (error) => {
|
67 | if (process.stdout) {
|
68 | process.stdout.destroy();
|
69 | }
|
70 | if (process.stderr) {
|
71 | process.stderr.destroy();
|
72 | }
|
73 | reject(error);
|
74 | };
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 | async function execFileAsync(...args) {
|
80 | return new Promise((resolve, reject) => {
|
81 | const process = crossSpawn(...args);
|
82 | const stdoutList = [];
|
83 | const stderrList = [];
|
84 | if (process.stdout) {
|
85 | process.stdout.on('data', (data) => stdoutList.push(data));
|
86 | }
|
87 | if (process.stderr) {
|
88 | process.stderr.on('data', (data) => stderrList.push(data));
|
89 | }
|
90 | process.on('close', execExithandler({
|
91 | command: args[0],
|
92 | args: args[1] || [],
|
93 | stdoutList,
|
94 | stderrList,
|
95 | resolve,
|
96 | reject,
|
97 | }));
|
98 | process.on('error', execErrorhandler({ process, reject }));
|
99 | });
|
100 | }
|
101 | exports.execFileAsync = execFileAsync;
|
102 | let isPrintedVerbose = false;
|
103 | function printVerbose(message) {
|
104 | if (!isPrintedVerbose) {
|
105 | console.error(`\n${message}`);
|
106 | isPrintedVerbose = true;
|
107 | }
|
108 | else {
|
109 | console.error(message);
|
110 | }
|
111 | }
|
112 | exports.printVerbose = printVerbose;
|
113 | function endPrintVerbose() {
|
114 | if (isPrintedVerbose) {
|
115 | console.error();
|
116 | }
|
117 | }
|
118 | exports.endPrintVerbose = endPrintVerbose;
|
119 |
|
120 |
|
121 |
|
122 | function getNpmExecPath() {
|
123 | const npmPath = process.env.npm_execpath;
|
124 | const npmPathIsJs = typeof npmPath === 'string' && /^\.m?js$/.test(path.extname(npmPath));
|
125 | const execPath = npmPathIsJs ? process.execPath : npmPath || 'npm';
|
126 | const isYarn = path.basename(npmPath || 'npm').startsWith('yarn');
|
127 | return {
|
128 | execPath,
|
129 | spawnArgs: typeof npmPath === 'string' && npmPathIsJs ? [npmPath] : [],
|
130 | isYarn,
|
131 | };
|
132 | }
|
133 | exports.getNpmExecPath = getNpmExecPath;
|
134 | async function getConfig(keyMap) {
|
135 | const { execPath, spawnArgs, isYarn } = getNpmExecPath();
|
136 | const { stdout } = await execFileAsync(execPath, [
|
137 | ...spawnArgs,
|
138 | 'config',
|
139 | 'get',
|
140 | (isYarn && keyMap.yarn) || keyMap.npm,
|
141 | ]);
|
142 | return stdout.replace(/\n$/, '');
|
143 | }
|
144 | exports.getConfig = getConfig;
|
145 |
|
\ | No newline at end of file |