Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): fix index generation #443

Merged
merged 1 commit into from
Apr 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions packages/cli/src/dirCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function isCompilable(filename) {
}

function defaultIndexTemplate(filePaths) {
const exportEntries = filePaths.map(filePath => {
const exportEntries = filePaths.map((filePath) => {
const basename = path.basename(filePath, path.extname(filePath))
const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename
return `export { default as ${exportName} } from './${basename}'`
Expand All @@ -54,7 +54,7 @@ export default async function dirCommand(
{ ext, filenameCase = CASE.PASCAL, ...options },
) {
async function write(src, dest) {
if (!isCompilable(src)) return null
if (!isCompilable(src)) return { transformed: false, dest: null }

ext = ext || getDefaultExtension(options)
dest = rename(dest, ext, filenameCase)
Expand All @@ -64,12 +64,12 @@ export default async function dirCommand(

if (program.ignoreExisting && (await exists(dest))) {
politeWrite(program, chalk.grey(logOutput))
return null
return { transformed: false, dest }
}

outputFileSync(dest, code)
politeWrite(program, chalk.white(logOutput))
return dest
return { transformed: true, dest }
}

async function generateIndex(dest, files) {
Expand All @@ -86,28 +86,29 @@ export default async function dirCommand(
const dirname = filename
const files = await readdir(dirname)
const results = await Promise.all(
files.map(async relativeFile => {
files.map(async (relativeFile) => {
const absFile = path.join(dirname, relativeFile)
return handle(absFile, root)
}),
)
const transformed = results.filter(Boolean)
const transformed = results.filter((result) => result.transformed)
if (transformed.length) {
const destFiles = results.map((result) => result.dest).filter(Boolean)
const dest = path.resolve(program.outDir, path.relative(root, dirname))
await generateIndex(dest, transformed)
await generateIndex(dest, destFiles)
}
return null
return { transformed: false, dest: null }
}

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

await Promise.all(
filenames.map(async file => {
filenames.map(async (file) => {
const stats = await stat(file)
const root = stats.isDirectory() ? file : path.dirname(file)
return handle(file, root)
await handle(file, root)
}),
)
}