From 4d8f3d6da82404ec083c3f54d677c7501a5a5412 Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Wed, 9 Aug 2023 12:49:02 -0600 Subject: [PATCH] fix: Add `$schema` field to the cspell-tools config (#4708) --- packages/cspell-tools/cspell-tools.config.schema.json | 5 +++++ packages/cspell-tools/package.json | 2 -- packages/cspell-tools/src/compile.test.ts | 3 +++ packages/cspell-tools/src/compile.ts | 11 ++++++----- packages/cspell-tools/src/config/config.ts | 9 +++++++++ pnpm-lock.yaml | 8 +------- 6 files changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/cspell-tools/cspell-tools.config.schema.json b/packages/cspell-tools/cspell-tools.config.schema.json index cab706be74a8..93f3d70a6f63 100644 --- a/packages/cspell-tools/cspell-tools.config.schema.json +++ b/packages/cspell-tools/cspell-tools.config.schema.json @@ -195,6 +195,11 @@ } }, "properties": { + "$schema": { + "default": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-tools/cspell-tools.config.schema.json", + "description": "Url to JSON Schema", + "type": "string" + }, "allowedSplitWords": { "anyOf": [ { diff --git a/packages/cspell-tools/package.json b/packages/cspell-tools/package.json index e1c3d2b6a613..12ff43e9a908 100644 --- a/packages/cspell-tools/package.json +++ b/packages/cspell-tools/package.json @@ -63,9 +63,7 @@ }, "devDependencies": { "@types/glob": "^8.1.0", - "@types/jest": "^29.5.3", "@types/shelljs": "^0.8.12", - "jest": "^29.6.2", "lorem-ipsum": "^2.0.8", "shelljs": "^0.8.5", "ts-json-schema-generator": "^1.2.0" diff --git a/packages/cspell-tools/src/compile.test.ts b/packages/cspell-tools/src/compile.test.ts index babcd0c33050..400d3366f981 100644 --- a/packages/cspell-tools/src/compile.test.ts +++ b/packages/cspell-tools/src/compile.test.ts @@ -3,6 +3,7 @@ import { writeFile } from 'node:fs/promises'; import { describe, expect, test, vi } from 'vitest'; import { configFileHeader, processCompileAction } from './compile.js'; +import { configFileSchemaURL } from './config/config.js'; vi.mock('node:fs/promises', () => ({ writeFile: vi.fn().mockImplementation(() => Promise.resolve(undefined)), @@ -25,6 +26,7 @@ describe('compile', () => { const expected = configFileHeader + `\ +$schema: ${configFileSchemaURL} targets: - name: public-licenses targetDirectory: . @@ -53,6 +55,7 @@ targets: const expected = configFileHeader + `\ +$schema: ${configFileSchemaURL} targets: - name: nl-nl targetDirectory: . diff --git a/packages/cspell-tools/src/compile.ts b/packages/cspell-tools/src/compile.ts index 57c0c33dfa41..8b175730c1c4 100644 --- a/packages/cspell-tools/src/compile.ts +++ b/packages/cspell-tools/src/compile.ts @@ -6,7 +6,7 @@ import YAML from 'yaml'; import type { CompileCommonAppOptions } from './AppOptions.js'; import { compile } from './compiler/compile.js'; import { createCompileRequest } from './compiler/createCompileRequest.js'; -import type { CompileRequest } from './config/config.js'; +import { configFileSchemaURL, type RunConfig } from './config/config.js'; import type { FeatureFlags } from './FeatureFlags/index.js'; import { getSystemFeatureFlags, parseFlags } from './FeatureFlags/index.js'; import { globP } from './util/globP.js'; @@ -15,8 +15,7 @@ getSystemFeatureFlags().register('compound', 'Enable compound dictionary sources const defaultConfigFile = 'cspell-tools.config.yaml'; -export const configFileHeader = - '# yaml-language-server: $schema=https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-tools/cspell-tools.config.schema.json\n\n'; +export const configFileHeader = `# yaml-language-server: $schema=${configFileSchemaURL}\n\n`; export async function processCompileAction( src: string[], @@ -52,8 +51,10 @@ async function useCompile(src: string[], options: CompileCommonAppOptions): Prom return options.init ? initConfig(request) : compile(request); } -async function initConfig(request: CompileRequest): Promise { - const content = configFileHeader + YAML.stringify(request, null, 2); +async function initConfig(runConfig: RunConfig): Promise { + const { $schema = configFileSchemaURL, ...cfg } = runConfig; + const config = { $schema, ...cfg }; + const content = configFileHeader + YAML.stringify(config, null, 2); console.log('Writing config file: %s', defaultConfigFile); await writeFile(defaultConfigFile, content); diff --git a/packages/cspell-tools/src/config/config.ts b/packages/cspell-tools/src/config/config.ts index ede99ccfacad..566f146a5e27 100644 --- a/packages/cspell-tools/src/config/config.ts +++ b/packages/cspell-tools/src/config/config.ts @@ -1,4 +1,10 @@ export interface RunConfig extends Partial { + /** + * Url to JSON Schema + * @default "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-tools/cspell-tools.config.schema.json" + */ + $schema?: string; + /** * Optional Target Dictionaries to create. */ @@ -144,3 +150,6 @@ export interface CompileSourceOptions { allowedSplitWords?: FilePath | FilePath[] | undefined; } + +export const configFileSchemaURL = + 'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-tools/cspell-tools.config.schema.json'; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b57398f6e8c8..6898793b1155 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,4 +1,4 @@ -lockfileVersion: '6.1' +lockfileVersion: '6.0' settings: autoInstallPeers: true @@ -715,15 +715,9 @@ importers: '@types/glob': specifier: ^8.1.0 version: 8.1.0 - '@types/jest': - specifier: ^29.5.3 - version: 29.5.3 '@types/shelljs': specifier: ^0.8.12 version: 0.8.12 - jest: - specifier: ^29.6.2 - version: 29.6.2(@types/node@18.17.3) lorem-ipsum: specifier: ^2.0.8 version: 2.0.8