Skip to content

Commit 9f7cf5e

Browse files
committed
fix: correctly load external config file
- external configuration files are properly handled - transform verbose parameter in config file to a 'loglevel' integer that can be increased. uniformity in handling of verbose param - add test cases
1 parent 7274d72 commit 9f7cf5e

File tree

11 files changed

+118
-45
lines changed

11 files changed

+118
-45
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Options:
6565
-r, --rules [ruleFile] provide multiple rules files
6666
-s, --skip [ruleName] provide multiple rules to skip
6767
-j, --json-schema treat $ref like JSON Schema and convert to OpenAPI Schema Objects
68-
-v, --verbose increase verbosity
68+
-v, --verbose set verbosity (use multiple times to increase level)
6969
-h, --help output usage information
7070
```
7171

@@ -98,7 +98,7 @@ Options:
9898
-o, --output <file> file to output to
9999
-q, --quiet reduce verbosity
100100
-j, --json-schema treat $ref like JSON Schema and convert to OpenAPI Schema Objects
101-
-v, --verbose increase verbosity
101+
-v, --verbose set verbosity (use multiple times to increase level)
102102
-h, --help output usage information
103103
```
104104

@@ -122,7 +122,7 @@ Options:
122122
-p, --port [value] port on which the server will listen (default: 5000)
123123
-q, --quiet reduce verbosity
124124
-j, --json-schema treat $ref like JSON Schema and convert to OpenAPI Schema Objects
125-
-v, --verbose increase verbosity
125+
-v, --verbose set verbosity (use multiple times to increase level)
126126
-h, --help output usage information
127127
```
128128

@@ -138,7 +138,8 @@ jsonSchema: true
138138
# Keep the noise down
139139
quiet: true
140140
# Output a lot of information about what is happening (wont work if you have quiet on)
141-
verbose: true
141+
# Default stdout = 1
142+
verbose: 2
142143
# Rules specific to the lint command
143144
lint:
144145
# rules files to load

lib/config.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ nconf.formats.yaml = require('nconf-yaml');
77
class Config {
88

99
init(args) {
10-
const configFile = args.config || './speccy.yaml';
10+
const configFile = (args.parent && args.parent.config) ? args.parent.config : './speccy.yaml';
1111

1212
this.load(configFile, {
1313
quiet: args.quiet,
@@ -40,20 +40,28 @@ class Config {
4040
format: nconf.formats.yaml,
4141
file,
4242
});
43+
44+
if (!nconf.get('quiet') && nconf.get('verbose') > 2) {
45+
console.error('LOADING CONFIG', file);
46+
}
4347
}
4448

4549
get(key, defaultValue) {
4650
// Search through all known stores for the value
4751
const value = nconf.get(key);
48-
return (value === undefined) ? defaultValue : value;
52+
const result = (value === undefined) ? defaultValue : value;
53+
if (!nconf.get('quiet') && nconf.get('verbose') > 2) {
54+
console.error(`CONFIG VALUE ${key} = ${result} (default = ${defaultValue})`)
55+
}
56+
return result
4957
}
5058

5159
// Don't want an object full of null
5260
cleanObject(object) {
5361
const cleaned = {};
5462
Object.keys(object).forEach(key => {
5563
const value = object[key];
56-
if (value === undefined || value === null) {
64+
if (value === undefined || value === null || (Array.isArray(value) && value.length === 0)) {
5765
return;
5866
} else if (typeof value === "object" && !Array.isArray(value)) {
5967
cleaned[key] = this.cleanObject(value);

lib/loader.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ async function asyncMap(array, callback) {
130130

131131
const loadRulesets = async (loadFiles, options = {}) => {
132132
const { verbose } = options;
133-
const rulesetList = (loadFiles.length > 0 ? loadFiles : ['default']);
134-
const allDependancies = await asyncMap(rulesetList, ruleset => recursivelyLoadRulesets(ruleset, [], { verbose }));
135-
const flatDependencies = [].concat(...allDependancies);
133+
const rulesetList = (loadFiles && loadFiles.length > 0 ? loadFiles : ['default']);
134+
const allDependencies = await asyncMap(rulesetList, ruleset => recursivelyLoadRulesets(ruleset, [], { verbose }));
135+
const flatDependencies = [].concat(...allDependencies);
136136
// Unique copy of the array
137137
return [...(new Set(flatDependencies))];
138138
}
@@ -146,7 +146,7 @@ const resolveContent = (openapi, options) => {
146146
externalRefs: {},
147147
rewriteRefs: true,
148148
openapi: openapi,
149-
verbose: options.verbose === 2,
149+
verbose: options.verbose > 1,
150150
});
151151
}
152152

lint.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ More information: https://speccy.io/rules/1-rulesets#${rule.name}
6868
const command = async (specFile, cmd) => {
6969
config.init(cmd);
7070
const jsonSchema = config.get('jsonSchema');
71-
const verbose = config.get('quiet') ? 0 : config.get('verbose', 1);
71+
const verbose = config.get('quiet') ? 0 : config.get('verbose', 1);
7272
const rulesets = config.get('lint:rules');
7373
const skip = config.get('lint:skip');
7474

@@ -80,7 +80,7 @@ const command = async (specFile, cmd) => {
8080

8181
const spec = await loader.readOrError(
8282
specFile,
83-
buildLoaderOptions(jsonSchema, verbose),
83+
buildLoaderOptions(jsonSchema, verbose)
8484
);
8585

8686
return new Promise((resolve, reject) => {

package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resolve.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ const fs = require('fs');
66
const yaml = require('js-yaml');
77
const config = require('./lib/config.js');
88
const loader = require('./lib/loader.js');
9-
const resolver = require('oas-resolver');
109
const fromJsonSchema = require('json-schema-to-openapi-schema');
1110

1211
const command = async (file, cmd) => {
1312
config.init(cmd);
1413
const jsonSchema = config.get('jsonSchema');
1514
const output = config.get('resolve:output');
16-
const verbose = config.get('quiet') ? 0 : (config.get('verbose') ? 2 : 1);
15+
const verbose = config.get('quiet') ? 0 : config.get('verbose', 1);
1716

18-
const spec = await loader.readOrError(file, buildLoaderOptions(jsonSchema, verbose));
17+
const spec = await loader.readOrError(
18+
file,
19+
buildLoaderOptions(jsonSchema, verbose)
20+
);
1921
const content = yaml.safeDump(spec, { lineWidth: -1 });
2022

2123
return new Promise((resolve, reject) => {
@@ -52,4 +54,4 @@ const buildLoaderOptions = (jsonSchema, verbose) => {
5254
return options;
5355
}
5456

55-
module.exports = { command }
57+
module.exports = { command };

serve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const launchServer = (app, port, specFile, { verbose }) => {
3737
const command = async (specFile, cmd) => {
3838
config.init(cmd);
3939
const jsonSchema = config.get('jsonSchema');
40-
const verbose = config.get('quiet') ? 0 : (config.get('verbose') ? 2 : 1);
40+
const verbose = config.get('quiet') ? 0 : config.get('verbose', 1);
4141
const port = config.get('serve:port', DEFAULT_PORT);
4242

4343
const app = express();

speccy.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ program
3232
.option('-r, --rules [ruleFile]', 'provide multiple rules files', collect, [])
3333
.option('-s, --skip [ruleName]', 'provide multiple rules to skip', collect, [])
3434
.option('-j, --json-schema', 'treat $ref like JSON Schema and convert to OpenAPI Schema Objects (default: false)')
35-
.option('-v, --verbose', 'increase verbosity', increaseVerbosity, 0)
35+
.option('-v, --verbose', 'increase verbosity', increaseVerbosity, 1)
3636
.action((specFile, cmd) => {
3737
lint.command(specFile, cmd)
3838
.then(() => { process.exit(0) })
39-
.catch(() => { process.exit(1) });
39+
.catch((err) => {
40+
if (err) {
41+
console.error(err.message);
42+
}
43+
process.exit(1);
44+
});
4045
});
4146

4247
program
@@ -45,11 +50,16 @@ program
4550
.option('-o, --output <file>', 'file to output to')
4651
.option('-q, --quiet', 'reduce verbosity')
4752
.option('-j, --json-schema', 'treat $ref like JSON Schema and convert to OpenAPI Schema Objects (default: false)')
48-
.option('-v, --verbose', 'increase verbosity', increaseVerbosity, 0)
49-
.action((file, cmd) => {
50-
resolve.command(file, cmd)
53+
.option('-v, --verbose', 'increase verbosity', increaseVerbosity,1)
54+
.action((specFile, cmd) => {
55+
resolve.command(specFile, cmd)
5156
.then(() => { process.exit(0) })
52-
.catch(() => { process.exit(1) });
57+
.catch((err) => {
58+
if (err) {
59+
console.error(err.message);
60+
}
61+
process.exit(1);
62+
});
5363
});
5464

5565
program
@@ -58,9 +68,17 @@ program
5868
.option('-p, --port [value]', 'port on which the server will listen (default: 5000)')
5969
.option('-q, --quiet', 'reduce verbosity')
6070
.option('-j, --json-schema', 'treat $ref like JSON Schema and convert to OpenAPI Schema Objects (default: false)')
61-
.option('-v, --verbose', 'increase verbosity', increaseVerbosity, 0)
71+
.option('-v, --verbose', 'increase verbosity', increaseVerbosity,1)
6272
// TODO .option('-w, --watch', 'reloading browser on spec file changes')
63-
.action(serve.command);
73+
.action((specFile, cmd) => {
74+
serve.command(specFile, cmd)
75+
.catch((err) => {
76+
if (err) {
77+
console.error(err.message);
78+
}
79+
process.exit(1);
80+
});
81+
});
6482

6583
program.parse(process.argv);
6684

test/fixtures/config/valid.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"jsonSchema": true,
33
"quiet": true,
4-
"verbose": true,
4+
"verbose": 2,
55
"lint": {
66
"rules": [
77
"strict",
@@ -18,4 +18,4 @@
1818
"serve": {
1919
"port": 8001
2020
}
21-
}
21+
}

0 commit comments

Comments
 (0)