@@ -4,10 +4,10 @@ import { promisify } from 'util'
44import path from 'path'
55import chalk from 'chalk'
66import outputFileSync from 'output-file-sync'
7- import readdir from 'recursive-readdir'
87import { convertFile , stat , transformFilename , CASE , politeWrite } from './util'
98
109const access = promisify ( fs . access )
10+ const readdir = promisify ( fs . readdir )
1111
1212async function exists ( file ) {
1313 try {
@@ -34,51 +34,61 @@ export function isCompilable(filename) {
3434 return COMPILABLE_EXTENSIONS . includes ( ext )
3535}
3636
37- async function dirCommand (
37+ export default async function dirCommand (
3838 program ,
3939 filenames ,
4040 { ext = 'js' , filenameCase = CASE . PASCAL , ...options } ,
4141) {
42- async function write ( src , relative ) {
43- if ( ! isCompilable ( relative ) ) return
44- relative = rename ( relative , ext , filenameCase )
42+ async function write ( src , dest ) {
43+ if ( ! isCompilable ( src ) ) return null
4544
46- const dest = path . resolve ( program . outDir , relative )
45+ dest = rename ( dest , ext , filenameCase )
4746 const code = await convertFile ( src , options )
47+ const cwdRelative = path . relative ( process . cwd ( ) , dest )
48+ const logOutput = `${ src } -> ${ cwdRelative } \n`
4849
4950 if ( program . ignoreExisting && ( await exists ( dest ) ) ) {
50- politeWrite (
51- program ,
52- chalk . grey ( `${ src } -> ${ path . relative ( process . cwd ( ) , dest ) } \n` ) ,
53- )
54- return
51+ politeWrite ( program , chalk . grey ( logOutput ) )
52+ return null
5553 }
5654
5755 outputFileSync ( dest , code )
58- politeWrite (
59- program ,
60- chalk . white ( `${ src } -> ${ path . relative ( process . cwd ( ) , dest ) } \n` ) ,
61- )
56+ politeWrite ( program , chalk . white ( logOutput ) )
57+ return dest
58+ }
59+
60+ async function generateIndex ( dest , files ) {
61+ const indexFile = path . join ( dest , `index.${ ext } ` )
62+ const exportEntries = files . map ( file => {
63+ const basename = path . basename ( file , path . extname ( file ) )
64+ return `export { default as ${ basename } } from './${ basename } '`
65+ } )
66+ fs . writeFileSync ( indexFile , exportEntries . join ( '\n' ) )
6267 }
6368
64- async function handle ( filename ) {
69+ async function handle ( filename , root ) {
6570 const stats = await stat ( filename )
6671
6772 if ( stats . isDirectory ( filename ) ) {
6873 const dirname = filename
6974 const files = await readdir ( dirname )
70- await Promise . all (
71- files . map ( async _filename => {
72- const relative = path . relative ( dirname , _filename )
73- return write ( _filename , relative )
75+ const results = await Promise . all (
76+ files . map ( async relativeFile => {
77+ const absFile = path . join ( dirname , relativeFile )
78+ return handle ( absFile , root )
7479 } ) ,
7580 )
76- } else {
77- await write ( filename , filename )
81+ const transformed = results . filter ( Boolean )
82+ if ( transformed . length ) {
83+ const dest = path . resolve ( program . outDir , path . relative ( root , dirname ) )
84+ await generateIndex ( dest , transformed )
85+ }
86+ return null
7887 }
88+
89+ const dest = path . resolve ( program . outDir , path . relative ( root , filename ) )
90+ return write ( filename , dest )
7991 }
8092
81- await Promise . all ( filenames . map ( handle ) )
93+ await Promise . all ( filenames . map ( file => handle ( file , file ) ) )
8294}
83-
84- export default dirCommand
0 commit comments