Skip to content

Commit c0b678e

Browse files
Merge pull request #102 from CodeshiftCommunity/build-validator
update transform files into valid configs
2 parents 3c390c1 + 78959ae commit c0b678e

File tree

27 files changed

+112
-77
lines changed

27 files changed

+112
-77
lines changed

.changeset/large-candles-appear.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@codeshift/initializer': minor
3+
'@codeshift/validator': minor
4+
---
5+
6+
Updates validation / init logic to be consistent across packages

community/@atlaskit__avatar/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'18.0.0': require.resolve('./18.0.0/transform'),

community/@atlaskit__breadcrumbs/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'11.0.0': require.resolve('./11.0.0/transform'),

community/@atlaskit__button/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'15.0.0': require.resolve('./15.0.0/transform'),

community/@atlaskit__calendar/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'11.0.0': require.resolve('./11.0.0/transform'),

community/@atlaskit__checkbox/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'12.0.0': require.resolve('./12.0.0/transform'),

community/@atlaskit__icon/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'21.2.0': require.resolve('./21.2.0/transform'),

community/@atlaskit__menu/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'0.7.0': require.resolve('./0.7.0/transform'),

community/@atlaskit__popper/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'5.0.0': require.resolve('./5.0.0/transform'),

community/@atlaskit__popup/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'1.0.0': require.resolve('./1.0.0/transform'),

community/@atlaskit__progress-indicator/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'9.0.0': require.resolve('./9.0.0/transform'),

community/@atlaskit__range/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'4.0.0': require.resolve('./4.0.0/transform'),

community/@atlaskit__section-message/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'6.0.0': require.resolve('./6.0.0/transform'),

community/@atlaskit__side-navigation/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'0.8.0': require.resolve('./0.8.0/transform'),

community/@atlaskit__spinner/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'13.0.0': require.resolve('./13.0.0/transform'),

community/@atlaskit__tag/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'11.0.0': require.resolve('./11.0.0/transform'),

community/@atlaskit__textarea/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'4.0.0': require.resolve('./4.0.0/transform'),

community/@atlaskit__textfield/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'5.0.0': require.resolve('./5.0.0/transform'),

community/@atlaskit__toggle/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'11.0.0': require.resolve('./11.0.0/transform'),

