-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys-info.js
More file actions
141 lines (120 loc) · 3.71 KB
/
sys-info.js
File metadata and controls
141 lines (120 loc) · 3.71 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"use strict"
const os = require("os")
const { existsSync, statSync, readdirSync, mkdirSync, writeFileSync, readFileSync } = require("fs")
const { resolve } = require("path")
const interactiveHelpObject = {
title: "Help",
message: "Available commands: help, system, user, directory, details {file}, exit"
+ "\n\n-s as last param to save output to a file under the info directory"
}
const scriptHelpObject = {
title: "Usage",
message: "node [command] # any of: help, system, user, directory, details {file}, display {info letter}, exit"
+ "\n\n-i for interactive mode, will ignore the rest of them"
+ "\n\n-s as last param to save output to a file under the info directory"
}
const getSystemInfoObject = () => {
return {
type: os.type(),
architecture: os.arch(),
hostname: os.hostname(),
platform: os.platform(),
cpus: os.cpus().length,
totalmem: `${os.totalmem()/(1024*1024*1024)}GB`,
uptime: os.uptime()
}
}
const getUserInfoObject = () => os.userInfo()
const getFilteredFileListObject = () => {
const commonlyIgnoredFileNames = [ "node_modules", ".git", ".nyc_output" ] // Quick fix
const rawFileList = readdirSync(__dirname)
return rawFileList.filter(filename => !commonlyIgnoredFileNames.includes(filename)
&& !__filename.includes(filename))
}
const getFileStats = (name) => {
try {
const stats = statSync(name)
return stats
} catch (error) {
return { type: "FILE_ERROR", hint: `${error.message}: check with "directory"` }
}
}
const getExistingInfo = (option) => {
const getInfo = filename => {
try {
const jsonObj = JSON.parse(readFileSync(filename).toString())
return jsonObj
} catch (error) {
return { type: "JSON_PARSING_ERROR", hint: error.message }
}
}
switch (option) {
case "s":
return getInfo(resolve(__dirname, "info/system.json"))
case "u":
return getInfo(resolve(__dirname, "info/user.json"))
case "d":
return getInfo(resolve(__dirname, "info/directory.json"))
default:
if (option) return { type: "WRONG_OPTION", hint: `unrecognized display option: ${option}` }
if (existsSync("./info")) {
const fileList = readdirSync("./info")
return fileList.length > 0 ?
fileList.reduce((acc, curr) => readFileSync(`./info/${curr}`) + acc, "") :
{ type: "EMPTY_INFO_DIR", hint: "generate some info, directory empty" }
}
return { type: "INFO_DIR_NOT_FOUND", hint: "generate some info, directory does not exist" }
}
}
const commandProcessor = (command) => {
let result
switch (command[0]) {
case "help":
result = interactiveHelpObject
break
case "system":
result = getSystemInfoObject()
break
case "user":
result = getUserInfoObject()
break
case "directory":
result = getFilteredFileListObject()
break
case "details":
result = getFileStats(command[1])
break
case "display":
result = getExistingInfo(command[1])
break
case "exit":
process.stdin.removeAllListeners("data")
process.exit(0)
default:
result = { type: "ERROR_MESSAGE", message: "unrecognized command" }
break
}
if (command.slice(-1).toString() === "-s") {
mkdirSync("./info", { recursive: true })
writeFileSync(`./info/${command[0]}.json`, JSON.stringify(result))
result = `./info/${command[0]}.json file created/updated`
}
console.log(result)
}
const processParams = (params) => {
if (params[0] === "-i") {
console.log("Entered the interactive mode, rest of params will be ignored.")
process.stdin.on("data", data => commandProcessor(data.toString().trim().split(' ')))
return
}
commandProcessor(params)
}
const run = () => {
const argvCount = process.argv.length - 2
if (argvCount < 1) {
console.log(JSON.stringify(scriptHelpObject))
process.exit(1)
}
processParams(process.argv.slice(2, process.argv.length))
}
run()