UNPKG

@cspell/cspell-tools

Version:
84 lines 3 kB
import * as path from 'node:path'; export function createCompileRequest(sourceFiles, options) { options = { ...options }; options.maxDepth ??= options.max_depth; const { maxDepth, split, keepRawCase, useLegacySplitter } = options; const sources = [...sourceFiles, ...(options.listFile || []).map((listFile) => ({ listFile }))]; const targets = calcTargets(sources, options); const req = { targets, maxDepth: parseNumber(maxDepth), split: useLegacySplitter ? 'legacy' : split, /** * Do not generate lower case / accent free versions of words. * @default false */ keepRawCase, }; return req; } function calcTargets(sources, 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 = { name: merge, targetDirectory: output, compress: options.compress, format, sources: sources.map(normalizeSource), sort, trieBase: parseNumber(options.trieBase), generateNonStrict, }; return [target]; } const targets = sources.map((source) => { const name = toTargetName(baseNameOfSource(source)); const target = { name, targetDirectory: output, compress: options.compress, format, sources: [normalizeSource(source)], sort: options.sort, trieBase: parseNumber(options.trieBase), generateNonStrict, }; return target; }); return targets; } function calcFormat(options) { return (options.trie4 && 'trie4') || (options.trie3 && 'trie3') || (options.trie && 'trie') || 'plaintext'; } function toTargetName(sourceFile) { return path.basename(sourceFile).replace(/((\.txt|\.dic|\.aff|\.trie)(\.gz)?)?$/, ''); } function parseNumber(s) { const n = Number.parseInt(s ?? ''); return Number.isNaN(n) ? undefined : n; } function baseNameOfSource(source) { return typeof source === 'string' ? source : isFileSource(source) ? source.filename : source.listFile; } function isFileSource(source) { return typeof source !== 'string' && source.filename !== undefined; } function normalizeSource(source) { 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) { const cwd = process.cwd(); const rel = path.relative(cwd, source); return rel.split('\\').join('/'); } //# sourceMappingURL=createCompileRequest.js.map