UNPKG

4.7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getConfig = exports.getNpmExecPath = exports.endPrintVerbose = exports.printVerbose = exports.execFileAsync = exports.readJSONFile = exports.isPkgData = exports.relativePath = exports.isObject = void 0;
4const command_join_1 = require("command-join");
5const fs = require("fs");
6const path = require("path");
7const util_1 = require("util");
8const crossSpawn = require("cross-spawn");
9const readFileAsync = util_1.promisify(fs.readFile);
10function isObject(value) {
11 return typeof value === 'object' && value !== null;
12}
13exports.isObject = isObject;
14function relativePath(pathStr) {
15 const relativePathStr = path.relative(process.cwd(), pathStr);
16 return path.isAbsolute(relativePathStr) || relativePathStr.startsWith('.')
17 ? relativePathStr
18 : `.${path.sep}${relativePathStr}`;
19}
20exports.relativePath = relativePath;
21function isPkgData(value) {
22 if (isObject(value)) {
23 return typeof value.version === 'string';
24 }
25 return false;
26}
27exports.isPkgData = isPkgData;
28async 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}
42exports.readJSONFile = readJSONFile;
43/**
44 * @see https://github.com/nodejs/node/blob/v12.13.0/lib/child_process.js#L250-L303
45 */
46function 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 * @see https://github.com/nodejs/node/blob/v12.13.0/lib/child_process.js#L305-L315
64 */
65function 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 * @see https://github.com/nodejs/node/blob/v12.13.0/lib/child_process.js#L178-L390
78 */
79async 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}
101exports.execFileAsync = execFileAsync;
102let isPrintedVerbose = false;
103function printVerbose(message) {
104 if (!isPrintedVerbose) {
105 console.error(`\n${message}`);
106 isPrintedVerbose = true;
107 }
108 else {
109 console.error(message);
110 }
111}
112exports.printVerbose = printVerbose;
113function endPrintVerbose() {
114 if (isPrintedVerbose) {
115 console.error();
116 }
117}
118exports.endPrintVerbose = endPrintVerbose;
119/**
120 * @see https://github.com/mysticatea/npm-run-all/blob/v4.1.5/lib/run-task.js#L157-L174
121 */
122function 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}
133exports.getNpmExecPath = getNpmExecPath;
134async 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}
144exports.getConfig = getConfig;
145//# sourceMappingURL=utils.js.map
\No newline at end of file