-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d713168
commit 3096cfc
Showing
13 changed files
with
287 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
npm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function getVersion() { | ||
return "1.2.2"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
Oops, something went wrong.