Skip to content

Commit f42be01

Browse files
committed
🔨 Refactors init step to enable external building
1 parent 9e876c1 commit f42be01

File tree

10 files changed

+130
-75
lines changed

10 files changed

+130
-75
lines changed

packages/initializer/src/index.ts

Lines changed: 94 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,89 @@ import fs from 'fs-extra';
22
import semver from 'semver';
33
import * as recast from 'recast';
44

5+
import { version as utilsVersion } from '../../utils/package.json';
6+
import { version as testUtilsVersion } from '../../test-utils/package.json';
7+
8+
function getPackageJson(packageName: string) {
9+
return `{
10+
"name": "${packageName}",
11+
"version": "0.0.1",
12+
"license": "MIT",
13+
"main": "dist/codeshift.config.js",
14+
"scripts": {
15+
"build": "tsc --build",
16+
"test": "jest"
17+
},
18+
"dependencies": {
19+
"@codeshift/utils": "^${utilsVersion}"
20+
},
21+
"devDependencies": {
22+
"@codeshift/test-utils": "^${testUtilsVersion}",
23+
"@types/jest": "^26.0.15",
24+
"jest": "^26.6.0",
25+
"jscodeshift": "^0.12.0",
26+
"prettier": "^1.16.4",
27+
"ts-jest": "^26.4.4",
28+
"typescript": "^4.3.5"
29+
}
30+
}`;
31+
}
32+
33+
function getConfig(packageName: string, version: string) {
34+
return `export default {
35+
maintainers: [],
36+
target: [],
37+
description: 'Codemods for ${packageName}',
38+
transforms: {
39+
'${version}': require('./${version}/transform'),
40+
},
41+
presets: {},
42+
};
43+
`;
44+
}
45+
46+
function updateConfig(path: string, packageName: string, version: string) {
47+
const source = fs.readFileSync(path, 'utf8');
48+
const ast = recast.parse(source);
49+
const b = recast.types.builders;
50+
51+
recast.visit(ast, {
52+
visitProperty(path) {
53+
// @ts-ignore
54+
if (path.node.key.name !== 'transforms') return false;
55+
// @ts-ignore
56+
const properties = path.node.value.properties;
57+
// @ts-ignore
58+
properties.forEach(property => {
59+
if (semver.eq(property.key.value, version)) {
60+
throw new Error(
61+
`Transform for ${packageName} version ${version} already exists`,
62+
);
63+
}
64+
});
65+
66+
properties.push(
67+
b.property(
68+
'init',
69+
b.stringLiteral(version),
70+
b.callExpression(b.identifier('require'), [
71+
b.stringLiteral(`./${version}/transform`),
72+
]),
73+
),
74+
);
75+
76+
return false;
77+
},
78+
});
79+
80+
return recast.prettyPrint(ast, { quote: 'single', trailingComma: true }).code;
81+
}
82+
583
export function initDirectory(
684
packageName: string,
785
version: string,
886
targetPath: string = './',
87+
isReduced: boolean = false,
988
) {
1089
if (!semver.valid(version)) {
1190
throw new Error(
@@ -14,22 +93,17 @@ export function initDirectory(
1493
}
1594

1695
const basePath = `${targetPath}/${packageName.replace('/', '__')}`;
17-
const codemodPath = `${basePath}/${version}`;
18-
const configPath = `${basePath}/codeshift.config.js`;
19-
const packagePath = `${basePath}/package.json`;
20-
const motionsPath = `${codemodPath}/motions`;
21-
22-
fs.mkdirSync(codemodPath, { recursive: true });
23-
24-
fs.copyFileSync(
25-
`${__dirname}/../template/transform.spec.ts`,
26-
`${codemodPath}/transform.spec.ts`,
27-
);
28-
fs.copyFileSync(
29-
`${__dirname}/../template/transform.ts`,
30-
`${codemodPath}/transform.ts`,
31-
);
32-
fs.copySync(`${__dirname}/../template/motions`, motionsPath);
96+
const codemodPath = `${basePath}${!isReduced ? '/src/' : ''}/${version}`;
97+
const configPath = `${basePath}${
98+
!isReduced ? '/src' : ''
99+
}/codeshift.config.ts`;
100+
101+
if (fs.existsSync(codemodPath)) {
102+
throw new Error(`Codemod for version "${version}" already exists`);
103+
}
104+
105+
fs.copySync(`${__dirname}/../template${isReduced ? '/src' : ''}`, basePath);
106+
fs.renameSync(`${basePath}${!isReduced ? '/src/' : ''}/codemod`, codemodPath);
33107

34108
const testFile = fs
35109
.readFileSync(`${codemodPath}/transform.spec.ts`, 'utf8')
@@ -38,70 +112,16 @@ export function initDirectory(
38112

39113
fs.writeFileSync(`${codemodPath}/transform.spec.ts`, testFile);
40114

41-
fs.writeFileSync(
42-
packagePath,
43-
`{
44-
"name": "${packageName}",
45-
"version": "0.0.1",
46-
"license": "MIT",
47-
"main": "dist/${packageName}.cjs.js",
48-
"dependencies": {
49-
"@codeshift/utils": "^0.1.2"
50-
},
51-
"devDependencies": {
52-
"jscodeshift": "^0.12.0"
115+
if (!isReduced) {
116+
fs.writeFileSync(`${basePath}/package.json`, getPackageJson(packageName));
53117
}
54-
}`,
55-
);
56118

57119
if (!fs.existsSync(configPath)) {
58-
fs.writeFileSync(
59-
configPath,
60-
`export default {
61-
maintainers: [],
62-
transforms: {
63-
'${version}': require('./${version}/transform'),
64-
}
65-
};
66-
`,
67-
);
120+
fs.writeFileSync(configPath, getConfig(packageName, version));
68121
} else {
69-
const source = fs.readFileSync(configPath, 'utf8');
70-
const ast = recast.parse(source);
71-
const b = recast.types.builders;
72-
73-
recast.visit(ast, {
74-
visitProperty(path) {
75-
// @ts-ignore
76-
if (path.node.key.name !== 'transforms') return false;
77-
// @ts-ignore
78-
const properties = path.node.value.properties;
79-
// @ts-ignore
80-
properties.forEach(property => {
81-
if (semver.eq(property.key.value, version)) {
82-
throw new Error(
83-
`Transform for ${packageName} version ${version} already exists`,
84-
);
85-
}
86-
});
87-
88-
properties.push(
89-
b.property(
90-
'init',
91-
b.stringLiteral(version),
92-
b.callExpression(b.identifier('require'), [
93-
b.stringLiteral(`./${version}/transform`),
94-
]),
95-
),
96-
);
97-
98-
return false;
99-
},
100-
});
101-
102122
fs.writeFileSync(
103123
configPath,
104-
recast.prettyPrint(ast, { quote: 'single', trailingComma: true }).code,
124+
updateConfig(configPath, packageName, version),
105125
);
106126
}
107127
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
node_modules
2+
src/
3+
**/__test__
4+
**/*.spec.(ts|js)
5+
.vscode
6+
jest.config.js
7+
tsconfig.json
8+
yarn-error.log
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
transform: {
3+
'^.+\\.ts$': 'ts-jest',
4+
},
5+
moduleFileExtensions: ['ts', 'js'],
6+
testRegex: '^.+\\.spec\\.(ts|js)$',
7+
globals: {
8+
'ts-jest': {
9+
tsconfig: 'tsconfig.json',
10+
},
11+
},
12+
testPathIgnorePatterns: ['/node_modules/'],
13+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"include": ["src/**/*"],
3+
"compilerOptions": {
4+
"outDir": "dist",
5+
"esModuleInterop": true,
6+
"allowJs": true
7+
}
8+
}

scripts/initialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function main(packageName: string, version: string) {
66
if (!packageName) throw new Error('Package name was not provided');
77
if (!version) throw new Error('Version was not provided');
88

9-
initDirectory(packageName, version, path);
9+
initDirectory(packageName, version, path, true);
1010

1111
console.log(
1212
`🚚 New codemod package created at: community/${packageName}/${version}`,

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7112,6 +7112,11 @@ typescript@^4.1.3:
71127112
resolved "https://packages.atlassian.com/api/npm/npm-remote/typescript/-/typescript-4.2.3.tgz#39062d8019912d43726298f09493d598048c1ce3"
71137113
integrity sha1-OQYtgBmRLUNyYpjwlJPVmASMHOM=
71147114

7115+
typescript@^4.3.5:
7116+
version "4.3.5"
7117+
resolved "https://packages.atlassian.com/api/npm/npm-remote/typescript/-/typescript-4.3.5.tgz#4d1c37cc16e893973c45a06886b7113234f119f4"
7118+
integrity sha1-TRw3zBbok5c8RaBohrcRMjTxGfQ=
7119+
71157120
unicode-canonical-property-names-ecmascript@^1.0.4:
71167121
version "1.0.4"
71177122
resolved "https://packages.atlassian.com/api/npm/npm-remote/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz#2619800c4c825800efdd8343af7dd9933cbe2818"

0 commit comments

Comments
 (0)