From f0699c178ac4e28b835692472c347fdc5776beac Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Tue, 31 Oct 2023 09:44:36 +1300 Subject: [PATCH] CLI to import style json into basemaps config repo. --- packages/cli/package.json | 2 + .../cli/src/cli/config/action.import.style.ts | 61 +++++++++++++++++++ packages/cli/src/cli/index.ts | 2 + yarn.lock | 12 ++++ 4 files changed, 77 insertions(+) create mode 100644 packages/cli/src/cli/config/action.import.style.ts diff --git a/packages/cli/package.json b/packages/cli/package.json index 338dd5d40..79e8c50b5 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -57,12 +57,14 @@ "flatgeobuf": "^3.23.1", "node-fetch": "^3.2.3", "p-limit": "^4.0.0", + "prettier": "^3.0.3", "pretty-json-log": "^1.0.0", "slugify": "^1.6.5", "zod": "^3.17.3" }, "devDependencies": { "@types/deep-diff": "^1.0.1", + "@types/prettier": "^3.0.0", "@types/sharp": "^0.31.0" }, "publishConfig": { diff --git a/packages/cli/src/cli/config/action.import.style.ts b/packages/cli/src/cli/config/action.import.style.ts new file mode 100644 index 000000000..fdcadfe6c --- /dev/null +++ b/packages/cli/src/cli/config/action.import.style.ts @@ -0,0 +1,61 @@ +import { CommandLineAction, CommandLineFlagParameter, CommandLineStringParameter } from '@rushstack/ts-command-line'; +import { fsa } from '@chunkd/fs'; +import { LogConfig } from '@basemaps/shared'; +import { StyleJson } from '@basemaps/config'; +import prettier from 'prettier'; + +export class CommandImportStyle extends CommandLineAction { + private style: CommandLineStringParameter; + private target: CommandLineStringParameter; + private commit: CommandLineFlagParameter; + + public constructor() { + super({ + actionName: 'import-style', + summary: 'import a style json into target json file', + documentation: 'Given a valid style json url/json and import into target json file.', + }); + } + + protected onDefineParameters(): void { + this.style = this.defineStringParameter({ + argumentName: 'STYLE', + parameterLongName: '--style', + description: 'Path of style json file or url of style json', + required: true, + }); + this.target = this.defineStringParameter({ + argumentName: 'TARGET', + parameterLongName: '--target', + description: 'target style json to update', + required: true, + }); + this.commit = this.defineFlagParameter({ + parameterLongName: '--commit', + description: 'Actually start the import', + required: false, + }); + } + + async onExecute(): Promise { + const logger = LogConfig.get(); + const commit = this.commit.value ?? false; + const style = this.style.value; + const target = this.target.value; + if (style == null || target == null) throw new Error('Please provide a valid style json and target'); + + const json = await fsa.readJson(style); + if (json.version !== 8) return logger.error('Style:Invalid - Invalid version'); + if (!Array.isArray(json.layers)) return logger.error('Style:Invalid - Missing layers'); + + const targetStyle = await fsa.readJson(target); + targetStyle.layers = json.layers; + const after = JSON.stringify(targetStyle); + + const cfg = await prettier.resolveConfigFile(); + if (cfg == null) return logger.error('Prettier:MissingConfig'); + const options = await prettier.resolveConfig(cfg); + const formatted = await prettier.format(after, { ...options, printWidth: 200, parser: 'json' }); + if (commit) await fsa.write(target, formatted); + } +} diff --git a/packages/cli/src/cli/index.ts b/packages/cli/src/cli/index.ts index 72def670f..f9f12607e 100644 --- a/packages/cli/src/cli/index.ts +++ b/packages/cli/src/cli/index.ts @@ -8,6 +8,7 @@ import { CommandCogMapSheet } from './config/action.cog.mapsheet.js'; import { CommandImageryConfig } from './config/action.imagery.config.js'; import { CommandImport } from './config/action.import.js'; import { CommandServe } from './server/action.serve.js'; +import { CommandImportStyle } from './config/action.import.style.js'; export class BasemapsConfigCommandLine extends BaseCommandLine { constructor() { @@ -21,6 +22,7 @@ export class BasemapsConfigCommandLine extends BaseCommandLine { this.addAction(new CommandBundleAssets()); this.addAction(new CommandImport()); this.addAction(new CommandImageryConfig()); + this.addAction(new CommandImportStyle()); // Argo this.addAction(new CommandCogMapSheet()); diff --git a/yarn.lock b/yarn.lock index ee229d95f..5ffa9524a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1888,6 +1888,13 @@ dependencies: "@types/node" "*" +"@types/prettier@^3.0.0": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-3.0.0.tgz#e9bc8160230d3a461dab5c5b41cceef1ef723057" + integrity sha512-mFMBfMOz8QxhYVbuINtswBp9VL2b4Y0QqYHwqLz3YbgtfAcat2Dl6Y1o4e22S/OVE6Ebl9m7wWiMT2lSbAs1wA== + dependencies: + prettier "*" + "@types/proj4@^2.5.2": version "2.5.2" resolved "https://registry.yarnpkg.com/@types/proj4/-/proj4-2.5.2.tgz#e3afa4e09e5cf08d8bc74e1b3de3b2111324ee33" @@ -6670,6 +6677,11 @@ prettier-linter-helpers@^1.0.0: dependencies: fast-diff "^1.1.2" +prettier@*, prettier@^3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.0.3.tgz#432a51f7ba422d1469096c0fdc28e235db8f9643" + integrity sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg== + prettier@^2.8.2: version "2.8.3" resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.3.tgz#ab697b1d3dd46fb4626fbe2f543afe0cc98d8632"