-
Notifications
You must be signed in to change notification settings - Fork 0
/
dixdyoumeanthis.js
125 lines (111 loc) · 3.65 KB
/
dixdyoumeanthis.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
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
#!/usr/bin/env node
'use strict'
var DixdYouMeanThis = function DixdYouMeanThis(){}
DixdYouMeanThis.spinnerChars = ["\\", "|", "/", "-"]
DixdYouMeanThis.spinnerIndex = 0
this.spinner = null
DixdYouMeanThis.isArgFlagOrDeclare = function isArgFlagOrDeclare(arg){
return (arg.charAt(0)=='-' || arg.includes('='))
}
DixdYouMeanThis.findArgsStart = function findArgsStart(args){
if(args){
return args.findIndex(this.isArgFlagOrDeclare)
}
return false
}
DixdYouMeanThis.splitArgs = function splitArgs(splitIndex,args){
var commands, dxArgs
if(splitIndex>-1){
commands = args.slice(0,splitIndex)
dxArgs = args.slice(splitIndex)
} else commands = args
return {"commands":commands,"dxArgs":dxArgs}
}
DixdYouMeanThis.parseArgs = function parseArgs(args){
if(args){
this.nodeLocation = args.shift()
this.scriptLocation = args.shift()
if(args.length>0){
var dxArgsStartIndex = this.findArgsStart.call(this,args)
return this.splitArgs.call(this,dxArgsStartIndex,args)
}
}
return {"commands":['force'],"dxArgs":['--help']}
}
DixdYouMeanThis.buildSfdxCommand = function buildSfdxCommand(commands,dxArgs){
if(commands[0]!='force') commands.unshift('force')
return 'sfdx '+commands.join(':')+(dxArgs ? ' '+dxArgs.join(' ') : '')
}
DixdYouMeanThis.formatSfdxCommand = function formatSfdxCommand(commands,dxArgs){
if(commands && commands.length) return this.buildSfdxCommand(commands,dxArgs)
else return 'sfdx force --help'
}
DixdYouMeanThis.spinnerStart = function spinnerStart(){
this.spinnerCharIndex = 0
this.spinner = setInterval((function spinnerInterval(){
process.stdout.write('\r'+this.command+'... '+this.spinnerChars[this.spinnerIndex++])
this.spinnerIndex &= 3
}).bind(this),250)
}
DixdYouMeanThis.spinnerStop = function spinnerStop(){
if(this.spinner){
clearInterval(this.spinner)
this.spinner = null
console.log('')
}
}
DixdYouMeanThis.sfdxCallback = function sfdxCallback(error,stdout,stderr){
this.spinnerStop.call(this)
if(error) console.log(error)
if(stderr) console.log(stderr)
if(stdout) console.log(stdout)
/*if(error){
console.log(error)
Object.keys(error).forEach(function errorKeys(key,index){
console.log(index+' : '+key)
console.log(error[key])
})
}else{
if(stderr) console.log(stderr)
if(stdout) console.log(stdout)
/*Object.keys(stderr).forEach(function errorKeys(key,index){
console.log(index+' : '+key)
console.log(error[key])
})
}*/
}
DixdYouMeanThis.executeSfdx = function executeSfdx(command){
this.spinnerStop.call(this)
var exec = require('child_process').exec
var sfdxProcess = exec(command)
sfdxProcess.stdout.pipe(process.stdout)
sfdxProcess.stderr.pipe(process.stderr)
sfdxProcess.on('exit',this.sfdxCallback.bind(this))
//const { exec } = require('child_process')
//exec(command,this.sfdxCallback.bind(this))
}
DixdYouMeanThis.handleException = function handleException(ex){
//parse error
//if not bad command
// print pretty error
// print raw error
// ?switch to return json error or only return json no pretty
//if bad command
// print your command is not recognize
// print your command
// print dixd you mean this?
// parse output of process for sfdx --help
// search command list for matching words to commands
}
DixdYouMeanThis.run = function run(args){
try{
this.spinnerStart.call(this)
var parsedArgs = this.parseArgs.call(this,args)
this.command = this.formatSfdxCommand.call(this,parsedArgs.commands,parsedArgs.dxArgs)
this.executeSfdx.call(this,this.command)
}catch(ex){
this.spinnerStop.call(this)
console.log(ex)
}
}
DixdYouMeanThis.run(process.argv)