Repository navigation
Expand file tree
/
Copy pathphp-to-js.js
More file actions
57 lines (57 loc) · 2.08 KB
/
Copy pathphp-to-js.js
File metadata and controls
57 lines (57 loc) · 2.08 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
import * as fs from "fs/promises";
import * as path from "path";
import { JSTranspiler } from "../parser/JSTranspiler.js";
export async function runPhpToJS(rawArgs) {
if (rawArgs.includes("-h") || rawArgs.includes("--help")) {
console.log("Usage: php-to-js <php-file> [javascript-output]");
console.log("\nTranspiles a PHP file into JavaScript.");
console.log("If javascript-output is omitted, the input file extension is replaced with .js.");
process.exit(0);
}
const args = rawArgs.filter((arg) => !arg.startsWith("-"));
if (args.length === 0) {
console.error("Usage: php-to-js <php-file> [javascript-output]");
process.exit(1);
}
const inputFile = args[0];
const inputPath = path.resolve(inputFile);
try {
await fs.access(inputPath);
}
catch (error) {
const code = error.code;
if (code !== "ENOENT" && code !== "ENOTDIR")
throw error;
console.error(`Error: File not found: ${inputFile}`);
process.exit(1);
}
let outputFile = args[1];
if (!outputFile) {
const parsed = path.parse(inputFile);
outputFile = path.join(parsed.dir, parsed.name + ".js");
}
const outputPath = path.resolve(outputFile);
let phpCode;
try {
phpCode = await fs.readFile(inputPath, "utf8");
}
catch (err) {
console.error(`Error reading file ${inputFile}: ${err.message}`);
process.exit(1);
}
try {
const transpiler = new JSTranspiler();
const format = outputPath.endsWith(".cjs") ? "cjs" : "esm";
const result = await transpiler.transpile(phpCode, inputPath, { format });
const jsCode = result.code;
const outputDir = path.dirname(outputPath);
await fs.mkdir(outputDir, { recursive: true });
await fs.writeFile(outputPath, jsCode, "utf8");
console.log(`Successfully transpiled ${inputFile} -> ${outputFile}`);
}
catch (err) {
console.error(`Transpilation error: ${err.message}`);
process.exit(1);
}
}
//# sourceMappingURL=php-to-js.js.map