Skip to content

Commit a58c286

Browse files
chore(info): changes base
1 parent 44df902 commit a58c286

File tree

9 files changed

+861
-54
lines changed

9 files changed

+861
-54
lines changed

packages/info/README.md

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,79 @@ This package returns a set of information related to the local environment.
99
## Installation
1010

1111
```bash
12+
#npm
1213
npm i -D @webpack-cli/info
14+
15+
#yarn
16+
yarn add @webpack-cli/info -D
17+
18+
#npx
19+
npx webpack info [options]
20+
1321
```
1422

1523
## Usage
1624

25+
### Args / Flags
26+
27+
#### Output format
28+
29+
| Flag | Description | Type |
30+
| ------------------- | ----------------------------- | ----------- |
31+
| `--output-json` | To get the output as JSON | [ boolean ] |
32+
| `--output-markdown` | To get the output as markdown | [ boolean ] |
33+
34+
#### Options
35+
36+
| Flag | Description | Type |
37+
| ------------------- | --------------------------------------------------------------- | ----------- |
38+
| `--help` | Show help | [ boolean ] |
39+
| `--version` | Show version number of `webpack-cli` | [ boolean ] |
40+
| `--system` , `-s` | System information ( OS, CPU ) | [ boolean ] |
41+
| `--binaries` , `-b` | Installed binaries (Node, yarn, npm) | [ boolean ] |
42+
| `--browsers` | Installed web browsers | [ boolean ] |
43+
| `--npmg` | Globally installed NPM packages ( webpack & webpack-cli only ) | [ boolean ] |
44+
| `--npmPackages` | Info about packages related to webpack installed in the project | [ boolean ] |
45+
1746
### Node
1847

1948
```js
20-
const envinfo = require("@webpack-cli/info").default;
21-
envinfo();
49+
const info = require('@webpack-cli/info').default;
50+
51+
async function wrapperFunc() {
52+
await info({
53+
/* Custom Config */
54+
});
55+
}
56+
wrapperFunc();
2257
```
2358

59+
#### Custom config
60+
61+
> Config has higher precedence than system flags
62+
63+
```json
64+
// Config's relative path
65+
{
66+
67+
"config": [string]
68+
}
69+
// System info
70+
{
71+
"binaries": [boolean],
72+
"system": [boolean],
73+
"browsers": [boolean],
74+
"npmg": [boolean],
75+
"npmPackages": [boolean],
76+
}
77+
```
78+
79+
The function returns `string` for `system` info, and returns an array of strings (`string[]`) for `config`
80+
2481
### CLI (via `webpack-cli`)
2582

2683
```bash
27-
npx webpack-cli info
84+
webpack-cli info --FLAGS #Flags are optional for custom output
2885
```
2986

3087
[downloads]: https://img.shields.io/npm/dm/@webpack-cli/info.svg

