From 291b8e50820b8599dd63009ff4155069c02cb8d3 Mon Sep 17 00:00:00 2001 From: shota kizawa <64523345+kiibo382@users.noreply.github.com> Date: Mon, 26 Jul 2021 14:39:03 +0900 Subject: [PATCH] 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 --- src/commands/infuse.ts | 10 +++++----- src/commands/squeeze.ts | 10 +++++----- src/utils.ts | 29 +++++++++++++++++++++-------- test/commands/infuse.test.ts | 16 +++++++++++++--- test/commands/squeeze.test.ts | 18 +++++++++++++++--- 5 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/commands/infuse.ts b/src/commands/infuse.ts index d431037..7c4d179 100644 --- a/src/commands/infuse.ts +++ b/src/commands/infuse.ts @@ -37,7 +37,7 @@ type InfuseOptions = { unbundleMatch?: string namespace?: string dryRun: boolean - ignorePath?: string + ignoreFileName?: string } export const command = 'infuse' @@ -84,10 +84,10 @@ export const builder = (args: Argv): Argv => { 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' }) } @@ -95,8 +95,8 @@ export const handler = async (args: Arguments) => { 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) } diff --git a/src/commands/squeeze.ts b/src/commands/squeeze.ts index e34f1de..3a115fa 100644 --- a/src/commands/squeeze.ts +++ b/src/commands/squeeze.ts @@ -30,7 +30,7 @@ type SqueezeOptions = { bundleMatch?: string namespace?: string output: string - ignorePath?: string + ignoreFileName?: string } export const command = 'squeeze' @@ -73,10 +73,10 @@ export const builder = (args: Argv): Argv => { 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' }) } @@ -86,8 +86,8 @@ export const handler = async (args: Arguments) => { 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 { diff --git a/src/utils.ts b/src/utils.ts index 44280db..3b013e7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 @@ -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 { diff --git a/test/commands/infuse.test.ts b/test/commands/infuse.test.ts index 07173d1..124b5db 100644 --- a/test/commands/infuse.test.ts +++ b/test/commands/infuse.test.ts @@ -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' @@ -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 @@ -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 + mockGlob.sync.mockImplementationOnce(p => [`${TARGET_PATH}/src/App.vue`]) + const mockPath = path as jest.Mocked + 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) }) }) diff --git a/test/commands/squeeze.test.ts b/test/commands/squeeze.test.ts index fe905e2..cb7ac3d 100644 --- a/test/commands/squeeze.test.ts +++ b/test/commands/squeeze.test.ts @@ -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' @@ -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', () => ({ @@ -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 @@ -167,12 +175,16 @@ test('ignore option', async () => { mockUtils.loadNamespaceDictionary.mockImplementation(async () => ({})) mockUtils.getExternalLocaleMessages.mockImplementation(() => ({})) const mockFS = fs as jest.Mocked - mockFS.readFileSync.mockImplementationOnce(path => MOCK_IGNORE_FILES); + mockFS.readFileSync.mockImplementationOnce(p => MOCK_IGNORE_FILES); + const mockGlob = glob as jest.Mocked + mockGlob.sync.mockImplementationOnce(p => [path.resolve('./test/fixtures/.ignore-i18n')]) + const mockPath = path as jest.Mocked + 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) }) })