Skip to content

Commit 2d1fff1

Browse files
committed
🐛 Fixes publishing scripts
1 parent 3d14938 commit 2d1fff1

File tree

9 files changed

+2499
-2528
lines changed

9 files changed

+2499
-2528
lines changed

.github/workflows/release-all-codemods.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
uses: actions/setup-node@v1
1919
with:
2020
node-version: ${{ matrix.node-version }}
21+
registry-url: https://registry.npmjs.org/
2122
- run: yarn install --frozen-lockfile
2223
- run: yarn validate
2324
- run: yarn test
@@ -26,3 +27,4 @@ jobs:
2627
env:
2728
CI: true
2829
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
30+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/release-codemods.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
uses: actions/setup-node@v1
2121
with:
2222
node-version: ${{ matrix.node-version }}
23+
registry-url: https://registry.npmjs.org/
2324
- run: yarn install --frozen-lockfile
2425
- run: yarn validate
2526
- run: yarn test
@@ -28,3 +29,4 @@ jobs:
2829
env:
2930
CI: true
3031
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
32+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

packages/publisher/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
"dependencies": {
99
"@codeshift/initializer": "*",
1010
"@preconstruct/cli": "^2.0.0",
11+
"chalk": "^4.1.0",
1112
"fs-extra": "^9.1.0",
1213
"npm-registry-client": "^8.6.0",
1314
"semver": "^7.3.5",
1415
"simple-git": "^2.37.0",
16+
"spawndamnit": "^2.0.0",
1517
"tar": "^6.1.0",
1618
"ts-node": "^9.1.1"
1719
}

