Skip to content

Commit

Permalink
fix: cspell-tools: fix init option output (#4650)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Jul 17, 2023
1 parent 2af9c36 commit da24c95
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 20 deletions.
35 changes: 33 additions & 2 deletions packages/cspell-tools/src/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { writeFile } from 'node:fs/promises';

import { describe, expect, test, vi } from 'vitest';

import { processCompileAction } from './compile.js';
import { configFileHeader, processCompileAction } from './compile.js';

vi.mock('node:fs/promises', () => ({
writeFile: vi.fn().mockImplementation(() => Promise.resolve(undefined)),
Expand All @@ -22,7 +22,9 @@ describe('compile', () => {
listFile: ['source-files.txt'],
init: true,
};
const expected = `\
const expected =
configFileHeader +
`\
targets:
- name: public-licenses
targetDirectory: .
Expand All @@ -31,6 +33,35 @@ targets:
sources:
- listFile: source-files.txt
sort: true
`;
await processCompileAction([], options, undefined);
expect(mockWriteFile).toHaveBeenLastCalledWith('cspell-tools.config.yaml', expected);
});

test('--init option', async () => {
const options = {
compress: false,
trie3: true,
experimental: ['compound'],
sort: true,
merge: 'nl-nl',
output: '.',
listFile: ['src\\source-files.txt'],
init: true,
max_depth: '5',
};
const expected =
configFileHeader +
`\
targets:
- name: nl-nl
targetDirectory: .
compress: false
format: trie3
sources:
- listFile: src/source-files.txt
generateNonStrict: true
maxDepth: 5
`;
await processCompileAction([], options, undefined);
expect(mockWriteFile).toHaveBeenLastCalledWith('cspell-tools.config.yaml', expected);
Expand Down
5 changes: 4 additions & 1 deletion packages/cspell-tools/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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 async function processCompileAction(
src: string[],
options: CompileCommonAppOptions,
Expand Down Expand Up @@ -50,7 +53,7 @@ async function useCompile(src: string[], options: CompileCommonAppOptions): Prom
}

async function initConfig(request: CompileRequest): Promise<void> {
const content = YAML.stringify(request, null, 2);
const content = configFileHeader + YAML.stringify(request, null, 2);
console.log('Writing config file: %s', defaultConfigFile);
await writeFile(defaultConfigFile, content);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,55 @@ exports[`createCompileRequest > createCompileRequest 7`] = `
],
}
`;

exports[`createCompileRequest > createCompileRequest 8`] = `
{
"targets": [
{
"compress": false,
"format": "plaintext",
"name": "company-words",
"sources": [
"src/c-words.txt",
],
"targetDirectory": ".",
},
],
}
`;

exports[`createCompileRequest > createCompileRequest 9`] = `
{
"targets": [
{
"compress": false,
"format": "plaintext",
"name": "python",
"sources": [
{
"listFile": "src/source-files.txt",
},
],
"targetDirectory": ".",
},
],
}
`;

exports[`createCompileRequest > createCompileRequest 10`] = `
{
"targets": [
{
"compress": false,
"format": "plaintext",
"name": "python",
"sources": [
{
"filename": "src/python.txt",
},
],
"targetDirectory": ".",
},
],
}
`;
21 changes: 12 additions & 9 deletions packages/cspell-tools/src/compiler/createCompileRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import { createCompileRequest } from './createCompileRequest.js';

describe('createCompileRequest', () => {
test.each`
source | options
${[]} | ${comp()}
${['src/words.txt']} | ${comp()}
${['src/words.txt', 'src/cities.txt']} | ${comp({ output: 'out' })}
${['src/words.txt', 'src/cities.txt']} | ${comp({ output: 'out', merge: 'combo' })}
${[]} | ${comp({ listFile: ['python-sources.txt'] })}
${[]} | ${comp({ listFile: ['python-sources.txt'], output: 'python' })}
${[]} | ${comp({ listFile: ['python-sources.txt'], merge: 'python' })}
source | options
${[]} | ${comp()}
${['src/words.txt']} | ${comp()}
${['src/words.txt', 'src/cities.txt']} | ${comp({ output: 'out' })}
${['src/words.txt', 'src/cities.txt']} | ${comp({ output: 'out', merge: 'combo' })}
${[]} | ${comp({ listFile: ['python-sources.txt'] })}
${[]} | ${comp({ listFile: ['python-sources.txt'], output: 'python' })}
${[]} | ${comp({ listFile: ['python-sources.txt'], merge: 'python' })}
${['src\\c-words.txt']} | ${comp({ merge: 'company-words' })}
${[{ listFile: 'src\\source-files.txt' }]} | ${comp({ merge: 'python' })}
${[{ filename: 'src\\python.txt' }]} | ${comp({ merge: 'python' })}
`('createCompileRequest', ({ source, options }) => {
const req = createCompileRequest(source, options);
// Make sure the test passes on Windows.
const reqClean = JSON.parse(JSON.stringify(req, null, 2).replace(/\\\\/g, '/'));
const reqClean = JSON.parse(JSON.stringify(req, null, 2) /* .replace(/\\\\/g, '/') */);
expect(reqClean).toMatchSnapshot();
});
});
Expand Down
39 changes: 31 additions & 8 deletions packages/cspell-tools/src/compiler/createCompileRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import type { CompileCommonAppOptions } from '../AppOptions.js';
import type { CompileRequest, DictionaryFormats, DictionarySource, FileSource, Target } from '../config/index.js';

export function createCompileRequest(sourceFiles: string[], options: CompileCommonAppOptions): CompileRequest {
const { max_depth, maxDepth, experimental = [], split, keepRawCase, useLegacySplitter } = options;
options = { ...options };
options.maxDepth ??= options.max_depth;

const { maxDepth, split, keepRawCase, useLegacySplitter } = options;

const sources: DictionarySource[] = [...sourceFiles, ...(options.listFile || []).map((listFile) => ({ listFile }))];

const targets = calcTargets(sources, options);
const generateNonStrict = experimental.includes('compound') || undefined;

const req: CompileRequest = {
targets,
generateNonStrict,
maxDepth: parseNumber(maxDepth) ?? parseNumber(max_depth),
maxDepth: parseNumber(maxDepth),
split: useLegacySplitter ? 'legacy' : split,
/**
* Do not generate lower case / accent free versions of words.
Expand All @@ -25,20 +26,27 @@ export function createCompileRequest(sourceFiles: string[], options: CompileComm

return req;
}

function calcTargets(sources: DictionarySource[], options: CompileCommonAppOptions): Target[] {
const { merge, output = '.' } = options;
const { merge, output = '.', experimental = [] } = options;

const generateNonStrict = experimental.includes('compound') || undefined;

// console.log('%o', sources);

const format = calcFormat(options);
const sort = (format === 'plaintext' && options.sort) || undefined;

if (merge) {
const target: Target = {
name: merge,
targetDirectory: output,
compress: options.compress,
format,
sources,
sort: options.sort,
sources: sources.map(normalizeSource),
sort,
trieBase: parseNumber(options.trieBase),
generateNonStrict,
};
return [target];
}
Expand All @@ -50,9 +58,10 @@ function calcTargets(sources: DictionarySource[], options: CompileCommonAppOptio
targetDirectory: output,
compress: options.compress,
format,
sources: [source],
sources: [normalizeSource(source)],
sort: options.sort,
trieBase: parseNumber(options.trieBase),
generateNonStrict,
};
return target;
});
Expand All @@ -79,3 +88,17 @@ function baseNameOfSource(source: DictionarySource): string {
function isFileSource(source: DictionarySource): source is FileSource {
return typeof source !== 'string' && (<FileSource>source).filename !== undefined;
}

function normalizeSource(source: DictionarySource): DictionarySource {
if (typeof source === 'string') {
return normalizeSourcePath(source);
}
if (isFileSource(source)) return { ...source, filename: normalizeSourcePath(source.filename) };
return { ...source, listFile: normalizeSourcePath(source.listFile) };
}

function normalizeSourcePath(source: string): string {
const cwd = process.cwd();
const rel = path.relative(cwd, source);
return rel.split('\\').join('/');
}

0 comments on commit da24c95

Please sign in to comment.