Skip to content

Commit de8f2e5

Browse files
committed
🔨 Refactors build step to use ts config + build tools
1 parent f42be01 commit de8f2e5

File tree

13 files changed

+107
-99
lines changed

13 files changed

+107
-99
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: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,33 @@ 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-
}`;
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+
);
3132
}
3233

3334
function getConfig(packageName: string, version: string) {

packages/publisher/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"license": "MIT",
77
"repository": "https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/master/packages/publisher",
88
"dependencies": {
9+
"@codeshift/initializer": "*",
910
"@preconstruct/cli": "^2.0.0",
1011
"fs-extra": "^9.1.0",
1112
"npm-registry-client": "^8.6.0",

packages/publisher/src/build.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import { exec } from 'child_process';
22

3-
export default function buildPackages() {
4-
return new Promise<void>((resolve, reject) => {
5-
// TODO stop this from building the entire repo.
6-
exec(`yarn build`, (error, stdout, stderr) => {
7-
if (error) {
8-
console.error(`exec error: ${error}`);
9-
reject('Unable to build codeshift packages');
10-
}
3+
export default function buildPackages(path: string, packages: string[]) {
4+
return Promise.all(
5+
packages.map(
6+
pkg =>
7+
new Promise<void>((resolve, reject) => {
8+
// TODO: stop this from building the entire repo.
9+
exec(
10+
`echo $PWD && cd ${path}/${pkg} && yarn build`,
11+
(error, stdout, stderr) => {
12+
if (error) {
13+
console.error(`exec error: ${error}`);
14+
reject('Unable to build codeshift packages');
15+
}
1116

12-
console.log(stdout);
13-
console.error(stderr);
17+
console.log(stdout);
18+
console.error(stderr);
1419

15-
resolve();
16-
});
17-
});
20+
resolve();
21+
},
22+
);
23+
}),
24+
),
25+
);
1826
}

packages/publisher/src/generate.ts

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import semver from 'semver';
22
import fs from 'fs-extra';
33
// @ts-ignore
44
import RegClient from 'npm-registry-client';
5+
import { getPackageJson } from '@codeshift/initializer';
56

67
const client = new RegClient();
78
const npmUri = 'https://registry.npmjs.org/';
@@ -26,62 +27,46 @@ function getPackageVersion(packageName: string) {
2627
);
2728
}
2829

29-
function getPackageJson(packageName: string, version: string) {
30-
return JSON.stringify(
31-
{
32-
name: packageName,
33-
version,
34-
main: `dist/codeshift-${packageName.replace('@codeshift/', '')}.cjs.js`,
35-
license: 'MIT',
36-
repository: 'https://github.com/CodeshiftCommunity/CodeshiftCommunity/',
37-
scripts: {},
38-
dependencies: {
39-
jscodeshift: '^0.12.0',
40-
'@codeshift/utils': '*',
41-
},
42-
},
43-
null,
44-
2,
45-
);
46-
}
47-
4830
export default async function generatePackages(
4931
sourcePath: string,
5032
targetPath: string,
5133
changedPackages: string[],
5234
) {
5335
await fs.mkdir(targetPath);
36+
5437
const directories = await fs.readdir(sourcePath);
5538

56-
directories
57-
.filter(dir => changedPackages.includes(dir))
58-
.forEach(async dir => {
59-
const packageName = `@codeshift/mod-${dir
60-
.replace('@', '')
61-
.replace('/', '__')}`;
62-
const packageVersion = await getPackageVersion(packageName);
63-
// We need to manually patch bump the codemod
64-
const nextPackageVersion = semver.inc(packageVersion, 'patch');
39+
await Promise.all(
40+
directories
41+
.filter(dir => changedPackages.includes(dir))
42+
.map(async dir => {
43+
const packageName = `@codeshift/mod-${dir
44+
.replace('@', '')
45+
.replace('/', '__')}`;
46+
const packageVersion = await getPackageVersion(packageName);
47+
// We need to manually patch bump the codemod
48+
const nextPackageVersion = semver.inc(packageVersion, 'patch');
6549

66-
const basePath = `${targetPath}/${dir}`;
67-
await fs.copy(`${sourcePath}/${dir}`, `${basePath}/src`);
68-
await fs.copyFile(
69-
`${__dirname}/../template/LICENSE`,
70-
`${basePath}/LICENSE`,
71-
);
72-
await fs.copyFile(
73-
`${__dirname}/../template/.npmignore`,
74-
`${basePath}/.npmignore`,
75-
);
76-
await fs.rename(
77-
`${basePath}/src/codeshift.config.js`,
78-
`${basePath}/src/index.js`,
79-
);
80-
await fs.writeFile(
81-
`${basePath}/package.json`,
82-
getPackageJson(packageName, nextPackageVersion!),
83-
);
84-
});
50+
const basePath = `${targetPath}/${dir}`;
51+
await fs.copy(`${sourcePath}/${dir}`, `${basePath}/src`);
52+
await fs.copyFile(
53+
`${__dirname}/../template/LICENSE`,
54+
`${basePath}/LICENSE`,
55+
);
56+
await fs.copyFile(
57+
`${__dirname}/../template/.npmignore`,
58+
`${basePath}/.npmignore`,
59+
);
60+
await fs.copyFile(
61+
`${__dirname}/../template/tsconfig.json`,
62+
`${basePath}/tsconfig.json`,
63+
);
64+
await fs.writeFile(
65+
`${basePath}/package.json`,
66+
getPackageJson(packageName, nextPackageVersion!),
67+
);
68+
}),
69+
);
8570
}
8671

8772
export function cleanTargetDir(path: string) {

packages/publisher/src/publish.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export default function publishPackages(path: string, authToken: string) {
6060
access: 'public',
6161
body: fs.createReadStream(tarballPath),
6262
token: authToken,
63+
}).catch(err => {
64+
throw new Error(err);
6365
});
6466
}),
6567
);
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
node_modules
2+
src/
23
**/__test__
3-
**/*.(spec|test).(ts|js)
4+
**/*.spec.(ts|js)
5+
.vscode
6+
jest.config.js
7+
tsconfig.json
8+
yarn-error.log
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/publish-all-dry.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ async function main(sourcePath: string, targetPath: string) {
99
cleanTargetDir(targetPath);
1010

1111
const packages = getAllPackages(sourcePath);
12-
console.log(packages);
1312

1413
console.log('Generating temporary directory');
1514
await generatePackages(sourcePath, targetPath, packages);
1615

1716
console.log('Building all packages');
18-
await buildPackages();
17+
await buildPackages(targetPath, packages);
1918
}
2019

2120
main(process.argv[2], process.argv[3]).catch(error => {

0 commit comments

Comments
 (0)