Skip to content

Commit 2dc9583

Browse files
authored
feat: support multi ignore files for squeeze and infuse (#217)
1 parent f988ed0 commit 2dc9583

File tree

5 files changed

+42
-36
lines changed

5 files changed

+42
-36
lines changed

src/commands/infuse.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111
readSFC,
1212
loadNamespaceDictionary,
1313
splitLocaleMessages,
14-
readIgnoreFile,
15-
returnIgnoreInstance,
14+
getIgnore,
1615
getPrettierConfig
1716
} from '../utils'
1817

@@ -91,7 +90,7 @@ export const builder = (args: Argv): Argv<InfuseOptions> => {
9190
.option('ignoreFileName', {
9291
type: 'string',
9392
alias: 'i',
94-
describe: 'dot ignore file name, i.e. .ignore-i18n'
93+
describe: 'ignore file names, i.e. .ignore-i18n .ignore-i18n-2'
9594
})
9695
.option('prettier', {
9796
type: 'string',
@@ -109,10 +108,9 @@ export const handler = async (args: Arguments<InfuseOptions>) => {
109108
const vue = args.vue || 2
110109
const targetPath = resolve(args.target)
111110
const messagesPath = resolve(args.locales)
112-
const ig = ignore()
113-
if (args.ignoreFileName && fs.existsSync(args.ignoreFileName)) {
114-
const ignoreFiles = readIgnoreFile(args.target, args.ignoreFileName)
115-
returnIgnoreInstance(ig, ignoreFiles)
111+
let ig = ignore()
112+
if (args.ignoreFileName) {
113+
ig = getIgnore(args.target, args.ignoreFileName)
116114
}
117115

118116
const prettierConfig = args.prettier

src/commands/squeeze.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {
55
readSFC,
66
loadNamespaceDictionary,
77
getExternalLocaleMessages,
8-
readIgnoreFile,
9-
returnIgnoreInstance
8+
getIgnore
109
} from '../utils'
1110
import squeeze from '../squeezer'
1211
import fs from 'fs'
@@ -76,7 +75,7 @@ export const builder = (args: Argv): Argv<SqueezeOptions> => {
7675
.option('ignoreFileName', {
7776
type: 'string',
7877
alias: 'i',
79-
describe: 'dot ignore file name, i.e. .ignore-i18n'
78+
describe: 'ignore file names, i.e. .ignore-i18n .ignore-i18n-2'
8079
})
8180
}
8281

@@ -85,11 +84,11 @@ export const handler = async (args: Arguments<SqueezeOptions>) => {
8584

8685
let nsDictionary = {} as NamespaceDictionary
8786
let externalMessages = {} as LocaleMessages
88-
const ig = ignore()
89-
if (args.ignoreFileName && fs.existsSync(args.ignoreFileName)) {
90-
const ignoreFiles = readIgnoreFile(args.target, args.ignoreFileName)
91-
returnIgnoreInstance(ig, ignoreFiles)
87+
let ig = ignore()
88+
if (args.ignoreFileName) {
89+
ig = getIgnore(args.target, args.ignoreFileName)
9290
}
91+
9392
try {
9493
if (args.namespace) {
9594
nsDictionary = await loadNamespaceDictionary(args.namespace)

src/utils.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ import JSON5 from 'json5'
3232
import yaml from 'js-yaml'
3333
import deepmerge from 'deepmerge'
3434
import { promisify } from 'util'
35-
import type { Ignore } from 'ignore'
3635
import querystring from 'query-string'
36+
import { debug as Debug } from 'debug'
37+
import ignore from 'ignore'
3738
import { flatten, unflatten } from 'flat'
3839
import { cosmiconfig } from 'cosmiconfig'
3940
const jsonDiff = require('json-diff') // NOTE: not provided type definition ...
4041

41-
import { debug as Debug } from 'debug'
42+
import type { Ignore } from 'ignore'
43+
4244
const debug = Debug('vue-i18n-locale-message:utils')
4345

4446
const readFile = promisify(fs.readFile)
@@ -478,32 +480,43 @@ export function splitLocaleMessages (
478480
return { sfc: messages, external: metaExternalLocaleMessages }
479481
}
480482

481-
export function readIgnoreFile (target: string, ignoreFileName: string): string[] {
482-
const ignoreFiles = glob.sync(`${target}/**/${ignoreFileName}`)
483-
console.log(`ignoreFiles ${ignoreFiles}`)
484-
const ignoreTargets = [] as string[]
485-
ignoreFiles.forEach(ignoreFile => {
486-
fs.readFileSync(ignoreFile, 'utf8')
487-
.split(/\r?\n/g)
488-
.filter(Boolean)
489-
.forEach(ignoreTarget => {
490-
ignoreTargets.push(formatPath(ignoreFile, ignoreTarget))
491-
})
483+
export function getIgnore (target:string, ignoreFileNames: string): Ignore {
484+
const ig = ignore()
485+
const files = ignoreFileNames.split(',').filter(Boolean)
486+
files.forEach(file => {
487+
const fullPath = resolve(path.join(target, path.normalize(file)))
488+
console.log('fullpaht', fullPath, fs.existsSync(fullPath))
489+
if (fs.existsSync(fullPath)) {
490+
const ignoreFiles = readIgnoreFile(fullPath)
491+
returnIgnoreInstance(ig, ignoreFiles)
492+
}
492493
})
493-
console.log(`ignoreTargets ${ignoreTargets}`)
494-
return ignoreTargets
494+
return ig
495495
}
496496

497-
function formatPath (ignoreFile: string, ignoreTarget: string): string {
498-
return path.join(path.relative(process.cwd(), path.dirname(ignoreFile)), ignoreTarget)
497+
function readIgnoreFile (ignoreFile: string): string[] {
498+
console.log('readIgnoreFile: ignoreFile', ignoreFile)
499+
const ignoreTargets = [] as string[]
500+
fs.readFileSync(ignoreFile, 'utf8')
501+
.split(/\r?\n/g)
502+
.filter(Boolean)
503+
.forEach(ignoreTarget => {
504+
ignoreTargets.push(formatPath(ignoreFile, ignoreTarget))
505+
})
506+
console.log(`ignoreTargets ${ignoreTargets}`)
507+
return ignoreTargets
499508
}
500509

501-
export function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void {
510+
function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void {
502511
ignoreFiles.forEach(ignoreRule => {
503512
ig.add(ignoreRule)
504513
})
505514
}
506515

516+
function formatPath (ignoreFile: string, ignoreTarget: string): string {
517+
return path.join(path.relative(process.cwd(), path.dirname(ignoreFile)), ignoreTarget)
518+
}
519+
507520
export async function returnDiff (options: DiffOptions): Promise<DiffInfo> {
508521
const format = 'json'
509522
const ProviderFactory = loadProvider(options.provider)

test/commands/infuse.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,6 @@ test('ignore option', async () => {
299299
writeFiles[path as string] = data.toString()
300300
})
301301
mockFS.readFileSync.mockImplementationOnce(path => MOCK_IGNORE_FILES)
302-
const mockGlob = glob as jest.Mocked<typeof glob>
303-
mockGlob.sync.mockImplementationOnce(p => [`${TARGET_PATH}/src/App.vue`])
304302
const mockPath = path as jest.Mocked<typeof path>
305303
mockPath.dirname.mockImplementationOnce(p => TARGET_PATH)
306304

test/commands/squeeze.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ test('ignore option', async () => {
176176
mockUtils.getExternalLocaleMessages.mockImplementation(() => ({}))
177177
const mockFS = fs as jest.Mocked<typeof fs>
178178
mockFS.readFileSync.mockImplementationOnce(p => MOCK_IGNORE_FILES);
179-
const mockGlob = glob as jest.Mocked<typeof glob>
180-
mockGlob.sync.mockImplementationOnce(p => [path.resolve('./test/fixtures/.ignore-i18n')])
181179
const mockPath = path as jest.Mocked<typeof path>
182180
mockPath.dirname.mockImplementationOnce(p => TARGET_PATH)
183181

0 commit comments

Comments
 (0)