forked from jbr/sibilant
/
cli.js
138 lines (124 loc) · 5.04 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
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
137
138
var sibilant = require("sibilant"),
path = require("path"),
options = require("sibilant/options"),
fs = require("fs"),
script = (process.binding("evals"))["Script"],
context = script.createContext();
var createContext = (function() {
context.initializedQ = true;;
(module)["filename"] = (process.cwd() + "/exec");;
(context)["module"] = module;;
(context)["require"] = require;;
return (function() {
for (var key in global) (function() {
return (context)[key] = (global)[key];;
})();
})();;
});
var runInSandbox = (function(js, inputPath) {
// js:required inputPath:optional
if (arguments.length < 2) // if inputPath is missing
var inputPath = undefined;
(function() {
if ((!context.initializedQ)) {
return createContext();
};
})();
(function() {
if (typeof(inputPath) !== 'undefined') {
(context)["__dirname"] = path.dirname(inputPath);;
return (module)["filename"] = inputPath;;
};
})();
return script.runInContext(js, context, "sibilant");
});
var cli = {
v: "version",
h: "help",
unhandled: "help",
i: "input",
o: "output",
x: "execute",
e: "eval",
execute: false,
unlabeled: "input"
};
cli.version = (function(args) {
// args:rest
var args = Array.prototype.slice.call(arguments, 0);
return console.log(sibilant.versionString());
});
cli.repl = (function(args) {
// args:rest
var args = Array.prototype.slice.call(arguments, 0);
return require("sibilant/repl");
});
cli.eval = (function(args) {
// args:rest
var args = Array.prototype.slice.call(arguments, 0);
return (function() {
if (((args).length === 0)) {
var stdin = process.openStdin(),
data = "";;
stdin.setEncoding("utf8");
stdin.on("data", (function(chunk) {
// chunk:required
return data = (data + chunk);;
}));
return stdin.on("end", (function(args) {
// args:rest
var args = Array.prototype.slice.call(arguments, 0);
return runInSandbox(sibilant.translateAll(data));
}));;
} else {
return args.forEach((function(arg) {
// arg:required
return runInSandbox(sibilant.translateAll(arg));
}));
};
})();
});
cli.help = (function(args) {
// args:rest
var args = Array.prototype.slice.call(arguments, 0);
return console.log("Hi there! Thanks for installing sibilant.\nPlease leave feedback on github issues (http://github.com/jbr/sibilant/issues)\n\nThe current commandline options are:\n--------------------------------------------------------------------------------\n\n-v / --version Print out a version string and exit\n\n-h / --help This message\n\n--repl / [no args] Sibilant interactive command prompt\n\n--eval [optional STRING] / -e [optional STRING]\n Evaluate STRING if provided, otherwise evaluate STDIN.\n\n--execute / -x This is a flag. Execute input files in order supplied.\n\n--output DIR / -o DIR Output input files to this dir, replacing .lisp with .js.\n\n--input FILE / -i FILE / FILE\n Add this file to the input files. If the execute flag is\n set, input files will be executed. If an output dir is\n specified, each file will be written to that dir.\n Otherwise, each file will be written to STDOUT.\n\n--------------------------------------------------------------------------------\n\nExamples\n\nto compile sibilant\n$ git clone git://github.com/jbr/sibilant.git\n$ npm link .\n$ sibilant src/*.lisp -o lib\n$ sibilant -x test/test.lisp # you're now running a sibilant you just compiled.\n\nto compile one file to stdout\n$ sibilant test/test.lisp\n\nto compile a file to a directory\n$ sibilant test/test.lisp -o . # put test.js here\nor\n$ sibilant --input test/test.lisp --output .\n\nto run a file\n$ sibilant -x test/test.lisp\n\nto enter the repl\n$ sibilant\nor\n$ sibilant --repl\n\n");
});
var cliOptions = options(cli);
(function() {
if (((Object.keys(cliOptions)).length === 0)) {
return cli.repl();
};
})()
var outputDir = (function() {
if (cliOptions.output) {
return (cliOptions.output)[0];
};
})();
var stripShebang = (function(data) {
// data:required
return data.replace(/^#!.*\n/, "");
});
sibilant.translateFile = (function(fileName) {
// fileName:required
return sibilant.translateAll(stripShebang(fs.readFileSync(fileName, "utf8")));
});
(cliOptions.input || [ ]).forEach((function(inputFile) {
// inputFile:required
var inputPath = path.join(process.cwd(), inputFile),
translated = sibilant.translateFile(inputPath);;
return (function() {
if (outputDir) {
var inputBasename = path.basename(inputPath, ".lisp"),
outputPath = (path.join(outputDir, inputBasename) + ".js");;
return fs.writeFile(outputPath, translated);;
} else {
return (function() {
if (cliOptions.execute) {
return runInSandbox(translated, inputPath);
} else {
return console.log(translated);
};
})();
};
})();
}))