Skip to content

Commit

Permalink
Improve cspell-tools (#134)
Browse files Browse the repository at this point in the history
* Remove unused imports

* Update hunspell-lib

* Add unit tests for app.ts

* Make snapshots of compiled output.

* Update package-lock.json

* Normalize snapshots

* Do not snapshot output -- issues with posix vs win32

cspell:ignore posix
  • Loading branch information
Jason3S committed Dec 5, 2019
1 parent a836804 commit 20ec671
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 79 deletions.
56 changes: 28 additions & 28 deletions package-lock.json

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

17 changes: 11 additions & 6 deletions packages/cspell-tools/package-lock.json

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

4 changes: 2 additions & 2 deletions packages/cspell-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
},
"homepage": "https://github.com/streetsidesoftware/cspell#readme",
"dependencies": {
"commander": "^2.20.3",
"commander": "^4.0.1",
"cspell-io": "^4.0.19",
"cspell-trie-lib": "^4.1.1",
"cspell-util-bundle": "^4.0.6",
"fs-extra": "^8.1.0",
"gensequence": "^2.1.3",
"glob": "^7.1.6",
"hunspell-reader": "^3.1.2",
"hunspell-reader": "^3.1.3",
"iconv-lite": "^0.4.24",
"minimatch": "^3.0.4"
},
Expand Down
60 changes: 60 additions & 0 deletions packages/cspell-tools/src/app.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as app from './app';
import * as Commander from 'commander';
import * as path from 'path';

const projectRoot = path.join(__dirname, '..');
const pathSamples = path.join(projectRoot, 'Samples');
const pathTemp = path.join(projectRoot, 'temp');

function argv(...args: string[]): string[] {
return [...process.argv.slice(0, 2), ...args];
}

function getCommander() {
return new Commander.Command;
}

describe('Validate the application', () => {
test('test app compile-trie', async () => {
const commander = getCommander();
const log = jest.spyOn(console, 'log').mockImplementation();
const args = argv('compile-trie', '-n', path.join(pathSamples, 'cities.txt'), '-o', pathTemp);
await expect(app.run(commander, args)).resolves.toBeUndefined();
expect(log).toHaveBeenCalled();
log.mockRestore();
});

test('test app compile', async () => {
const commander = getCommander();
const log = jest.spyOn(console, 'log').mockImplementation();
const args = argv('compile', '-n', path.join(pathSamples, 'cities.txt'), '-o', pathTemp);
await expect(app.run(commander, args)).resolves.toBeUndefined();
expect(log).toHaveBeenCalled();
log.mockRestore();
});

test('test app no args', () => {
const commander = getCommander();
const mock = jest.fn();
commander.on('--help', mock);
expect(app.run(commander, argv())).rejects.toThrowError(Commander.CommanderError);
expect(mock.mock.calls.length).toBe(1);
});

test('test app --help', async () => {
const commander = getCommander();
const mock = jest.fn();
commander.on('--help', mock);
await expect(app.run(commander, argv('--help'))).rejects.toThrowError(Commander.CommanderError);
expect(mock.mock.calls.length).toBe(1);
});

test('test app -V', async () => {
const commander = getCommander();
const mock = jest.fn();
commander.on('option:version', mock);
await expect(app.run(commander, argv('-V')) ).rejects.toThrowError(Commander.CommanderError);
expect(mock.mock.calls.length).toBe(1);
});

});
103 changes: 62 additions & 41 deletions packages/cspell-tools/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,65 @@ function globP(pattern: string): Promise<string[]> {
});
}

program
.version(npmPackage.version);

