UNPKG

3.82 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Path = require("path");
4const fs = require("fs-extra-plus");
5const jsyaml = require("js-yaml");
6const stripJsonComments = require("strip-json-comments");
7/**
8 * Split the eventual comman deliminated string as array of trim items.
9 * If array just return the array, if null, return empty array
10 * @param srcNames comma deliminated, single name, or array of names
11 */
12function 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}
25exports.asNames = asNames;
26// --------- /Lang & Type Utils --------- //
27// --------- Utils --------- //
28async 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}
35exports.yaml = yaml;
36async function loadYaml(path) {
37 const yamlContent = await fs.readFile(path, 'utf8');
38 return yaml(yamlContent);
39}
40exports.loadYaml = loadYaml;
41async function readJsonFileWithComments(path) {
42 const content = await fs.readFile(path, 'utf8');
43 return JSON.parse(stripJsonComments(content));
44}
45exports.readJsonFileWithComments = readJsonFileWithComments;
46async function saferRemove(relPath, log) {
47 log = (log !== false); // default is true
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}
66exports.saferRemove = saferRemove;
67async function wait(ms) {
68 return new Promise(function (resolve) {
69 setTimeout(() => { resolve(); }, ms);
70 });
71}
72exports.wait = wait;
73// return now in milliseconds using high precision
74function now() {
75 var hrTime = process.hrtime();
76 return hrTime[0] * 1000 + hrTime[1] / 1000000;
77}
78exports.now = now;
79async 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}
92exports.printLog = printLog;
93async function prompt(message) {
94 // console.log(`\n${message}: `);
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}
105exports.prompt = prompt;
106/** Attempted to return the obj value given a dottedNamePath (i.e. 'author.name').
107 * @returns undefined if nothing found or obj or dottedNamePath is null/undefined. Return obj if dottedNamePath == ''.
108 **/
109function 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) { // if null or undefined, stop and return
121 return val;
122 }
123 }
124 return val;
125}
126exports.findVal = findVal;
127// --------- /Utils --------- //