-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (59 loc) · 1.83 KB
/
index.js
File metadata and controls
69 lines (59 loc) · 1.83 KB
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
#! /usr/bin/env node
/**
* Package: @bitsoft-network/debug
* Repository: @bitsoft-network/debug-cli
* Author: Leevi Halme <leevi@bitsoft.network> (https://leevihal.me)
*/
import { createSpinner } from "nanospinner";
import chalk from "chalk";
import * as platform from "platform";
import { promises as fs } from "fs";
// Create init spinner
const spinner = createSpinner(
`${chalk.bgHex("#304164")(
" @bitsoft-network/debug "
)} - Detecting environment`
).start();
// Dependencies array mapper
const mapper = object =>
Object.keys(object)
.map(dep => dep + ` (${object[dep]})`)
.join(", ");
// Detect environment
const detectEnvironment = async () => {
return new Promise(async (resolve, reject) => {
try {
// Get package.json
const file = await fs.readFile("package.json");
const pkg = JSON.parse(file);
// Resolve spinner
spinner.success();
// Create other spinners
const name = chalk.hex("#304164").bold(pkg.name);
const plat = platform.default.toString();
const env = `Running ${name} v${pkg.version} on ${plat}`;
const depCount = Object.keys(pkg.dependencies).length;
const deps = `Found ${depCount} dependencies: ${mapper(
pkg.dependencies
)}`;
createSpinner(env).success();
createSpinner(deps).success();
// Resolve promise
return resolve();
} catch (error) {
// If this error was due missing package.json
if (error.code === "ENOENT") {
spinner.error();
createSpinner(
"Failed to parse package.json. Make sure to run this tool on the root directory."
).error();
return resolve();
}
console.error("ERROR:", error);
spinner.error();
return reject(error);
}
});
};
// Await top-level (NodeJS >= v.14.8)
await detectEnvironment();