Skip to content

Commit 291b8e5

Browse files
authored
change how to specify dot ignore files: from file path to file name (#161)
* 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 * feat: change how to specify dot ignore files: from file path to file name
1 parent 7c4fe05 commit 291b8e5

File tree

5 files changed

+59
-24
lines changed

5 files changed

+59
-24
lines changed

src/commands/infuse.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type InfuseOptions = {
3737
unbundleMatch?: string
3838
namespace?: string
3939
dryRun: boolean
40-
ignorePath?: string
40+
ignoreFileName?: string
4141
}
4242

4343
export const command = 'infuse'
@@ -84,19 +84,19 @@ export const builder = (args: Argv): Argv<InfuseOptions> => {
8484
default: false,
8585
describe: 'run the infuse command, but do not apply them'
8686
})
87-
.option('ignorePath', {
87+
.option('ignoreFileName', {
8888
type: 'string',
8989
alias: 'i',
90-
describe: 'path to dot ignore file, i.e. ./.ignore-i18n'
90+
describe: 'dot ignore file name, i.e. .ignore-i18n'
9191
})
9292
}
9393

9494
export const handler = async (args: Arguments<InfuseOptions>) => {
9595
const targetPath = resolve(args.target)
9696
const messagesPath = resolve(args.locales)
9797
const ig = ignore()
98-
if (args.ignorePath && fs.existsSync(args.ignorePath)) {
99-
const ignoreFiles = readIgnoreFile(args.ignorePath)
98+
if (args.ignoreFileName && fs.existsSync(args.ignoreFileName)) {
99+
const ignoreFiles = readIgnoreFile(args.target, args.ignoreFileName)
100100
returnIgnoreInstance(ig, ignoreFiles)
101101
}
102102

src/commands/squeeze.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type SqueezeOptions = {
3030
bundleMatch?: string
3131
namespace?: string
3232
output: string
33-
ignorePath?: string
33+
ignoreFileName?: string
3434
}
3535

3636
export const command = 'squeeze'
@@ -73,10 +73,10 @@ export const builder = (args: Argv): Argv<SqueezeOptions> => {
7373
default: outputDefault,
7474
describe: 'path to output squeezed locale messages'
7575
})
76-
.option('ignorePath', {
76+
.option('ignoreFileName', {
7777
type: 'string',
7878
alias: 'i',
79-
describe: 'path to dot ignore file, i.e. ./.ignore-i18n'
79+
describe: 'dot ignore file name, i.e. .ignore-i18n'
8080
})
8181
}
8282

@@ -86,8 +86,8 @@ export const handler = async (args: Arguments<SqueezeOptions>) => {
8686
let nsDictionary = {} as NamespaceDictionary
8787
let externalMessages = {} as LocaleMessages
8888
const ig = ignore()
89-
if (args.ignorePath && fs.existsSync(args.ignorePath)) {
90-
const ignoreFiles = readIgnoreFile(args.ignorePath)
89+
if (args.ignoreFileName && fs.existsSync(args.ignoreFileName)) {
90+
const ignoreFiles = readIgnoreFile(args.target, args.ignoreFileName)
9191
returnIgnoreInstance(ig, ignoreFiles)
9292
}
9393
try {

src/utils.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,10 @@ export function stringifyContent (content: any, lang: string, options?: FormatOp
142142

143143
export function readSFC (target: string, ig: Ignore): SFCFileInfo[] {
144144
const targets = resolveGlob(target)
145-
const cookedTargets = targets.filter(t => {
145+
const tmp = targets.filter(t => {
146146
return !ig.ignores(path.relative(process.cwd(), t))
147-
}).map(p => path.resolve(p))
147+
})
148+
const cookedTargets = tmp.map(p => path.resolve(p))
148149
debug('readSFC: targets = ', cookedTargets)
149150

150151
// TODO: async implementation
@@ -428,12 +429,24 @@ export function splitLocaleMessages (
428429
return { sfc: messages, external: metaExternalLocaleMessages }
429430
}
430431

431-
export function readIgnoreFile (ignorePath: string): string[] {
432-
const ignoreFiles = fs.readFileSync(ignorePath, 'utf8')
433-
.split(/\r?\n/g)
434-
.filter(Boolean)
435-
console.log(`ignoreFiles ${ignoreFiles}`)
436-
return ignoreFiles
432+
export function readIgnoreFile (target: string, ignoreFileName: string): string[] {
433+
const ignoreFiles = glob.sync(`${target}/**/${ignoreFileName}`)
434+
console.log(`allignore ${ignoreFiles}`)
435+
const ignoreTargets = [] as string[]
436+
ignoreFiles.forEach(ignoreFile => {
437+
fs.readFileSync(ignoreFile, 'utf8')
438+
.split(/\r?\n/g)
439+
.filter(Boolean)
440+
.forEach(ignoreTarget => {
441+
ignoreTargets.push(formatPath(ignoreFile, ignoreTarget))
442+
})
443+
})
444+
console.log(`ignoreTargets ${ignoreTargets}`)
445+
return ignoreTargets
446+
}
447+
448+
function formatPath (ignoreFile: string, ignoreTarget: string): string {
449+
return path.join(path.relative(process.cwd(), path.dirname(ignoreFile)), ignoreTarget)
437450
}
438451

439452
export function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void {

test/commands/infuse.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as yargs from 'yargs'
22
import deepmerge from 'deepmerge'
3-
import path from 'path'
43

54
import jsonMetaInfo from '../fixtures/meta/json'
65
import json from '../fixtures/squeeze'
@@ -55,7 +54,14 @@ jest.mock('glob', () => ({ sync: jest.fn((pattern) => {
5554
return SFC_FILES
5655
}
5756
}) }))
58-
import * as glob from "glob"
57+
import glob from 'glob'
58+
59+
// mock: path
60+
jest.mock('path', () => ({
61+
...jest.requireActual('path'),
62+
dirname: jest.fn()
63+
}))
64+
import path from 'path'
5965

6066
// -------------------
6167
// setup/teadown hooks
@@ -293,12 +299,16 @@ test('ignore option', async () => {
293299
writeFiles[path as string] = data.toString()
294300
})
295301
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`])
304+
const mockPath = path as jest.Mocked<typeof path>
305+
mockPath.dirname.mockImplementationOnce(p => TARGET_PATH)
296306

297307
// run
298308
const infuse = await import('../../src/commands/infuse')
299309
const cmd = yargs.command(infuse)
300310
const output = await new Promise(resolve => {
301-
cmd.parse(`infuse --target=${TARGET_PATH}/src --locales=${TARGET_PATH}/locales.json --ignorePath=./test/fixtures/.ignore-i18n`, () => {
311+
cmd.parse(`infuse --target=${TARGET_PATH}/src --locales=${TARGET_PATH}/locales.json --ignoreFileName=.ignore-i18n`, () => {
302312
resolve(writeFiles)
303313
})
304314
})

test/commands/squeeze.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as yargs from 'yargs'
2-
import * as fs from 'fs';
2+
import * as fs from 'fs'
33
import jsonMetaInfo from '../fixtures/meta/json'
44
import external from '../fixtures/external'
55

@@ -33,6 +33,7 @@ import * as utils from '../../src/utils'
3333

3434
// mock: glob
3535
jest.mock('glob', () => ({ sync: jest.fn(() => SFC_FILES) }))
36+
import glob from 'glob'
3637

3738
// mock: fs
3839
jest.mock('fs', () => ({
@@ -45,6 +46,13 @@ jest.mock('fs', () => ({
4546
existsSync: jest.fn().mockImplementation(path => true)
4647
}))
4748

49+
// mock: path
50+
jest.mock('path', () => ({
51+
...jest.requireActual('path'),
52+
dirname: jest.fn()
53+
}))
54+
import path from 'path'
55+
4856
// -------------------
4957
// setup/teadown hooks
5058

@@ -167,12 +175,16 @@ test('ignore option', async () => {
167175
mockUtils.loadNamespaceDictionary.mockImplementation(async () => ({}))
168176
mockUtils.getExternalLocaleMessages.mockImplementation(() => ({}))
169177
const mockFS = fs as jest.Mocked<typeof fs>
170-
mockFS.readFileSync.mockImplementationOnce(path => MOCK_IGNORE_FILES);
178+
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')])
181+
const mockPath = path as jest.Mocked<typeof path>
182+
mockPath.dirname.mockImplementationOnce(p => TARGET_PATH)
171183

172184
const squeeze = await import('../../src/commands/squeeze')
173185
const cmd = yargs.command(squeeze)
174186
const output = await new Promise(resolve => {
175-
cmd.parse(`squeeze --target=${TARGET_PATH}/src --output=${TARGET_PATH}/locales --ignorePath=./test/fixtures/.ignore-i18n`, () => {
187+
cmd.parse(`squeeze --target=${TARGET_PATH}/src --output=${TARGET_PATH}/locales --ignoreFileName=.ignore-i18n`, () => {
176188
resolve(writeFiles)
177189
})
178190
})

0 commit comments

Comments
 (0)