forked from InsightSoftwareConsortium/itk-wasm
/
itk-js-cli.js
executable file
·136 lines (119 loc) · 3.94 KB
/
itk-js-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
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
#!/usr/bin/env node
const fs = require('fs-extra')
const path = require('path')
const spawnSync = require('child_process').spawnSync
const program = require('commander')
const build = (sourceDir) => {
// Check that the source directory exists and chdir to it.
if (!fs.existsSync(sourceDir)) {
console.error('The source directory: ' + sourceDir + ' does not exist!')
process.exit(1)
}
process.chdir(sourceDir)
// Make the 'web-build' directory to hold the dockcross script and the CMake
// build.
try {
fs.mkdirSync('web-build')
} catch (err) {
if (err.code !== 'EEXIST') throw err
}
// Check that we have docker and can run it.
const dockerVersion = spawnSync('docker', ['--version'], {
env: process.env,
stdio: [ 'ignore', 'ignore', 'ignore' ]
})
if (dockerVersion.status !== 0) {
console.error("Could not run the 'docker' command.")
console.error('This tool requires Docker.')
console.error('')
console.error('Please find installation instructions at:')
console.error('')
console.error(' https://docs.docker.com/install/')
console.error('')
process.exit(dockerVersion.status)
}
let dockerImage = 'insighttoolkit/itk-js'
if (program.commands[0].image) {
dockerImage = program.commands[0].image
}
// Ensure we have the 'dockcross' Docker build environment driver script
const dockcrossScript = path.join('web-build', 'itk-js-build-env')
try {
fs.statSync(dockcrossScript)
} catch (err) {
if (err.code === 'ENOENT') {
const output = fs.openSync(dockcrossScript, 'w')
const dockerCall = spawnSync('docker', ['run', '--rm', dockerImage], {
env: process.env,
stdio: [ 'ignore', output, null ]
})
if (dockerCall.status !== 0) {
process.exit(dockerCall.status)
}
fs.closeSync(output)
fs.chmodSync(dockcrossScript, '755')
} else {
throw err
}
}
const hypenIndex = program.rawArgs.findIndex((arg) => arg === '--')
let cmakeArgs = []
if (hypenIndex !== -1) {
cmakeArgs = program.rawArgs.slice(hypenIndex + 1)
}
const dockerBuild = spawnSync(dockcrossScript, ['web-build'].concat(cmakeArgs), {
env: process.env,
stdio: 'inherit'
})
process.exit(dockerBuild.status)
}
const test = (sourceDir) => {
// Check that the source directory exists and chdir to it.
if (!fs.existsSync(sourceDir)) {
console.error('The source directory: ' + sourceDir + ' does not exist!')
process.exit(1)
}
process.chdir(sourceDir)
const dockcrossScript = path.join('web-build', 'itk-js-build-env')
try {
fs.statSync(dockcrossScript)
} catch (err) {
console.error('Could not find ' + sourceDir + '/web-build/itk-js-build-env')
console.error('')
console.error('Has')
console.error('')
console.error(' itk-js build ' + sourceDir)
console.error('')
console.error('been executed?')
process.exit(1)
}
const hypenIndex = program.rawArgs.findIndex((arg) => arg === '--')
let ctestArgs = ''
if (hypenIndex !== -1) {
ctestArgs = program.rawArgs.slice(hypenIndex + 1).join(' ')
}
const dockerBuild = spawnSync(dockcrossScript,
['bash', '-c',
'cd web-build && ctest ' + ctestArgs],
{
env: process.env,
stdio: 'inherit'
})
process.exit(dockerBuild.status)
}
program
.command('build <sourceDir>')
.usage('[options] <sourceDir> [-- <cmake arguments>]')
.description('build the CMake project found in the given source directory')
.action(build)
.option('-i, --image <image>', 'build environment Docker image, defaults to insighttoolkit/itk-js')
// todo: needs a wrapper in web_add_test that 1) mount /work into the emscripten filesystem
// and 2) invokes the runtime
// program
// .command('test <sourceDir>')
// .usage('[options] <sourceDir> [-- <ctest arguments>]')
// .description('run ctest on the project previously built from the given source directory')
// .action(test)
program
.parse(process.argv)
program.help()