Skip to content

Commit

Permalink
publish to NPM
Browse files Browse the repository at this point in the history
  • Loading branch information
bcheidemann committed Aug 23, 2023
1 parent d713168 commit 3096cfc
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Ben Heidemann

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ Entype is a CLI tool and library which ingests serialized data formats

## Installation

### Deno

Entype can be installed using the Deno CLI:

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

And can then be run using the `entype` commands:
And can then be run using the `entype` command:

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

### NPM

Entype can be installed using NPM:

```sh
npm i -g typegen-json
```

And can then be run using the `typegen-json` command:

```sh
typegen-json --lang rust fixtures/datapack/blockstates/*.json
```

Alternatively, it can be run using NPX without the need to install the command
globally:

```sh
npx typegen-json --lang rust fixtures/datapack/blockstates/*.json
```

## Usage

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

### Node.js

For Node.js versions of entype (i.e. the one installed from NPM) plugins cannot
be imported by URL. Instead, third party plugins must be published to NPM and
can then be imported by their package name.

## How it works

Entype tries to generate the simplest possible type that accurately describes
Expand Down
6 changes: 5 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"tasks": {
"dev": "deno run --watch main.ts"
"dev": "deno run --allow-read=. --allow-write=. --allow-net main.ts",
"debug": "deno run --allow-read=. --allow-write=. --allow-net --inspect-brk main.ts",
"test": "deno test --allow-read=. --allow-write=.",
"bench": "deno bench --allow-read=. --allow-write=.",
"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"
}
}
90 changes: 89 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions lib/cli/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import "npm:@total-typescript/ts-reset";
import { Json } from "../types.ts";
import { parseJson } from "../parse.ts";
import { collapseTypes } from "../collapse-types.ts";
import { emitRootType } from "../emit/mod.ts";
import { isPromiseFulfilledResult, isPromiseRejectedResult } from "./util.ts";
import { loadPlugin } from "../plugins/mod.ts";
import { getVersion } from "../version.ts";
import { parseArgs } from "./parse-args.ts";

export async function main(
runtime: "deno" | "node",
args: string[],
): Promise<0 | 1> {
const config = parseArgs(args);

if ("help" in config) {
if (runtime === "deno") {
console.log(
"Usage: entype --lang <rust|typescript> [files]\n",
);
} else {
console.log(
"Usage: node typegen-json --lang <rust|typescript> [files]\n",
);
}

if ("message" in config) {
console.error(config.message);
}

if (config.invalidArgs) {
return 1;
}

return 0;
}

if ("version" in config) {
console.log(getVersion());
return 0;
}

if ("lang" in config) {
let exitStatusCode: 0 | 1 = 0;

const types = await Promise.allSettled(
config.files.map(async (file) => {
const json = await Deno.readTextFile(file);
const obj = JSON.parse(json) as Json;
return parseJson(obj);
}),
);

if (types.some(isPromiseRejectedResult)) {
console.error([
"Failed to parse some files:",
...types.map((result, i) => {
if (result.status === "fulfilled") {
return ` ${config.files[i]}: OK`;
} else {
return ` ${config.files[i]}: ${result.reason}`;
}
}),
].join("\n"));
exitStatusCode = 1;
}

const type = collapseTypes(
types
.filter(isPromiseFulfilledResult)
.map((result) => result.value),
);

const plugins = await Promise.all(config.plugins.map(loadPlugin));

await emitRootType(config.lang, type, plugins, console.log);

return exitStatusCode;
}

throw new Error("Unreachable (received invalid config)");
}
2 changes: 1 addition & 1 deletion lib/cli/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { parseArgs } from "./parse-args.ts";
export * from "./main.ts";
File renamed without changes.
8 changes: 8 additions & 0 deletions lib/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from "./cli/mod.ts";
export * from "./emit/mod.ts";
export * from "./plugins/mod.ts";
export * from "./collapse-types.ts";
export * from "./parse.ts";
export * from "./type-guards.ts";
export * from "./util.ts";
export * from "./version.ts";
3 changes: 3 additions & 0 deletions lib/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getVersion() {
return "1.2.2";
}
76 changes: 2 additions & 74 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,6 @@
import "npm:@total-typescript/ts-reset";
import { Json } from "./lib/types.ts";
import { parseJson } from "./lib/parse.ts";
import { collapseTypes } from "./lib/collapse-types.ts";
import { emitRootType } from "./lib/emit/mod.ts";
import { parseArgs } from "./lib/cli/mod.ts";
import { isPromiseFulfilledResult, isPromiseRejectedResult } from "./util.ts";
import { loadPlugin } from "./lib/plugins/mod.ts";

async function main(args: string[]): Promise<0 | 1> {
const config = parseArgs(args);

if ("help" in config) {
console.log(
"Usage: deno run --allow-read main.ts --lang <rust|typescript> [files]\n",
);

if ("message" in config) {
console.error(config.message);
}

if (config.invalidArgs) {
return 1;
}

return 0;
}

if ("version" in config) {
console.log("1.2.1");
return 0;
}

if ("lang" in config) {
let exitStatusCode: 0 | 1 = 0;

const types = await Promise.allSettled(
config.files.map(async (file) => {
const json = await Deno.readTextFile(file);
const obj = JSON.parse(json) as Json;
return parseJson(obj);
}),
);

if (types.some(isPromiseRejectedResult)) {
console.error([
"Failed to parse some files:",
...types.map((result, i) => {
if (result.status === "fulfilled") {
return ` ${config.files[i]}: OK`;
} else {
return ` ${config.files[i]}: ${result.reason}`;
}
}),
].join("\n"));
exitStatusCode = 1;
}

const type = collapseTypes(
types
.filter(isPromiseFulfilledResult)
.map((result) => result.value),
);

const plugins = await Promise.all(config.plugins.map(loadPlugin));

await emitRootType(config.lang, type, plugins, console.log);

return exitStatusCode;
}

throw new Error("Unreachable (received invalid config)");
}
import { main } from "./lib/cli/mod.ts";

if (import.meta.main) {
const code = await main(Deno.args);
const code = await main("deno", Deno.args);
Deno.exit(code);
}
4 changes: 4 additions & 0 deletions npm-main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { main } from "./lib/cli/mod.ts";

main("node", Deno.args)
.then((code) => Deno.exit(code));
Loading

0 comments on commit 3096cfc

Please sign in to comment.