Skip to content

Commit

Permalink
CLI to import style json into basemaps config repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentao-Kuang committed Oct 30, 2023
1 parent bd8f183 commit f0699c1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
61 changes: 61 additions & 0 deletions packages/cli/src/cli/config/action.import.style.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<StyleJson>(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<StyleJson>(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);
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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());
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit f0699c1

Please sign in to comment.