1 | import * as fs from 'fs-extra';
|
2 | import * as jsyaml from 'js-yaml';
|
3 | import { ParsedArgs } from 'minimist';
|
4 | import stripJsonComments from 'strip-json-comments';
|
5 | import { split } from 'utils-min';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export type Partial<T> = {
|
11 | [P in keyof T]?: T[P];
|
12 | }
|
13 |
|
14 |
|
15 | export type CmdMap = { [fnName: string]: (miniArgv: ParsedArgs, rawArgv: string[]) => void }
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | export 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 |
|
34 |
|
35 |
|
36 |
|
37 | export 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 |
|
45 | export async function loadYaml(path: string) {
|
46 | const yamlContent = await fs.readFile(path, 'utf8');
|
47 | return yaml(yamlContent);
|
48 | }
|
49 |
|
50 | export async function readJsonFileWithComments(path: string) {
|
51 | const content = await fs.readFile(path, 'utf8');
|
52 | return JSON.parse(stripJsonComments(content));
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 | export function now() {
|
58 | var hrTime = process.hrtime();
|
59 | return hrTime[0] * 1000 + hrTime[1] / 1000000;
|
60 | }
|
61 |
|
62 | export 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 |
|
82 | export async function prompt(message: string) {
|
83 |
|
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 |
|
96 |
|
97 |
|
98 | export 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) {
|
111 | return val;
|
112 | }
|
113 | }
|
114 | return val;
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|