Skip to content

Commit f401efe

Browse files
Merge pull request #36 from CodeshiftCommunity/fix-mod-build-step
Fix mod build step
2 parents 0899857 + de8f2e5 commit f401efe

File tree

20 files changed

+204
-141
lines changed

20 files changed

+204
-141
lines changed

.changeset/green-lobsters-explain.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@codeshift/initializer': patch
3+
'@codeshift/publisher': patch
4+
---
5+
6+
Refactores Community folder build step

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@
6969
],
7070
"preconstruct": {
7171
"packages": [
72-
"packages/*",
73-
".tmp/*"
72+
"packages/*"
7473
]
7574
}
7675
}

packages/initializer/src/index.ts

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

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

1696
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`;
97+
const codemodPath = `${basePath}${!isReduced ? '/src/' : ''}/${version}`;
98+
const configPath = `${basePath}${
99+
!isReduced ? '/src' : ''
100+
}/codeshift.config.ts`;
21101

22-
fs.mkdirSync(codemodPath, { recursive: true });
102+
if (fs.existsSync(codemodPath)) {
103+
throw new Error(`Codemod for version "${version}" already exists`);
104+
}
23105

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);
106+
fs.copySync(`${__dirname}/../template${isReduced ? '/src' : ''}`, basePath);
107+
fs.renameSync(`${basePath}${!isReduced ? '/src/' : ''}/codemod`, codemodPath);
33108

34109
const testFile = fs
35110
.readFileSync(`${codemodPath}/transform.spec.ts`, 'utf8')
@@ -38,70 +113,16 @@ export function initDirectory(
38113

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

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"
116+
if (!isReduced) {
117+
fs.writeFileSync(`${basePath}/package.json`, getPackageJson(packageName));
53118
}
54-
}`,
55-
);
56119

57120
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-
);
121+
fs.writeFileSync(configPath, getConfig(packageName, version));
68122
} 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-
102123
fs.writeFileSync(
103124
configPath,
104-
recast.prettyPrint(ast, { quote: 'single', trailingComma: true }).code,
125+
updateConfig(configPath, packageName, version),
105126
);
106127
}
107128
}
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+
}

0 commit comments

Comments
 (0)