Skip to content

Commit

Permalink
feat(sdef-to-dfs): add command line converter as bin
Browse files Browse the repository at this point in the history
Adds a converter as the default bin which will convert a '.app'
application container into a '.d.ts' file.
  • Loading branch information
havardh committed Sep 6, 2018
1 parent 742e050 commit 7ac5224
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
70 changes: 70 additions & 0 deletions packages/@jxa/sdef-to-dts/bin/cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env node

const fs = require("fs");
const path = require("path");
const child_process = require("child_process");
const transform = require("../lib/sdef-to-dts").transform;

function usage() {
console.log();
console.log("Usage: npx @jxa/sdef-to-dts inFile [out]");
console.log();
console.log(" Converts Script Definitions into TypeScript Type Definitions")
console.log();
console.log(" inFile - path to an Application.app to read");
console.log(" out - path to an Application.d.ts or a directory to write to");
console.log();
}

if (process.argv.length !== 3 && process.argv.length !== 4) {
usage();
process.exit(1);
}

const [node, cmd, inFile, out] = process.argv;

run(inFile, out);

function run(inPath, out) {
const appName = path.basename(inPath, ".app").replace(/\s/g, "");
const outPath = outFile(appName, out);

const sdef = readSdef(inPath);
writeDTS(appName, outPath, sdef)
.then(() => {
console.log("Converted: " + inPath + " to " + outPath);
});
}

function outFile(appName, out) {
if (out) {
if (fs.existsSync(out) && fs.lstatSync(out).isDirectory()) {
return path.resolve(out, appName) + ".d.ts";
} else {
return path.resolve(out).replace(/.d.ts$/, "") + ".d.ts";
}
} else {
return path.resolve(appName) + ".d.ts";
}
}

function readSdef(path) {
try {
return child_process.execSync("sdef " + path, {stdio: ["ignore"]}).toString();
} catch (e) {
console.error(e.toString());
usage();
process.exit(2);
}
}

function writeDTS(appName, outPath, sdef) {
return transform(appName, sdef).then(content => {
fs.writeFileSync(outPath, content, "utf-8");
}).catch(err => {
console.error(err.toString());
usage();
process.exit(3);
});
}

1 change: 1 addition & 0 deletions packages/@jxa/sdef-to-dts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
],
"main": "lib/sdef-to-dts.js",
"types": "lib/sdef-to-dts.d.ts",
"bin": "bin/cmd.js",
"directories": {
"lib": "lib",
"test": "test"
Expand Down

0 comments on commit 7ac5224

Please sign in to comment.