-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsimpleCli.js
81 lines (66 loc) · 2.52 KB
/
simpleCli.js
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
#! /usr/bin/env node
// NPM ecosystem includes
const parseArgs = require("minimist")
const path = require("path")
const fs = require("fs")
const isUserPipingInput = () => {
if (process.platform === "win32") return false
// Check if stdin is explicitly set to a TTY
if (process.stdin.isTTY === true) return false
return fs.fstatSync(0).isFIFO()
}
class SimpleCLI {
CommandFnDecoratorSuffix = "Command"
async executeUsersInstructionsFromShell(args = parseArgs(process.argv.slice(2))._, userIsPipingInput = isUserPipingInput()) {
const command = args[0]
const commandName = `${command}${this.CommandFnDecoratorSuffix}`
if (this[commandName]) return userIsPipingInput ? this._runCommandOnPipedStdIn(commandName) : this[commandName](process.cwd(), args.slice(1))
else if (command) this.log(`No command '${command}'. Running help command.`)
else this.log(`No command provided. Running help command.`)
return this.helpCommand()
}
_runCommandOnPipedStdIn(commandName) {
this.log(`Running ${commandName} on piped input`)
let pipedData = ""
process.stdin.on("readable", function () {
pipedData += this.read() // todo: what's the lambda way to do this?
})
process.stdin.on("end", async () => {
const folders = pipedData
.trim()
.split("\n")
.map(line => line.trim())
.filter(line => fs.existsSync(line))
for (const line of folders) {
await this[commandName](line)
}
if (folders.length === 0)
// Hacky to make sure this at least does something in all environments.
// process.stdin.isTTY is not quite accurate for pipe detection
this[commandName](process.cwd())
})
}
silence() {
this.verbose = false
return this
}
verbose = true
log(message) {
if (this.verbose) console.log(message)
return message
}
get _allCommands() {
return Object.getOwnPropertyNames(Object.getPrototypeOf(this))
.filter(atom => atom.endsWith(this.CommandFnDecoratorSuffix))
.sort()
}
// Normalize 3 possible inputs: 1) cwd of the process 2) provided absolute path 3) cwd of process + provided relative path
resolvePath(folder = "") {
return path.isAbsolute(folder) ? path.normalize(folder) : path.resolve(path.join(process.cwd(), folder))
}
helpCommand() {
this.log(this.welcomeMessage)
return this.log(`\nAvailable commands:\n\n${this._allCommands.map(comm => `🖌️ ` + comm.replace(this.CommandFnDecoratorSuffix, "")).join("\n")}\n`)
}
}
module.exports = { SimpleCLI }