Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
*.md
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node_modules
node_modules/
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:18-alpine

COPY . /src
WORKDIR /src
RUN npm install
WORKDIR /work

ENTRYPOINT [ "node", "/src/dereference.js" ]
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@

## Usage

```
```bash
# Install globally:
npm install -g json-dereference-cli
json-dereference -s my-schema.json -o compiled-schema.yaml
json-dereference -s <schema> [-b <s3bucket>] [-i <spaces>] [-o <output>] [-t <type>]

# Using npx:
npx json-dereference-cli -s <schema> [-b <s3bucket>] [-i <spaces>] [-o <output>] [-t <type>]

# Using Docker:
docker build . -t json-dereference-cli
docker run --rm -v $PWD:/work json-dereference-cli:latest -s <schema> [-b <s3bucket>] [-i <spaces>] [-o <output>] [-t <type>]
```

_Note: The input file can either be `json`, or `yaml` / `yml`._
*Note:* The input file can either be `json`, or `yaml` / `yml`.

_Note: The output file types are either `json` or `yaml` / `yml`. This is determined from the file extension for the output file path passed in._
*Note:* The output file types are either `json` or `yaml` / `yml`. This is determined from the file extension for the output file path passed in or using `-t json|yaml` when writing to stdout.

### $ref: "s3://.."
## Resolving s3 references

This CLI tool will also attempt to resolve S3 references using the `aws-sdk`. Take a look [here](/s3-resolver.js) to see the custom resolver code.
This CLI tool will also attempt to resolve S3 references (`$ref: "s3://.."`) using the `aws-sdk`. Take a look [here](s3-resolver.js) to see the custom resolver code.
58 changes: 40 additions & 18 deletions dereference.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,62 @@
var fs = require('fs');
var path = require('path');
var util = require('util');
var $RefParser = require('json-schema-ref-parser');
var $RefParser = require('@apidevtools/json-schema-ref-parser');
var argv = require('minimist')(process.argv.slice(2));
var s3Resolver = require('./s3-resolver');
var yaml = require('js-yaml')

if (!argv.s || !argv.o) {
console.log('USAGE: ' + process.argv[1] + ' -s <schema> -o <output> [...]');
// Argument handling
if (!argv.s) {
console.error('Usage: ' + process.argv[1] + ' -s <schema> [-b <s3bucket>] [-i <spaces>] [-o <output>] [-t <type>]');
process.exit(1);
}

if (argv.b) s3Resolver.bucket = argv.b;

var input = path.resolve(argv.s);
var output = argv.o ? path.resolve(argv.o) : undefined;
var indent = argv.i !== undefined ? argv.i : 2

var schema = fs.readFileSync(input, { encoding: 'utf8' });

console.log("Dereferencing schema: " + input);

// Schema dereferencing
console.warn("Dereferencing schema: " + input);
$RefParser.dereference(input, { resolve: { s3: s3Resolver } }, function(err, schema) {
if (err) {
console.error(err);
} else {
var output = path.resolve(argv.o);
console.error('ERROR: ' + err);
return
}

// Detect output format
var type = 'json'
if (argv.t) {
type = argv.t
} else if (output) {
var ext = path.parse(output).ext;

if (ext == '.json') {
var data = JSON.stringify(schema);
fs.writeFileSync(output, data, { encoding: 'utf8', flag: 'w' });
type = 'json'
} else if (ext.match(/^\.?(yaml|yml)$/)) {
var yaml = require('node-yaml');
yaml.writeSync(output, schema, { encoding: 'utf8' })
type = 'yaml'
} else {
console.error("Unrecognised output file type: " + output);
console.error('ERROR: Cannot detect output file type from file name (please set -t <type>): ' + output);
process.exit(1);
}
console.log("Wrote file: " + output);
}

// Generate resolved schema type
var data = ''
if (type == 'json') {
data = JSON.stringify(schema, null, indent);
} else if (type == 'yaml') {
data = yaml.dump(schema, { encoding: 'utf8', indent: indent, noRefs: true });
} else {
console.error('ERROR: Unsupported output file type: ' + type);
process.exit(1);
}

// Output resolved schema
if (output) {
fs.writeFileSync(output, data, { encoding: 'utf8', flag: 'w' });
console.warn('Wrote file: ' + output);
} else {
console.log(data)
}
});
Loading