Skip to content

Commit 47db9b5

Browse files
authored
fix(deps): replace jsonpath with jsonpath-plus (#2391)
1 parent bb5e69b commit 47db9b5

File tree

8 files changed

+131
-256
lines changed

8 files changed

+131
-256
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"http-proxy-agent": "^7.0.0",
8787
"https-proxy-agent": "^7.0.0",
8888
"js-yaml": "^4.0.0",
89-
"jsonpath": "^1.1.1",
89+
"jsonpath-plus": "^9.0.0",
9090
"node-html-parser": "^6.0.0",
9191
"parse-github-repo-url": "^1.4.1",
9292
"semver": "^7.5.3",

src/updaters/generic-json.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import {Updater} from '../update';
1616
import {Version} from '../version';
17-
import * as jp from 'jsonpath';
17+
import {JSONPath} from 'jsonpath-plus';
1818
import {jsonStringify} from '../util/json-stringify';
1919
import {logger as defaultLogger, Logger} from '../util/logger';
2020

@@ -36,21 +36,25 @@ export class GenericJson implements Updater {
3636
*/
3737
updateContent(content: string, logger: Logger = defaultLogger): string {
3838
const data = JSON.parse(content);
39-
const nodes = jp.apply(data, this.jsonpath, value => {
40-
if (typeof value !== 'string') {
41-
logger.warn(`No string in ${this.jsonpath}. Skipping.`);
42-
return value;
43-
}
44-
if (!value.match(VERSION_REGEX)) {
45-
logger.warn(`No version found in ${this.jsonpath}. Skipping.`);
46-
return value;
47-
}
48-
return value.replace(VERSION_REGEX, this.version.toString());
39+
JSONPath({
40+
resultType: 'all',
41+
path: this.jsonpath,
42+
json: data,
43+
callback: (payload, _payloadType, _fullPayload) => {
44+
if (typeof payload.value !== 'string') {
45+
logger.warn(`No string in ${this.jsonpath}. Skipping.`);
46+
return payload;
47+
}
48+
if (!payload.value.match(VERSION_REGEX)) {
49+
logger.warn(`No version found in ${this.jsonpath}. Skipping.`);
50+
return payload;
51+
}
52+
payload.parent[payload.parentProperty] = payload.parent[
53+
payload.parentProperty
54+
].replace(VERSION_REGEX, this.version.toString());
55+
return payload;
56+
},
4957
});
50-
if (!nodes) {
51-
logger.warn(`No entries modified in ${this.jsonpath}`);
52-
return content;
53-
}
5458
return jsonStringify(data, content);
5559
}
5660
}

src/updaters/generic-toml.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import {Updater} from '../update';
1616
import {Version} from '../version';
17-
import * as jp from 'jsonpath';
17+
import {JSONPath} from 'jsonpath-plus';
1818
import {parseWith, replaceTomlValue} from '../util/toml-edit';
1919
import * as toml from '@iarna/toml';
2020
import {logger as defaultLogger, Logger} from '../util/logger';
@@ -48,7 +48,12 @@ export class GenericToml implements Updater {
4848
return content;
4949
}
5050

51-
const paths = jp.paths(data, this.jsonpath);
51+
const pointers: string[] = JSONPath({
52+
path: this.jsonpath,
53+
json: data,
54+
resultType: 'pointer',
55+
});
56+
const paths = pointers.map(pointer => pointer.split('/').filter(Boolean));
5257
if (!paths || paths.length === 0) {
5358
logger.warn(`No entries modified in ${this.jsonpath}`);
5459
return content;

src/updaters/generic-yaml.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import {Updater} from '../update';
1616
import {Version} from '../version';
17-
import * as jp from 'jsonpath';
17+
import {JSONPath} from 'jsonpath-plus';
1818
import * as yaml from 'js-yaml';
1919
import {logger as defaultLogger, Logger} from '../util/logger';
2020

@@ -55,12 +55,21 @@ export class GenericYaml implements Updater {
5555
// Update each document
5656
let modified = false;
5757
docs.forEach(data => {
58-
const nodes = jp.apply(data, this.jsonpath, _val => {
59-
return this.version.toString();
58+
JSONPath({
59+
resultType: 'all',
60+
path: this.jsonpath,
61+
json: data as any,
62+
callback: (payload, _payloadType, _fullPayload) => {
63+
if (typeof payload.value !== 'string') {
64+
logger.warn(`No string in ${this.jsonpath}. Skipping.`);
65+
return payload;
66+
}
67+
68+
modified = true;
69+
payload.parent[payload.parentProperty] = this.version.toString();
70+
return payload;
71+
},
6072
});
61-
if (nodes && nodes.length) {
62-
modified = true;
63-
}
6473
});
6574

6675
// If nothing was modified, return original content

test/updaters/generic-json.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as snapshot from 'snap-shot-it';
1818
import {describe, it} from 'mocha';
1919
import {Version} from '../../src/version';
2020
import {GenericJson} from '../../src/updaters/generic-json';
21-
import {expect, assert} from 'chai';
21+
import {expect} from 'chai';
2222

2323
const fixturesPath = './test/updaters/fixtures';
2424

@@ -81,15 +81,5 @@ describe('GenericJson', () => {
8181
const newContent = updater.updateContent(oldContent);
8282
expect(newContent).to.eql(oldContent);
8383
});
84-
it('warns on invalid jsonpath', async () => {
85-
const oldContent = readFileSync(
86-
resolve(fixturesPath, './esy.json'),
87-
'utf8'
88-
).replace(/\r\n/g, '\n');
89-
const updater = new GenericJson('bad jsonpath', Version.parse('v2.3.4'));
90-
assert.throws(() => {
91-
updater.updateContent(oldContent);
92-
});
93-
});
9484
});
9585
});

test/updaters/generic-toml.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {resolve} from 'path';
1717
import * as snapshot from 'snap-shot-it';
1818
import {describe, it} from 'mocha';
1919
import {Version} from '../../src/version';
20-
import {expect, assert} from 'chai';
20+
import {expect} from 'chai';
2121
import {GenericToml} from '../../src/updaters/generic-toml';
2222

2323
const fixturesPath = './test/updaters/fixtures';
@@ -57,16 +57,6 @@ describe('GenericToml', () => {
5757
const newContent = updater.updateContent(oldContent);
5858
expect(newContent).to.eql(oldContent);
5959
});
60-
it('warns on invalid jsonpath', async () => {
61-
const oldContent = readFileSync(
62-
resolve(fixturesPath, './Cargo.toml'),
63-
'utf8'
64-
).replace(/\r\n/g, '\n');
65-
const updater = new GenericToml('bad jsonpath', Version.parse('v2.3.4'));
66-
assert.throws(() => {
67-
updater.updateContent(oldContent);
68-
});
69-
});
7060
it('ignores invalid file', async () => {
7161
const oldContent = readFileSync(
7262
resolve(fixturesPath, './toml/invalid.txt'),

test/updaters/generic-yaml.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {resolve} from 'path';
1717
import * as snapshot from 'snap-shot-it';
1818
import {describe, it} from 'mocha';
1919
import {Version} from '../../src/version';
20-
import {expect, assert} from 'chai';
20+
import {expect} from 'chai';
2121
import {GenericYaml} from '../../src/updaters/generic-yaml';
2222

2323
const fixturesPath = './test/updaters/fixtures';
@@ -66,16 +66,6 @@ describe('GenericYaml', () => {
6666
const newContent = updater.updateContent(oldContent);
6767
expect(newContent).to.eql(oldContent);
6868
});
69-
it('warns on invalid jsonpath', async () => {
70-
const oldContent = readFileSync(
71-
resolve(fixturesPath, './helm/Chart.yaml'),
72-
'utf8'
73-
).replace(/\r\n/g, '\n');
74-
const updater = new GenericYaml('bad jsonpath', Version.parse('v2.3.4'));
75-
assert.throws(() => {
76-
updater.updateContent(oldContent);
77-
});
78-
});
7969
it('ignores invalid file', async () => {
8070
const oldContent = readFileSync(
8171
resolve(fixturesPath, './yaml/invalid.txt'),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy