Skip to content

Commit f84d915

Browse files
author
Daniel Del Core
committed
initialise commands are now working with the new file structure
1 parent c092f23 commit f84d915

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"test": "jest",
1616
"test:watch": "jest --watch",
1717
"test:coverage": "jest --coverage",
18-
"validate": "yarn build && yarn lint && yarn lint:file-structure && yarn monorepo:check && yarn community:validate",
18+
"validate": "yarn build && yarn lint && yarn monorepo:check && yarn community:validate",
1919
"lint": "eslint --config .eslintrc.js --ext tsx,ts ./packages/**/src ./community/**/src",
2020
"lint:fix": "yarn lint -- --fix",
2121
"types:check": "tsc --noEmit --skipLibCheck",

packages/initializer/src/index.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export function getPackageJson(packageName: string, version = '0.0.0') {
2929
devDependencies: {
3030
'@codeshift/cli': `^${cliVersion}`,
3131
'@codeshift/test-utils': `^${testUtilVersion}`,
32-
'@types/node': '^16.11.0',
3332
'@types/jest': '^26.0.15',
33+
'@types/node': '^16.11.0',
3434
jest: '^26.6.0',
3535
parcel: '^2.8.3',
3636
prettier: '^2.0.0',
@@ -59,9 +59,9 @@ function getConfig(packageName: string, transform?: string, preset?: string) {
5959
targets: [],
6060
description: 'Codemods for ${packageName}',
6161
transforms: {${
62-
transform ? `'${transform}': resolve('./${transform}/transform'),` : ''
62+
transform ? `'${transform}': require('./${transform}/transform'),` : ''
6363
}},
64-
presets: {${preset ? `'${preset}': resolve('./${preset}/transform'),` : ''}},
64+
presets: {${preset ? `'${preset}': require('./${preset}/transform'),` : ''}},
6565
};
6666
`;
6767
}
@@ -139,20 +139,26 @@ export function initDirectory(
139139
targetPath = './',
140140
isReduced = false,
141141
) {
142+
const sourcePath = path.join(targetPath, 'src');
143+
142144
fs.copySync(
143145
path.join(TEMPLATE_PATH, isReduced ? 'codemods' : ''),
144-
targetPath,
146+
sourcePath,
145147
{
146148
filter: src => !src.includes('codemods/codemod'),
147149
},
148150
);
149151

150-
if (!isReduced) {
151-
fs.writeFileSync(
152-
path.join(targetPath, 'package.json'),
153-
getPackageJson(packageName),
154-
);
152+
fs.writeFileSync(
153+
path.join(targetPath, 'package.json'),
154+
getPackageJson(
155+
isReduced
156+
? `@codeshift/mod-${packageName.replace('/', '__').replace('@', '')}`
157+
: packageName,
158+
),
159+
);
155160

161+
if (!isReduced) {
156162
fs.writeFileSync(path.join(targetPath, '.npmignore'), getNpmIgnore());
157163

158164
const readmeFilePath = path.join(targetPath, 'README.md');
@@ -163,7 +169,7 @@ export function initDirectory(
163169
fs.writeFileSync(readmeFilePath, readmeFile);
164170
}
165171

166-
initConfig(packageName, targetPath);
172+
initConfig(packageName, sourcePath);
167173
}
168174

169175
export function initTransform(
@@ -177,7 +183,8 @@ export function initTransform(
177183
throw new Error(`Provided version ${id} is not a valid semver version`);
178184
}
179185

180-
const transformPath = path.join(targetPath, !isReduced ? 'codemods' : '', id);
186+
const sourcePath = path.join(targetPath, 'src');
187+
const transformPath = path.join(sourcePath, id);
181188

182189
if (fs.existsSync(transformPath)) {
183190
throw new Error(`Codemod for ${type} "${id}" already exists`);
@@ -213,5 +220,5 @@ export function initTransform(
213220

214221
fs.writeFileSync(readmeFilePath, readmeFile);
215222

216-
updateConfig(targetPath, packageName, id || '', type, isReduced);
223+
updateConfig(sourcePath, packageName, id || '', type, isReduced);
217224
}

scripts/initialize-preset.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import chalk from 'chalk';
2-
import { initDirectory, initTransform } from '@codeshift/initializer';
32
import path from 'path';
43

4+
import { initDirectory, initTransform } from '@codeshift/initializer';
5+
56
const communityPath = `${__dirname}/../community`;
67

78
export function main(packageName: string, preset?: string) {
8-
if (!packageName) throw new Error('Package name was not provided');
9-
if (!preset) throw new Error('Preset name was not provided');
9+
if (!packageName)
10+
throw new Error(
11+
chalk.red(
12+
'Package name was not provided. Example: yarn community:init foobar my-preset',
13+
),
14+
);
15+
if (!preset)
16+
throw new Error(
17+
chalk.red(
18+
'Preset name was not provided. Example: Example: yarn community:init foobar my-preset',
19+
),
20+
);
1021

1122
const targetPath = path.join(communityPath, packageName.replace('/', '__'));
1223

scripts/initialize.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import chalk from 'chalk';
2-
import { initDirectory, initTransform } from '@codeshift/initializer';
32
import path from 'path';
43

4+
import { initDirectory, initTransform } from '@codeshift/initializer';
5+
56
const communityPath = `${__dirname}/../community`;
67

78
export function main(packageName: string, transform?: string) {
8-
if (!packageName) throw new Error('Package name was not provided');
9-
if (!transform) throw new Error('Version was not provided');
9+
if (!packageName)
10+
throw new Error(
11+
chalk.red(
12+
'Package name was not provided. Example: yarn community:init foobar 12.0.0',
13+
),
14+
);
15+
if (!transform)
16+
throw new Error(
17+
chalk.red(
18+
'Version was not provided. Example: Example: yarn community:init foobar 12.0.0',
19+
),
20+
);
1021

1122
const targetPath = path.join(communityPath, packageName.replace('/', '__'));
1223

0 commit comments

Comments
 (0)