Skip to content

Commit

Permalink
change how to specify dot ignore files: from file path to file name (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
kiibo382 committed Jul 26, 2021
1 parent 7c4fe05 commit 291b8e5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/commands/infuse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type InfuseOptions = {
unbundleMatch?: string
namespace?: string
dryRun: boolean
ignorePath?: string
ignoreFileName?: string
}

export const command = 'infuse'
Expand Down Expand Up @@ -84,19 +84,19 @@ export const builder = (args: Argv): Argv<InfuseOptions> => {
default: false,
describe: 'run the infuse command, but do not apply them'
})
.option('ignorePath', {
.option('ignoreFileName', {
type: 'string',
alias: 'i',
describe: 'path to dot ignore file, i.e. ./.ignore-i18n'
describe: 'dot ignore file name, i.e. .ignore-i18n'
})
}

export const handler = async (args: Arguments<InfuseOptions>) => {
const targetPath = resolve(args.target)
const messagesPath = resolve(args.locales)
const ig = ignore()
if (args.ignorePath && fs.existsSync(args.ignorePath)) {
const ignoreFiles = readIgnoreFile(args.ignorePath)
if (args.ignoreFileName && fs.existsSync(args.ignoreFileName)) {
const ignoreFiles = readIgnoreFile(args.target, args.ignoreFileName)
returnIgnoreInstance(ig, ignoreFiles)
}

Expand Down
10 changes: 5 additions & 5 deletions src/commands/squeeze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type SqueezeOptions = {
bundleMatch?: string
namespace?: string
output: string
ignorePath?: string
ignoreFileName?: string
}

export const command = 'squeeze'
Expand Down Expand Up @@ -73,10 +73,10 @@ export const builder = (args: Argv): Argv<SqueezeOptions> => {
default: outputDefault,
describe: 'path to output squeezed locale messages'
})
.option('ignorePath', {
.option('ignoreFileName', {
type: 'string',
alias: 'i',
describe: 'path to dot ignore file, i.e. ./.ignore-i18n'
describe: 'dot ignore file name, i.e. .ignore-i18n'
})
}

Expand All @@ -86,8 +86,8 @@ export const handler = async (args: Arguments<SqueezeOptions>) => {
let nsDictionary = {} as NamespaceDictionary
let externalMessages = {} as LocaleMessages
const ig = ignore()
if (args.ignorePath && fs.existsSync(args.ignorePath)) {
const ignoreFiles = readIgnoreFile(args.ignorePath)
if (args.ignoreFileName && fs.existsSync(args.ignoreFileName)) {
const ignoreFiles = readIgnoreFile(args.target, args.ignoreFileName)
returnIgnoreInstance(ig, ignoreFiles)
}
try {
Expand Down
29 changes: 21 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ export function stringifyContent (content: any, lang: string, options?: FormatOp

export function readSFC (target: string, ig: Ignore): SFCFileInfo[] {
const targets = resolveGlob(target)
const cookedTargets = targets.filter(t => {
const tmp = targets.filter(t => {
return !ig.ignores(path.relative(process.cwd(), t))
}).map(p => path.resolve(p))
})
const cookedTargets = tmp.map(p => path.resolve(p))
debug('readSFC: targets = ', cookedTargets)

// TODO: async implementation
Expand Down Expand Up @@ -428,12 +429,24 @@ export function splitLocaleMessages (
return { sfc: messages, external: metaExternalLocaleMessages }
}

export function readIgnoreFile (ignorePath: string): string[] {
const ignoreFiles = fs.readFileSync(ignorePath, 'utf8')
.split(/\r?\n/g)
.filter(Boolean)
console.log(`ignoreFiles ${ignoreFiles}`)
return ignoreFiles
export function readIgnoreFile (target: string, ignoreFileName: string): string[] {
const ignoreFiles = glob.sync(`${target}/**/${ignoreFileName}`)
console.log(`allignore ${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))
})
})
console.log(`ignoreTargets ${ignoreTargets}`)
return ignoreTargets
}

function formatPath (ignoreFile: string, ignoreTarget: string): string {
return path.join(path.relative(process.cwd(), path.dirname(ignoreFile)), ignoreTarget)
}

export function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void {
Expand Down
16 changes: 13 additions & 3 deletions test/commands/infuse.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as yargs from 'yargs'
import deepmerge from 'deepmerge'
import path from 'path'

import jsonMetaInfo from '../fixtures/meta/json'
import json from '../fixtures/squeeze'
Expand Down Expand Up @@ -55,7 +54,14 @@ jest.mock('glob', () => ({ sync: jest.fn((pattern) => {
return SFC_FILES
}
}) }))
import * as glob from "glob"
import glob from 'glob'

// mock: path
jest.mock('path', () => ({
...jest.requireActual('path'),
dirname: jest.fn()
}))
import path from 'path'

// -------------------
// setup/teadown hooks
Expand Down Expand Up @@ -293,12 +299,16 @@ test('ignore option', async () => {
writeFiles[path as string] = data.toString()
})
mockFS.readFileSync.mockImplementationOnce(path => MOCK_IGNORE_FILES);
const mockGlob = glob as jest.Mocked<typeof glob>
mockGlob.sync.mockImplementationOnce(p => [`${TARGET_PATH}/src/App.vue`])
const mockPath = path as jest.Mocked<typeof path>
mockPath.dirname.mockImplementationOnce(p => TARGET_PATH)

// run
const infuse = await import('../../src/commands/infuse')
const cmd = yargs.command(infuse)
const output = await new Promise(resolve => {
cmd.parse(`infuse --target=${TARGET_PATH}/src --locales=${TARGET_PATH}/locales.json --ignorePath=./test/fixtures/.ignore-i18n`, () => {
cmd.parse(`infuse --target=${TARGET_PATH}/src --locales=${TARGET_PATH}/locales.json --ignoreFileName=.ignore-i18n`, () => {
resolve(writeFiles)
})
})
Expand Down
18 changes: 15 additions & 3 deletions test/commands/squeeze.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as yargs from 'yargs'
import * as fs from 'fs';
import * as fs from 'fs'
import jsonMetaInfo from '../fixtures/meta/json'
import external from '../fixtures/external'

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

// mock: glob
jest.mock('glob', () => ({ sync: jest.fn(() => SFC_FILES) }))
import glob from 'glob'

// mock: fs
jest.mock('fs', () => ({
Expand All @@ -45,6 +46,13 @@ jest.mock('fs', () => ({
existsSync: jest.fn().mockImplementation(path => true)
}))

// mock: path
jest.mock('path', () => ({
...jest.requireActual('path'),
dirname: jest.fn()
}))
import path from 'path'

// -------------------
// setup/teadown hooks

Expand Down Expand Up @@ -167,12 +175,16 @@ test('ignore option', async () => {
mockUtils.loadNamespaceDictionary.mockImplementation(async () => ({}))
mockUtils.getExternalLocaleMessages.mockImplementation(() => ({}))
const mockFS = fs as jest.Mocked<typeof fs>
mockFS.readFileSync.mockImplementationOnce(path => MOCK_IGNORE_FILES);
mockFS.readFileSync.mockImplementationOnce(p => MOCK_IGNORE_FILES);
const mockGlob = glob as jest.Mocked<typeof glob>
mockGlob.sync.mockImplementationOnce(p => [path.resolve('./test/fixtures/.ignore-i18n')])
const mockPath = path as jest.Mocked<typeof path>
mockPath.dirname.mockImplementationOnce(p => TARGET_PATH)

const squeeze = await import('../../src/commands/squeeze')
const cmd = yargs.command(squeeze)
const output = await new Promise(resolve => {
cmd.parse(`squeeze --target=${TARGET_PATH}/src --output=${TARGET_PATH}/locales --ignorePath=./test/fixtures/.ignore-i18n`, () => {
cmd.parse(`squeeze --target=${TARGET_PATH}/src --output=${TARGET_PATH}/locales --ignoreFileName=.ignore-i18n`, () => {
resolve(writeFiles)
})
})
Expand Down

0 comments on commit 291b8e5

Please sign in to comment.