Skip to content

Commit

Permalink
fix(deps): replace jsonpath with jsonpath-plus (#2391)
Browse files Browse the repository at this point in the history
  • Loading branch information
chingor13 authored Oct 1, 2024
1 parent bb5e69b commit 47db9b5
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 256 deletions.
285 changes: 86 additions & 199 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"http-proxy-agent": "^7.0.0",
"https-proxy-agent": "^7.0.0",
"js-yaml": "^4.0.0",
"jsonpath": "^1.1.1",
"jsonpath-plus": "^9.0.0",
"node-html-parser": "^6.0.0",
"parse-github-repo-url": "^1.4.1",
"semver": "^7.5.3",
Expand Down
34 changes: 19 additions & 15 deletions src/updaters/generic-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import {Updater} from '../update';
import {Version} from '../version';
import * as jp from 'jsonpath';
import {JSONPath} from 'jsonpath-plus';
import {jsonStringify} from '../util/json-stringify';
import {logger as defaultLogger, Logger} from '../util/logger';

Expand All @@ -36,21 +36,25 @@ export class GenericJson implements Updater {
*/
updateContent(content: string, logger: Logger = defaultLogger): string {
const data = JSON.parse(content);
const nodes = jp.apply(data, this.jsonpath, value => {
if (typeof value !== 'string') {
logger.warn(`No string in ${this.jsonpath}. Skipping.`);
return value;
}
if (!value.match(VERSION_REGEX)) {
logger.warn(`No version found in ${this.jsonpath}. Skipping.`);
return value;
}
return value.replace(VERSION_REGEX, this.version.toString());
JSONPath({
resultType: 'all',
path: this.jsonpath,
json: data,
callback: (payload, _payloadType, _fullPayload) => {
if (typeof payload.value !== 'string') {
logger.warn(`No string in ${this.jsonpath}. Skipping.`);
return payload;
}
if (!payload.value.match(VERSION_REGEX)) {
logger.warn(`No version found in ${this.jsonpath}. Skipping.`);
return payload;
}
payload.parent[payload.parentProperty] = payload.parent[
payload.parentProperty
].replace(VERSION_REGEX, this.version.toString());
return payload;
},
});
if (!nodes) {
logger.warn(`No entries modified in ${this.jsonpath}`);
return content;
}
return jsonStringify(data, content);
}
}
9 changes: 7 additions & 2 deletions src/updaters/generic-toml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import {Updater} from '../update';
import {Version} from '../version';
import * as jp from 'jsonpath';
import {JSONPath} from 'jsonpath-plus';
import {parseWith, replaceTomlValue} from '../util/toml-edit';
import * as toml from '@iarna/toml';
import {logger as defaultLogger, Logger} from '../util/logger';
Expand Down Expand Up @@ -48,7 +48,12 @@ export class GenericToml implements Updater {
return content;
}

const paths = jp.paths(data, this.jsonpath);
const pointers: string[] = JSONPath({
path: this.jsonpath,
json: data,
resultType: 'pointer',
});
const paths = pointers.map(pointer => pointer.split('/').filter(Boolean));
if (!paths || paths.length === 0) {
logger.warn(`No entries modified in ${this.jsonpath}`);
return content;
Expand Down
21 changes: 15 additions & 6 deletions src/updaters/generic-yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import {Updater} from '../update';
import {Version} from '../version';
import * as jp from 'jsonpath';
import {JSONPath} from 'jsonpath-plus';
import * as yaml from 'js-yaml';
import {logger as defaultLogger, Logger} from '../util/logger';

Expand Down Expand Up @@ -55,12 +55,21 @@ export class GenericYaml implements Updater {
// Update each document
let modified = false;
docs.forEach(data => {
const nodes = jp.apply(data, this.jsonpath, _val => {
return this.version.toString();
JSONPath({
resultType: 'all',
path: this.jsonpath,
json: data as any,

Check warning on line 61 in src/updaters/generic-yaml.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 61 in src/updaters/generic-yaml.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
callback: (payload, _payloadType, _fullPayload) => {
if (typeof payload.value !== 'string') {
logger.warn(`No string in ${this.jsonpath}. Skipping.`);
return payload;
}

modified = true;
payload.parent[payload.parentProperty] = this.version.toString();
return payload;
},
});
if (nodes && nodes.length) {
modified = true;
}
});

// If nothing was modified, return original content
Expand Down
12 changes: 1 addition & 11 deletions test/updaters/generic-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as snapshot from 'snap-shot-it';
import {describe, it} from 'mocha';
import {Version} from '../../src/version';
import {GenericJson} from '../../src/updaters/generic-json';
import {expect, assert} from 'chai';
import {expect} from 'chai';

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

Expand Down Expand Up @@ -81,15 +81,5 @@ describe('GenericJson', () => {
const newContent = updater.updateContent(oldContent);
expect(newContent).to.eql(oldContent);
});
it('warns on invalid jsonpath', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './esy.json'),
'utf8'
).replace(/\r\n/g, '\n');
const updater = new GenericJson('bad jsonpath', Version.parse('v2.3.4'));
assert.throws(() => {
updater.updateContent(oldContent);
});
});
});
});
12 changes: 1 addition & 11 deletions test/updaters/generic-toml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {resolve} from 'path';
import * as snapshot from 'snap-shot-it';
import {describe, it} from 'mocha';
import {Version} from '../../src/version';
import {expect, assert} from 'chai';
import {expect} from 'chai';
import {GenericToml} from '../../src/updaters/generic-toml';

const fixturesPath = './test/updaters/fixtures';
Expand Down Expand Up @@ -57,16 +57,6 @@ describe('GenericToml', () => {
const newContent = updater.updateContent(oldContent);
expect(newContent).to.eql(oldContent);
});
it('warns on invalid jsonpath', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './Cargo.toml'),
'utf8'
).replace(/\r\n/g, '\n');
const updater = new GenericToml('bad jsonpath', Version.parse('v2.3.4'));
assert.throws(() => {
updater.updateContent(oldContent);
});
});
it('ignores invalid file', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './toml/invalid.txt'),
Expand Down
12 changes: 1 addition & 11 deletions test/updaters/generic-yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {resolve} from 'path';
import * as snapshot from 'snap-shot-it';
import {describe, it} from 'mocha';
import {Version} from '../../src/version';
import {expect, assert} from 'chai';
import {expect} from 'chai';
import {GenericYaml} from '../../src/updaters/generic-yaml';

const fixturesPath = './test/updaters/fixtures';
Expand Down Expand Up @@ -66,16 +66,6 @@ describe('GenericYaml', () => {
const newContent = updater.updateContent(oldContent);
expect(newContent).to.eql(oldContent);
});
it('warns on invalid jsonpath', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './helm/Chart.yaml'),
'utf8'
).replace(/\r\n/g, '\n');
const updater = new GenericYaml('bad jsonpath', Version.parse('v2.3.4'));
assert.throws(() => {
updater.updateContent(oldContent);
});
});
it('ignores invalid file', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './yaml/invalid.txt'),
Expand Down

0 comments on commit 47db9b5

Please sign in to comment.