-
Notifications
You must be signed in to change notification settings - Fork 27
/
js2php.js
executable file
·175 lines (161 loc) · 4.84 KB
/
js2php.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env node
/*global process, require, module, global*/
(function () {
// #default is runtime embedded in output file
// --runtime runtime.php #runtime exists at path; link to via require()
// --runtime-only #output only the runtime
// --fragment #no runtime included
// --test #run tests only
var fs = require('fs');
var path = require('path');
var yargs = require('yargs');
var transform = require('./tools/transform.js');
var childProcess = require('child_process');
var argv = yargs
.boolean(['debug', 'fragment', 'runtime-only', 'test', 'quiet'])
.alias('out', 'o')
.alias('quiet', 'q')
.alias('modules', 'm').argv;
var logTo = argv.quiet ? 'none' : 'stdout';
if (argv.test) {
require('./test/transforms').run();
compileTests();
runTests();
} else {
processTransform(argv);
}
function processTransform(argv) {
var outfile = argv.out;
if (logTo === 'stdout' && !outfile) {
logTo = 'stderr';
}
var pathToRuntime = argv.runtime;
if (!argv.fragment && !pathToRuntime) {
var includeModules = argv.modules || '';
var includeAllModules = false;
if (includeModules === 'all') {
includeAllModules = true;
includeModules = '';
}
includeModules = includeModules.split(',');
var runtime = transform.buildRuntime({
includeModules: includeModules,
includeAllModules: includeAllModules,
includeDebug: argv.debug,
includeTest: argv.test,
log: log,
});
if (argv['runtime-only']) {
outputContent(outfile, '<?php\n' + runtime, 'runtime.php');
process.exit(0);
}
}
var infiles = argv._;
if (infiles.length) {
//process input from files specified as arguments
processInputFiles(infiles);
} else if (!process.stdin.isTTY) {
//process input from stdin
processInputStream();
} else {
log('No input file(s) specified.');
process.exit(1);
}
function processInputStream() {
var input = [];
var stdin = process.stdin;
stdin.setEncoding('utf8');
stdin.on('data', function (data) {
input.push(data);
});
stdin.on('end', function () {
var source = input.join('');
log('Processing input from STDIN ...');
var output = transform({
source: source,
});
output = output.replace(/^\n+|\n+$/g, '');
if (runtime) {
output = runtime + '\n\n' + output;
} else if (pathToRuntime) {
output =
'require_once(' + JSON.stringify(pathToRuntime) + ');\n\n' + output;
}
outputContent(outfile, '<?php\n' + output + '\n');
});
stdin.resume();
}
function processInputFiles(infiles) {
var output = infiles.map(function (infile) {
try {
var source = fs.readFileSync(infile, 'utf8');
} catch (e) {
if (e.code === 'ENOENT') {
log('Unable to open file `' + infile + '`');
process.exit(1);
}
throw e;
}
log('Processing file `' + infile + '` ...');
var output = transform({
source: source,
});
output = output.replace(/^\n+|\n+$/g, '');
return output;
});
if (runtime) {
output.unshift(runtime);
} else if (pathToRuntime) {
output.unshift('require_once(' + JSON.stringify(pathToRuntime) + ');');
}
outputContent(outfile, '<?php\n' + output.join('\n\n') + '\n');
}
}
function outputContent(outfile, content, defaultFileName) {
if (!outfile) {
process.stdout.write(content);
log('Output sent to STDOUT');
return;
}
if (defaultFileName && !outfile.match(/\.php$/)) {
outfile = path.join(outfile, defaultFileName);
}
fs.writeFileSync(outfile, content, 'utf8');
log('Output saved to', outfile);
}
function compileTests() {
var testSource = fs.readFileSync(__dirname + '/tests.php', 'utf8');
testSource.replace(
/require_once\('test\/compiled\/(.*?)\.php'\)/g,
function (_, name) {
var argv = {
fragment: true,
out: __dirname + '/test/compiled/' + name + '.php',
_: [__dirname + '/test/' + name + '.js'],
};
processTransform(argv);
}
);
}
function runTests() {
var child = childProcess.spawn('php', ['-f', 'tests.php'], {
cwd: __dirname,
});
child.stdout.on('data', function (data) {
process.stdout.write(data);
});
child.stderr.on('data', function (data) {
process.stderr.write(data);
});
child.on('close', function (code) {
process.exit(code);
});
}
function log() {
if (logTo === 'stdout') {
console.log.apply(console, arguments);
} else if (logTo === 'stderr') {
console.error.apply(console, arguments);
}
}
})();