-
Notifications
You must be signed in to change notification settings - Fork 8
/
cli.js
70 lines (61 loc) · 1.9 KB
/
cli.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
const { spawnSync } = require('child_process')
const path = require('path')
//const { remote } = require('electron')
const rootConfigPath = global.rootConfigPath
function executablePath() {
let executable;
if (process.platform === "win32") {
executable = "./hc.exe"
} else if (process.platform === "darwin") {
executable = "./hc-darwin"
} else if (process.platform === "linux") {
executable = "./hc-linux"
} else {
log('error', "unsupported platform: "+process.platform)
return
}
return path.join(__dirname, executable)
}
module.exports = {
hash: (filePath, properties) => {
console.log("CLI: hashing file", filePath)
let params = ["hash", "--path", filePath]
if(properties) {
for(let name in properties) {
params.push("--property")
params.push(`${name}=${properties[name]}`)
}
}
let { stdout, stderr, error } = spawnSync(
executablePath(),
params,
{cwd: __dirname}
)
stderr = stderr.toString()
stdout = stdout.toString()
console.log("CLI: got results:")
console.log("stdout:", stdout)
console.log("stderr:", stderr)
if(error) {
console.log('Error executing hc:', error)
return {error}
}
if(stderr.length > 0) {
error = stderr
console.log('Error executing hc:', error)
return {error}
}
console.log(`hc stdout: ${JSON.stringify(stdout)}`)
let lines = stdout.split('\n')
let hash
for(line of lines) {
if(line.startsWith('DNA Hash: ')) {
hash = line.split("Hash: ")[1]
}
if(line.startsWith('Error:')) {
error = line
}
}
return { hash, error }
}
}