From fd120d11c81395353f300da487295b769f6b9501 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 18 Jan 2020 11:14:00 +0300 Subject: [PATCH] feat: add Svg prefix to exports that starts with a number (#383) Closes #379 --- __fixtures__/numeric/2.file.svg | 20 +++++++++++++++++++ __fixtures__/numeric/file.svg | 20 +++++++++++++++++++ .../cli/src/__snapshots__/index.test.js.snap | 7 +++++++ packages/cli/src/dirCommand.js | 3 ++- packages/cli/src/index.test.js | 17 ++++++++++++++-- 5 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 __fixtures__/numeric/2.file.svg create mode 100644 __fixtures__/numeric/file.svg diff --git a/__fixtures__/numeric/2.file.svg b/__fixtures__/numeric/2.file.svg new file mode 100644 index 00000000..94bf4b1e --- /dev/null +++ b/__fixtures__/numeric/2.file.svg @@ -0,0 +1,20 @@ + + + + Rectangle 5 + Created with Sketch. + + + + + + + + + + + + + + + diff --git a/__fixtures__/numeric/file.svg b/__fixtures__/numeric/file.svg new file mode 100644 index 00000000..94bf4b1e --- /dev/null +++ b/__fixtures__/numeric/file.svg @@ -0,0 +1,20 @@ + + + + Rectangle 5 + Created with Sketch. + + + + + + + + + + + + + + + diff --git a/packages/cli/src/__snapshots__/index.test.js.snap b/packages/cli/src/__snapshots__/index.test.js.snap index 3135862e..8cfb8bc2 100644 --- a/packages/cli/src/__snapshots__/index.test.js.snap +++ b/packages/cli/src/__snapshots__/index.test.js.snap @@ -1,5 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`cli should add Svg prefix to index.js exports staring with number 1`] = ` +"export { default as Svg2File } from './2File' +export { default as File } from './File'" +`; + exports[`cli should not override config with cli defaults 1`] = ` "import React from \\"react\\"; @@ -412,6 +417,8 @@ __fixtures__/complex/telegram.svg -> __fixtures_build__/whole/complex/Telegram.j __fixtures__/nesting/a/c/three.svg -> __fixtures_build__/whole/nesting/a/c/Three.js __fixtures__/nesting/a/two.svg -> __fixtures_build__/whole/nesting/a/Two.js __fixtures__/nesting/one.svg -> __fixtures_build__/whole/nesting/One.js +__fixtures__/numeric/2.file.svg -> __fixtures_build__/whole/numeric/2File.js +__fixtures__/numeric/file.svg -> __fixtures_build__/whole/numeric/File.js __fixtures__/simple/file.svg -> __fixtures_build__/whole/simple/File.js __fixtures__/withPrettierRc/file.svg -> __fixtures_build__/whole/withPrettierRc/File.js __fixtures__/withSvgoYml/file.svg -> __fixtures_build__/whole/withSvgoYml/File.js diff --git a/packages/cli/src/dirCommand.js b/packages/cli/src/dirCommand.js index bdac654b..2219ec4f 100644 --- a/packages/cli/src/dirCommand.js +++ b/packages/cli/src/dirCommand.js @@ -38,7 +38,8 @@ export function isCompilable(filename) { function defaultIndexTemplate(files) { const exportEntries = files.map(file => { const basename = path.basename(file, path.extname(file)) - return `export { default as ${basename} } from './${basename}'` + const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename + return `export { default as ${exportName} } from './${basename}'` }) return exportEntries.join('\n') } diff --git a/packages/cli/src/index.test.js b/packages/cli/src/index.test.js index d393f92c..d75fe367 100644 --- a/packages/cli/src/index.test.js +++ b/packages/cli/src/index.test.js @@ -167,11 +167,22 @@ describe('cli', () => { expect(result).toMatchSnapshot() }, 10000) + it('should add Svg prefix to index.js exports staring with number', async () => { + const inDir = '__fixtures__/numeric' + const outDir = `__fixtures_build__/prefix-exports` + await del(outDir) + await cli(`${inDir} --out-dir=${outDir}`) + const content = fs.readFileSync(path.join(outDir, 'index.js'), 'utf-8') + expect(content).toMatchSnapshot() + }, 10000) + it('should support custom index.js with directory output', async () => { const inDir = '__fixtures__/simple' const outDir = `__fixtures_build__/custom-index` await del(outDir) - await cli(`${inDir} --out-dir=${outDir} --config-file=__fixtures__/custom-index.config.js`) + await cli( + `${inDir} --out-dir=${outDir} --config-file=__fixtures__/custom-index.config.js`, + ) const content = fs.readFileSync(path.join(outDir, 'index.js'), 'utf-8') expect(content).toMatchSnapshot() }, 10000) @@ -180,7 +191,9 @@ describe('cli', () => { const inDir = '__fixtures__/simple' const outDir = `__fixtures_build__/custom-index-arg` await del(outDir) - await cli(`${inDir} --out-dir=${outDir} --index-template=__fixtures__/custom-index-template.js`) + await cli( + `${inDir} --out-dir=${outDir} --index-template=__fixtures__/custom-index-template.js`, + ) const content = fs.readFileSync(path.join(outDir, 'index.js'), 'utf-8') expect(content).toMatchSnapshot() }, 10000)