|
1 | 1 | import fs from 'fs-extra'; |
2 | 2 | import semver from 'semver'; |
3 | 3 | import path from 'path'; |
| 4 | +import { CodeshiftConfig } from '@codeshift/types'; |
4 | 5 |
|
5 | | -export function isValidPackageName(dir: string) { |
6 | | - return dir.match(/^(@[a-z0-9-~][a-z0-9-._~]*__)?[a-z0-9-~][a-z0-9-._~]*$/); |
7 | | -} |
8 | | - |
9 | | -export async function isValidConfig(filePath: string) { |
| 6 | +function getConfigFromPath(filePath: string): CodeshiftConfig { |
10 | 7 | const configPath = path.join(process.cwd(), filePath, 'codeshift.config.js'); |
11 | 8 | // eslint-disable-next-line @typescript-eslint/no-var-requires |
12 | | - let config = require(configPath); |
| 9 | + const config = require(configPath); |
| 10 | + |
| 11 | + return !!config.default ? config.default : config; |
| 12 | +} |
13 | 13 |
|
14 | | - config = !!config.default ? config.default : config; |
| 14 | +function hasValidTransforms(transforms?: Record<string, string>) { |
| 15 | + if (!transforms || !Object.keys(transforms).length) return false; |
15 | 16 |
|
16 | | - const invalidSemverIds = []; |
17 | | - const invalidPresetIds = []; |
| 17 | + let isValid = true; |
18 | 18 |
|
19 | | - let hasTransforms = false; |
| 19 | + Object.entries(transforms).forEach(([key]) => { |
| 20 | + if (!semver.valid(key)) isValid = false; |
| 21 | + }); |
20 | 22 |
|
21 | | - if (config.transforms && Object.keys(config.transforms).length) { |
22 | | - Object.entries(config.transforms).forEach(([key]) => { |
23 | | - hasTransforms = true; |
24 | | - if (!semver.valid(key)) invalidSemverIds.push(key); |
25 | | - }); |
26 | | - } |
| 23 | + return isValid; |
| 24 | +} |
27 | 25 |
|
28 | | - if (config.presets && Object.keys(config.presets).length) { |
29 | | - hasTransforms = true; |
30 | | - Object.entries(config.presets).forEach(([key]) => { |
31 | | - if (key.includes(' ')) invalidPresetIds.push(key); |
32 | | - }); |
33 | | - } |
| 26 | +function hasValidPresets(presets?: Record<string, string>) { |
| 27 | + if (!presets || !Object.keys(presets).length) return false; |
| 28 | + |
| 29 | + let isValid = true; |
| 30 | + |
| 31 | + Object.entries(presets).forEach(([key]) => { |
| 32 | + if (!key.match(/^[0-9a-zA-Z\-]+$/)) isValid = false; |
| 33 | + }); |
| 34 | + |
| 35 | + return isValid; |
| 36 | +} |
| 37 | + |
| 38 | +export function isValidPackageName(dir: string) { |
| 39 | + return dir.match(/^(@[a-z0-9-~][a-z0-9-._~]*__)?[a-z0-9-~][a-z0-9-._~]*$/); |
| 40 | +} |
| 41 | + |
| 42 | +export async function isValidConfig(config: CodeshiftConfig) { |
| 43 | + return ( |
| 44 | + hasValidTransforms(config.transforms) || hasValidPresets(config.presets) |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +export async function isValidConfigAtPath(filePath: string) { |
| 49 | + const config = getConfigFromPath(filePath); |
34 | 50 |
|
35 | | - if (!hasTransforms) { |
| 51 | + if ( |
| 52 | + !hasValidTransforms(config.transforms) && |
| 53 | + !hasValidPresets(config.presets) |
| 54 | + ) { |
36 | 55 | throw new Error( |
37 | | - `At least one transform should be specified for config at "${configPath}"`, |
| 56 | + `At least one transform should be specified for config at "${filePath}"`, |
38 | 57 | ); |
39 | 58 | } |
40 | 59 |
|
41 | | - if (invalidSemverIds.length) { |
42 | | - throw new Error(`Invalid transform ids found for config at "${configPath}". |
| 60 | + if (!hasValidTransforms(config.transforms)) { |
| 61 | + throw new Error(`Invalid transform ids found for config at "${filePath}". |
43 | 62 | Please make sure all transforms are identified by a valid semver version. ie 10.0.0`); |
44 | 63 | } |
45 | 64 |
|
46 | | - if (invalidPresetIds.length) { |
47 | | - throw new Error(`Invalid preset ids found for config at "${configPath}". |
| 65 | + if (!hasValidPresets(config.presets)) { |
| 66 | + throw new Error(`Invalid preset ids found for config at "${filePath}". |
48 | 67 | Please make sure all presets are kebab case and contain no spaces or special characters. ie sort-imports-by-scope`); |
49 | 68 | } |
50 | 69 | } |
|
0 commit comments