Skip to content

Commit 94b43c1

Browse files
authored
fix: reuse prepared node dist during release (#388)
1 parent 53c7fbb commit 94b43c1

5 files changed

Lines changed: 67 additions & 8 deletions

File tree

.github/workflows/release.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ jobs:
2626
- run: corepack yarn
2727
- run: corepack yarn tsc:utils
2828
- run: corepack yarn tsc:zod
29+
- run: corepack yarn tsc:node
2930
- uses: changesets/action@v1
3031
with:
3132
version: corepack yarn changeset:version:release
3233
publish: corepack yarn changeset publish
3334
env:
3435
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3536
NPM_CONFIG_PROVENANCE: "true"
37+
TRANSLOADIT_PUBLISH_PREBUILT_NODE: "true"

packages/node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"fix": "yarn fix:js",
9191
"lint:deps": "knip --dependencies --no-progress",
9292
"fix:deps": "knip --dependencies --no-progress --fix",
93-
"prepack": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && rm -f tsconfig.tsbuildinfo tsconfig.build.tsbuildinfo && yarn --cwd ../.. tsc:node && chmod +x dist/cli.js",
93+
"prepack": "node ../../scripts/prepare-node-package.ts",
9494
"test:unit": "yarn --cwd ../.. tsc:utils && ../../node_modules/.bin/vitest run --coverage ./test/unit",
9595
"test:e2e": "yarn --cwd ../.. tsc:utils && ../../node_modules/.bin/vitest run ./test/e2e",
9696
"test": "yarn --cwd ../.. tsc:utils && ../../node_modules/.bin/vitest run --coverage"

packages/node/test/unit/prepare-transloadit.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22

3+
import { shouldReusePreparedNodeDist } from '../../../../scripts/prepare-node-package.ts'
34
import { buildLegacyPackageJson } from '../../../../scripts/prepare-transloadit.ts'
45

56
describe('prepare-transloadit', () => {
@@ -52,4 +53,24 @@ describe('prepare-transloadit', () => {
5253

5354
expect(legacyPackageJson.version).toBe('4.8.1')
5455
})
56+
57+
it('only reuses a prepared node dist during the explicit publish flow', () => {
58+
const original = process.env.TRANSLOADIT_PUBLISH_PREBUILT_NODE
59+
60+
try {
61+
expect(shouldReusePreparedNodeDist(false)).toBe(false)
62+
63+
process.env.TRANSLOADIT_PUBLISH_PREBUILT_NODE = 'true'
64+
expect(shouldReusePreparedNodeDist(true)).toBe(true)
65+
66+
delete process.env.TRANSLOADIT_PUBLISH_PREBUILT_NODE
67+
expect(shouldReusePreparedNodeDist(true)).toBe(false)
68+
} finally {
69+
if (original == null) {
70+
delete process.env.TRANSLOADIT_PUBLISH_PREBUILT_NODE
71+
} else {
72+
process.env.TRANSLOADIT_PUBLISH_PREBUILT_NODE = original
73+
}
74+
}
75+
})
5576
})

scripts/prepare-node-package.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { execFile } from 'node:child_process'
2+
import { access, chmod, rm } from 'node:fs/promises'
3+
import { dirname, resolve } from 'node:path'
4+
import { fileURLToPath } from 'node:url'
5+
import { promisify } from 'node:util'
6+
7+
const execFileAsync = promisify(execFile)
8+
9+
const filePath = fileURLToPath(import.meta.url)
10+
const repoRoot = resolve(dirname(filePath), '..')
11+
const nodePackage = resolve(repoRoot, 'packages/node')
12+
const nodeDistCli = resolve(nodePackage, 'dist/cli.js')
13+
14+
function shouldReusePreparedNodeDist(hasNodeDist: boolean): boolean {
15+
return process.env.TRANSLOADIT_PUBLISH_PREBUILT_NODE === 'true' && hasNodeDist
16+
}
17+
18+
const ensureNodePackagePrepared = async (): Promise<void> => {
19+
const hasNodeDist = await access(nodeDistCli)
20+
.then(() => true)
21+
.catch(() => false)
22+
23+
if (shouldReusePreparedNodeDist(hasNodeDist)) {
24+
await chmod(nodeDistCli, 0o755)
25+
return
26+
}
27+
28+
await rm(resolve(nodePackage, 'dist'), { recursive: true, force: true })
29+
await rm(resolve(nodePackage, 'tsconfig.tsbuildinfo'), { force: true })
30+
await rm(resolve(nodePackage, 'tsconfig.build.tsbuildinfo'), { force: true })
31+
await execFileAsync('yarn', ['tsc:node'], {
32+
cwd: repoRoot,
33+
})
34+
await chmod(nodeDistCli, 0o755)
35+
}
36+
37+
if (process.argv[1] != null && resolve(process.argv[1]) === filePath) {
38+
await ensureNodePackagePrepared()
39+
}
40+
41+
export { ensureNodePackagePrepared, shouldReusePreparedNodeDist }

scripts/prepare-transloadit.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import { execFile } from 'node:child_process'
21
import { chmod, cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
32
import { dirname, resolve } from 'node:path'
43
import { fileURLToPath } from 'node:url'
5-
import { promisify } from 'node:util'
6-
7-
const execFileAsync = promisify(execFile)
4+
import { ensureNodePackagePrepared } from './prepare-node-package.ts'
85

96
const filePath = fileURLToPath(import.meta.url)
107
const repoRoot = resolve(dirname(filePath), '..')
@@ -127,9 +124,7 @@ const writeLegacyChangelog = async (): Promise<void> => {
127124
}
128125

129126
const main = async (): Promise<void> => {
130-
await execFileAsync('yarn', ['workspace', '@transloadit/node', 'prepack'], {
131-
cwd: repoRoot,
132-
})
127+
await ensureNodePackagePrepared()
133128

134129
await copyDir(resolve(nodePackage, 'dist'), resolve(legacyPackage, 'dist'))
135130
await copyDir(resolve(nodePackage, 'src'), resolve(legacyPackage, 'src'))

0 commit comments

Comments
 (0)