main
js 62 lines 1.38 KB
Raw
1 'use strict';
2
3 const fs = require('fs');
4 const path = require('path');
5 const {execFile} = require('child_process');
6 const {promisify} = require('util');
7
8 const execFileAsync = promisify(execFile);
9 const repoRoot = path.resolve(__dirname, '..', '..', '..');
10
11 function isCommandAvailable(command) {
12 const paths = (process.env.PATH || '').split(path.delimiter);
13 const extensions =
14 process.platform === 'win32' && process.env.PATHEXT
15 ? process.env.PATHEXT.split(';')
16 : [''];
17
18 for (let i = 0; i < paths.length; i++) {
19 const dir = paths[i];
20 if (!dir) {
21 continue;
22 }
23 for (let j = 0; j < extensions.length; j++) {
24 const ext = extensions[j];
25 const fullPath = path.join(dir, `${command}${ext}`);
26 try {
27 fs.accessSync(fullPath, fs.constants.X_OK);
28 return true;
29 } catch {
30 // Keep searching.
31 }
32 }
33 }
34 return false;
35 }
36
37 function noopLogger() {}
38
39 function escapeCsvValue(value) {
40 if (value == null) {
41 return '';
42 }
43
44 const stringValue = String(value).replace(/\r?\n|\r/g, ' ');
45 if (stringValue.includes('"') || stringValue.includes(',')) {
46 return `"${stringValue.replace(/"/g, '""')}"`;
47 }
48 return stringValue;
49 }
50
51 function toCsvRow(values) {
52 return values.map(escapeCsvValue).join(',');
53 }
54
55 module.exports = {
56 execFileAsync,
57 repoRoot,
58 isCommandAvailable,
59 noopLogger,
60 escapeCsvValue,
61 toCsvRow,
62 };