Skip to content

Commit 1f225c2

Browse files
shota-kizawakazupon
authored andcommitted
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 087392d commit 1f225c2

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
@@ -26,7 +26,6 @@ import JSON5 from 'json5'
2626
import yaml from 'js-yaml'
2727
import deepmerge from 'deepmerge'
2828
import { promisify } from 'util'
29-
import ignore from 'ignore'
3029
import type { Ignore } from 'ignore'
3130

3231
import { debug as Debug } from 'debug'
@@ -134,18 +133,15 @@ export function stringifyContent (content: any, lang: string, options?: FormatOp
134133
return result
135134
}
136135

137-
export function readSFC (target: string, ignorePath?: string): SFCFileInfo[] {
138-
let targets = resolveGlob(path.relative(process.cwd(), target))
139-
if ((ignorePath !== undefined)) {
140-
const ig = returnIgnoreInstance(ignorePath)
141-
targets = targets.filter(t => {
142-
return !ig.ignores(path.relative(process.cwd(), t))
143-
})
144-
}
145-
debug('readSFC: targets = ', targets)
136+
export function readSFC (target: string, ig: Ignore): SFCFileInfo[] {
137+
const targets = resolveGlob(target)
138+
const cookedTargets = targets.filter(t => {
139+
return !ig.ignores(path.relative(process.cwd(), t))
140+
}).map(p => path.resolve(p))
141+
debug('readSFC: targets = ', cookedTargets)
146142

147143
// TODO: async implementation
148-
return targets.map(t => {
144+
return cookedTargets.map(t => {
149145
const data = fs.readFileSync(t)
150146
return {
151147
path: t,
@@ -155,8 +151,9 @@ export function readSFC (target: string, ignorePath?: string): SFCFileInfo[] {
155151
}
156152

157153
function resolveGlob (target: string) {
154+
const relativeTarget = path.relative(process.cwd(), target)
158155
// TODO: async implementation
159-
return glob.sync(`${target}/**/*.vue`)
156+
return glob.sync(`${relativeTarget}/**/*.vue`)
160157
}
161158

162159
export const DEFUALT_CONF = { provider: {}} as ProviderConfiguration
@@ -337,14 +334,16 @@ function getLocaleMessagePathInfo (fullPath: string, bundleMatch?: string): Pars
337334
}
338335

339336
export function getExternalLocaleMessages (
340-
dictionary: NamespaceDictionary, bundleWith?: string, bundleMatch?: string
337+
dictionary: NamespaceDictionary, ig: Ignore, bundleWith?: string, bundleMatch?: string
341338
) {
342339
if (!bundleWith) { return {} }
343340

344341
const bundleTargetPaths = bundleWith.split(',').filter(p => p)
345342
return bundleTargetPaths.reduce((messages, targetPath) => {
346343
const namespace = dictionary[targetPath] || ''
347-
const globedPaths = glob.sync(targetPath).map(p => resolve(p))
344+
const globedPaths = glob.sync(path.relative(process.cwd(), targetPath)).filter(t => {
345+
return !ig.ignores(t)
346+
}).map(p => resolve(p))
348347
return globedPaths.reduce((messages, fullPath) => {
349348
const { locale, filename } = getLocaleMessagePathInfo(fullPath, bundleMatch)
350349
if (!locale) { return messages }
@@ -422,29 +421,16 @@ export function splitLocaleMessages (
422421
return { sfc: messages, external: metaExternalLocaleMessages }
423422
}
424423

425-
function returnIgnoreInstance (ignorePath: string): Ignore {
426-
const ig = ignore()
427-
if (fs.existsSync(ignorePath)) {
428-
addIgnoreFile(ig, ignorePath)
429-
} else {
430-
console.warn('cannot find ignore file.')
431-
}
432-
return ig
433-
}
434-
435-
function readIgnoreFile (ignorePath: string): string[] {
424+
export function readIgnoreFile (ignorePath: string): string[] {
436425
const ignoreFiles = fs.readFileSync(ignorePath, 'utf8')
437426
.split(/\r?\n/g)
438427
.filter(Boolean)
439428
console.log(`ignoreFiles ${ignoreFiles}`)
440429
return ignoreFiles
441430
}
442431

443-
function addIgnoreFile (
444-
ig: Ignore,
445-
ignorePath: string
446-
): void {
447-
readIgnoreFile(ignorePath).forEach(ignoreRule =>
432+
export function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void {
433+
ignoreFiles.forEach(ignoreRule => {
448434
ig.add(ignoreRule)
449-
)
435+
})
450436
}

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)