|
| 1 | +// import fs from 'fs'; |
| 2 | + |
| 3 | +import yargs from 'yargs' |
| 4 | + |
| 5 | +// import generateIndexFromFiles from './generateIndexFromFiles' |
| 6 | +// import generateIndexFromFolders from './generateIndexFromFolders' |
| 7 | + |
| 8 | +// import { |
| 9 | +// sourceFolder, |
| 10 | +// } from './utils' |
| 11 | + |
| 12 | +export function runCli() { |
| 13 | + const argv = yargs(process.argv.slice(2)) |
| 14 | + .option('from', { |
| 15 | + alias: 'from', |
| 16 | + demandOption: false, |
| 17 | + default: 'folder', |
| 18 | + describe: 'specify if you want to use folders or files to generate the index file.', |
| 19 | + type: 'string', |
| 20 | + choices: ['files', 'folder'], |
| 21 | + }) |
| 22 | + .argv |
| 23 | + |
| 24 | + const sourceFolder = argv._[0]; |
| 25 | + |
| 26 | + console.log({ sourceFolder }) |
| 27 | + |
| 28 | + console.log({ argv }) |
| 29 | + |
| 30 | +// const content = |
| 31 | +// argvs[0] === '--from' && argvs[1] === 'files' |
| 32 | +// ? generateIndexFromFiles() |
| 33 | +// : generateIndexFromFolders(); |
| 34 | + |
| 35 | +// fs.writeFile(indexFilePath, content, err => { |
| 36 | +// if (err) { |
| 37 | +// console.error(err); |
| 38 | +// return; |
| 39 | +// } |
| 40 | +// console.log('> Successfully to generate the index file.'); |
| 41 | +// }); |
| 42 | + |
| 43 | +} |
| 44 | + |
| 45 | +// export const mkdir = promisify(fs.mkdir) |
| 46 | +// export const writeFile = promisify(fs.writeFile) |
| 47 | +// export const readFile = promisify(fs.readFile); |
| 48 | +// export const readdir = promisify(fs.readdir) |
| 49 | + |
| 50 | +// export function generateIndexFromFolders(srcFolder: string) { |
| 51 | +// const fileList = getFileList(srcFolder); |
| 52 | +// const folders = fileList.filter(isDirectory).filter(folderIncludesIndexFile); |
| 53 | + |
| 54 | +// const indexContentArr = folders.map((currentFolder: string) => { |
| 55 | +// const files = fs.readdirSync(path.join(sourceFolder, currentFolder)); |
| 56 | + |
| 57 | +// // the folder can have an index.ts or an index.tsx |
| 58 | +// const hasIndexWithTsExtension = files.includes('index.ts'); |
| 59 | +// const fileContent = fs.readFileSync( |
| 60 | +// path.join( |
| 61 | +// sourceFolder, |
| 62 | +// `${currentFolder}/index.${hasIndexWithTsExtension ? 'ts' : 'tsx'}`, |
| 63 | +// ), |
| 64 | +// 'utf8', |
| 65 | +// ); |
| 66 | + |
| 67 | +// const hasExportDefault = fileContent.includes('export { default } from'); |
| 68 | + |
| 69 | +// return hasExportDefault |
| 70 | +// ? `export { default as ${currentFolder} } from './${currentFolder}';\n` |
| 71 | +// : `export * as ${currentFolder} from './${currentFolder}';\n`; |
| 72 | +// }); |
| 73 | + |
| 74 | +// return indexContentArr.join(''); |
| 75 | +// } |
| 76 | + |
| 77 | +// export function generateIndexFromFiles(srcFolder: string) { |
| 78 | +// const fileList = getFileList(srcFolder); |
| 79 | +// const files = fileList |
| 80 | +// .filter(isFile) |
| 81 | +// .filter(f => f !== 'index.ts' && f !== 'index.tsx'); |
| 82 | + |
| 83 | +// const indexContentArr = files.map((file: string) => { |
| 84 | +// const name = file.split('.tsx')[0]; |
| 85 | + |
| 86 | +// return `export { default as ${name} } from './${name}';\n`; |
| 87 | +// }); |
| 88 | + |
| 89 | +// return indexContentArr.join(''); |
| 90 | +// } |
| 91 | + |
| 92 | +// const content = |
| 93 | +// argvs[0] === '--from' && argvs[1] === 'files' |
| 94 | +// ? generateIndexFromFiles() |
| 95 | +// : generateIndexFromFolders(); |
| 96 | + |
| 97 | +// fs.writeFile(indexFilePath, content, err => { |
| 98 | +// if (err) { |
| 99 | +// console.error(err); |
| 100 | +// return; |
| 101 | +// } |
| 102 | +// console.log('> Successfully to generate the index file.'); |
| 103 | +// }); |
0 commit comments