Skip to content

Commit

Permalink
feat(cli): generate index.js when --out-dir is used
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Dec 22, 2019
1 parent dcd824c commit 9d7fb34
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 35 deletions.
24 changes: 24 additions & 0 deletions __fixtures__/nesting/a/c/three.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
"commander": "^4.0.1",
"dashify": "^2.0.0",
"glob": "^7.1.4",
"output-file-sync": "^2.0.1",
"recursive-readdir": "^2.2.2"
"output-file-sync": "^2.0.1"
},
"devDependencies": {
"del": "^5.0.0"
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default SvgFile
exports[`cli should support custom file extension 1`] = `
Array [
"File.ts",
"index.ts",
]
`;

Expand All @@ -89,12 +90,14 @@ Array [
"KebabCase.js",
"MultipleDashes.js",
"PascalCase.js",
"index.js",
]
`;

exports[`cli should support different filename cases with directory output: --filename-case=camel 1`] = `
Array [
"camelCase.js",
"index.js",
"kebabCase.js",
"multipleDashes.js",
"pascalCase.js",
Expand All @@ -104,6 +107,7 @@ Array [
exports[`cli should support different filename cases with directory output: --filename-case=kebab 1`] = `
Array [
"camel-case.js",
"index.js",
"kebab-case.js",
"multiple-dashes.js",
"pascal-case.js",
Expand All @@ -116,6 +120,7 @@ Array [
"KebabCase.js",
"MultipleDashes.js",
"PascalCase.js",
"index.js",
]
`;

Expand Down Expand Up @@ -383,6 +388,7 @@ exports[`cli should transform a whole directory and output relative destination
__fixtures__/cased/multiple---dashes.svg -> __fixtures_build__/whole/cased/MultipleDashes.js
__fixtures__/complex/skype.svg -> __fixtures_build__/whole/complex/Skype.js
__fixtures__/complex/telegram.svg -> __fixtures_build__/whole/complex/Telegram.js
__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__/simple/file.svg -> __fixtures_build__/whole/simple/File.js
Expand Down
60 changes: 35 additions & 25 deletions packages/cli/src/dirCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { promisify } from 'util'
import path from 'path'
import chalk from 'chalk'
import outputFileSync from 'output-file-sync'
import readdir from 'recursive-readdir'
import { convertFile, stat, transformFilename, CASE, politeWrite } from './util'

const access = promisify(fs.access)
const readdir = promisify(fs.readdir)

async function exists(file) {
try {
Expand All @@ -34,51 +34,61 @@ export function isCompilable(filename) {
return COMPILABLE_EXTENSIONS.includes(ext)
}

async function dirCommand(
export default async function dirCommand(
program,
filenames,
{ ext = 'js', filenameCase = CASE.PASCAL, ...options },
) {
async function write(src, relative) {
if (!isCompilable(relative)) return
relative = rename(relative, ext, filenameCase)
async function write(src, dest) {
if (!isCompilable(src)) return null

const dest = path.resolve(program.outDir, relative)
dest = rename(dest, ext, filenameCase)
const code = await convertFile(src, options)
const cwdRelative = path.relative(process.cwd(), dest)
const logOutput = `${src} -> ${cwdRelative}\n`

if (program.ignoreExisting && (await exists(dest))) {
politeWrite(
program,
chalk.grey(`${src} -> ${path.relative(process.cwd(), dest)}\n`),
)
return
politeWrite(program, chalk.grey(logOutput))
return null
}

outputFileSync(dest, code)
politeWrite(
program,
chalk.white(`${src} -> ${path.relative(process.cwd(), dest)}\n`),
)
politeWrite(program, chalk.white(logOutput))
return dest
}

async function generateIndex(dest, files) {
const indexFile = path.join(dest, `index.${ext}`)
const exportEntries = files.map(file => {
const basename = path.basename(file, path.extname(file))
return `export { default as ${basename} } from './${basename}'`
})
fs.writeFileSync(indexFile, exportEntries.join('\n'))
}

async function handle(filename) {
async function handle(filename, root) {
const stats = await stat(filename)

if (stats.isDirectory(filename)) {
const dirname = filename
const files = await readdir(dirname)
await Promise.all(
files.map(async _filename => {
const relative = path.relative(dirname, _filename)
return write(_filename, relative)
const results = await Promise.all(
files.map(async relativeFile => {
const absFile = path.join(dirname, relativeFile)
return handle(absFile, root)
}),
)
} else {
await write(filename, filename)
const transformed = results.filter(Boolean)
if (transformed.length) {
const dest = path.resolve(program.outDir, path.relative(root, dirname))
await generateIndex(dest, transformed)
}
return null
}

const dest = path.resolve(program.outDir, path.relative(root, filename))
return write(filename, dest)
}

await Promise.all(filenames.map(handle))
await Promise.all(filenames.map(file => handle(file, file)))
}

export default dirCommand
9 changes: 1 addition & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7182,7 +7182,7 @@ minimalistic-crypto-utils@^1.0.0, minimalistic-crypto-utils@^1.0.1:
resolved "https://registry.yarnpkg.com/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a"
integrity sha1-9sAMHAsIIkblxNmd+4x8CDsrWCo=

minimatch@3.0.4, minimatch@^3.0.2, minimatch@^3.0.4:
minimatch@^3.0.2, minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==
Expand Down Expand Up @@ -9012,13 +9012,6 @@ realpath-native@^1.1.0:
dependencies:
util.promisify "^1.0.0"

recursive-readdir@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/recursive-readdir/-/recursive-readdir-2.2.2.tgz#9946fb3274e1628de6e36b2f6714953b4845094f"
integrity sha512-nRCcW9Sj7NuZwa2XvH9co8NPeXUBhZP7CRKJtU+cS6PW9FpCIFoI5ib0NT1ZrbNuPoRy0ylyCaUL8Gih4LSyFg==
dependencies:
minimatch "3.0.4"

redent@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/redent/-/redent-1.0.0.tgz#cf916ab1fd5f1f16dfb20822dd6ec7f730c2afde"
Expand Down

0 comments on commit 9d7fb34

Please sign in to comment.