-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44df902
commit a58c286
Showing
9 changed files
with
861 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const AVAILABLE_COMMANDS: string[] = [ | ||
"binaries", | ||
"system", | ||
"browsers", | ||
"npmGlobalPackages", | ||
"npmPackages", | ||
"npmg", | ||
"npm", | ||
"b", | ||
"s" | ||
]; | ||
export const AVAILABLE_FORMATS: string[] = ["output-json", "output-markdown"]; | ||
export const IGNORE_FLAGS: string[] = ["_", "$0", "outputMarkdown", "outputJson", "npm-packages"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as path from 'path'; | ||
import chalk from 'chalk'; | ||
import * as prettyjson from 'prettyjson'; | ||
|
||
export function getNameFromPath(fullPath: string): string { | ||
const filename = fullPath.replace(/^.*[\\\/]/, ''); | ||
return filename; | ||
} | ||
|
||
export function resolveFilePath(relativeFilePath: string): string { | ||
const configPath = path.resolve(process.cwd() + '/' + relativeFilePath); | ||
return configPath; | ||
} | ||
|
||
export function fetchConfig(configPath: string): object { | ||
let config = null; | ||
try { | ||
config = require(configPath); | ||
} catch (e) { | ||
process.stdout.write(chalk.red(`Error:`, e.code) + `\n` + e); | ||
} | ||
return config; | ||
} | ||
|
||
const CONFIG_SCHEMA = { | ||
plugins: 'Array', | ||
}; | ||
|
||
function modifyConfig(config, key) { | ||
switch (CONFIG_SCHEMA[key]) { | ||
case 'Array': | ||
config[key].forEach((element, idx) => { | ||
config[key][idx] = { | ||
name: chalk.greenBright(element.constructor.name), | ||
...element, | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
export function configReader(config): string[] { | ||
let filteredArray = []; | ||
|
||
let options = { | ||
noColor: true, | ||
}; | ||
Object.keys(config).map((key): void => { | ||
if (CONFIG_SCHEMA[key]) { | ||
modifyConfig(config, key); | ||
} | ||
let rowArray = [key]; | ||
rowArray.push(prettyjson.render(config[key], options)); | ||
filteredArray = [...filteredArray, rowArray]; | ||
}); | ||
|
||
return filteredArray; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,90 @@ | ||
import chalk from "chalk"; | ||
import * as envinfo from "envinfo"; | ||
import * as process from "process"; | ||
import { argv } from "./options"; | ||
|
||
/** | ||
* Prints debugging information for webpack issue reporting | ||
*/ | ||
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[]; | ||
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*" | ||
}; | ||
|
||
// eslint-disable-next-line | ||
export function information(): Information { | ||
return { | ||
Binaries: ["Node", "Yarn", "npm"], | ||
Browsers: ["Chrome", "Firefox", "Safari"], | ||
System: ["OS", "CPU"], | ||
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; | ||
} | ||
}); | ||
|
||
export default async function info(): Promise<void> { | ||
process.stdout.write(await envinfo.run(information())); | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as yargs from "yargs"; | ||
export const argv = yargs | ||
.option("system", { | ||
alias: "s", | ||
demandOption: false, | ||
describe: "System information (OS, CPU)", | ||
type: "boolean" | ||
}) | ||
.option("binaries", { | ||
alias: "b", | ||
demandOption: false, | ||
describe: "Installed binaries (Node, yarn, npm)", | ||
type: "boolean" | ||
}) | ||
|
||
.option("browsers", { | ||
demandOption: false, | ||
describe: "Installed web browsers", | ||
type: "boolean" | ||
}) | ||
|
||
.option("npmg", { | ||
demandOption: false, | ||
describe: "Globally installed NPM packages (webpack & webpack-cli only)", | ||
type: "boolean" | ||
}) | ||
.option("npmPackages", { | ||
demandOption: false, | ||
describe: "Info about packages related to webpack installed in the project", | ||
type: "boolean" | ||
}) | ||
.option("output-json", { | ||
demandOption: false, | ||
describe: "To get the output as JSON", | ||
type: "boolean" | ||
}) | ||
.option("output-markdown", { | ||
demandOption: false, | ||
describe: "To get the output as markdown", | ||
type: "boolean" | ||
}) | ||
.group(["output-json", "output-markdown"], `Output format`).argv; |
Oops, something went wrong.