Skip to content

Commit

Permalink
fix: Add $schema field to the cspell-tools config (#4708)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Aug 9, 2023
1 parent a14dbe3 commit 4d8f3d6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
5 changes: 5 additions & 0 deletions packages/cspell-tools/cspell-tools.config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
2 changes: 0 additions & 2 deletions packages/cspell-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions packages/cspell-tools/src/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -25,6 +26,7 @@ describe('compile', () => {
const expected =
configFileHeader +
`\
$schema: ${configFileSchemaURL}
targets:
- name: public-licenses
targetDirectory: .
Expand Down Expand Up @@ -53,6 +55,7 @@ targets:
const expected =
configFileHeader +
`\
$schema: ${configFileSchemaURL}
targets:
- name: nl-nl
targetDirectory: .
Expand Down
11 changes: 6 additions & 5 deletions packages/cspell-tools/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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[],
Expand Down Expand Up @@ -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<void> {
const content = configFileHeader + YAML.stringify(request, null, 2);
async function initConfig(runConfig: RunConfig): Promise<void> {
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);

Expand Down
9 changes: 9 additions & 0 deletions packages/cspell-tools/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export interface RunConfig extends Partial<CompileRequest> {
/**
* 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.
*/
Expand Down Expand Up @@ -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';
8 changes: 1 addition & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d8f3d6

Please sign in to comment.