-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathmorphir-elm-make.js
67 lines (56 loc) · 2.03 KB
/
morphir-elm-make.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
#!/usr/bin/env node
'use strict'
// NPM imports
const commander = require('commander')
// logging
require('log-timestamp')
// Set up Commander
const program = new commander.Command()
program
.name('morphir-elm make')
.description('Translate Elm sources to Morphir IR')
.option('-p, --project-dir <path>', 'Root directory of the project where morphir.json is located.', '.')
.option('-o, --output <path>', 'Target file location where the Morphir IR will be saved.', 'morphir-ir.json')
.option('-t, --types-only', 'Only include type information in the IR, no values.', false)
.option('-f, --fallback-cli', 'Use old cli make function.', false)
.option('-i, --indent-json', 'Use indentation in the generated JSON file.', false)
.parse(process.argv)
const programOptions = program.opts()
// running function
runAppropriateCli(programOptions.projectDir, programOptions)
// runs cli1 if flag passed, else cli2
function runAppropriateCli(projectDir, opts) {
if (opts.fallbackCli) {
make(projectDir, opts)
}
else {
const cli2 = require('../cli2/lib/cliAPI')
cli2.make(projectDir, opts)
}
}
function make(projectDir, opts) {
const cli = require('./cli')
cli.make(projectDir, opts)
.then((packageDef) => {
console.log(`Writing file ${opts.output}.`)
cli.writeFile(opts.output, JSON.stringify(packageDef, null, opts.indentJson ? 4 : 0))
.then(() => {
console.log('Done.')
})
.catch((err) => {
console.error(`Could not write file: ${err}`)
})
})
.catch((err) => {
if (err.code == 'ENOENT') {
console.error(`Could not find file at '${err.path}'`)
} else {
if (err instanceof Error) {
console.error(err)
} else {
console.error(`Error: ${JSON.stringify(err, null, 2)}`)
}
}
process.exit(1)
})
}