Skip to content

Commit 50f34b2

Browse files
authored
Merge pull request #8 from havardh/feat/npx-sdef-to-dfs
Feat/npx sdef to dfs
2 parents 742e050 + 5c20260 commit 50f34b2

File tree

4 files changed

+164
-5
lines changed

4 files changed

+164
-5
lines changed

packages/@jxa/sdef-to-dts/README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Install with [npm](https://www.npmjs.com/):
88

99
npm install @jxa/sdef-to-dts
1010

11-
## Usage
11+
## Usage as library
1212

1313
Convert `./input/*.sdef` file and output it as `d.ts` to `./output` directory.
1414

@@ -31,6 +31,27 @@ Promise.all(promises).then(() => {
3131
});
3232
```
3333

34+
## Usage as command
35+
36+
Convert application bundles (e.g. `/Applications/Safari.app`) and output as `d.ts`.
37+
38+
```
39+
Scripting definition files (sdefs) to TypeScript (d.ts)
40+
41+
Usage
42+
$ npx @jxa/sdef-to-dts <input> --output <output>
43+
44+
<input> path to an Application.app to read
45+
46+
Options
47+
--output, -o path to an Application.d.ts or a directory to write to
48+
--version show the version
49+
--help show this help page
50+
51+
Examples
52+
$ npx @jxa/sdef-to-dts /Applications/Safari.app --output ./safari.d.ts
53+
```
54+
3455
## Running tests
3556

3657
yarn test

packages/@jxa/sdef-to-dts/bin/cmd.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
const child_process = require("child_process");
6+
const transform = require("../lib/sdef-to-dts").transform;
7+
const meow = require("meow");
8+
const execa = require("execa");
9+
10+
const cli = meow(`
11+
Usage
12+
$ npx @jxa/sdef-to-dts <input> --output <output>
13+
14+
<input> path to an Application.app to read
15+
16+
Options
17+
--output, -o path to an Application.d.ts or a directory to write to
18+
--version show the version
19+
--help show this help page
20+
21+
Examples
22+
$ npx @jxa/sdef-to-dts /Applications/Safari.app --output ./safari.d.ts
23+
`, {
24+
flags: {
25+
output: {
26+
type: 'string',
27+
alias: 'o'
28+
}
29+
},
30+
autoVersion: true,
31+
autoHelp: true
32+
});
33+
34+
run(cli.input[0], cli.flags.output);
35+
36+
function run(inPath, out) {
37+
if (!inPath) {
38+
console.error("Error: Missing output parameter.");
39+
cli.showHelp();
40+
process.exit(1);
41+
}
42+
43+
const appName = path.basename(inPath, ".app").replace(/\s/g, "");
44+
const outPath = outFile(appName, out);
45+
46+
const sdef = readSdef(inPath);
47+
writeDTS(appName, outPath, sdef)
48+
.then(() => {
49+
console.log("Converted: " + inPath + " to " + outPath);
50+
});
51+
}
52+
53+
function outFile(appName, out) {
54+
if (out) {
55+
if (fs.existsSync(out) && fs.lstatSync(out).isDirectory()) {
56+
return path.resolve(out, appName) + ".d.ts";
57+
} else {
58+
return path.resolve(out).replace(/.d.ts$/, "") + ".d.ts";
59+
}
60+
} else {
61+
return path.resolve(appName) + ".d.ts";
62+
}
63+
}
64+
65+
function readSdef(path) {
66+
try {
67+
return execa.sync("sdef", [path], {stdio: ["ignore"]}).stdout;
68+
} catch (e) {
69+
console.error(e.toString());
70+
cli.showHelp();
71+
process.exit(2);
72+
}
73+
}
74+
75+
function writeDTS(appName, outPath, sdef) {
76+
return transform(appName, sdef).then(content => {
77+
fs.writeFileSync(outPath, content, "utf-8");
78+
}).catch(err => {
79+
console.error(err.toString());
80+
cli.showHelp();
81+
process.exit(3);
82+
});
83+
}
84+

packages/@jxa/sdef-to-dts/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@jxa/sdef-to-dts",
33
"version": "0.3.1",
4-
"description": "Scripting definition files (sdefs) to TypeScript(d.ts)",
4+
"description": "Scripting definition files (sdefs) to TypeScript (d.ts)",
55
"keywords": [
66
"applescript",
77
"dts",
@@ -22,6 +22,7 @@
2222
],
2323
"main": "lib/sdef-to-dts.js",
2424
"types": "lib/sdef-to-dts.d.ts",
25+
"bin": "bin/cmd.js",
2526
"directories": {
2627
"lib": "lib",
2728
"test": "test"
@@ -62,9 +63,11 @@
6263
"@rgrove/parse-xml": "^1.1.1",
6364
"@types/camelcase": "^4.1.0",
6465
"camelcase": "^5.0.0",
66+
"execa": "^1.0.0",
6567
"indent-string": "^3.2.0",
6668
"is-keyword": "^1.2.2",
6769
"is-var-name": "^2.0.0",
68-
"json-schema-to-typescript": "^5.4.0"
70+
"json-schema-to-typescript": "^5.4.0",
71+
"meow": "^5.0.0"
6972
}
7073
}

yarn.lock

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ cross-spawn@^5.0.1, cross-spawn@^5.1.0:
539539
shebang-command "^1.2.0"
540540
which "^1.2.9"
541541

542-
cross-spawn@^6.0.5:
542+
cross-spawn@^6.0.0, cross-spawn@^6.0.5:
543543
version "6.0.5"
544544
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
545545
dependencies:
@@ -628,6 +628,12 @@ duplexer@^0.1.1:
628628
version "0.1.1"
629629
resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.1.tgz#ace6ff808c1ce66b57d1ebf97977acb02334cfc1"
630630

631+
end-of-stream@^1.1.0:
632+
version "1.4.1"
633+
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.1.tgz#ed29634d19baba463b6ce6b80a37213eab71ec43"
634+
dependencies:
635+
once "^1.4.0"
636+
631637
error-ex@^1.2.0, error-ex@^1.3.1:
632638
version "1.3.1"
633639
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.1.tgz#f855a86ce61adc4e8621c3cda21e7a7612c3a8dc"
@@ -705,6 +711,18 @@ execa@^0.8.0:
705711
signal-exit "^3.0.0"
706712
strip-eof "^1.0.0"
707713

714+
execa@^1.0.0:
715+
version "1.0.0"
716+
resolved "https://registry.yarnpkg.com/execa/-/execa-1.0.0.tgz#c6236a5bb4df6d6f15e88e7f017798216749ddd8"
717+
dependencies:
718+
cross-spawn "^6.0.0"
719+
get-stream "^4.0.0"
720+
is-stream "^1.1.0"
721+
npm-run-path "^2.0.0"
722+
p-finally "^1.0.0"
723+
signal-exit "^3.0.0"
724+
strip-eof "^1.0.0"
725+
708726
external-editor@^2.0.4:
709727
version "2.2.0"
710728
resolved "https://registry.yarnpkg.com/external-editor/-/external-editor-2.2.0.tgz#045511cfd8d133f3846673d1047c154e214ad3d5"
@@ -787,6 +805,12 @@ get-stream@^3.0.0:
787805
version "3.0.0"
788806
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14"
789807

808+
get-stream@^4.0.0:
809+
version "4.0.0"
810+
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-4.0.0.tgz#9e074cb898bd2b9ebabb445a1766d7f43576d977"
811+
dependencies:
812+
pump "^3.0.0"
813+
790814
git-raw-commits@^1.3.0, git-raw-commits@^1.3.6:
791815
version "1.3.6"
792816
resolved "https://registry.yarnpkg.com/git-raw-commits/-/git-raw-commits-1.3.6.tgz#27c35a32a67777c1ecd412a239a6c19d71b95aff"
@@ -1335,6 +1359,20 @@ meow@^4.0.0:
13351359
redent "^2.0.0"
13361360
trim-newlines "^2.0.0"
13371361

1362+
meow@^5.0.0:
1363+
version "5.0.0"
1364+
resolved "https://registry.yarnpkg.com/meow/-/meow-5.0.0.tgz#dfc73d63a9afc714a5e371760eb5c88b91078aa4"
1365+
dependencies:
1366+
camelcase-keys "^4.0.0"
1367+
decamelize-keys "^1.0.0"
1368+
loud-rejection "^1.0.0"
1369+
minimist-options "^3.0.1"
1370+
normalize-package-data "^2.3.4"
1371+
read-pkg-up "^3.0.0"
1372+
redent "^2.0.0"
1373+
trim-newlines "^2.0.0"
1374+
yargs-parser "^10.0.0"
1375+
13381376
mimic-fn@^1.0.0:
13391377
version "1.2.0"
13401378
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.2.0.tgz#820c86a39334640e99516928bd03fca88057d022"
@@ -1470,7 +1508,7 @@ object-assign@^4.0.1, object-assign@^4.1.0:
14701508
version "4.1.1"
14711509
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
14721510

1473-
once@^1.3.0:
1511+
once@^1.3.0, once@^1.3.1, once@^1.4.0:
14741512
version "1.4.0"
14751513
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
14761514
dependencies:
@@ -1633,6 +1671,13 @@ pseudomap@^1.0.2:
16331671
version "1.0.2"
16341672
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"
16351673

1674+
pump@^3.0.0:
1675+
version "3.0.0"
1676+
resolved "https://registry.yarnpkg.com/pump/-/pump-3.0.0.tgz#b4a2116815bde2f4e1ea602354e8c75565107a64"
1677+
dependencies:
1678+
end-of-stream "^1.1.0"
1679+
once "^1.3.1"
1680+
16361681
q@^1.4.1, q@^1.5.1:
16371682
version "1.5.1"
16381683
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"
@@ -2252,6 +2297,12 @@ yallist@^2.1.2:
22522297
version "2.1.2"
22532298
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"
22542299

2300+
yargs-parser@^10.0.0:
2301+
version "10.1.0"
2302+
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"
2303+
dependencies:
2304+
camelcase "^4.1.0"
2305+
22552306
yargs-parser@^7.0.0:
22562307
version "7.0.0"
22572308
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-7.0.0.tgz#8d0ac42f16ea55debd332caf4c4038b3e3f5dfd9"

0 commit comments

Comments
 (0)