diff --git a/packages/cli/package.json b/packages/cli/package.json index 640334ef3..00349599c 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -60,12 +60,14 @@ "get-port": "^6.1.2", "node-fetch": "^3.2.3", "p-limit": "^4.0.0", + "prettier": "^2.8.8", "pretty-json-log": "^1.0.0", "slugify": "^1.6.5", "zod": "^3.17.3" }, "devDependencies": { "@types/deep-diff": "^1.0.1", + "@types/prettier": "^2.7.2", "@types/proj4": "^2.5.2", "@types/sharp": "^0.31.0" }, diff --git a/packages/cli/src/cli/github/github.ts b/packages/cli/src/cli/github/github.ts index 8d5102fa4..b0b250d46 100644 --- a/packages/cli/src/cli/github/github.ts +++ b/packages/cli/src/cli/github/github.ts @@ -61,9 +61,7 @@ export class Github { */ commit(message: string): void { this.logger.info({ repository: this.repo }, 'GitHub: Commit all'); - execFileSync('git', ['commit', '-am', JSON.stringify(message)], { cwd: this.repoName }) - .toString() - .trim(); + execFileSync('git', ['commit', '-am', message], { cwd: this.repoName }).toString().trim(); } /** diff --git a/packages/cli/src/cli/github/make.cog.pr.ts b/packages/cli/src/cli/github/make.cog.pr.ts index a9c79debd..d43199c99 100644 --- a/packages/cli/src/cli/github/make.cog.pr.ts +++ b/packages/cli/src/cli/github/make.cog.pr.ts @@ -1,7 +1,8 @@ -import { ConfigLayer, ConfigTileSetRaster, ConfigTileSetVector } from '@basemaps/config'; +import { ConfigLayer, ConfigTileSet, ConfigTileSetRaster, ConfigTileSetVector } from '@basemaps/config'; import { LogType, fsa } from '@basemaps/shared'; import { Category, DefaultCategorySetting } from '../cogify/action.make.cog.pr.js'; import { Github } from './github.js'; +import prettier from 'prettier'; export class MakeCogGithub extends Github { imagery: string; @@ -10,6 +11,18 @@ export class MakeCogGithub extends Github { this.imagery = imagery; } + async formatConfigFile(targetPath: string, tileSet: ConfigTileSet): Promise { + const cfg = await prettier.resolveConfigFile(targetPath); + if (cfg == null) { + this.logger.error('Prettier:MissingConfig'); + return JSON.stringify(tileSet, null, 2); + } + const options = await prettier.resolveConfig(cfg); + this.logger.info({ configPath: cfg, prettierOptions: options }, 'Prettier:Config'); + const formatted = prettier.format(JSON.stringify(tileSet), { ...options, parser: 'json' }); + return formatted; + } + /** * Prepare and create pull request for the aerial tileset config */ @@ -23,13 +36,13 @@ export class MakeCogGithub extends Github { // Prepare new aerial tileset config this.logger.info({ imagery: this.imagery }, 'GitHub: Get the master TileSet config file'); - const path = `${this.repoName}/config/tileset/${filename}.json`; - const tileSet = await fsa.readJson(path); + const tileSetPath = fsa.joinAll(this.repoName, 'config', 'tileset', `${filename}.json`); + const tileSet = await fsa.readJson(tileSetPath); const newTileSet = await this.prepareRasterTileSetConfig(layer, tileSet, category); // skip pull request if not an urban or rural imagery if (newTileSet == null) return; - await fsa.write(path, JSON.stringify(newTileSet, null, 2)); + await fsa.write(tileSetPath, await this.formatConfigFile(tileSetPath, newTileSet)); // Commit and push the changes const message = `config(raster): Add imagery ${this.imagery} to ${filename} config file.`; @@ -118,13 +131,13 @@ export class MakeCogGithub extends Github { // Prepare new aerial tileset config this.logger.info({ imagery: this.imagery }, 'GitHub: Get the master TileSet config file'); - const path = `${this.repoName}/config/tileset/${filename}.json`; - const tileSet = await fsa.readJson(path); + const tileSetPath = fsa.joinAll(this.repoName, 'config', 'tileset', `${filename}.json`); + const tileSet = await fsa.readJson(tileSetPath); const newTileSet = await this.prepareVectorTileSetConfig(layer, tileSet); // skip pull request if not an urban or rural imagery if (newTileSet == null) return; - await fsa.write(path, JSON.stringify(newTileSet, null, 2)); + await fsa.write(tileSetPath, await this.formatConfigFile(tileSetPath, newTileSet)); // Commit and push the changes const message = `config(vector): Update the ${this.imagery} to ${filename} config file.`; diff --git a/yarn.lock b/yarn.lock index 145290e20..878f3e937 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1959,6 +1959,11 @@ dependencies: "@types/node" "*" +"@types/prettier@^2.7.2": + version "2.7.2" + resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.7.2.tgz#6c2324641cc4ba050a8c710b2b251b377581fbf0" + integrity sha512-KufADq8uQqo1pYKVIYzfKbJfBAc0sOeXqGbFaSpv8MRmC/zXgowNZmFcbngndGk922QDmOASEXUZCaY48gs4cg== + "@types/proj4@^2.5.2": version "2.5.2" resolved "https://registry.yarnpkg.com/@types/proj4/-/proj4-2.5.2.tgz#e3afa4e09e5cf08d8bc74e1b3de3b2111324ee33" @@ -6751,6 +6756,11 @@ prettier@^2.8.2: resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.3.tgz#ab697b1d3dd46fb4626fbe2f543afe0cc98d8632" integrity sha512-tJ/oJ4amDihPoufT5sM0Z1SKEuKay8LfVAMlbbhnnkvt6BUserZylqo2PN+p9KeljLr0OHa2rXHU1T8reeoTrw== +prettier@^2.8.8: + version "2.8.8" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da" + integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q== + pretty-json-log@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/pretty-json-log/-/pretty-json-log-1.0.0.tgz#8417783f93a114418dbccdb1302a6a2c0ed38654"