Skip to content

Commit 703878c

Browse files
committed
style: use 'standard' eslint rules to have a consistent code style
This commit adds eslint library with the standard ruleset and applies all rules to the existing codebase. This will help have a unique code style accross the repository.
1 parent e58b66e commit 703878c

File tree

6 files changed

+190
-176
lines changed

6 files changed

+190
-176
lines changed

Diff for: .eslintrc.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es2021": true,
5+
"mocha": true,
6+
"jest": true
7+
},
8+
"extends": "standard",
9+
"parserOptions": {
10+
"ecmaVersion": "latest",
11+
"sourceType": "module"
12+
},
13+
"rules": {
14+
}
15+
}

Diff for: .github/workflows/tests.yml

+1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ jobs:
2222
with:
2323
node-version: ${{ matrix.node-version }}
2424
- run: npm install
25+
- run: npm run lint
2526
- run: npm test

Diff for: index.js

+24-26
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
#!/usr/bin/env node
2-
import arg from 'arg';
3-
import {overlayFiles} from './src/overlay.js'
2+
import arg from 'arg'
3+
import { overlayFiles } from './src/overlay.js'
44

5-
function showHelp() {
6-
console.log("Usage: overlayjs --openapi FILEPATH --overlay FILEPATH");
7-
console.log(" use --help to see this help");
5+
function showHelp () {
6+
console.log('Usage: overlayjs --openapi FILEPATH --overlay FILEPATH')
7+
console.log(' use --help to see this help')
88
}
99

1010
try {
11-
const args = arg({
12-
'--openapi': String,
13-
'--overlay': String,
14-
'--help': String
15-
});
16-
17-
if(args['--overlay'] && args['--openapi']) {
18-
const openapiFile = args['--openapi'];
19-
const overlayFile = args['--overlay'];
20-
var spec = overlayFiles(openapiFile, overlayFile);
21-
console.log(spec);
22-
} else {
23-
showHelp()
24-
}
11+
const args = arg({
12+
'--openapi': String,
13+
'--overlay': String,
14+
'--help': String
15+
})
2516

17+
if (args['--overlay'] && args['--openapi']) {
18+
const openapiFile = args['--openapi']
19+
const overlayFile = args['--overlay']
20+
const spec = overlayFiles(openapiFile, overlayFile)
21+
console.log(spec)
22+
} else {
23+
showHelp()
24+
}
2625
} catch (err) {
27-
if (err.code === 'ARG_UNKNOWN_OPTION') {
28-
console.warn(err.message);
29-
showHelp()
30-
} else {
31-
throw err;
32-
}
26+
if (err.code === 'ARG_UNKNOWN_OPTION') {
27+
console.warn(err.message)
28+
showHelp()
29+
} else {
30+
throw err
31+
}
3332
}
34-

Diff for: package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
"description": "",
55
"main": "index.js",
66
"bin": {
7-
"overlayjs": "./index.js"
7+
"overlayjs": "./index.js"
88
},
99
"scripts": {
10-
"test": "node_modules/.bin/jest"
10+
"test": "node_modules/.bin/jest",
11+
"lint": "eslint . --config .eslintrc.json",
12+
"fmt": "eslint . --config .eslintrc.json --fix"
1113
},
1214
"type": "module",
1315
"author": "",
@@ -23,6 +25,11 @@
2325
"@babel/core": "^7.20.12",
2426
"@babel/preset-env": "^7.20.2",
2527
"babel-jest": "^29.4.1",
28+
"eslint": "^8.57.0",
29+
"eslint-config-standard": "^17.1.0",
30+
"eslint-plugin-import": "^2.29.1",
31+
"eslint-plugin-n": "^16.6.2",
32+
"eslint-plugin-promise": "^6.1.1",
2633
"jest": "^29.4.1"
2734
},
2835
"jest": {

Diff for: src/overlay.js

+62-65
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,76 @@
1-
import fs from 'fs';
2-
import jsonpath from 'jsonpath';
3-
import {safeStringify,parse,parseWithPointers} from "@stoplight/yaml";
4-
import mergician from 'mergician';
1+
import fs from 'fs'
2+
import jsonpath from 'jsonpath'
3+
import { safeStringify, parseWithPointers } from '@stoplight/yaml'
4+
import mergician from 'mergician'
55

6-
function applyOverlayToOpenAPI(spec, overlay) {
7-
// Use jsonpath.apply to do the changes
8-
if (overlay.actions && overlay.actions.length >= 1) overlay.actions.forEach((a)=>{
9-
// Is it a remove?
10-
if (a.hasOwnProperty('remove')) {
11-
while(true) {
12-
var path = jsonpath.paths(spec, a.target)
13-
if (path.length == 0) {
14-
break
15-
}
16-
var parent = jsonpath.parent(spec, a.target)
17-
const thingToRemove = path[0][path[0].length - 1]
18-
if (Array.isArray(parent)) {
19-
parent.splice(thingToRemove, 1);
20-
} else {
21-
delete parent[thingToRemove];
22-
}
23-
}
6+
function applyOverlayToOpenAPI (spec, overlay) {
7+
// Use jsonpath.apply to do the changes
8+
if (overlay.actions && overlay.actions.length >= 1) {
9+
overlay.actions.forEach((a) => {
10+
// Is it a remove?
11+
if (Object.prototype.hasOwnProperty.call(a, 'remove')) {
12+
while (true) {
13+
const path = jsonpath.paths(spec, a.target)
14+
if (path.length === 0) {
15+
break
16+
}
17+
const parent = jsonpath.parent(spec, a.target)
18+
const thingToRemove = path[0][path[0].length - 1]
19+
if (Array.isArray(parent)) {
20+
parent.splice(thingToRemove, 1)
21+
} else {
22+
delete parent[thingToRemove]
23+
}
24+
}
25+
} else {
26+
try {
27+
// It must be an update
28+
jsonpath.apply(spec, a.target, (chunk) => {
29+
// Deep merge using a module (built-in spread operator is only shallow)
30+
const merger = mergician({ appendArrays: true })
31+
return merger(chunk, a.update)
32+
})
33+
} catch (ex) {
34+
process.stderr.write(`Error applying overlay: ${ex.message}\n`)
35+
// return chunk
36+
}
37+
}
38+
})
39+
}
2440

25-
} else {
26-
try {
27-
// It must be an update
28-
jsonpath.apply(spec, a.target, (chunk) => {
29-
// Deep merge using a module (built-in spread operator is only shallow)
30-
const merger = mergician({appendArrays: true})
31-
return merger(chunk, a.update)
32-
});
33-
}
34-
catch (ex) {
35-
process.stderr.write(`Error applying overlay: ${ex.message}\n`)
36-
//return chunk
37-
}
38-
39-
}
40-
})
41-
42-
return spec;
41+
return spec
4342
}
4443

45-
function sortOpenAPIFields(field1, field2) {
46-
const orderedKeys = ["info", "servers", "summary", "operationId", "tags", "paths", "components", "description", "parameters", "responses"];
44+
function sortOpenAPIFields (field1, field2) {
45+
const orderedKeys = ['info', 'servers', 'summary', 'operationId', 'tags', 'paths', 'components', 'description', 'parameters', 'responses']
4746

48-
const index1 = orderedKeys.indexOf(field1);
49-
const index2 = orderedKeys.indexOf(field2);
47+
const index1 = orderedKeys.indexOf(field1)
48+
const index2 = orderedKeys.indexOf(field2)
5049

51-
if (index1 === -1 || index2 === -1) {
52-
return 0;
53-
} else if (index1 > index2) {
54-
return 1;
55-
} else {
56-
return -1;
57-
}
50+
if (index1 === -1 || index2 === -1) {
51+
return 0
52+
} else if (index1 > index2) {
53+
return 1
54+
} else {
55+
return -1
56+
}
5857
}
5958

60-
export function applyOverlay(definition, overlay) {
61-
return applyOverlayToOpenAPI(definition, overlay);
59+
export function applyOverlay (definition, overlay) {
60+
return applyOverlayToOpenAPI(definition, overlay)
6261
}
6362

64-
export function overlayFiles(openapiFile, overlayFile) {
65-
// Parse the "input" OpenAPI document
66-
const specraw = fs.readFileSync(openapiFile, 'utf8');
67-
var spec = parseWithPointers(specraw).data;
63+
export function overlayFiles (openapiFile, overlayFile) {
64+
// Parse the "input" OpenAPI document
65+
const specraw = fs.readFileSync(openapiFile, 'utf8')
66+
let spec = parseWithPointers(specraw).data
6867

69-
// Parse the "overlay" document
70-
const overlayraw = fs.readFileSync(overlayFile, 'utf8');
71-
const overlay = parseWithPointers(overlayraw).data;
68+
// Parse the "overlay" document
69+
const overlayraw = fs.readFileSync(overlayFile, 'utf8')
70+
const overlay = parseWithPointers(overlayraw).data
7271

73-
spec = applyOverlayToOpenAPI(spec, overlay);
72+
spec = applyOverlayToOpenAPI(spec, overlay)
7473

75-
// Return the new spec
76-
return safeStringify(spec, {"sortKeys": sortOpenAPIFields});
74+
// Return the new spec
75+
return safeStringify(spec, { sortKeys: sortOpenAPIFields })
7776
}
78-
79-

0 commit comments

Comments
 (0)