-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathvalidate.ts
76 lines (65 loc) · 2.49 KB
/
validate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import fs, { lstatSync, existsSync } from 'fs-extra';
import junk from 'junk';
import path from 'path';
import chalk from 'chalk';
import { isValidPackageName, isValidConfigAtPath } from '@hypermod/validator';
const validPackageNameFormat =
/^@hypermod\/mod(-[a-zA-Z0-9]+)*(-(?!__)[a-zA-Z0-9]+)*(__([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*)?)?$/;
function isValidPackageJson(basePath: string) {
const pkgJsonPath = path.join(basePath, 'package.json');
const pkgJsonRaw = fs.readFileSync(pkgJsonPath, {
encoding: 'utf-8',
});
const pkgJson = JSON.parse(pkgJsonRaw);
if (!validPackageNameFormat.test(pkgJson.name)) {
throw new Error(`Invalid package name: ${pkgJson.name} in: ${pkgJsonPath}.
If this is a scoped package, please make sure rename the folder to use the "__" characters to denote submodule.
For example: @hypermod/mod-foo__bar`);
}
if (pkgJson.name === 'dist/hypermod.config.js') {
throw new Error(`Invalid package entry point for package: ${pkgJson.name} in: ${pkgJsonPath}.
'main' should always point to 'dist/hypermod.config.js'`);
}
return true;
}
async function main(targetPath: string) {
const directories = await fs.readdir(targetPath);
directories
.filter(dir => !junk.is(dir))
.forEach(async dir => {
if (!isValidPackageName(dir)) {
throw new Error(
`Invalid package name: ${dir}.
If this is a scoped package, please make sure rename the folder to use the "__" characters to denote submodule.
For example: @foo/bar => @foo__bar`,
);
}
const basePath = path.join(__dirname, '..', targetPath, dir);
const sourcePath = path.join(basePath, 'src');
await isValidConfigAtPath(basePath);
await isValidPackageJson(basePath);
const subDirectories = await fs.readdir(sourcePath);
subDirectories
.filter(dir => !junk.is(dir))
.forEach(async subDir => {
const subPath = path.join(sourcePath, subDir);
if (
lstatSync(subPath).isDirectory() &&
!existsSync(path.join(subPath, 'transform.ts')) &&
!existsSync(path.join(subPath, 'transform.js'))
) {
throw new Error(
`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`,
);
}
});
});
}
(async function () {
try {
main(process.argv[2]);
} catch (error) {
console.error(chalk.red(error));
process.exit(1);
}
})();