Skip to content

Commit

Permalink
feat: support multi ignore files for squeeze and infuse (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Dec 9, 2021
1 parent 36129c6 commit 1b6a3fa
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 37 deletions.
12 changes: 5 additions & 7 deletions src/commands/infuse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
readSFC,
loadNamespaceDictionary,
splitLocaleMessages,
readIgnoreFile,
returnIgnoreInstance,
getIgnore,
getPrettierConfig
} from '../utils'

Expand Down Expand Up @@ -90,7 +89,7 @@ export const builder = (args: Argv): Argv<InfuseOptions> => {
.option('ignoreFileName', {
type: 'string',
alias: 'i',
describe: 'dot ignore file name, i.e. .ignore-i18n'
describe: 'ignore file names, i.e. .ignore-i18n .ignore-i18n-2'
})
.option('prettier', {
type: 'string',
Expand All @@ -102,10 +101,9 @@ export const builder = (args: Argv): Argv<InfuseOptions> => {
export const handler = async (args: Arguments<InfuseOptions>) => {
const targetPath = resolve(args.target)
const messagesPath = resolve(args.locales)
const ig = ignore()
if (args.ignoreFileName && fs.existsSync(args.ignoreFileName)) {
const ignoreFiles = readIgnoreFile(args.target, args.ignoreFileName)
returnIgnoreInstance(ig, ignoreFiles)
let ig = ignore()
if (args.ignoreFileName) {
ig = getIgnore(args.target, args.ignoreFileName)
}

const prettierConfig = args.prettier
Expand Down
13 changes: 6 additions & 7 deletions src/commands/squeeze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
readSFC,
loadNamespaceDictionary,
getExternalLocaleMessages,
readIgnoreFile,
returnIgnoreInstance
getIgnore
} from '../utils'
import squeeze from '../squeezer'
import fs from 'fs'
Expand Down Expand Up @@ -76,7 +75,7 @@ export const builder = (args: Argv): Argv<SqueezeOptions> => {
.option('ignoreFileName', {
type: 'string',
alias: 'i',
describe: 'dot ignore file name, i.e. .ignore-i18n'
describe: 'ignore file names, i.e. .ignore-i18n .ignore-i18n-2'
})
}

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

let nsDictionary = {} as NamespaceDictionary
let externalMessages = {} as LocaleMessages
const ig = ignore()
if (args.ignoreFileName && fs.existsSync(args.ignoreFileName)) {
const ignoreFiles = readIgnoreFile(args.target, args.ignoreFileName)
returnIgnoreInstance(ig, ignoreFiles)
let ig = ignore()
if (args.ignoreFileName) {
ig = getIgnore(args.target, args.ignoreFileName)
}

try {
if (args.namespace) {
nsDictionary = await loadNamespaceDictionary(args.namespace)
Expand Down
49 changes: 31 additions & 18 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ import JSON5 from 'json5'
import yaml from 'js-yaml'
import deepmerge from 'deepmerge'
import { promisify } from 'util'
import type { Ignore } from 'ignore'
import querystring from 'query-string'
import { debug as Debug } from 'debug'
import ignore from 'ignore'
import { flatten, unflatten } from 'flat'
import { cosmiconfig } from 'cosmiconfig'
const jsonDiff = require('json-diff') // NOTE: not provided type definition ...

import { debug as Debug } from 'debug'
import type { Ignore } from 'ignore'

const debug = Debug('vue-i18n-locale-message:utils')

const readFile = promisify(fs.readFile)
Expand Down Expand Up @@ -471,32 +473,43 @@ export function splitLocaleMessages (
return { sfc: messages, external: metaExternalLocaleMessages }
}

export function readIgnoreFile (target: string, ignoreFileName: string): string[] {
const ignoreFiles = glob.sync(`${target}/**/${ignoreFileName}`)
console.log(`ignoreFiles ${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))
})
export function getIgnore (target:string, ignoreFileNames: string): Ignore {
const ig = ignore()
const files = ignoreFileNames.split(',').filter(Boolean)
files.forEach(file => {
const fullPath = resolve(path.join(target, path.normalize(file)))
console.log('fullpaht', fullPath, fs.existsSync(fullPath))
if (fs.existsSync(fullPath)) {
const ignoreFiles = readIgnoreFile(fullPath)
returnIgnoreInstance(ig, ignoreFiles)
}
})
console.log(`ignoreTargets ${ignoreTargets}`)
return ignoreTargets
return ig
}

function formatPath (ignoreFile: string, ignoreTarget: string): string {
return path.join(path.relative(process.cwd(), path.dirname(ignoreFile)), ignoreTarget)
function readIgnoreFile (ignoreFile: string): string[] {
console.log('readIgnoreFile: ignoreFile', ignoreFile)
const ignoreTargets = [] as string[]
fs.readFileSync(ignoreFile, 'utf8')
.split(/\r?\n/g)
.filter(Boolean)
.forEach(ignoreTarget => {
ignoreTargets.push(formatPath(ignoreFile, ignoreTarget))
})
console.log(`ignoreTargets ${ignoreTargets}`)
return ignoreTargets
}

export function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void {
function returnIgnoreInstance (ig: Ignore, ignoreFiles: string[]): void {
ignoreFiles.forEach(ignoreRule => {
ig.add(ignoreRule)
})
}

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

export async function returnDiff (options: DiffOptions): Promise<DiffInfo> {
const format = 'json'
const ProviderFactory = loadProvider(options.provider)
Expand Down
4 changes: 1 addition & 3 deletions test/commands/infuse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,7 @@ test('ignore option', async () => {
mockFS.writeFileSync.mockImplementation((path, data) => {
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`])
mockFS.readFileSync.mockImplementationOnce(path => MOCK_IGNORE_FILES)
const mockPath = path as jest.Mocked<typeof path>
mockPath.dirname.mockImplementationOnce(p => TARGET_PATH)

Expand Down
2 changes: 0 additions & 2 deletions test/commands/squeeze.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@ test('ignore option', async () => {
mockUtils.getExternalLocaleMessages.mockImplementation(() => ({}))
const mockFS = fs as jest.Mocked<typeof fs>
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)

Expand Down

0 comments on commit 1b6a3fa

Please sign in to comment.