Skip to content

Commit efdca0c

Browse files
author
Daniel Del Core
committed
adds package.json validation to community folder
1 parent a2e76ab commit efdca0c

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

scripts/validate.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ import path from 'path';
44
import chalk from 'chalk';
55
import { isValidPackageName, isValidConfigAtPath } from '@codeshift/validator';
66

7+
const validPackageNameFormat =
8+
/^@codeshift\/mod(-[a-zA-Z0-9]+)*(-(?!__)[a-zA-Z0-9]+)*(__([a-zA-Z0-9]+(-[a-zA-Z0-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+
726
async 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

Comments
 (0)