1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const Path = require("path");
|
4 | const fs = require("fs-extra-plus");
|
5 | const jsyaml = require("js-yaml");
|
6 | const stripJsonComments = require("strip-json-comments");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function asNames(srcNames) {
|
13 | if (srcNames) {
|
14 | if (typeof srcNames === 'string') {
|
15 | return srcNames.split(',').map((s) => s.trim());
|
16 | }
|
17 | else {
|
18 | return srcNames;
|
19 | }
|
20 | }
|
21 | else {
|
22 | return [];
|
23 | }
|
24 | }
|
25 | exports.asNames = asNames;
|
26 |
|
27 |
|
28 | async function yaml(content) {
|
29 | const yamlObj = jsyaml.load(content);
|
30 | if (!yamlObj) {
|
31 | throw new Error(`Could not load yaml`);
|
32 | }
|
33 | return yamlObj;
|
34 | }
|
35 | exports.yaml = yaml;
|
36 | async function loadYaml(path) {
|
37 | const yamlContent = await fs.readFile(path, 'utf8');
|
38 | return yaml(yamlContent);
|
39 | }
|
40 | exports.loadYaml = loadYaml;
|
41 | async function readJsonFileWithComments(path) {
|
42 | const content = await fs.readFile(path, 'utf8');
|
43 | return JSON.parse(stripJsonComments(content));
|
44 | }
|
45 | exports.readJsonFileWithComments = readJsonFileWithComments;
|
46 | async function saferRemove(relPath, log) {
|
47 | log = (log !== false);
|
48 | const baseDir = Path.resolve('./');
|
49 | const fullPath = Path.resolve(relPath);
|
50 | if (!fullPath.startsWith(baseDir)) {
|
51 | throw new Error(`Path to be removed does not look safe (nothing done): ${fullPath}`);
|
52 | }
|
53 | const exists = await fs.pathExists(fullPath);
|
54 | if (exists) {
|
55 | if (log) {
|
56 | console.log(`Deleting: ${relPath}`);
|
57 | }
|
58 | return fs.remove(fullPath);
|
59 | }
|
60 | else {
|
61 | if (log) {
|
62 | console.log(`Already deleted: ${relPath}`);
|
63 | }
|
64 | }
|
65 | }
|
66 | exports.saferRemove = saferRemove;
|
67 | async function wait(ms) {
|
68 | return new Promise(function (resolve) {
|
69 | setTimeout(() => { resolve(); }, ms);
|
70 | });
|
71 | }
|
72 | exports.wait = wait;
|
73 |
|
74 | function now() {
|
75 | var hrTime = process.hrtime();
|
76 | return hrTime[0] * 1000 + hrTime[1] / 1000000;
|
77 | }
|
78 | exports.now = now;
|
79 | async function printLog(txt, dist, start) {
|
80 | const timeStr = Math.round(now() - start) + 'ms';
|
81 | let msg = `${txt} - `;
|
82 | if (dist) {
|
83 | let size = (await fs.stat(dist)).size;
|
84 | size = Math.round(size / 1000.0);
|
85 | msg += `${dist} - ${timeStr} - ${size} kb`;
|
86 | }
|
87 | else {
|
88 | msg += `${timeStr}`;
|
89 | }
|
90 | console.log(msg);
|
91 | }
|
92 | exports.printLog = printLog;
|
93 | async function prompt(message) {
|
94 |
|
95 | process.stdout.write(`\n${message}: `);
|
96 | return new Promise(function (resolve, reject) {
|
97 | process.stdin.resume();
|
98 | process.stdin.setEncoding('utf8');
|
99 | process.stdin.on('data', function (text) {
|
100 | process.stdin.pause();
|
101 | resolve(text.trim());
|
102 | });
|
103 | });
|
104 | }
|
105 | exports.prompt = prompt;
|
106 |
|
107 |
|
108 |
|
109 | function findVal(obj, dottedNamePath) {
|
110 | if (obj == null || dottedNamePath == null) {
|
111 | return;
|
112 | }
|
113 | if (dottedNamePath.trim() === '') {
|
114 | return obj;
|
115 | }
|
116 | let val = obj;
|
117 | const names = dottedNamePath.split('.');
|
118 | for (let name of names) {
|
119 | val = val[name];
|
120 | if (val == null) {
|
121 | return val;
|
122 | }
|
123 | }
|
124 | return val;
|
125 | }
|
126 | exports.findVal = findVal;
|
127 |
|