program
.command('compile <src...>')
.description('compile words lists into simple dictionary files.')
.option('-o, --output <path>', 'Specify the output directory, otherwise files are written back to the same location.')
.option('-n, --no-compress', 'By default the files are Gzipped, this will turn that off.')
.option('-m, --max_depth <limit>', 'Maximum depth to apply suffix rules.')
.option('-s, --no-split', 'Treat each line as a dictionary entry, do not split')
.option('--no-sort', 'Do not sort the result')
.action(async (src: string[], options: { output?: string, compress: boolean, split: boolean, sort: boolean, case: boolean, max_depth?: string }) => {
const { max_depth } = options;
const maxDepth = max_depth !== undefined ? Number.parseInt(max_depth) : undefined;
return processAction(src, '.txt', options, async (src, dst) => {
console.log('Process "%s" to "%s"', src, dst);
await compileWordList(src, dst, { splitWords: options.split, sort: options.sort, maxDepth }).then(() => src);
console.log('Done "%s" to "%s"', src, dst);
return src;
});
});
export function run(
program: program.Command,
argv: string[]
): Promise<void> {
(program as any).exitOverride();

program
.command('compile-trie <src...>')
.description('Compile words lists or Hunspell dictionary into trie files used by cspell.')
.option('-o, --output <path>', 'Specify the output directory, otherwise files are written back to the same location.')
.option('-m, --max_depth <limit>', 'Maximum depth to apply suffix rules.')
.option('-n, --no-compress', 'By default the files are Gzipped, this will turn that off.')
.action((src: string[], options: { output?: string, compress: boolean, max_depth?: string }) => {
const { max_depth } = options;
const maxDepth = max_depth !== undefined ? Number.parseInt(max_depth) : undefined;
return processAction(src, '.trie', options, async (src, dst) => {
console.log('Process "%s" to "%s"', src, dst);
return compileTrie(src, dst, { maxDepth } ).then(() => src);
});
return new Promise((resolve, rejects) => {
program
.version(npmPackage.version);

program
.command('compile <src...>')
.description('compile words lists into simple dictionary files.')
.option('-o, --output <path>', 'Specify the output directory, otherwise files are written back to the same location.')
.option('-n, --no-compress', 'By default the files are Gzipped, this will turn that off.')
.option('-m, --max_depth <limit>', 'Maximum depth to apply suffix rules.')
.option('-s, --no-split', 'Treat each line as a dictionary entry, do not split')
.option('--no-sort', 'Do not sort the result')
.action((src: string[], options: { output?: string, compress: boolean, split: boolean, sort: boolean, case: boolean, max_depth?: string }) => {
const { max_depth } = options;
const maxDepth = max_depth !== undefined ? Number.parseInt(max_depth) : undefined;
const result = processAction(src, '.txt', options, async (src, dst) => {
console.log('Process "%s" to "%s"', src, dst);
await compileWordList(src, dst, { splitWords: options.split, sort: options.sort, maxDepth }).then(() => src);
console.log('Done "%s" to "%s"', src, dst);
return src;
});
resolve(result);
});

program
.command('compile-trie <src...>')
.description('Compile words lists or Hunspell dictionary into trie files used by cspell.')
.option('-o, --output <path>', 'Specify the output directory, otherwise files are written back to the same location.')
.option('-m, --max_depth <limit>', 'Maximum depth to apply suffix rules.')
.option('-n, --no-compress', 'By default the files are Gzipped, this will turn that off.')
.action((src: string[], options: { output?: string, compress: boolean, max_depth?: string }) => {
const { max_depth } = options;
const maxDepth = max_depth !== undefined ? Number.parseInt(max_depth) : undefined;
const result = processAction(src, '.trie', options, async (src, dst) => {
console.log('Process "%s" to "%s"', src, dst);
return compileTrie(src, dst, { maxDepth }).then(() => src);
});
resolve(result);
});

try {
program.parse(argv);
if (!argv.slice(2).length) {
program.help();
}
} catch (e) {
rejects(e);
}

resolve();
});
}


async function processAction(
src: string[],
Expand All @@ -63,9 +86,9 @@ async function processAction(
action: (src: string, dst: string) => Promise<any>)
: Promise<void> {
console.log('Compile:\n output: %s\n compress: %s\n files:\n %s \n\n',
options.output || 'default',
options.compress ? 'true' : 'false',
src.join('\n ') );
options.output || 'default',
options.compress ? 'true' : 'false',
src.join('\n '));

const ext = fileExt + (options.compress ? '.gz' : '');

Expand All @@ -85,8 +108,6 @@ async function processAction(
console.log(`Complete.`);
}

program.parse(process.argv);

if (!process.argv.slice(2).length) {
program.help();
if (require.main === module) {
run(program, process.argv).catch(() => process.exit(1));
}
2 changes: 0 additions & 2 deletions packages/cspell-tools/src/compiler/wordListCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { xregexp as XRegExp } from 'cspell-util-bundle';
import { genSequence, Sequence } from 'gensequence';
import * as Text from './text';
import * as lib from 'cspell-io';
import * as path from 'path';
import { mkdirp } from 'fs-extra';
import * as Trie from 'cspell-trie-lib';
import * as HR from 'hunspell-reader';
import { streamWordsFromFile, HunspellOptions } from './iterateWordsFromFile';
import { writeSeqToFile } from './fileWriter';
import { uniqueFilter } from 'hunspell-reader/dist/util';
Expand Down

0 comments on commit 20ec671

Please sign in to comment.