packages/info/__tests__/index.test.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
1-
import { information } from "../index";
2-
3-
describe("info", () => {
4-
it("should return the information of the enviroment", async () => {
5-
const returnedInformation = information();
6-
const expectedInformation = {
7-
Binaries: ["Node", "Yarn", "npm"],
8-
Browsers: ["Chrome", "Firefox", "Safari"],
9-
System: ["OS", "CPU"],
10-
npmGlobalPackages: ["webpack", "webpack-cli"],
11-
npmPackages: "*webpack*"
12-
};
1+
import { informationType } from "../index";
2+
3+
describe("infoSystem", () => {
4+
it("should return the information of the system", async () => {
5+
const returnedInformation = informationType("system");
6+
const expectedInformation = { System: ["OS", "CPU", "Memory"] };
7+
8+
expect(returnedInformation).toEqual(expectedInformation);
9+
});
10+
});
11+
12+
describe("infoBinaries", () => {
13+
it("should return the information of the binaries", async () => {
14+
const returnedInformation = informationType("binaries");
15+
const expectedInformation = { Binaries: ["Node", "Yarn", "npm"] };
16+
17+
expect(returnedInformation).toEqual(expectedInformation);
18+
});
19+
});
20+
21+
describe("infoBrowsers", () => {
22+
it("should return the information of the browsers installed", async () => {
23+
const returnedInformation = informationType("browsers");
24+
const expectedInformation = { Browsers: ["Chrome", "Firefox", "Safari"] };
25+
26+
expect(returnedInformation).toEqual(expectedInformation);
27+
});
28+
});
29+
30+
describe("infoNpmGlobal", () => {
31+
it("should return the information of the NPM global packages", async () => {
32+
const returnedInformation = informationType("npmg");
33+
const expectedInformation = { npmGlobalPackages: ["webpack", "webpack-cli"] };
34+
35+
expect(returnedInformation).toEqual(expectedInformation);
36+
});
37+
});
38+
39+
describe("infoNpm", () => {
40+
it("should return the information of the NPM packages (webpack)", async () => {
41+
const returnedInformation = informationType("npmPackages");
42+
const expectedInformation = { npmPackages: "*webpack*" };
1343

1444
expect(returnedInformation).toEqual(expectedInformation);
1545
});

packages/info/commands.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const AVAILABLE_COMMANDS: string[] = [
2+
"binaries",
3+
"system",
4+
"browsers",
5+
"npmGlobalPackages",
6+
"npmPackages",
7+
"npmg",
8+
"npm",
9+
"b",
10+
"s"
11+
];
12+
export const AVAILABLE_FORMATS: string[] = ["output-json", "output-markdown"];
13+
export const IGNORE_FLAGS: string[] = ["_", "$0", "outputMarkdown", "outputJson", "npm-packages"];

packages/info/configParser.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as path from 'path';
2+
import chalk from 'chalk';
3+
import * as prettyjson from 'prettyjson';
4+
5+
export function getNameFromPath(fullPath: string): string {
6+
const filename = fullPath.replace(/^.*[\\\/]/, '');
7+
return filename;
8+
}
9+
10+
export function resolveFilePath(relativeFilePath: string): string {
11+
const configPath = path.resolve(process.cwd() + '/' + relativeFilePath);
12+
return configPath;
13+
}
14+
15+
export function fetchConfig(configPath: string): object {
16+
let config = null;
17+
try {
18+
config = require(configPath);
19+
} catch (e) {
20+
process.stdout.write(chalk.red(`Error:`, e.code) + `\n` + e);
21+
}
22+
return config;
23+
}
24+
25+
const CONFIG_SCHEMA = {
26+
plugins: 'Array',
27+
};
28+
29+
function modifyConfig(config, key) {
30+
switch (CONFIG_SCHEMA[key]) {
31+
case 'Array':
32+
config[key].forEach((element, idx) => {
33+
config[key][idx] = {
34+
name: chalk.greenBright(element.constructor.name),
35+
...element,
36+
};
37+
});
38+
}
39+
}
40+
41+
export function configReader(config): string[] {
42+
let filteredArray = [];
43+
44+
let options = {
45+
noColor: true,
46+
};
47+
Object.keys(config).map((key): void => {
48+
if (CONFIG_SCHEMA[key]) {
49+
modifyConfig(config, key);
50+
}
51+
let rowArray = [key];
52+
rowArray.push(prettyjson.render(config[key], options));
53+
filteredArray = [...filteredArray, rowArray];
54+
});
55+
56+
return filteredArray;
57+
}

packages/info/index.ts

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,90 @@
1+
import chalk from "chalk";
12
import * as envinfo from "envinfo";
23
import * as process from "process";
4+
import { argv } from "./options";
35

4-
/**
5-
* Prints debugging information for webpack issue reporting
6-
*/
6+
import { AVAILABLE_COMMANDS, AVAILABLE_FORMATS, IGNORE_FLAGS } from "./commands";
7+
import { configReader, fetchConfig, resolveFilePath, getNameFromPath } from "./configParser";
8+
import { renderTable } from "./renderTable";
79

810
interface Information {
9-
Binaries: string[];
10-
Browsers: string[];
11-
System: string[];
12-
npmGlobalPackages: string[];
13-
npmPackages: string | string[];
11+
Binaries?: string[];
12+
Browsers?: string[];
13+
System?: string[];
14+
npmGlobalPackages?: string[];
15+
npmPackages?: string | string[];
1416
}
17+
interface ArgvI {
18+
_?: string[];
19+
bin?: boolean;
20+
binaries?: boolean;
21+
config?: string;
22+
}
23+
24+
const CONFIG = {};
25+
const DEFAULT_DETAILS: Information = {
26+
Binaries: ["Node", "Yarn", "npm"],
27+
Browsers: ["Chrome", "Firefox", "Safari"],
28+
System: ["OS", "CPU", "Memory"],
29+
npmGlobalPackages: ["webpack", "webpack-cli"],
30+
npmPackages: "*webpack*"
31+
};
1532

16-
// eslint-disable-next-line
17-
export function information(): Information {
18-
return {
19-
Binaries: ["Node", "Yarn", "npm"],
20-
Browsers: ["Chrome", "Firefox", "Safari"],
21-
System: ["OS", "CPU"],
22-
npmGlobalPackages: ["webpack", "webpack-cli"],
23-
npmPackages: "*webpack*"
24-
};
33+
let DETAILS_OBJ = {};
34+
35+
export function informationType(type: string): Information {
36+
switch (type) {
37+
case "system":
38+
return { System: ["OS", "CPU", "Memory"] };
39+
case "binaries":
40+
return { Binaries: ["Node", "Yarn", "npm"] };
41+
case "browsers":
42+
return { Browsers: ["Chrome", "Firefox", "Safari"] };
43+
case "npmg":
44+
return { npmGlobalPackages: ["webpack", "webpack-cli"] };
45+
case "npmPackages":
46+
return { npmPackages: "*webpack*" };
47+
}
2548
}
49+
export default async function info(CustomArgv: object): Promise<string[]> {
50+
const CUSTOM_AGRUMENTS: boolean = typeof CustomArgv === "object";
51+
const args: ArgvI = CUSTOM_AGRUMENTS ? CustomArgv : argv;
52+
const configRelativePath = argv._[1] ? argv._[1] : args.config;
53+
if (configRelativePath) {
54+
const fullConfigPath = resolveFilePath(configRelativePath);
55+
const fileName = getNameFromPath(fullConfigPath);
56+
const config = fetchConfig(fullConfigPath);
57+
const parsedConfig = configReader(config);
58+
59+
const stringifiedTable = renderTable(parsedConfig, fileName);
60+
if (args.config) return parsedConfig;
61+
else process.stdout.write(stringifiedTable + "\n");
62+
} else {
63+
Object.keys(args).forEach((flag: string) => {
64+
if (IGNORE_FLAGS.includes(flag)) {
65+
return;
66+
} else if (AVAILABLE_COMMANDS.includes(flag)) {
67+
const flagVal = informationType(flag);
68+
DETAILS_OBJ = { ...DETAILS_OBJ, ...flagVal };
69+
} else if (AVAILABLE_FORMATS.includes(flag)) {
70+
switch (flag) {
71+
case "output-json":
72+
CONFIG["json"] = true;
73+
break;
74+
case "output-markdown":
75+
CONFIG["markdown"] = true;
76+
break;
77+
}
78+
} else {
79+
// Invalid option
80+
process.stdout.write("\n" + chalk.bgRed(flag) + chalk.red(" is an invalid option" + "\n"));
81+
return;
82+
}
83+
});
2684

27-
export default async function info(): Promise<void> {
28-
process.stdout.write(await envinfo.run(information()));
85+
const OUTPUT = await envinfo.run(Object.keys(DETAILS_OBJ).length ? DETAILS_OBJ : DEFAULT_DETAILS, CONFIG);
86+
!CUSTOM_AGRUMENTS ? process.stdout.write(OUTPUT + "\n") : null;
87+
return OUTPUT;
88+
}
89+
process.exit(0);
2990
}

packages/info/options.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as yargs from "yargs";
2+
export const argv = yargs
3+
.option("system", {
4+
alias: "s",
5+
demandOption: false,
6+
describe: "System information (OS, CPU)",
7+
type: "boolean"
8+
})
9+
.option("binaries", {
10+
alias: "b",
11+
demandOption: false,
12+
describe: "Installed binaries (Node, yarn, npm)",
13+
type: "boolean"
14+
})
15+
16+
.option("browsers", {
17+
demandOption: false,
18+
describe: "Installed web browsers",
19+
type: "boolean"
20+
})
21+
22+
.option("npmg", {
23+
demandOption: false,
24+
describe: "Globally installed NPM packages (webpack & webpack-cli only)",
25+
type: "boolean"
26+
})
27+
.option("npmPackages", {
28+
demandOption: false,
29+
describe: "Info about packages related to webpack installed in the project",
30+
type: "boolean"
31+
})
32+
.option("output-json", {
33+
demandOption: false,
34+
describe: "To get the output as JSON",
35+
type: "boolean"
36+
})
37+
.option("output-markdown", {
38+
demandOption: false,
39+
describe: "To get the output as markdown",
40+
type: "boolean"
41+
})
42+
.group(["output-json", "output-markdown"], `Output format`).argv;

0 commit comments

Comments
 (0)