community/@emotion__monorepo/codeshift.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['danieldelcore'],
33
transforms: {
44
'11.0.0': require.resolve('./11.0.0/transform'),
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
module.exports = {
22
maintainers: ['alexreardon'],
33
transforms: { '5.0.0': require.resolve('./5.0.0/transform') },
44
};

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"jest-serializer-html-string": "^1.0.1",
5454
"jest-watch-typeahead": "^0.4.2",
5555
"jscodeshift": "^0.13.1",
56+
"junk": "^3.1.0",
5657
"prettier": "^1.16.4",
5758
"rimraf": "^2.6.3",
5859
"semver": "^7.3.5",
@@ -61,7 +62,7 @@
6162
"ts-loader": "^6.2.1",
6263
"ts-node": "^9.1.1",
6364
"tslib": "^2.2.0",
64-
"typescript": "^4.1.3"
65+
"typescript": "^4.3.5"
6566
},
6667
"resolutions": {
6768
"colors": "1.4.0"

packages/validator/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function isValidConfigAtPath(filePath: string) {
3131
const config = await fetchConfig(filePath);
3232

3333
if (!config) {
34-
throw new Error('Unable to locate config file');
34+
throw new Error(`Unable to locate config file at path: ${filePath}`);
3535
}
3636

3737
if (!hasValidTransforms(config)) {

scripts/docs.ts

+27-26
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
import fs from 'fs-extra';
2+
import junk from 'junk';
3+
import path from 'path';
24

3-
const COMMUNITY_PATH = `${__dirname}/../community`;
4-
const DOCS_PATH = `${__dirname}/../website/docs/registry`;
5+
import { CodeshiftConfig } from '@codeshift/types';
6+
import { fetchConfig } from '@codeshift/fetcher';
57

6-
function cleanTargetDir(path: string) {
7-
if (fs.existsSync(path)) fs.emptyDirSync(path);
8-
}
8+
const COMMUNITY_PATH = path.join(__dirname, '..', 'community');
9+
const DOCS_PATH = path.join(__dirname, '..', 'website', 'docs', 'registry');
910

10-
interface Config {
11-
maintainers: string[];
12-
transforms: {
13-
[key: string]: any;
14-
};
15-
presets: {
16-
[key: string]: any;
17-
};
11+
function cleanTargetDir(targetPath: string) {
12+
if (fs.existsSync(targetPath)) fs.emptyDirSync(targetPath);
1813
}
1914

2015
interface DocsData {
2116
name: string;
22-
config: Config;
17+
config: CodeshiftConfig;
2318
}
2419

25-
function main() {
20+
async function main() {
2621
const communityCodemods = fs.readdirSync(COMMUNITY_PATH);
2722
const data: DocsData[] = [];
23+
const directories = communityCodemods.filter(dir => junk.not(dir));
2824

29-
communityCodemods.forEach(dir => {
30-
// eslint-disable-next-line @typescript-eslint/no-var-requires
31-
const config = require(`${COMMUNITY_PATH}/${dir}/codeshift.config.js`)
32-
.default;
25+
for (const dir of directories) {
26+
const config = await fetchConfig(path.join(COMMUNITY_PATH, dir));
3327

34-
data.push({
35-
name: dir,
36-
config,
37-
});
38-
});
28+
if (!config) {
29+
throw new Error(`Unable to locate config for path: ${dir}`);
30+
}
31+
32+
data.push({ name: dir, config });
33+
}
3934

4035
cleanTargetDir(DOCS_PATH);
4136

@@ -48,7 +43,7 @@ function main() {
4843
const packageLink = `[${rawName}](https://www.npmjs.com/package/${rawName})`;
4944

5045
fs.outputFileSync(
51-
`${DOCS_PATH}/${name}.mdx`,
46+
path.join(DOCS_PATH, `${name}.mdx`),
5247
`---
5348
id: ${safeName}
5449
title: ${safeName.replace('__', '/')}
@@ -58,10 +53,13 @@ slug: /${safeName}
5853
**Target package:** ${packageLink}
5954
6055
**Maintainers:**
61-
${config.maintainers.map(
56+
${config.maintainers!.map(
6257
maintainer => `- [${maintainer}](https://github.com/${maintainer})`,
6358
)}
6459
60+
${
61+
config.transforms
62+
? `
6563
## Transforms
6664
6765
${Object.keys(config.transforms)
@@ -80,6 +78,9 @@ $ codeshift --packages ${name}@${key} path/to/source
8078
`,
8179
)
8280
.join('')}
81+
`
82+
: ''
83+
}
8384
8485
${
8586
config.presets

scripts/initialize.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export function main(packageName: string, transform?: string) {
77
if (!transform) throw new Error('Version was not provided');
88

99
if (transform) {
10-
initDirectory(packageName, targetPath);
11-
initTransform(packageName, transform, 'version', targetPath);
10+
initDirectory(packageName, targetPath, true);
11+
initTransform(packageName, transform, 'version', targetPath, true);
1212
}
1313

1414
console.log(

scripts/validate.ts

+40-23
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,50 @@
11
import fs, { lstatSync, existsSync } from 'fs-extra';
2+
import junk from 'junk';
3+
import path from 'path';
4+
import chalk from 'chalk';
25
import { isValidPackageName, isValidConfigAtPath } from '@codeshift/validator';
36

4-
async function main(path: string) {
5-
const directories = await fs.readdir(path);
7+
async function main(targetPath: string) {
8+
const directories = await fs.readdir(targetPath);
69

7-
directories.forEach(async dir => {
8-
if (!isValidPackageName(dir)) {
9-
throw new Error(
10-
`Invalid package name: ${dir}.
11-
If this is a scoped package, please make sure rename the folder to use the "__" characters to denote submodule.
12-
For example: @foo/bar => @foo__bar`,
13-
);
14-
}
15-
16-
await isValidConfigAtPath(`${path}/${dir}`);
17-
18-
const subDirectories = await fs.readdir(`${path}/${dir}`);
19-
subDirectories.forEach(async subDir => {
20-
if (
21-
lstatSync(`${path}/${dir}/${subDir}`).isDirectory() &&
22-
!existsSync(`${path}/${dir}/${subDir}/transform.ts`) &&
23-
!existsSync(`${path}/${dir}/${subDir}/transform.js`)
24-
) {
10+
directories
11+
.filter(dir => !junk.is(dir))
12+
.forEach(async dir => {
13+
if (!isValidPackageName(dir)) {
2514
throw new Error(
26-
`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`,
15+
`Invalid package name: ${dir}.
16+
If this is a scoped package, please make sure rename the folder to use the "__" characters to denote submodule.
17+
For example: @foo/bar => @foo__bar`,
2718
);
2819
}
20+
21+
const basePath = path.join(__dirname, '..', targetPath, dir);
22+
await isValidConfigAtPath(basePath);
23+
24+
const subDirectories = await fs.readdir(basePath);
25+
subDirectories
26+
.filter(dir => !junk.is(dir))
27+
.forEach(async subDir => {
28+
const subPath = path.join(basePath, subDir);
29+
30+
if (
31+
lstatSync(subPath).isDirectory() &&
32+
!existsSync(path.join(subPath, 'transform.ts')) &&
33+
!existsSync(path.join(subPath, 'transform.js'))
34+
) {
35+
throw new Error(
36+
`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`,
37+
);
38+
}
39+
});
2940
});
30-
});
3141
}
3242

33-
main(process.argv[2]);
43+
(async function() {
44+
try {
45+
main(process.argv[2]);
46+
} catch (error) {
47+
console.error(chalk.red(error));
48+
process.exit(1);
49+
}
50+
})();

yarn.lock

+14-4
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,11 @@
17671767
resolved "https://registry.npmjs.org/@types/node/-/node-12.20.21.tgz#575e91f59c2e79318c2d39a48286c6954e484fd5"
17681768
integrity sha512-Qk7rOvV2A4vNgXNS88vEvbJE1NDFPCQ8AU+pNElrU2bA4yrRDef3fg3SUe+xkwyin3Bpg/Xh5JkNWTlsOcS2tA==
17691769

1770+
"@types/node@^16.11.0":
1771+
version "16.11.21"
1772+
resolved "https://packages.atlassian.com/api/npm/npm-remote/@types/node/-/node-16.11.21.tgz#474d7589a30afcf5291f59bd49cca9ad171ffde4"
1773+
integrity sha512-Pf8M1XD9i1ksZEcCP8vuSNwooJ/bZapNmIzpmsMaL+jMI+8mEYU3PKvs+xDNuQcJWF/x24WzY4qxLtB0zNow9A==
1774+
17701775
"@types/normalize-package-data@^2.4.0":
17711776
version "2.4.1"
17721777
resolved "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz#d3357479a0fdfdd5907fe67e17e0a85c906e1301"
@@ -4943,6 +4948,11 @@ jsprim@^1.2.2:
49434948
json-schema "0.2.3"
49444949
verror "1.10.0"
49454950

4951+
junk@^3.1.0:
4952+
version "3.1.0"
4953+
resolved "https://packages.atlassian.com/api/npm/npm-remote/junk/-/junk-3.1.0.tgz#31499098d902b7e98c5d9b9c80f43457a88abfa1"
4954+
integrity sha512-pBxcB3LFc8QVgdggvZWyeys+hnrNWg4OcZIU/1X59k5jQdLBlCsYGRQaz234SqoRLTCgMH00fY0xRJH+F9METQ==
4955+
49464956
kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
49474957
version "3.2.2"
49484958
resolved "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
@@ -7179,10 +7189,10 @@ typedarray@^0.0.6:
71797189
resolved "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
71807190
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
71817191

7182-
typescript@^4.1.3:
7183-
version "4.4.2"
7184-
resolved "https://registry.npmjs.org/typescript/-/typescript-4.4.2.tgz#6d618640d430e3569a1dfb44f7d7e600ced3ee86"
7185-
integrity sha512-gzP+t5W4hdy4c+68bfcv0t400HVJMMd2+H9B7gae1nQlBzCqvrXX+6GL/b3GAgyTH966pzrZ70/fRjwAtZksSQ==
7192+
typescript@^4.3.5:
7193+
version "4.5.5"
7194+
resolved "https://packages.atlassian.com/api/npm/npm-remote/typescript/-/typescript-4.5.5.tgz#d8c953832d28924a9e3d37c73d729c846c5896f3"
7195+
integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==
71867196

71877197
unicode-canonical-property-names-ecmascript@^1.0.4:
71887198
version "1.0.4"

0 commit comments

Comments
 (0)