Skip to content

Commit 3096cfc

Browse files
committed
publish to NPM
1 parent d713168 commit 3096cfc

File tree

13 files changed

+287
-78
lines changed

13 files changed

+287
-78
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npm

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Ben Heidemann
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice (including the next
13+
paragraph) shall be included in all copies or substantial portions of the
14+
Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ Entype is a CLI tool and library which ingests serialized data formats
66

77
## Installation
88

9+
### Deno
10+
911
Entype can be installed using the Deno CLI:
1012

1113
```sh
1214
deno install --allow-read --allow-net https://deno.land/x/entype/main.ts
1315
```
1416

15-
And can then be run using the `entype` commands:
17+
And can then be run using the `entype` command:
1618

1719
```sh
1820
entype --lang rust fixtures/datapack/blockstates/*.json
@@ -25,6 +27,27 @@ command globally:
2527
deno run --allow-read https://deno.land/x/entype/main.ts --lang rust fixtures/datapack/blockstates/*.json
2628
```
2729

30+
### NPM
31+
32+
Entype can be installed using NPM:
33+
34+
```sh
35+
npm i -g typegen-json
36+
```
37+
38+
And can then be run using the `typegen-json` command:
39+
40+
```sh
41+
typegen-json --lang rust fixtures/datapack/blockstates/*.json
42+
```
43+
44+
Alternatively, it can be run using NPX without the need to install the command
45+
globally:
46+
47+
```sh
48+
npx typegen-json --lang rust fixtures/datapack/blockstates/*.json
49+
```
50+
2851
## Usage
2952

3053
Entype accepts files to generate type definitions for and emits type definitions
@@ -364,6 +387,12 @@ to `main` and `path` defaults to `mod.ts`. The plugin speicifier
364387
`github:bcheidemann/entype-plugin-example` would be resolved to
365388
`https://raw.githubusercontent.com/bcheidemann/entype-plugin-example/main/mod.ts`.
366389

390+
### Node.js
391+
392+
For Node.js versions of entype (i.e. the one installed from NPM) plugins cannot
393+
be imported by URL. Instead, third party plugins must be published to NPM and
394+
can then be imported by their package name.
395+
367396
## How it works
368397

369398
Entype tries to generate the simplest possible type that accurately describes

deno.jsonc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"tasks": {
3-
"dev": "deno run --watch main.ts"
3+
"dev": "deno run --allow-read=. --allow-write=. --allow-net main.ts",
4+
"debug": "deno run --allow-read=. --allow-write=. --allow-net --inspect-brk main.ts",
5+
"test": "deno test --allow-read=. --allow-write=.",
6+
"bench": "deno bench --allow-read=. --allow-write=.",
7+
"build:npm": "deno run --allow-read --allow-write --allow-env=DENO_DIR,XDG_CACHE_HOME,HOME,DENO_AUTH_TOKENS,XDG_DATA_HOME --allow-net=deno.land --allow-run=npm scripts/build-npm.ts"
48
}
59
}

deno.lock

Lines changed: 89 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/cli/main.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import "npm:@total-typescript/ts-reset";
2+
import { Json } from "../types.ts";
3+
import { parseJson } from "../parse.ts";
4+
import { collapseTypes } from "../collapse-types.ts";
5+
import { emitRootType } from "../emit/mod.ts";
6+
import { isPromiseFulfilledResult, isPromiseRejectedResult } from "./util.ts";
7+
import { loadPlugin } from "../plugins/mod.ts";
8+
import { getVersion } from "../version.ts";
9+
import { parseArgs } from "./parse-args.ts";
10+
11+
export async function main(
12+
runtime: "deno" | "node",
13+
args: string[],
14+
): Promise<0 | 1> {
15+
const config = parseArgs(args);
16+
17+
if ("help" in config) {
18+
if (runtime === "deno") {
19+
console.log(
20+
"Usage: entype --lang <rust|typescript> [files]\n",
21+
);
22+
} else {
23+
console.log(
24+
"Usage: node typegen-json --lang <rust|typescript> [files]\n",
25+
);
26+
}
27+
28+
if ("message" in config) {
29+
console.error(config.message);
30+
}
31+
32+
if (config.invalidArgs) {
33+
return 1;
34+
}
35+
36+
return 0;
37+
}
38+
39+
if ("version" in config) {
40+
console.log(getVersion());
41+
return 0;
42+
}
43+
44+
if ("lang" in config) {
45+
let exitStatusCode: 0 | 1 = 0;
46+
47+
const types = await Promise.allSettled(
48+
config.files.map(async (file) => {
49+
const json = await Deno.readTextFile(file);
50+
const obj = JSON.parse(json) as Json;
51+
return parseJson(obj);
52+
}),
53+
);
54+
55+
if (types.some(isPromiseRejectedResult)) {
56+
console.error([
57+
"Failed to parse some files:",
58+
...types.map((result, i) => {
59+
if (result.status === "fulfilled") {
60+
return ` ${config.files[i]}: OK`;
61+
} else {
62+
return ` ${config.files[i]}: ${result.reason}`;
63+
}
64+
}),
65+
].join("\n"));
66+
exitStatusCode = 1;
67+
}
68+
69+
const type = collapseTypes(
70+
types
71+
.filter(isPromiseFulfilledResult)
72+
.map((result) => result.value),
73+
);
74+
75+
const plugins = await Promise.all(config.plugins.map(loadPlugin));
76+
77+
await emitRootType(config.lang, type, plugins, console.log);
78+
79+
return exitStatusCode;
80+
}
81+
82+
throw new Error("Unreachable (received invalid config)");
83+
}

lib/cli/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { parseArgs } from "./parse-args.ts";
1+
export * from "./main.ts";

util.ts renamed to lib/cli/util.ts

File renamed without changes.

lib/mod.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export * from "./cli/mod.ts";
2+
export * from "./emit/mod.ts";
3+
export * from "./plugins/mod.ts";
4+
export * from "./collapse-types.ts";
5+
export * from "./parse.ts";
6+
export * from "./type-guards.ts";
7+
export * from "./util.ts";
8+
export * from "./version.ts";

lib/version.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function getVersion() {
2+
return "1.2.2";
3+
}

0 commit comments

Comments
 (0)