packages/publisher/src/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ export default function buildPackages(path: string, packages: string[]) {
55
packages.map(
66
pkg =>
77
new Promise<void>((resolve, reject) => {
8-
// TODO: stop this from building the entire repo.
98
exec(
10-
`echo $PWD && cd ${path}/${pkg} && yarn build`,
9+
`yarn build`,
10+
{ cwd: `${path}/${pkg}` },
1111
(error, stdout, stderr) => {
1212
if (error) {
1313
console.error(`exec error: ${error}`);

packages/publisher/src/publish.ts

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,86 @@
11
import fs from 'fs-extra';
2-
import tar from 'tar';
3-
2+
import chalk from 'chalk';
43
// @ts-ignore
5-
import RegClient from 'npm-registry-client';
6-
7-
// TODO: dependency inject this instance
8-
const client = new RegClient();
9-
const npmUri = 'https://registry.npmjs.org/';
4+
import spawn from 'spawndamnit';
105

11-
interface PublishPackageOptions {
12-
metadata: Record<string, string>;
13-
access: 'public' | 'restricted';
14-
token: string;
15-
body: any;
6+
function jsonParse(input: string) {
7+
try {
8+
return JSON.parse(input);
9+
} catch (err) {
10+
if (err instanceof SyntaxError) {
11+
console.error('error parsing json:', input);
12+
}
13+
throw err;
14+
}
1615
}
1716

18-
function publishPackage(
19-
packageName: string,
20-
{ metadata, access, body, token }: PublishPackageOptions,
17+
async function publish(
18+
pkg: { dir: string; packageJson: Record<string, any> },
19+
opts: { dry?: boolean },
20+
authToken: string,
2121
) {
22-
return new Promise<void>((resolve, reject) =>
23-
client.publish(
24-
npmUri,
25-
{ metadata, access, body, auth: { token } },
26-
(error: any) => {
27-
if (error) {
28-
reject(
29-
`Unexpected error when publishing ${packageName} to NPM: ${error}`,
30-
);
31-
}
32-
resolve();
33-
},
34-
),
22+
const { name, version } = pkg.packageJson;
23+
24+
console.log(
25+
`Publishing ${chalk.cyan(`"${name}"`)} at ${chalk.green(`"${version}"`)}`,
3526
);
27+
28+
// Due to a super annoying issue in yarn, we have to manually override this env variable
29+
// See: https://github.com/yarnpkg/yarn/issues/2935#issuecomment-355292633
30+
const envOverride = {
31+
// eslint-disable-next-line @typescript-eslint/camelcase
32+
npm_config_registry: `//registry.npmjs.org/:_authToken=${authToken}`,
33+
};
34+
35+
const publishOpts = ['--access', 'public'];
36+
37+
if (opts?.dry) {
38+
publishOpts.push('--dry-run');
39+
}
40+
41+
const { stdout } = await spawn(
42+
'npm',
43+
['publish', pkg.dir, '--json', ...publishOpts],
44+
{
45+
env: Object.assign({}, process.env, envOverride),
46+
},
47+
);
48+
49+
const json = jsonParse(stdout.toString().replace(/[^{]*/, ''));
50+
51+
if (json.error) {
52+
throw new Error(
53+
`An error occurred while publishing ${name}: ${json.error.code}
54+
${json.error.summary}
55+
${json.error.detail ? '\n' + json.error.detail : ''}
56+
`,
57+
);
58+
}
3659
}
3760

38-
export default function publishPackages(path: string, authToken: string) {
61+
export default function publishPackages(
62+
path: string,
63+
opts: { dry?: boolean },
64+
authToken: string,
65+
) {
3966
return Promise.all(
4067
fs.readdirSync(path).map(async dir => {
41-
const packageName = `@codeshift/mod-${dir
42-
.replace('@', '')
43-
.replace('/', '__')}`;
4468
const packagePath = `${path}/${dir}`;
45-
const packageJson = await fs.readFile(`${packagePath}/package.json`);
46-
const tarballPath = `${packagePath}/tarball.tgz`;
69+
const packageJson = await fs.readFile(
70+
`${packagePath}/package.json`,
71+
'utf8',
72+
);
4773

48-
await tar.create(
74+
await publish(
4975
{
50-
cwd: packagePath,
51-
file: tarballPath,
52-
gzip: true,
76+
dir: packagePath,
77+
packageJson: jsonParse(packageJson),
5378
},
54-
['.'],
79+
opts,
80+
authToken,
5581
);
56-
57-
await publishPackage(packageName, {
58-
// @ts-ignore
59-
metadata: JSON.parse(packageJson),
60-
access: 'public',
61-
body: fs.createReadStream(tarballPath),
62-
token: authToken,
63-
});
6482
}),
65-
).catch(err => {
66-
throw new Error(err);
83+
).catch(error => {
84+
throw new Error(error);
6785
});
6886
}

scripts/publish-all-dry.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
getAllPackages,
33
buildPackages,
44
generatePackages,
5+
publishPackages,
56
cleanTargetDir,
67
} from '@codeshift/publisher';
78

@@ -15,6 +16,9 @@ async function main(sourcePath: string, targetPath: string) {
1516

1617
console.log('🏗 Building all packages');
1718
await buildPackages(targetPath, packages);
19+
20+
console.log('📦 Publishing changed packages (dry)');
21+
await publishPackages(targetPath, { dry: true }, process.env.NPM_TOKEN!);
1822
}
1923

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

scripts/publish-all.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ async function main(sourcePath: string, targetPath: string) {
1717
console.log('🏗 Building all packages');
1818
await buildPackages(targetPath, packages);
1919

20-
await publishPackages(targetPath, process.env.NPM_TOKEN!);
2120
console.log('📦 Publishing all packages');
21+
await publishPackages(targetPath, {}, process.env.NPM_TOKEN!);
2222

2323
console.log('🧹 Cleaning up temporary directory');
2424
cleanTargetDir(targetPath);

scripts/publish.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function main(sourcePath: string, targetPath: string) {
2828
await buildPackages(targetPath, changedPackages);
2929

3030
console.log('📦 Publishing changed packages');
31-
await publishPackages(targetPath, process.env.NPM_TOKEN!);
31+
await publishPackages(targetPath, {}, process.env.NPM_TOKEN!);
3232

3333
console.log('🧹 Cleaning up temporary directory');
3434
cleanTargetDir(targetPath);

0 commit comments

Comments
 (0)