|
| 1 | +# TypeScript Schema Automatic Generator |
| 2 | + |
| 3 | +Read config files and generate json schema files |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +This program requires [Node.js](https://nodejs.org) to run. |
| 8 | + |
| 9 | +```sh |
| 10 | +npm install --global @ts-schema-autogen/cli |
| 11 | +``` |
| 12 | + |
| 13 | +## Usages |
| 14 | + |
| 15 | +The program will scan through directory tree from working directory and search for [config files](#config-file) whose names end with `.schema.autogen.{json,yaml}` to perform actions. |
| 16 | + |
| 17 | +### Config File |
| 18 | + |
| 19 | +A config file is a JSON or YAML file that satisfies [the `Config` type](https://github.com/ksxnodeapps/ts-schema-autogen/blob/master/packages/types/types.ts#L72-L77) or [config.schema.json JSON schema](https://raw.githubusercontent.com/ksxnodeapps/ts-schema-autogen/master/packages/schemas/config.schema.json). |
| 20 | + |
| 21 | +Config files whose names end with either `.schema.autogen.json` or `.schema.autogen.yaml` will be scanned by [`ts-schema-autogen`](cli-commands), others will be skipped. |
| 22 | + |
| 23 | +A config file can inherit `compilerOptions` and `schemaSettings` from other config files via `extends` property. |
| 24 | + |
| 25 | +If your editor support IntelliSense for JSON schemas (such as Visual Studio Code), it is recommended that you have `$schema` property set to `https://raw.githubusercontent.com/ksxnodeapps/ts-schema-autogen/master/packages/schemas/config.schema.json`. Alternatively, you can use `npm` to install `@ts-schema-autogen/schemas` and set `$schema` property to `./node_modules/@ts-schema-autogen/schemas/config.schema.json`. |
| 26 | + |
| 27 | +#### Examples |
| 28 | + |
| 29 | +##### Read 1 symbol from 1 TypeScript file and create 1 schema file |
| 30 | + |
| 31 | +**input.ts** |
| 32 | + |
| 33 | +```typescript |
| 34 | +export interface MyType { |
| 35 | + foo: 123 |
| 36 | + bar: 456 |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +**.schema.autogen.json** |
| 41 | + |
| 42 | +```json |
| 43 | +{ |
| 44 | + "$schema": "https://raw.githubusercontent.com/ksxnodeapps/ts-schema-autogen/master/packages/schemas/config.schema.json", |
| 45 | + "instruction": { |
| 46 | + "compilerOptions": { |
| 47 | + "strictNullChecks": true, |
| 48 | + "strict": true, |
| 49 | + "target": "ES2018", |
| 50 | + "noEmit": true, |
| 51 | + "lib": ["ESNext"] |
| 52 | + }, |
| 53 | + "schemaSettings": { |
| 54 | + "required": true |
| 55 | + }, |
| 56 | + "input": "input.ts", |
| 57 | + "symbol": "MyType", |
| 58 | + "output": "output.json" |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +##### Read 2 symbols from 2 TypeScript files and create 2 schema files |
| 64 | + |
| 65 | +**foo.ts** |
| 66 | + |
| 67 | +```typescript |
| 68 | +/// <reference path="./bar.ts" /> |
| 69 | +export interface Foo { |
| 70 | + readonly bar?: Bar |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +**bar.ts** |
| 75 | + |
| 76 | +```typescript |
| 77 | +/// <reference path="./foo.ts" /> |
| 78 | +export interface Bar { |
| 79 | + readonly foo?: Foo |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +**.schema.autogen.json** |
| 84 | + |
| 85 | +```json |
| 86 | +{ |
| 87 | + "$schema": "https://raw.githubusercontent.com/ksxnodeapps/ts-schema-autogen/master/packages/schemas/config.schema.json", |
| 88 | + "instruction": { |
| 89 | + "compilerOptions": { |
| 90 | + "strictNullChecks": true, |
| 91 | + "strict": true, |
| 92 | + "target": "ES2018", |
| 93 | + "noEmit": true, |
| 94 | + "lib": ["ESNext"] |
| 95 | + }, |
| 96 | + "schemaSettings": { |
| 97 | + "required": true |
| 98 | + }, |
| 99 | + "input": [ |
| 100 | + "foo.ts", |
| 101 | + "bar.ts" |
| 102 | + ], |
| 103 | + "list": [ |
| 104 | + { |
| 105 | + "symbol": "Foo", |
| 106 | + "output": "foo.schema.json" |
| 107 | + }, |
| 108 | + { |
| 109 | + "symbol": "Bar", |
| 110 | + "output": "bar.schema.json" |
| 111 | + } |
| 112 | + ] |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +##### If you have multiple config files, you may want to use `extends` to inherit `compilerOptions` and `schemaSettings` from a common source |
| 118 | + |
| 119 | +**settings.json** |
| 120 | + |
| 121 | +```json |
| 122 | +{ |
| 123 | + "$schema": "https://raw.githubusercontent.com/ksxnodeapps/ts-schema-autogen/master/packages/schemas/config.schema.json", |
| 124 | + "instruction": { |
| 125 | + "compilerOptions": { |
| 126 | + "strictNullChecks": true, |
| 127 | + "strict": true, |
| 128 | + "target": "ES2018", |
| 129 | + "noEmit": true, |
| 130 | + "lib": ["ESNext"] |
| 131 | + }, |
| 132 | + "schemaSettings": { |
| 133 | + "required": true |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +**foo.schema.autogen.json** |
| 140 | + |
| 141 | +```json |
| 142 | +{ |
| 143 | + "$schema": "https://raw.githubusercontent.com/ksxnodeapps/ts-schema-autogen/master/packages/schemas/config.schema.json", |
| 144 | + "extends": "settings.json", |
| 145 | + "instruction": { |
| 146 | + "input": "foo.ts", |
| 147 | + "symbol": "Foo", |
| 148 | + "output": "foo.schema.json" |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +**bar.schema.autogen.json** |
| 154 | + |
| 155 | +```json |
| 156 | +{ |
| 157 | + "$schema": "https://raw.githubusercontent.com/ksxnodeapps/ts-schema-autogen/master/packages/schemas/config.schema.json", |
| 158 | + "extends": "settings.json", |
| 159 | + "instruction": { |
| 160 | + "input": "bar.ts", |
| 161 | + "symbol": "Bar", |
| 162 | + "output": "bar.schema.json" |
| 163 | + } |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +### CLI Commands |
| 168 | + |
| 169 | +``` |
| 170 | +ts-schema-autogen <command> |
| 171 | +
|
| 172 | +Commands: |
| 173 | + ts-schema-autogen test Check for out-of-date schema files |
| 174 | + ts-schema-autogen generate Generate schema files |
| 175 | + ts-schema-autogen clean Delete generated schema files |
| 176 | +
|
| 177 | +Options: |
| 178 | + --version Show version number [boolean] |
| 179 | + --help Show help [boolean] |
| 180 | + --pattern Regular expression that matches basename of config files |
| 181 | + [string] [default: "\.schema\.autogen(\.(json|yaml|yml))?$"] |
| 182 | + --ignored Name of directories to be ignored |
| 183 | + [array] [default: [".git","node_modules"]] |
| 184 | + --version Show version number [boolean] |
| 185 | + --help Show help [boolean] |
| 186 | +``` |
| 187 | + |
| 188 | +## Development |
| 189 | + |
| 190 | +### System Requirements |
| 191 | + |
| 192 | +* Node.js ≥ 13.6.0 |
| 193 | +* Package Manager: [pnpm](https://pnpm.js.org/) |
| 194 | +* Git |
| 195 | + |
| 196 | +### Scripts |
| 197 | + |
| 198 | +#### Build |
| 199 | + |
| 200 | +```sh |
| 201 | +pnpm run build |
| 202 | +``` |
| 203 | + |
| 204 | +#### Clean |
| 205 | + |
| 206 | +```sh |
| 207 | +pnpm run clean |
| 208 | +``` |
| 209 | + |
| 210 | +#### Test |
| 211 | + |
| 212 | +##### Test Everything |
| 213 | + |
| 214 | +```sh |
| 215 | +pnpm test |
| 216 | +``` |
| 217 | + |
| 218 | +##### Test Everything Without Coverage |
| 219 | + |
| 220 | +```sh |
| 221 | +pnpm run test:no-coverage |
| 222 | +``` |
| 223 | + |
| 224 | +##### Test Changed Files Only |
| 225 | + |
| 226 | +```sh |
| 227 | +pnpm test -- --onlyChanged |
| 228 | +``` |
| 229 | + |
| 230 | +##### Update Jest Snapshot |
| 231 | + |
| 232 | +```sh |
| 233 | +pnpm test -- -u |
| 234 | +``` |
| 235 | + |
| 236 | +#### Start Node.js REPL |
| 237 | + |
| 238 | +This starts a Node.js REPL where you can import every module inside `packages/` folder. |
| 239 | + |
| 240 | +```sh |
| 241 | +pnpm run repl |
| 242 | +``` |
| 243 | + |
| 244 | +## License |
| 245 | + |
| 246 | +[MIT](https://git.io/Jvntb) © [Hoàng Văn Khải](https://github.com/KSXGitHub) |
0 commit comments