-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
index.ts
90 lines (83 loc) · 2.74 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import chalk from "chalk";
import * as envinfo from "envinfo";
import * as process from "process";
import { argv } from "./options";
import { AVAILABLE_COMMANDS, AVAILABLE_FORMATS, IGNORE_FLAGS } from "./commands";
import { configReader, fetchConfig, resolveFilePath, getNameFromPath } from "./configParser";
import { renderTable } from "./renderTable";
interface Information {
Binaries?: string[];
Browsers?: string[];
System?: string[];
npmGlobalPackages?: string[];
npmPackages?: string | string[];
}
interface ArgvI {
_?: string[];
bin?: boolean;
binaries?: boolean;
config?: string;
}
const CONFIG = {};
const DEFAULT_DETAILS: Information = {
Binaries: ["Node", "Yarn", "npm"],
Browsers: ["Chrome", "Firefox", "Safari"],
System: ["OS", "CPU", "Memory"],
npmGlobalPackages: ["webpack", "webpack-cli"],
npmPackages: "*webpack*"
};
let DETAILS_OBJ = {};
export function informationType(type: string): Information {
switch (type) {
case "system":
return { System: ["OS", "CPU", "Memory"] };
case "binaries":
return { Binaries: ["Node", "Yarn", "npm"] };
case "browsers":
return { Browsers: ["Chrome", "Firefox", "Safari"] };
case "npmg":
return { npmGlobalPackages: ["webpack", "webpack-cli"] };
case "npmPackages":
return { npmPackages: "*webpack*" };
}
}
export default async function info(CustomArgv: object): Promise<string[]> {
const CUSTOM_AGRUMENTS: boolean = typeof CustomArgv === "object";
const args: ArgvI = CUSTOM_AGRUMENTS ? CustomArgv : argv;
const configRelativePath = argv._[1] ? argv._[1] : args.config;
if (configRelativePath) {
const fullConfigPath = resolveFilePath(configRelativePath);
const fileName = getNameFromPath(fullConfigPath);
const config = fetchConfig(fullConfigPath);
const parsedConfig = configReader(config);
const stringifiedTable = renderTable(parsedConfig, fileName);
if (args.config) return parsedConfig;
else process.stdout.write(stringifiedTable + "\n");
} else {
Object.keys(args).forEach((flag: string) => {
if (IGNORE_FLAGS.includes(flag)) {
return;
} else if (AVAILABLE_COMMANDS.includes(flag)) {
const flagVal = informationType(flag);
DETAILS_OBJ = { ...DETAILS_OBJ, ...flagVal };
} else if (AVAILABLE_FORMATS.includes(flag)) {
switch (flag) {
case "output-json":
CONFIG["json"] = true;
break;
case "output-markdown":
CONFIG["markdown"] = true;
break;
}
} else {
// Invalid option
process.stdout.write("\n" + chalk.bgRed(flag) + chalk.red(" is an invalid option" + "\n"));
return;
}
});
const OUTPUT = await envinfo.run(Object.keys(DETAILS_OBJ).length ? DETAILS_OBJ : DEFAULT_DETAILS, CONFIG);
!CUSTOM_AGRUMENTS ? process.stdout.write(OUTPUT + "\n") : null;
return OUTPUT;
}
process.exit(0);
}