Skip to content

Commit 76a77ae

Browse files
Wentao-Kuangblacha
andauthored
fix(cli): Fix config json format by adding the missing newline and remove double quotes from commit message. (#2795)
Co-authored-by: Blayne Chard <bchard@linz.govt.nz>
1 parent c33dda4 commit 76a77ae

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

packages/cli/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@
6060
"get-port": "^6.1.2",
6161
"node-fetch": "^3.2.3",
6262
"p-limit": "^4.0.0",
63+
"prettier": "^2.8.8",
6364
"pretty-json-log": "^1.0.0",
6465
"slugify": "^1.6.5",
6566
"zod": "^3.17.3"
6667
},
6768
"devDependencies": {
6869
"@types/deep-diff": "^1.0.1",
70+
"@types/prettier": "^2.7.2",
6971
"@types/proj4": "^2.5.2",
7072
"@types/sharp": "^0.31.0"
7173
},

packages/cli/src/cli/github/github.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ export class Github {
6161
*/
6262
commit(message: string): void {
6363
this.logger.info({ repository: this.repo }, 'GitHub: Commit all');
64-
execFileSync('git', ['commit', '-am', JSON.stringify(message)], { cwd: this.repoName })
65-
.toString()
66-
.trim();
64+
execFileSync('git', ['commit', '-am', message], { cwd: this.repoName }).toString().trim();
6765
}
6866

6967
/**

packages/cli/src/cli/github/make.cog.pr.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { ConfigLayer, ConfigTileSetRaster, ConfigTileSetVector } from '@basemaps/config';
1+
import { ConfigLayer, ConfigTileSet, ConfigTileSetRaster, ConfigTileSetVector } from '@basemaps/config';
22
import { LogType, fsa } from '@basemaps/shared';
33
import { Category, DefaultCategorySetting } from '../cogify/action.make.cog.pr.js';
44
import { Github } from './github.js';
5+
import prettier from 'prettier';
56

67
export class MakeCogGithub extends Github {
78
imagery: string;
@@ -10,6 +11,18 @@ export class MakeCogGithub extends Github {
1011
this.imagery = imagery;
1112
}
1213

14+
async formatConfigFile(targetPath: string, tileSet: ConfigTileSet): Promise<string> {
15+
const cfg = await prettier.resolveConfigFile(targetPath);
16+
if (cfg == null) {
17+
this.logger.error('Prettier:MissingConfig');
18+
return JSON.stringify(tileSet, null, 2);
19+
}
20+
const options = await prettier.resolveConfig(cfg);
21+
this.logger.info({ configPath: cfg, prettierOptions: options }, 'Prettier:Config');
22+
const formatted = prettier.format(JSON.stringify(tileSet), { ...options, parser: 'json' });
23+
return formatted;
24+
}
25+
1326
/**
1427
* Prepare and create pull request for the aerial tileset config
1528
*/
@@ -23,13 +36,13 @@ export class MakeCogGithub extends Github {
2336

2437
// Prepare new aerial tileset config
2538
this.logger.info({ imagery: this.imagery }, 'GitHub: Get the master TileSet config file');
26-
const path = `${this.repoName}/config/tileset/${filename}.json`;
27-
const tileSet = await fsa.readJson<ConfigTileSetRaster>(path);
39+
const tileSetPath = fsa.joinAll(this.repoName, 'config', 'tileset', `${filename}.json`);
40+
const tileSet = await fsa.readJson<ConfigTileSetRaster>(tileSetPath);
2841
const newTileSet = await this.prepareRasterTileSetConfig(layer, tileSet, category);
2942

3043
// skip pull request if not an urban or rural imagery
3144
if (newTileSet == null) return;
32-
await fsa.write(path, JSON.stringify(newTileSet, null, 2));
45+
await fsa.write(tileSetPath, await this.formatConfigFile(tileSetPath, newTileSet));
3346

3447
// Commit and push the changes
3548
const message = `config(raster): Add imagery ${this.imagery} to ${filename} config file.`;
@@ -118,13 +131,13 @@ export class MakeCogGithub extends Github {
118131

119132
// Prepare new aerial tileset config
120133
this.logger.info({ imagery: this.imagery }, 'GitHub: Get the master TileSet config file');
121-
const path = `${this.repoName}/config/tileset/${filename}.json`;
122-
const tileSet = await fsa.readJson<ConfigTileSetVector>(path);
134+
const tileSetPath = fsa.joinAll(this.repoName, 'config', 'tileset', `${filename}.json`);
135+
const tileSet = await fsa.readJson<ConfigTileSetVector>(tileSetPath);
123136
const newTileSet = await this.prepareVectorTileSetConfig(layer, tileSet);
124137

125138
// skip pull request if not an urban or rural imagery
126139
if (newTileSet == null) return;
127-
await fsa.write(path, JSON.stringify(newTileSet, null, 2));
140+
await fsa.write(tileSetPath, await this.formatConfigFile(tileSetPath, newTileSet));
128141

129142
// Commit and push the changes
130143
const message = `config(vector): Update the ${this.imagery} to ${filename} config file.`;

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,11 @@
19591959
dependencies:
19601960
"@types/node" "*"
19611961

1962+
"@types/prettier@^2.7.2":
1963+
version "2.7.2"
1964+
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.7.2.tgz#6c2324641cc4ba050a8c710b2b251b377581fbf0"
1965+
integrity sha512-KufADq8uQqo1pYKVIYzfKbJfBAc0sOeXqGbFaSpv8MRmC/zXgowNZmFcbngndGk922QDmOASEXUZCaY48gs4cg==
1966+
19621967
"@types/proj4@^2.5.2":
19631968
version "2.5.2"
19641969
resolved "https://registry.yarnpkg.com/@types/proj4/-/proj4-2.5.2.tgz#e3afa4e09e5cf08d8bc74e1b3de3b2111324ee33"
@@ -6751,6 +6756,11 @@ prettier@^2.8.2:
67516756
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.3.tgz#ab697b1d3dd46fb4626fbe2f543afe0cc98d8632"
67526757
integrity sha512-tJ/oJ4amDihPoufT5sM0Z1SKEuKay8LfVAMlbbhnnkvt6BUserZylqo2PN+p9KeljLr0OHa2rXHU1T8reeoTrw==
67536758

6759+
prettier@^2.8.8:
6760+
version "2.8.8"
6761+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da"
6762+
integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==
6763+
67546764
pretty-json-log@^1.0.0:
67556765
version "1.0.0"
67566766
resolved "https://registry.yarnpkg.com/pretty-json-log/-/pretty-json-log-1.0.0.tgz#8417783f93a114418dbccdb1302a6a2c0ed38654"

0 commit comments

Comments
 (0)