Skip to content

Commit

Permalink
feat: colored output when printint to stdout in TTY mode
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed May 29, 2024
1 parent cb41da8 commit 804050f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 28 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ $ TSJG_DEV=1 node_modules/.bin/tsjg -t 'My.Type.Name' 'my/project/**/*.ts'
<!-- commands -->

- [`tjsg autocomplete [SHELL]`](#tjsg-autocomplete-shell)
- [`tjsg generate [PATH]`](#tjsg-generate-path)
- [`tjsg generate PATH`](#tjsg-generate-path)
- [`tjsg help [COMMAND]`](#tjsg-help-command)

## `tjsg autocomplete [SHELL]`
Expand Down Expand Up @@ -100,19 +100,19 @@ EXAMPLES

_See code: [@oclif/plugin-autocomplete](https://github.com/oclif/plugin-autocomplete/blob/v3.0.18/src/commands/autocomplete/index.ts)_

## `tjsg generate [PATH]`
## `tjsg generate PATH`

Generate JSON schema from your Typescript sources

```
USAGE
$ tjsg generate [PATH] [-t <value>] [--top-ref] [-i <value>] [-p <value>] [-e all|none|export] [-j
$ tjsg generate PATH [-t <value>] [--top-ref] [-i <value>] [-p <value>] [-e all|none|export] [-j
none|basic|extended] [--markdown-description] [--sort-props] [--strict-tuples] [--type-check] [--ref-encode]
[--additional-properties] [--functions fail|comment|hide] [--minify] [-o <value>] [--extra-tags <value>]
[--additional-properties] [--functions fail|comment|hide] [--minify | --color] [-o <value>] [--extra-tags <value>]
[--discriminator-type json-schema|open-api]
ARGUMENTS
PATH Source file path
PATH Source root filepath
FLAGS
-e, --expose=<option> [default: export] Type exposing
Expand All @@ -125,6 +125,8 @@ FLAGS
settings
-t, --type=<value> Type name
--additional-properties Allow additional properties for objects with no index signature
--[no-]color Pretty print the json with colors. Only works when outputting to stdout. Defaults
to true when TTY
--discriminator-type=<option> [default: json-schema] Type of discriminator to use
<options: json-schema|open-api>
--extra-tags=<value>... [default: ] Provide additional validation keywords to include
Expand Down
9 changes: 4 additions & 5 deletions bin/tjsg.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node

import { execute } from "@oclif/core";

await execute({
development: !!process.env.TJSG_DEV,
dir: import.meta.url,
require("@oclif/core").execute({

Check failure on line 3 in bin/tjsg.js

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Require statement not part of import statement

Check failure on line 3 in bin/tjsg.js

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

A `require()` style import is forbidden

Check failure on line 3 in bin/tjsg.js

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Require statement not part of import statement

Check failure on line 3 in bin/tjsg.js

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

A `require()` style import is forbidden
// development mode prints stack traces and more useful information
development: !!JSON.parse(process.env.DEBUG || "false"),
dir: __dirname,
});
35 changes: 22 additions & 13 deletions commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from "node:fs";
import { dirname } from "node:path";
import { Command, Flags, Args } from "@oclif/core";
import { Args, Command, Flags } from "@oclif/core";
import stableStringify from "safe-stable-stringify";
import { createGenerator } from "../factory/generator.js";
import type { CompletedConfig, Config } from "../src/Config.js";
import type { CompletedConfig } from "../src/Config.js";
import { BaseError } from "../src/Error/BaseError.js";

export default class Generate extends Command {
Expand All @@ -22,7 +22,8 @@ export default class Generate extends Command {

static override args = {
path: Args.file({
description: "Source file path",
description: "Source root filepath",
required: true,
}),
};

Expand Down Expand Up @@ -96,6 +97,14 @@ export default class Generate extends Command {
minify: Flags.boolean({
description: "Minify generated schema",
default: false,
exclusive: ["color"],
}),
color: Flags.boolean({
description:
"Pretty print the json with colors. Only works when outputting to stdout. Defaults to true when TTY",
default: async (c) => !c.flags.minify && (!!process.env.TTY || process.stdout.isTTY),
allowNo: true,
exclusive: ["minify"],
}),
out: Flags.file({
char: "o",
Expand Down Expand Up @@ -164,24 +173,24 @@ export default class Generate extends Command {

await fs.promises.mkdir(outPath, { recursive: true });
await fs.promises.writeFile(flags.out, schemaString, { encoding: "utf-8" });
} else {
// write to stdout
process.stdout.write(`${schemaString}\n`);
}

// When printing to stdout, we cannot output anything else than a JSON
if (flags.out) {
// When printing to stdout, we cannot output anything else than a JSON
this.log("Schema generated successfully");
return;
}
} catch (error) {
this.error("Could not generate schema", { exit: false });

if (flags.color) {
this.logJson(schema);
} else {
this.log(schemaString);
}
} catch (error) {
if (error instanceof BaseError) {
process.stderr.write(error.format());
process.exit(1);
this.exit(1);
}

throw error;
this.error(error);
}
}
}
13 changes: 11 additions & 2 deletions oclif.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"aliases": [],
"args": {
"path": {
"description": "Source file path",
"name": "path"
"description": "Source root filepath",
"name": "path",
"required": true
}
},
"description": "Generate JSON schema from your Typescript sources",
Expand Down Expand Up @@ -121,10 +122,18 @@
},
"minify": {
"description": "Minify generated schema",
"exclusive": ["color"],
"name": "minify",
"allowNo": false,
"type": "boolean"
},
"color": {
"description": "Pretty print the json with colors. Only works when outputting to stdout. Defaults to true when TTY",
"exclusive": ["minify"],
"name": "color",
"allowNo": true,
"type": "boolean"
},
"out": {
"char": "o",
"description": "Set the output file (default: stdout)",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
"ts-json-schema-generator": "bin/tjsg.js"
"ts-json-schema-generator": "bin/ts-json-schema-generator.js",
"tsjg": "bin/tsjg.js"
},
"files": [
"dist",
Expand Down
2 changes: 1 addition & 1 deletion src/Error/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class UnknownTypeError extends BaseError {
constructor(readonly type: BaseType) {
super({
code: 101,
messageText: `Unknown type "${type.getId()}"`,
messageText: `Unknown type "${type?.getId()}"`,
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"incremental": true,
"importHelpers": true
},
"files": ["index.ts"],
"files": ["index.ts", "ts-json-schema-generator.ts"],
"include": ["src/**/*.ts", "factory/**/*.ts", "commands/**/*.ts"],
"exclude": ["node_modules", "dist", "cjs"]
}

0 comments on commit 804050f

Please sign in to comment.