Skip to content

Commit

Permalink
feat: enable cli flags to quicktype wrapper (#63)
Browse files Browse the repository at this point in the history
* feat: enable cli flags to quicktype wrapper

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>

* fix: remove npm start edit

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>

* fix: change default to use flag override

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
  • Loading branch information
grant authored Sep 3, 2020
1 parent 206f83b commit a2d1fc4
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 38 deletions.
15 changes: 12 additions & 3 deletions tools/quicktype-wrapper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,26 @@ npm run start

## Configure

To configure the script, set required environment variables. (Command-line flags aren't supported yet.)
To configure the script, use _environment variables_ or _command-line flags_:

Here's an example:
### Environment Variables

```sh
IN=~/Documents/github/googleapis/google-cloudevents/proto
OUT=~/Documents/out
L=typescript

qt
```

Then run `qt`, which will pick up these environment variables.
### Command-line Flags

```sh
qt \
--in=~/Documents/github/googleapis/google-cloudevents/proto \
--out=~/Documents/out \
--l=typescript
```

## Develop/Watch

Expand Down
126 changes: 98 additions & 28 deletions tools/quicktype-wrapper/package-lock.json

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

3 changes: 2 additions & 1 deletion tools/quicktype-wrapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"mkdirp": "^1.0.4",
"quicktype-core": "^6.0.68",
"recursive-readdir": "^2.2.2"
"recursive-readdir": "^2.2.2",
"yargs": "^15.4.1"
}
}
15 changes: 9 additions & 6 deletions tools/quicktype-wrapper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

import { readFileSync, writeFileSync } from "fs";
import {jsonschema2language, LANGUAGE, LANGUAGE_EXT, TARGET_LANGUAGE} from './quickstype';
const {argv} = require('yargs')
const mkdirp = require('mkdirp');
const HOMEDIR = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];

/**
* A simple tool that generates code using quicktype.
*
* Configuration (via environment variables or command-line flags):
* @param {string} IN The directory for JSON Schema input. Must have trailing /.
* @param {string} OUT The directory for generated output. Must have trailing /.
* @param {string} L The target language
*/
const IN = process.env.IN;
const OUT = process.env.OUT;
const L = (process.env.L || LANGUAGE.TYPESCRIPT).toUpperCase() as TARGET_LANGUAGE;
const IN = argv.in || process.env.IN;
const OUT = argv.out || process.env.OUT;
const L = (argv.l || process.env.L || LANGUAGE.TYPESCRIPT).toUpperCase() as TARGET_LANGUAGE;

console.log(
`***********************
** Quicktype Wrapper **
***********************
* Valid Languages (L): ${Object.values(LANGUAGE)}
***********************
* Environment Variables:
* Config:
- IN=${IN}
- OUT=${OUT}
- L=${L}
Expand All @@ -46,8 +49,8 @@ async function getJSONSchemasPaths(directory: string) {
(async () => {
console.log('== START ==');
// Validate configuration
if (!IN) console.error('Error: Environment variable `IN` not set');
if (!OUT) console.error('Error: Environment variable `OUT` not set');
if (!IN) console.error('Error in config: `IN` not set');
if (!OUT) console.error('Error in config: `OUT` not set');
if (!IN || !OUT) return;

// Get all paths for input.
Expand Down

0 comments on commit a2d1fc4

Please sign in to comment.