Skip to content

Commit eb2a257

Browse files
authored
fix: ignore files when using bundle option (#158)
* feat: Add ignore option in cli (infuse, squeeze) * fix: utils & ignore option test * fix: lint * feat: infuse ignore test * fix: path relative * fix: ignore files when using bundle option
1 parent 09ab65d commit eb2a257

File tree

5 files changed

+49
-40
lines changed

5 files changed

+49
-40
lines changed

src/commands/infuse.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import {
55
parsePath,
66
readSFC,
77
loadNamespaceDictionary,
8-
splitLocaleMessages
8+
splitLocaleMessages,
9+
readIgnoreFile,
10+
returnIgnoreInstance
911
} from '../utils'
1012

1113
import infuse from '../infuser'
@@ -24,6 +26,7 @@ import {
2426
} from '../../types'
2527

2628
import { debug as Debug } from 'debug'
29+
import ignore from 'ignore'
2730
const debug = Debug('vue-i18n-locale-message:commands:infuse')
2831

2932
type InfuseOptions = {
@@ -91,6 +94,11 @@ export const builder = (args: Argv): Argv<InfuseOptions> => {
9194
export const handler = async (args: Arguments<InfuseOptions>) => {
9295
const targetPath = resolve(args.target)
9396
const messagesPath = resolve(args.locales)
97+
const ig = ignore()
98+
if (args.ignorePath && fs.existsSync(args.ignorePath)) {
99+
const ignoreFiles = readIgnoreFile(args.ignorePath)
100+
returnIgnoreInstance(ig, ignoreFiles)
101+
}
94102

95103
let nsDictionary = {} as NamespaceDictionary
96104
try {
@@ -102,7 +110,7 @@ export const handler = async (args: Arguments<InfuseOptions>) => {
102110
}
103111
debug('namespace dictionary:', nsDictionary)
104112

105-
const sources = readSFC(targetPath, args.ignorePath)
113+
const sources = readSFC(targetPath, ig)
106114
const messages = readLocaleMessages(messagesPath, args.match)
107115

108116
const { sfc, external } = splitLocaleMessages(messages, nsDictionary, args.unbundleTo, args.unbundleMatch)

src/commands/squeeze.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
parsePath,
55
readSFC,
66
loadNamespaceDictionary,
7-
getExternalLocaleMessages
7+
getExternalLocaleMessages,
8+
readIgnoreFile,
9+
returnIgnoreInstance
810
} from '../utils'
911
import squeeze from '../squeezer'
1012
import fs from 'fs'
@@ -18,6 +20,7 @@ import {
1820
} from '../../types'
1921

2022
import { debug as Debug } from 'debug'
23+
import ignore from 'ignore'
2124
const debug = Debug('vue-i18n-locale-message:commands:squeeze')
2225

2326
type SqueezeOptions = {
@@ -82,16 +85,21 @@ export const handler = async (args: Arguments<SqueezeOptions>) => {
8285

8386
let nsDictionary = {} as NamespaceDictionary
8487
let externalMessages = {} as LocaleMessages
88+
const ig = ignore()
89+
if (args.ignorePath && fs.existsSync(args.ignorePath)) {
90+
const ignoreFiles = readIgnoreFile(args.ignorePath)
91+
returnIgnoreInstance(ig, ignoreFiles)
92+
}
8593
try {
8694
if (args.namespace) {
8795
nsDictionary = await loadNamespaceDictionary(args.namespace)
8896
}
89-
externalMessages = getExternalLocaleMessages(nsDictionary, args.bundleWith, args.bundleMatch)
97+
externalMessages = getExternalLocaleMessages(nsDictionary, ig, args.bundleWith, args.bundleMatch)
9098
} catch (e) {
9199
console.warn('cannot load external locale messages failed')
92100
}
93101

94-
const meta = squeeze(targetPath, readSFC(targetPath, args.ignorePath))
102+
const meta = squeeze(targetPath, readSFC(targetPath, ig))
95103
const messages = deepmerge(generate(meta), externalMessages)
96104

97105
writeLocaleMessages(messages, args)

src/utils.ts

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import JSON5 from 'json5'
2828
import yaml from 'js-yaml'
2929
import deepmerge from 'deepmerge'
3030
import { promisify } from 'util'
31-
import ignore from 'ignore'
3231
import type { Ignore } from 'ignore'
3332

3433
import { debug as Debug } from 'debug'
@@ -141,18 +140,15 @@ export function stringifyContent (content: any, lang: string, options?: FormatOp
141140
return result
142141
}
143142

144-
export function readSFC (target: string, ignorePath?: string): SFCFileInfo[] {
145-
let targets = resolveGlob(path.relative(process.cwd(), target))
146-
if ((ignorePath !== undefined)) {
147-
const ig = returnIgnoreInstance(ignorePath)
148-
targets = targets.filter(t => {
149-
return !ig.ignores(path.relative(process.cwd(), t))
150-
})
151-
}
152-
debug('readSFC: targets = ', targets)
143+
export function readSFC (target: string, ig: Ignore): SFCFileInfo[] {
144+
const targets = resolveGlob(target)
145+
const cookedTargets = targets.filter(t => {
146+
return !ig.ignores(path.relative(process.cwd(), t))
147+
}).map(p => path.resolve(p))
148+
debug('readSFC: targets = ', cookedTargets)
153149

154150
// TODO: async implementation
155-
return targets.map(t => {
151+
return cookedTargets.map(t => {
156152
const data = fs.readFileSync(t)
157153
return {
158154
path: t,
@@ -162,8 +158,9 @@ export function readSFC (target: string, ignorePath?: string): SFCFileInfo[] {
162158
}
163159

164160
function resolveGlob (target: string) {
161+
const relativeTarget = path.relative(process.cwd(), target)
165162
// TODO: async implementation
166-
return glob.sync(`${target}/**/*.vue`)
163+
return glob.sync(`${relativeTarget}/**/*.vue`)
167164
}
168165

169166
export const DEFUALT_CONF = { provider: {}} as ProviderConfiguration
@@ -344,14 +341,16 @@ function getLocaleMessagePathInfo (fullPath: string, bundleMatch?: string): Pars
344341
}
345342

346343
export function getExternalLocaleMessages (
347-
dictionary: NamespaceDictionary, bundleWith?: string, bundleMatch?: string
344+
dictionary: NamespaceDictionary, ig: Ignore, bundleWith?: string, bundleMatch?: string
348345
) {
349346
if (!bundleWith) { return {} }
350347

351348
const bundleTargetPaths = bundleWith.split(',').filter(p => p)
352349
return bundleTargetPaths.reduce((messages, targetPath) => {
353350
const namespace = dictionary[targetPath] || ''
354-
const globedPaths = glob.sync(targetPath).map(p => resolve(p))
351+
const globedPaths = glob.sync(path.relative(process.cwd(), targetPath)).filter(t => {
352+
return !ig.ignores(t)
353+
}).map(p => resolve(p))
355354
return globedPaths.reduce((messages, fullPath) => {
356355
const { locale, filename } = getLocaleMessagePathInfo(fullPath, bundleMatch)
357356
if (!locale) { return messages }
@@ -429,29 +428,16 @@ export function splitLocaleMessages (
429428
return { sfc: messages, external: metaExternalLocaleMessages }
430429
}
431430

432-
function returnIgnoreInstance (ignorePath: string): Ignore {
433-
const ig = ignore()
434-
if (fs.existsSync(ignorePath)) {
435-
addIgnoreFile(ig, ignorePath)
436-
} else {
437-
console.warn('cannot find ignore file.')
438-
}
439-
return ig
440-
}
441-
442-
function readIgnoreFile (ignorePath: string): string[] {
431+
export function readIgnoreFile (ignorePath: string): string[] {
443432
const ignoreFiles = fs.readFileSync(ignorePath, 'utf8')
444433
.split(/\r?\n/g)
445434
.filter(Boolean)
446435
console.log(`ignoreFiles ${ignoreFiles}`)
447436
return ignoreFiles
448437
}
449438

450-
function addIgnoreFile (
451-
ig: Ignore,
452-
ignorePath: string
453-
): void {
454-
readIgnoreFile(ignorePath).forEach(ignoreRule =>
439+
export function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void {
440+
ignoreFiles.forEach(ignoreRule => {
455441
ig.add(ignoreRule)
456-
)
442+
})
457443
}

test/commands/infuse.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ jest.mock('glob', () => ({ sync: jest.fn((pattern) => {
5555
return SFC_FILES
5656
}
5757
}) }))
58+
import * as glob from "glob"
5859

5960
// -------------------
6061
// setup/teadown hooks

test/utils.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
splitLocaleMessages
1414
} from '../src/utils'
1515

16+
import ignore from 'ignore'
17+
1618
// ------
1719
// mocks
1820

@@ -63,23 +65,26 @@ test('getExternalLocaleMessages: basic usage', async () => {
6365
}
6466
const withBundle = './test/fixtures/packages/package1/locales/**/*.json,./test/fixtures/packages/package2/locales/**/*.json'
6567
const withBundleMatch = '([\\w]*)/([\\w]*)\\.json$'
66-
const messages = getExternalLocaleMessages(namespaces, withBundle, withBundleMatch)
68+
const ig = ignore()
69+
const messages = getExternalLocaleMessages(namespaces, ig, withBundle, withBundleMatch)
6770

6871
expect(messages).toMatchSnapshot()
6972
})
7073

7174
test('getExternalLocaleMessages: no namespace', async () => {
7275
const withBundle = './test/fixtures/packages/package1/locales/**/*.json,./test/fixtures/packages/package2/locales/**/*.json'
7376
const withBundleMatch = '([\\w]*)/([\\w]*)\\.json$'
74-
const messages = getExternalLocaleMessages({}, withBundle, withBundleMatch)
77+
const ig = ignore()
78+
const messages = getExternalLocaleMessages({}, ig, withBundle, withBundleMatch)
7579

7680
expect(messages).toMatchSnapshot()
7781
})
7882

7983
test('getExternalLocaleMessages: no filename', async () => {
8084
const withBundle = './test/fixtures/packages/package1/locales/**/*.json,./test/fixtures/packages/package2/locales/**/*.json'
8185
const withBundleMatch = '([\\w]*)/[\\w]*\\.json$'
82-
const messages = getExternalLocaleMessages({}, withBundle, withBundleMatch)
86+
const ig = ignore()
87+
const messages = getExternalLocaleMessages({}, ig, withBundle, withBundleMatch)
8388

8489
expect(messages).toMatchSnapshot()
8590
})
@@ -89,7 +94,8 @@ test('getExternalLocaleMessages: no bundle option', async () => {
8994
'./test/fixtures/packages/package1/locales/**/*.json': 'package1',
9095
'./test/fixtures/packages/package2/locales/**/*.json': 'package2'
9196
}
92-
const messages = getExternalLocaleMessages(namespaces)
97+
const ig = ignore()
98+
const messages = getExternalLocaleMessages(namespaces, ig)
9399

94100
expect(messages).toMatchObject({})
95101
})

0 commit comments

Comments
 (0)