@@ -4,6 +4,25 @@ import path from 'path';
44import chalk from 'chalk' ;
55import { isValidPackageName , isValidConfigAtPath } from '@codeshift/validator' ;
66
7+ const validPackageNameFormat =
8+ / ^ @ c o d e s h i f t \/ m o d ( - [ a - z A - Z 0 - 9 ] + ) * ( - (? ! _ _ ) [ a - z A - Z 0 - 9 ] + ) * ( _ _ ( [ a - z A - Z 0 - 9 ] + ( - [ a - z A - Z 0 - 9 ] + ) * ) ? ) ? $ / ;
9+
10+ function isValidPackageJson ( basePath : string ) {
11+ const pkgJsonPath = path . join ( basePath , 'package.json' ) ;
12+ const pkgJsonRaw = fs . readFileSync ( pkgJsonPath , {
13+ encoding : 'utf-8' ,
14+ } ) ;
15+ const pkgJson = JSON . parse ( pkgJsonRaw ) ;
16+
17+ if ( ! validPackageNameFormat . test ( pkgJson . name ) ) {
18+ throw new Error ( `Invalid package name: ${ pkgJson . name } in: ${ pkgJsonPath } .
19+ If this is a scoped package, please make sure rename the folder to use the "__" characters to denote submodule.
20+ For example: @codeshift/mod-foo__bar` ) ;
21+ }
22+
23+ return true ;
24+ }
25+
726async function main ( targetPath : string ) {
827 const directories = await fs . readdir ( targetPath ) ;
928
@@ -20,6 +39,7 @@ For example: @foo/bar => @foo__bar`,
2039
2140 const basePath = path . join ( __dirname , '..' , targetPath , dir ) ;
2241 await isValidConfigAtPath ( basePath ) ;
42+ await isValidPackageJson ( basePath ) ;
2343
2444 const subDirectories = await fs . readdir ( basePath ) ;
2545 subDirectories
@@ -40,7 +60,7 @@ For example: @foo/bar => @foo__bar`,
4060 } ) ;
4161}
4262
43- ( async function ( ) {
63+ ( async function ( ) {
4464 try {
4565 main ( process . argv [ 2 ] ) ;
4666 } catch ( error ) {
0 commit comments