-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcli.coffee
More file actions
executable file
·87 lines (77 loc) · 3.02 KB
/
cli.coffee
File metadata and controls
executable file
·87 lines (77 loc) · 3.02 KB
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
#!/usr/bin/env coffee
child_process = require 'child_process'
path = require 'path'
fs = require 'fs'
js2cpp = require './index'
cli = (inpt, outp = null, cb) ->
read_string_or_stream(inpt)
.then (js_code) ->
cpp_code = js2cpp js_code
if outp
outp.write cpp_code + '\n'
return cpp_code
cli.sync = (inpt, options) ->
return js2cpp inpt, options
cli.run = (inpt, run_args) ->
binary_path = process.cwd() + '/a.out'
cli(inpt)
.then (cpp_code) ->
compile_command = process.env['GPP_BINARY'] or 'g++'
relative = (pt) -> path.join(__dirname, '..', pt)
args = [
'-std=c++14',
'-x', 'c++', # Take an input c++ file as STDIN
'-'
'-x', 'none', # And the following files aren't c++, start autodetecting pls
'-I', relative('include/'),
'-lrt',
'-lpthread',
'-o', binary_path,
]
return new Promise (resolve, reject) ->
compiler = child_process.spawn(compile_command, args)
compiler.stderr.pipe(process.stderr)
compiler.stdin.write(cpp_code + '\n')
compiler.stdin.end()
compiler.on 'exit', (status_code) ->
compiler.stderr.unpipe(process.stderr)
if status_code is 0
resolve()
else
reject({ status_code })
.then () ->
return new Promise (resolve) ->
if process.env['RUN_VALGRIND']
run_args = [binary_path].concat(run_args)
binary_path = 'valgrind'
program = child_process.spawn(binary_path, run_args or [])
promiseForEndedStreams = new Promise (resolve) ->
stdoutEnded = false
stderrEnded = false
tryFinish = () ->
if stdoutEnded and stderrEnded
resolve()
program.stderr.on('end', () -> stderrEnded = true; tryFinish())
program.stdout.on('end', () -> stdoutEnded = true; tryFinish())
program.stderr.on('error', () -> stderrEnded = true; tryFinish())
program.stdout.on('error', () -> stdoutEnded = true; tryFinish())
program.stderr.pipe(process.stderr)
program.stdout.pipe(process.stdout)
process.stdin.pipe(program.stdin)
program.on 'exit', (status_code) ->
try
fs.unlinkSync binary_path
promiseForEndedStreams.then () ->
resolve({ status_code })
module.exports = cli
read_string_or_stream = (string_or_stream, cb) ->
return new Promise (resolve, reject) ->
if typeof string_or_stream == 'string'
return resolve(string_or_stream)
data = ''
string_or_stream.on 'data', (d) ->
data += d
string_or_stream.on 'error', (err) ->
reject(err)
string_or_stream.on 'end', () ->
resolve(data)