-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (55 loc) · 1.43 KB
/
index.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
const toCamelCase = require('to-camel-case')
const spawn = require('buffered-spawn')
const rename = require('deep-rename-keys')
function shell (command) {
return spawn('powershell.exe', ['-command', command])
.then((io) => {
try {
const json = rename(JSON.parse(io.stdout), toCamelCase)
return Object.assign(io, { json })
} catch (err) {
return io
}
})
}
function create (obj) {
const identifier = 'a'
const newObj = assign(identifier, 'new-object PSObject')
const cmd = Object.keys(obj).reduce(
(acc, key) => {
const val = obj[key]
return compose(acc, addMember(identifier, key, val))
},
newObj
)
// return variable for further manipulation
return compose(cmd, variable(identifier))
}
function compose () {
const args = Array.prototype.slice.call(arguments)
if (args.length == 1) return args[0]
return args.reduce((acc, arg) => `${statement(acc)} ${arg}`, '')
}
function toJson (command) {
return pipe(command, 'convertto-json')
}
function addMember (identifier, key, val) {
return pipe(variable(identifier), `add-member ${key} ${val}`)
}
function pipe(a, b) {
return `${a} | ${b}`
}
function statement (cmd) {
return `${cmd};`
}
function assign(a, b) {
return `${variable(a)} = ${b}`
}
function variable (a) {
return `$${a}`
}
exports.compose = compose
exports.toJson = toJson
exports.create = create
exports.shell = shell
exports.pipe = pipe