Skip to content

Commit

Permalink
feat(cli): Create pull request cli to format the config files by npx …
Browse files Browse the repository at this point in the history
…prettier. BM-810 (#2898)

### Why?
When we make changes to config file in the basemaps-config repo, we need
to format it before commit. Current way of using prettier is broken
because we need to install @linzjs/style into the cli docker container.
A better solution is just run prettier in the config directory before
commit the changes.

#### Issue Link:
[BM-810](https://toitutewhenua.atlassian.net/browse/BM-810)

### Description of Changes:
- Remove all the prettier stuff from cli
- Run npx prettier in the chirld process in config repo.

#### Author Checklist:
- [ ] Tests updated
- [ ] Docs updated
- [ ] Issue linked in PR title

[BM-810]:
https://toitutewhenua.atlassian.net/browse/BM-810?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
Wentao-Kuang authored Aug 21, 2023
1 parent e70280a commit 061b605
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 34 deletions.
1 change: 0 additions & 1 deletion .github/workflows/containers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ jobs:
# Files are packed into the base directory
cp *.tgz packages/server/
cp *.tgz packages/cli/
cp node_modules/@linzjs/style/.prettierrc.js packages/cli/
- name: Log in to registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin
Expand Down
1 change: 0 additions & 1 deletion packages/cli/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ RUN npm install [email protected]
# Install the landing assets
COPY ./basemaps-landing*.tgz /app/
COPY ./basemaps-cogify*.tgz /app/
COPY ./.prettierrc.js /app/node_modules/@linzjs/style/.prettierrc.js

RUN npm install ./basemaps-landing*.tgz ./basemaps-cogify*.tgz

Expand Down
2 changes: 0 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@
"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"
},
Expand Down
44 changes: 24 additions & 20 deletions packages/cli/src/cli/github/make.cog.pr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import {
ConfigId,
ConfigLayer,
ConfigPrefix,
ConfigTileSet,
ConfigTileSetRaster,
ConfigTileSetVector,
TileSetType,
} 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';
import { TileSetConfigSchema } from '@basemaps/config/build/json/parse.tile.set.js';
import { execFileSync } from 'child_process';

export class MakeCogGithub extends Github {
imagery: string;
Expand All @@ -20,16 +19,12 @@ export class MakeCogGithub extends Github {
this.imagery = imagery;
}

async formatConfigFile(targetPath: string, tileSet: ConfigTileSet | TileSetConfigSchema): Promise<string> {
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;
/**
* Format the config files by prettier
*/
formatConfigFile(path = './config/'): void {
this.logger.info({ repository: this.repo }, 'GitHub: Prettier');
execFileSync('npx', ['prettier', '-w', path], { cwd: this.repoName });
}

/**
Expand Down Expand Up @@ -57,16 +52,22 @@ export class MakeCogGithub extends Github {
title: layer.title,
layers: [layer],
};
const tileSetPath = fsa.joinAll(this.repoName, 'config', 'tileset', 'individual', `${layer.name}.json`);
await fsa.write(tileSetPath, await this.formatConfigFile(tileSetPath, tileSet));
const tileSetPath = fsa.joinAll('config', 'tileset', 'individual', `${layer.name}.json`);
const fullPath = fsa.join(this.repoName, tileSetPath);
await fsa.write(fullPath, JSON.stringify(tileSet));
// Format the config file by prettier
this.formatConfigFile(tileSetPath);
} else {
// Prepare new aerial tileset config
const tileSetPath = fsa.joinAll(this.repoName, 'config', 'tileset', `${filename}.json`);
const tileSet = await fsa.readJson<ConfigTileSetRaster>(tileSetPath);
const tileSetPath = fsa.joinAll('config', 'tileset', `${filename}.json`);
const fullPath = fsa.join(this.repoName, tileSetPath);
const tileSet = await fsa.readJson<ConfigTileSetRaster>(fullPath);
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(tileSetPath, await this.formatConfigFile(tileSetPath, newTileSet));
await fsa.write(fullPath, JSON.stringify(newTileSet));
// Format the config file by prettier
this.formatConfigFile(tileSetPath);
}

// Commit and push the changes
Expand Down Expand Up @@ -153,13 +154,16 @@ export class MakeCogGithub extends Github {

// Prepare new aerial tileset config
this.logger.info({ imagery: this.imagery }, 'GitHub: Get the master TileSet config file');
const tileSetPath = fsa.joinAll(this.repoName, 'config', 'tileset', `${filename}.json`);
const tileSet = await fsa.readJson<ConfigTileSetVector>(tileSetPath);
const tileSetPath = fsa.joinAll('config', 'tileset', `${filename}.json`);
const fullPath = fsa.join(this.repoName, tileSetPath);
const tileSet = await fsa.readJson<ConfigTileSetVector>(fullPath);
const newTileSet = await this.prepareVectorTileSetConfig(layer, tileSet);

// skip pull request if not an urban or rural imagery
if (newTileSet == null) return;
await fsa.write(tileSetPath, await this.formatConfigFile(tileSetPath, newTileSet));
await fsa.write(fullPath, JSON.stringify(newTileSet));
// Format the config file by prettier
this.formatConfigFile(tileSetPath);

// Commit and push the changes
const message = `config(vector): Update the ${this.imagery} to ${filename} config file.`;
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1959,11 +1959,6 @@
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"
Expand Down Expand Up @@ -6756,11 +6751,6 @@ 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"
Expand Down

0 comments on commit 061b605

Please sign in to comment.