Skip to content

Commit de3039f

Browse files
author
Daniel Del Core
committed
refactor init scripts to ensure pacakges are generated correctly
1 parent f84d915 commit de3039f

File tree

5 files changed

+25
-42
lines changed

5 files changed

+25
-42
lines changed

packages/initializer/src/index.ts

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ function updateConfig(
7171
packageName: string,
7272
transformName: string,
7373
type: 'version' | 'preset',
74-
isReduced = false,
7574
) {
7675
const configPath = path.join(targetPath, 'codeshift.config.js');
7776
const source = fs.readFileSync(configPath, 'utf8');
@@ -94,21 +93,15 @@ function updateConfig(
9493
}
9594
});
9695

97-
const transformPath = `./${
98-
!isReduced ? 'codemods/' : ''
99-
}${transformName}/transform`;
96+
const transformPath = `./${transformName}/transform`;
10097

10198
properties.push(
10299
b.property(
103100
'init',
104101
b.stringLiteral(transformName),
105-
b.callExpression(
106-
b.memberExpression(
107-
b.identifier('require'),
108-
b.identifier('resolve'),
109-
),
110-
[b.stringLiteral(transformPath)],
111-
),
102+
b.callExpression(b.identifier('require'), [
103+
b.stringLiteral(transformPath),
104+
]),
112105
),
113106
);
114107

@@ -130,6 +123,7 @@ export function initConfig(packageName: string, targetPath = './') {
130123
const configPath = path.join(targetPath, 'codeshift.config.js');
131124

132125
if (!fs.existsSync(configPath)) {
126+
fs.mkdirSync(targetPath, { recursive: true });
133127
fs.writeFileSync(configPath, getConfig(packageName));
134128
}
135129
}
@@ -139,15 +133,9 @@ export function initDirectory(
139133
targetPath = './',
140134
isReduced = false,
141135
) {
142-
const sourcePath = path.join(targetPath, 'src');
143-
144-
fs.copySync(
145-
path.join(TEMPLATE_PATH, isReduced ? 'codemods' : ''),
146-
sourcePath,
147-
{
148-
filter: src => !src.includes('codemods/codemod'),
149-
},
150-
);
136+
if (!fs.existsSync(targetPath)) {
137+
fs.mkdirSync(targetPath);
138+
}
151139

152140
fs.writeFileSync(
153141
path.join(targetPath, 'package.json'),
@@ -159,8 +147,11 @@ export function initDirectory(
159147
);
160148

161149
if (!isReduced) {
162-
fs.writeFileSync(path.join(targetPath, '.npmignore'), getNpmIgnore());
150+
fs.copySync(path.join(TEMPLATE_PATH), targetPath, {
151+
filter: src => !src.includes('/codemods'),
152+
});
163153

154+
fs.writeFileSync(path.join(targetPath, '.npmignore'), getNpmIgnore());
164155
const readmeFilePath = path.join(targetPath, 'README.md');
165156
const readmeFile = fs
166157
.readFileSync(readmeFilePath, 'utf8')
@@ -169,15 +160,14 @@ export function initDirectory(
169160
fs.writeFileSync(readmeFilePath, readmeFile);
170161
}
171162

172-
initConfig(packageName, sourcePath);
163+
initConfig(packageName, path.join(targetPath, 'src'));
173164
}
174165

175166
export function initTransform(
176167
packageName: string,
177168
id: string,
178169
type: 'version' | 'preset',
179170
targetPath = './',
180-
isReduced = false,
181171
) {
182172
if (type === 'version' && !semver.valid(id)) {
183173
throw new Error(`Provided version ${id} is not a valid semver version`);
@@ -190,17 +180,10 @@ export function initTransform(
190180
throw new Error(`Codemod for ${type} "${id}" already exists`);
191181
}
192182

193-
const codemodTemplateDestinationPath = path.join(
194-
targetPath,
195-
!isReduced ? 'codemods' : '',
196-
'codemod',
197-
);
183+
const destinationPath = path.join(sourcePath, 'codemod');
198184

199-
fs.copySync(
200-
path.join(TEMPLATE_PATH, 'codemods', 'codemod'),
201-
codemodTemplateDestinationPath,
202-
);
203-
fs.renameSync(codemodTemplateDestinationPath, transformPath);
185+
fs.copySync(path.join(TEMPLATE_PATH, 'codemods', 'codemod'), destinationPath);
186+
fs.renameSync(destinationPath, transformPath);
204187

205188
const testFilePath = path.join(transformPath, 'transform.spec.ts');
206189
const testFile = fs
@@ -220,5 +203,5 @@ export function initTransform(
220203

221204
fs.writeFileSync(readmeFilePath, readmeFile);
222205

223-
updateConfig(sourcePath, packageName, id || '', type, isReduced);
206+
updateConfig(sourcePath, packageName, id || '', type);
224207
}

scripts/initialize-preset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function main(packageName: string, preset?: string) {
2323

2424
if (preset) {
2525
initDirectory(packageName, targetPath, true);
26-
initTransform(packageName, preset, 'preset', targetPath, true);
26+
initTransform(packageName, preset, 'preset', targetPath);
2727
}
2828

2929
console.log(

scripts/initialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function main(packageName: string, transform?: string) {
2323

2424
if (transform) {
2525
initDirectory(packageName, targetPath, true);
26-
initTransform(packageName, transform, 'version', targetPath, true);
26+
initTransform(packageName, transform, 'version', targetPath);
2727
}
2828

2929
console.log(

website/docs/configuration.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ module.exports = {
1717
description: 'Codemods for the foobar package',
1818
targets: ['foobar'],
1919
transforms: {
20-
'18.0.0': require.resolve('./18.0.0/transform'),
21-
'19.0.0': require.resolve('./19.0.0/transform'),
20+
'18.0.0': require('./18.0.0/transform'),
21+
'19.0.0': require('./19.0.0/transform'),
2222
},
2323
presets: {
24-
'sort-imports': require.resolve('./sort-imports/transform'),
24+
'sort-imports': require('./sort-imports/transform'),
2525
},
2626
};
2727
```

website/src/pages/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@ const App = () => (
240240
<CodeBlock className="language-js">
241241
{`export.module = {
242242
transforms: {
243-
'12.0.0': require.resolve('./18.0.0/transform'),
244-
'13.0.0': require.resolve('./19.0.0/transform'),
243+
'12.0.0': require('./18.0.0/transform'),
244+
'13.0.0': require('./19.0.0/transform'),
245245
},
246246
presets: {
247-
'format-imports': require.resolve('./format-imports/transform')
247+
'format-imports': require('./format-imports/transform')
248248
}
249249
};`}
250250
</CodeBlock>

0 commit comments

Comments
 (0)