|
1 | | -import fs, { lstatSync, existsSync } from 'fs-extra'; |
| 1 | +import fs from 'fs-extra'; |
2 | 2 | import semver from 'semver'; |
| 3 | +import * as recast from 'recast'; |
3 | 4 |
|
4 | 5 | const packageNameRegex = /^(@[a-z0-9-~][a-z0-9-._~]*__)?[a-z0-9-~][a-z0-9-._~]*$/; |
5 | 6 |
|
6 | | -async function main(path: string) { |
7 | | - const directories = await fs.readdir(path); |
8 | | - |
9 | | - directories.forEach(async dir => { |
10 | | - if (!dir.match(packageNameRegex)) { |
11 | | - throw new Error( |
12 | | - `Invalid package name: ${dir}. |
13 | | - If this is a scoped package, please make sure rename the folder to use the "__" characters to denote submodule. |
14 | | - For example: @atlaskit/avatar => @atlaskit__avatar`, |
15 | | - ); |
16 | | - } |
17 | | - |
18 | | - const subDirectories = await fs.readdir(`${path}/${dir}`); |
19 | | - let hasConfigFile = false; |
20 | | - |
21 | | - subDirectories.forEach(subDir => { |
22 | | - if (subDir === 'codeshift.config.js') { |
23 | | - hasConfigFile = true; |
24 | | - } |
| 7 | +export function isValidPackageName(dir: string) { |
| 8 | + return dir.match(packageNameRegex); |
| 9 | +} |
25 | 10 |
|
26 | | - if ( |
27 | | - lstatSync(`${path}/${dir}/${subDir}`).isDirectory() && |
28 | | - !semver.valid(subDir) |
29 | | - ) { |
30 | | - throw new Error( |
31 | | - `Codemod folder name "${subDir}" has an invalid version name. Please make sure the file name is valid semver. For example "18.0.0"`, |
32 | | - ); |
| 11 | +export async function isValidConfig(path: string) { |
| 12 | + const configPath = path + `/codeshift.config.js`; |
| 13 | + const source = await fs.readFile(configPath, 'utf8'); |
| 14 | + const ast = recast.parse(source); |
| 15 | + |
| 16 | + let hasTransforms = false; |
| 17 | + let invalidSemverIds = []; |
| 18 | + let transformCount = 0; |
| 19 | + |
| 20 | + recast.visit(ast, { |
| 21 | + visitProperty(path) { |
| 22 | + // @ts-ignore |
| 23 | + if (path.node.key.name === 'transforms') { |
| 24 | + hasTransforms = true; |
| 25 | + // @ts-ignore |
| 26 | + const properties = path.node.value.properties; |
| 27 | + transformCount = properties.length; |
| 28 | + // @ts-ignore |
| 29 | + properties.forEach(property => { |
| 30 | + if (!semver.valid(property.key.value)) { |
| 31 | + invalidSemverIds.push(property.key.value); |
| 32 | + } |
| 33 | + }); |
33 | 34 | } |
34 | 35 |
|
35 | | - if ( |
36 | | - lstatSync(`${path}/${dir}/${subDir}`).isDirectory() && |
37 | | - !existsSync(`${path}/${dir}/${subDir}/transform.ts`) && |
38 | | - !existsSync(`${path}/${dir}/${subDir}/transform.js`) |
39 | | - ) { |
40 | | - throw new Error( |
41 | | - `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`, |
42 | | - ); |
43 | | - } |
44 | | - }); |
45 | | - |
46 | | - if (!hasConfigFile) { |
47 | | - throw new Error( |
48 | | - `No config file found at: ${path}/${dir}. |
49 | | - Please create a config file named "codeshift.config.js"`, |
50 | | - ); |
51 | | - } |
| 36 | + return false; |
| 37 | + }, |
52 | 38 | }); |
| 39 | + |
| 40 | + if (!hasTransforms || !transformCount) { |
| 41 | + throw new Error( |
| 42 | + 'At least one transform should be specified for config at "${configPath}"', |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + if (invalidSemverIds.length) { |
| 47 | + throw new Error(`Invalid transform ids found for config at "${configPath}". |
| 48 | + Please make sure all transforms are identified by a valid semver version. ie 10.0.0`); |
| 49 | + } |
53 | 50 | } |
54 | 51 |
|
55 | | -main(process.argv[2]); |
| 52 | +export async function isValidPackageJson(path: string) { |
| 53 | + const packageJsonRaw = await fs.readFile(path + '/package.json', 'utf8'); |
| 54 | + const packageJson = JSON.parse(packageJsonRaw); |
| 55 | + |
| 56 | + if (!packageJson.name) { |
| 57 | + throw new Error('No package name provided in package.json'); |
| 58 | + } |
| 59 | + |
| 60 | + if (!packageJson.main) { |
| 61 | + throw new Error('No main entrypoint provided in package.json'); |
| 62 | + } |
| 63 | +} |
0 commit comments