|
1 | 1 | import fs, { lstatSync, existsSync } from 'fs-extra';
|
| 2 | +import junk from 'junk'; |
| 3 | +import path from 'path'; |
| 4 | +import chalk from 'chalk'; |
2 | 5 | import { isValidPackageName, isValidConfigAtPath } from '@codeshift/validator';
|
3 | 6 |
|
4 |
| -async function main(path: string) { |
5 |
| - const directories = await fs.readdir(path); |
| 7 | +async function main(targetPath: string) { |
| 8 | + const directories = await fs.readdir(targetPath); |
6 | 9 |
|
7 |
| - directories.forEach(async dir => { |
8 |
| - if (!isValidPackageName(dir)) { |
9 |
| - throw new Error( |
10 |
| - `Invalid package name: ${dir}. |
11 |
| - If this is a scoped package, please make sure rename the folder to use the "__" characters to denote submodule. |
12 |
| - For example: @foo/bar => @foo__bar`, |
13 |
| - ); |
14 |
| - } |
15 |
| - |
16 |
| - await isValidConfigAtPath(`${path}/${dir}`); |
17 |
| - |
18 |
| - const subDirectories = await fs.readdir(`${path}/${dir}`); |
19 |
| - subDirectories.forEach(async subDir => { |
20 |
| - if ( |
21 |
| - lstatSync(`${path}/${dir}/${subDir}`).isDirectory() && |
22 |
| - !existsSync(`${path}/${dir}/${subDir}/transform.ts`) && |
23 |
| - !existsSync(`${path}/${dir}/${subDir}/transform.js`) |
24 |
| - ) { |
| 10 | + directories |
| 11 | + .filter(dir => !junk.is(dir)) |
| 12 | + .forEach(async dir => { |
| 13 | + if (!isValidPackageName(dir)) { |
25 | 14 | throw new Error(
|
26 |
| - `Unable to find transform entry-point for directory "${path}/${dir}/${subDir}". Please ensure you have a valid transform.(ts|js) file containing the entry-point for your codemod`, |
| 15 | + `Invalid package name: ${dir}. |
| 16 | +If this is a scoped package, please make sure rename the folder to use the "__" characters to denote submodule. |
| 17 | +For example: @foo/bar => @foo__bar`, |
27 | 18 | );
|
28 | 19 | }
|
| 20 | + |
| 21 | + const basePath = path.join(__dirname, '..', targetPath, dir); |
| 22 | + await isValidConfigAtPath(basePath); |
| 23 | + |
| 24 | + const subDirectories = await fs.readdir(basePath); |
| 25 | + subDirectories |
| 26 | + .filter(dir => !junk.is(dir)) |
| 27 | + .forEach(async subDir => { |
| 28 | + const subPath = path.join(basePath, subDir); |
| 29 | + |
| 30 | + if ( |
| 31 | + lstatSync(subPath).isDirectory() && |
| 32 | + !existsSync(path.join(subPath, 'transform.ts')) && |
| 33 | + !existsSync(path.join(subPath, 'transform.js')) |
| 34 | + ) { |
| 35 | + throw new Error( |
| 36 | + `Unable to find transform entry-point for directory "${subPath}". Please ensure you have a valid transform.(ts|js) file containing the entry-point for your codemod`, |
| 37 | + ); |
| 38 | + } |
| 39 | + }); |
29 | 40 | });
|
30 |
| - }); |
31 | 41 | }
|
32 | 42 |
|
33 |
| -main(process.argv[2]); |
| 43 | +(async function() { |
| 44 | + try { |
| 45 | + main(process.argv[2]); |
| 46 | + } catch (error) { |
| 47 | + console.error(chalk.red(error)); |
| 48 | + process.exit(1); |
| 49 | + } |
| 50 | +})(); |
0 commit comments