diff --git a/src/commands/infuse.ts b/src/commands/infuse.ts index e1f4aed..6a3a95e 100644 --- a/src/commands/infuse.ts +++ b/src/commands/infuse.ts @@ -11,8 +11,7 @@ import { readSFC, loadNamespaceDictionary, splitLocaleMessages, - readIgnoreFile, - returnIgnoreInstance, + getIgnore, getPrettierConfig } from '../utils' @@ -90,7 +89,7 @@ export const builder = (args: Argv): Argv => { .option('ignoreFileName', { type: 'string', alias: 'i', - describe: 'dot ignore file name, i.e. .ignore-i18n' + describe: 'ignore file names, i.e. .ignore-i18n .ignore-i18n-2' }) .option('prettier', { type: 'string', @@ -102,10 +101,9 @@ export const builder = (args: Argv): Argv => { export const handler = async (args: Arguments) => { const targetPath = resolve(args.target) const messagesPath = resolve(args.locales) - const ig = ignore() - if (args.ignoreFileName && fs.existsSync(args.ignoreFileName)) { - const ignoreFiles = readIgnoreFile(args.target, args.ignoreFileName) - returnIgnoreInstance(ig, ignoreFiles) + let ig = ignore() + if (args.ignoreFileName) { + ig = getIgnore(args.target, args.ignoreFileName) } const prettierConfig = args.prettier diff --git a/src/commands/squeeze.ts b/src/commands/squeeze.ts index 3a115fa..cbac2e0 100644 --- a/src/commands/squeeze.ts +++ b/src/commands/squeeze.ts @@ -5,8 +5,7 @@ import { readSFC, loadNamespaceDictionary, getExternalLocaleMessages, - readIgnoreFile, - returnIgnoreInstance + getIgnore } from '../utils' import squeeze from '../squeezer' import fs from 'fs' @@ -76,7 +75,7 @@ export const builder = (args: Argv): Argv => { .option('ignoreFileName', { type: 'string', alias: 'i', - describe: 'dot ignore file name, i.e. .ignore-i18n' + describe: 'ignore file names, i.e. .ignore-i18n .ignore-i18n-2' }) } @@ -85,11 +84,11 @@ export const handler = async (args: Arguments) => { let nsDictionary = {} as NamespaceDictionary let externalMessages = {} as LocaleMessages - const ig = ignore() - if (args.ignoreFileName && fs.existsSync(args.ignoreFileName)) { - const ignoreFiles = readIgnoreFile(args.target, args.ignoreFileName) - returnIgnoreInstance(ig, ignoreFiles) + let ig = ignore() + if (args.ignoreFileName) { + ig = getIgnore(args.target, args.ignoreFileName) } + try { if (args.namespace) { nsDictionary = await loadNamespaceDictionary(args.namespace) diff --git a/src/utils.ts b/src/utils.ts index ed270e4..263d7fc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -30,13 +30,15 @@ import JSON5 from 'json5' import yaml from 'js-yaml' import deepmerge from 'deepmerge' import { promisify } from 'util' -import type { Ignore } from 'ignore' import querystring from 'query-string' +import { debug as Debug } from 'debug' +import ignore from 'ignore' import { flatten, unflatten } from 'flat' import { cosmiconfig } from 'cosmiconfig' const jsonDiff = require('json-diff') // NOTE: not provided type definition ... -import { debug as Debug } from 'debug' +import type { Ignore } from 'ignore' + const debug = Debug('vue-i18n-locale-message:utils') const readFile = promisify(fs.readFile) @@ -471,32 +473,43 @@ export function splitLocaleMessages ( return { sfc: messages, external: metaExternalLocaleMessages } } -export function readIgnoreFile (target: string, ignoreFileName: string): string[] { - const ignoreFiles = glob.sync(`${target}/**/${ignoreFileName}`) - console.log(`ignoreFiles ${ignoreFiles}`) - const ignoreTargets = [] as string[] - ignoreFiles.forEach(ignoreFile => { - fs.readFileSync(ignoreFile, 'utf8') - .split(/\r?\n/g) - .filter(Boolean) - .forEach(ignoreTarget => { - ignoreTargets.push(formatPath(ignoreFile, ignoreTarget)) - }) +export function getIgnore (target:string, ignoreFileNames: string): Ignore { + const ig = ignore() + const files = ignoreFileNames.split(',').filter(Boolean) + files.forEach(file => { + const fullPath = resolve(path.join(target, path.normalize(file))) + console.log('fullpaht', fullPath, fs.existsSync(fullPath)) + if (fs.existsSync(fullPath)) { + const ignoreFiles = readIgnoreFile(fullPath) + returnIgnoreInstance(ig, ignoreFiles) + } }) - console.log(`ignoreTargets ${ignoreTargets}`) - return ignoreTargets + return ig } -function formatPath (ignoreFile: string, ignoreTarget: string): string { - return path.join(path.relative(process.cwd(), path.dirname(ignoreFile)), ignoreTarget) +function readIgnoreFile (ignoreFile: string): string[] { + console.log('readIgnoreFile: ignoreFile', ignoreFile) + const ignoreTargets = [] as string[] + fs.readFileSync(ignoreFile, 'utf8') + .split(/\r?\n/g) + .filter(Boolean) + .forEach(ignoreTarget => { + ignoreTargets.push(formatPath(ignoreFile, ignoreTarget)) + }) + console.log(`ignoreTargets ${ignoreTargets}`) + return ignoreTargets } -export function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void { +function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void { ignoreFiles.forEach(ignoreRule => { ig.add(ignoreRule) }) } +function formatPath (ignoreFile: string, ignoreTarget: string): string { + return path.join(path.relative(process.cwd(), path.dirname(ignoreFile)), ignoreTarget) +} + export async function returnDiff (options: DiffOptions): Promise { const format = 'json' const ProviderFactory = loadProvider(options.provider) diff --git a/test/commands/infuse.test.ts b/test/commands/infuse.test.ts index b35ab09..36e1b12 100644 --- a/test/commands/infuse.test.ts +++ b/test/commands/infuse.test.ts @@ -305,9 +305,7 @@ test('ignore option', async () => { mockFS.writeFileSync.mockImplementation((path, data) => { writeFiles[path as string] = data.toString() }) - mockFS.readFileSync.mockImplementationOnce(path => MOCK_IGNORE_FILES); - const mockGlob = glob as jest.Mocked - mockGlob.sync.mockImplementationOnce(p => [`${TARGET_PATH}/src/App.vue`]) + mockFS.readFileSync.mockImplementationOnce(path => MOCK_IGNORE_FILES) const mockPath = path as jest.Mocked mockPath.dirname.mockImplementationOnce(p => TARGET_PATH) diff --git a/test/commands/squeeze.test.ts b/test/commands/squeeze.test.ts index cb7ac3d..11f71f9 100644 --- a/test/commands/squeeze.test.ts +++ b/test/commands/squeeze.test.ts @@ -176,8 +176,6 @@ test('ignore option', async () => { mockUtils.getExternalLocaleMessages.mockImplementation(() => ({})) const mockFS = fs as jest.Mocked mockFS.readFileSync.mockImplementationOnce(p => MOCK_IGNORE_FILES); - const mockGlob = glob as jest.Mocked - mockGlob.sync.mockImplementationOnce(p => [path.resolve('./test/fixtures/.ignore-i18n')]) const mockPath = path as jest.Mocked mockPath.dirname.mockImplementationOnce(p => TARGET_PATH)