UNPKG

3.04 kBPlain TextView Raw
1import * as fs from 'fs-extra-plus';
2import * as jsyaml from 'js-yaml';
3import { ParsedArgs } from 'minimist';
4import stripJsonComments from 'strip-json-comments';
5import { split } from 'utils-min';
6
7
8// --------- Lang & Type Utils --------- //
9
10export type Partial<T> = {
11 [P in keyof T]?: T[P];
12}
13
14// for the bin-vdev & cmd-*
15export type CmdMap = { [fnName: string]: (miniArgv: ParsedArgs, rawArgv: string[]) => void }
16
17/**
18 * Split the eventual comman deliminated string as array of trim items.
19 * If array just return the array, if null, return empty array
20 * @param srcNames comma deliminated, single name, or array of names
21 */
22export function asNames(srcNames?: string | string[] | null) {
23 if (srcNames) {
24 if (typeof srcNames === 'string') {
25 return split(srcNames, ',');
26 } else {
27 return srcNames;
28 }
29 } else {
30 return [];
31 }
32}
33// --------- /Lang & Type Utils --------- //
34
35
36// --------- Utils --------- //
37export async function yaml(content: string) {
38 const yamlObj = jsyaml.load(content);
39 if (!yamlObj) {
40 throw new Error(`Could not load yaml`);
41 }
42 return yamlObj;
43}
44
45export async function loadYaml(path: string) {
46 const yamlContent = await fs.readFile(path, 'utf8');
47 return yaml(yamlContent);
48}
49
50export async function readJsonFileWithComments(path: string) {
51 const content = await fs.readFile(path, 'utf8');
52 return JSON.parse(stripJsonComments(content));
53}
54
55
56// return now in milliseconds using high precision
57export function now() {
58 var hrTime = process.hrtime();
59 return hrTime[0] * 1000 + hrTime[1] / 1000000;
60}
61
62export async function printLog(txt: string, start: number, dist?: string | null) {
63 const timeStr = Math.round(now() - start) + 'ms';
64
65 let msg = `${txt} - `;
66
67 if (dist == null) {
68 msg += timeStr;
69 } else {
70 const distExists = await fs.pathExists(dist);
71 if (distExists) {
72 let size = (await fs.stat(dist)).size;
73 size = Math.round(size / 1000.0);
74 msg += `${dist} - ${timeStr} - ${size} kb`;
75 } else {
76 msg += `${dist} - ${timeStr} - ... watch mode started ...`
77 }
78 }
79 console.log(msg);
80}
81
82export async function prompt(message: string) {
83 // console.log(`\n${message}: `);
84 process.stdout.write(`\n${message}: `);
85 return new Promise(function (resolve, reject) {
86 process.stdin.resume();
87 process.stdin.setEncoding('utf8');
88 process.stdin.on('data', function (text) {
89 process.stdin.pause();
90 resolve(text.toString().trim());
91 });
92 });
93}
94
95/** Attempted to return the obj value given a dottedNamePath (i.e. 'author.name').
96 * @returns undefined if nothing found or obj or dottedNamePath is null/undefined. Return obj if dottedNamePath == ''.
97 **/
98export function findVal(obj: any, dottedNamePath: string) {
99 if (obj == null || dottedNamePath == null) {
100 return;
101 }
102 if (dottedNamePath.trim() === '') {
103 return obj;
104 }
105
106 let val: any = obj;
107 const names = dottedNamePath.split('.');
108 for (let name of names) {
109 val = val[name];
110 if (val == null) { // if null or undefined, stop and return
111 return val;
112 }
113 }
114 return val;
115}
116// --------- /Utils --------- //
117
118
119
120
121
122