Skip to content

Commit d90a343

Browse files
committed
fix: harden zod scripts and tooling
1 parent c63cfff commit d90a343

11 files changed

Lines changed: 77 additions & 33 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ View the coverage report locally by opening `coverage/index.html` in your browse
8181

8282
## Packaging the legacy `transloadit` package
8383

84-
The `packages/transloadit` folder is a generated legacy wrapper. The `src` directory and top-level `README.md`, `CHANGELOG.md`, and `LICENSE` are produced during packing by `scripts/prepare-transloadit.js` and are not tracked in git. If you need to validate the legacy package contents, run:
84+
The `packages/transloadit` folder is a generated legacy wrapper. The `src` directory and top-level `README.md`, `CHANGELOG.md`, and `LICENSE` are produced during packing by `scripts/prepare-transloadit.ts` and are not tracked in git. If you need to validate the legacy package contents, run:
8585

8686
```sh
8787
yarn pack

docs/todo.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
- [x] Assert byte-for-byte identity
1111

1212
## Fingerprint script
13-
- [x] Add `scripts/fingerprint-pack.js`
13+
- [x] Add `scripts/fingerprint-pack.ts`
1414
- [x] Use `npm pack --json` to get tarball name
1515
- [x] Hash the tarball SHA256
1616
- [x] Hash each file via `tar -tf` + `tar -xOf`
1717
- [x] Output JSON artifact for comparison
18-
- [x] Support `node scripts/fingerprint-pack.js .`
19-
- [x] Support `node scripts/fingerprint-pack.js packages/transloadit`
18+
- [x] Support `node --experimental-strip-types scripts/fingerprint-pack.ts .`
19+
- [x] Support `node --experimental-strip-types scripts/fingerprint-pack.ts packages/transloadit`
2020

2121
## Versioning and releases
2222
- [x] Add Changesets at workspace root
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export default class InconsistentResponseError extends Error {
1+
export class InconsistentResponseError extends Error {
22
override name = 'InconsistentResponseError'
33
}

packages/node/src/Transloadit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import type {
4242
TemplateCredentialsResponse,
4343
TemplateResponse,
4444
} from './apiTypes.ts'
45-
import InconsistentResponseError from './InconsistentResponseError.ts'
45+
import { InconsistentResponseError } from './InconsistentResponseError.ts'
4646
import PaginationStream from './PaginationStream.ts'
4747
import PollingTimeoutError from './PollingTimeoutError.ts'
4848
import type { Stream } from './tus.ts'

packages/transloadit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"lint:deps": "knip --dependencies --no-progress",
7070
"fix:deps": "knip --dependencies --no-progress --fix",
7171
"knip": "knip --no-config-hints --no-progress",
72-
"prepack": "node ../../scripts/prepare-transloadit.js",
72+
"prepack": "node --experimental-strip-types ../../scripts/prepare-transloadit.ts",
7373
"test:unit": "vitest run --coverage ./test/unit",
7474
"test:e2e": "vitest run ./test/e2e",
7575
"test": "vitest run --coverage"

packages/zod/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
}
2929
},
3030
"scripts": {
31-
"sync:v3": "node scripts/sync-v3.ts",
32-
"sync:v4": "node scripts/sync-v4.ts",
31+
"sync:v3": "node --experimental-strip-types scripts/sync-v3.ts",
32+
"sync:v4": "node --experimental-strip-types scripts/sync-v4.ts",
3333
"sync": "yarn sync:v3 && yarn sync:v4",
3434
"lint:ts": "yarn sync && tsc --build tsconfig.build.json",
35-
"test:unit": "node test/exports-sync.test.ts && node test/runtime-parity.test.ts",
35+
"test:unit": "node --experimental-strip-types test/scripts-config.test.ts && node --experimental-strip-types test/exports-sync.test.ts && node --experimental-strip-types test/runtime-parity.test.ts",
3636
"build": "yarn sync && tsc --build tsconfig.build.json",
37-
"check": "yarn sync && tsc --build tsconfig.build.json && tsc --noEmit --project tsconfig.test.json && node test/exports-sync.test.ts && node test/runtime-parity.test.ts"
37+
"check": "yarn sync && tsc --build tsconfig.build.json && tsc --noEmit --project tsconfig.test.json && yarn test:unit"
3838
},
3939
"dependencies": {
4040
"zod": "^4.0.0"

packages/zod/scripts/sync-v3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const indexContents = [
2424
'',
2525
].join('\n')
2626

27-
const collectFiles = async (dir, acc = []) => {
27+
const collectFiles = async (dir: string, acc: string[] = []): Promise<string[]> => {
2828
const entries = await readdir(dir, { withFileTypes: true })
2929
for (const entry of entries) {
3030
const full = resolve(dir, entry.name)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import assert from 'node:assert/strict'
2+
import { readFile } from 'node:fs/promises'
3+
import { dirname, resolve } from 'node:path'
4+
import { fileURLToPath } from 'node:url'
5+
6+
const filePath = fileURLToPath(import.meta.url)
7+
const zodRoot = resolve(dirname(filePath), '..')
8+
const packageJsonPath = resolve(zodRoot, 'package.json')
9+
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf8')) as {
10+
scripts?: Record<string, string>
11+
}
12+
13+
const scripts = packageJson.scripts ?? {}
14+
const requiredFlag = '--experimental-strip-types'
15+
const requiredScripts = ['sync:v3', 'sync:v4', 'test:unit']
16+
17+
for (const scriptName of requiredScripts) {
18+
const script = scripts[scriptName]
19+
assert.ok(script, `Missing ${scriptName} script in package.json`)
20+
assert.ok(
21+
script.includes(requiredFlag),
22+
`${scriptName} should include ${requiredFlag} so Node can run TypeScript`,
23+
)
24+
}
25+
26+
console.log('zod scripts config: ok')
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { promisify } from 'node:util'
77

88
const execFileAsync = promisify(execFile)
99

10-
const usage = () => {
11-
console.log(`Usage: node scripts/fingerprint-pack.js [path] [options]
10+
const usage = (): void => {
11+
console.log(`Usage: node --experimental-strip-types scripts/fingerprint-pack.ts [path] [options]
1212
1313
Options:
1414
-o, --out <file> Write JSON output to a file
@@ -18,7 +18,14 @@ Options:
1818
`)
1919
}
2020

21-
const parseArgs = () => {
21+
interface ParsedArgs {
22+
target: string
23+
out: string | null
24+
keep: boolean
25+
ignoreScripts: boolean
26+
}
27+
28+
const parseArgs = (): ParsedArgs => {
2229
const args = process.argv.slice(2)
2330
let target = '.'
2431
let out = null
@@ -53,7 +60,7 @@ const parseArgs = () => {
5360
return { target, out, keep, ignoreScripts }
5461
}
5562

56-
const hashFile = async (filePath) =>
63+
const hashFile = async (filePath: string): Promise<string> =>
5764
new Promise((resolvePromise, reject) => {
5865
const hash = createHash('sha256')
5966
const stream = createReadStream(filePath)
@@ -62,7 +69,10 @@ const hashFile = async (filePath) =>
6269
stream.on('end', () => resolvePromise(hash.digest('hex')))
6370
})
6471

65-
const hashTarEntry = async (tarballPath, entry) =>
72+
const hashTarEntry = async (
73+
tarballPath: string,
74+
entry: string,
75+
): Promise<{ sha256: string; sizeBytes: number }> =>
6676
new Promise((resolvePromise, reject) => {
6777
const hash = createHash('sha256')
6878
let sizeBytes = 0
@@ -87,20 +97,22 @@ const hashTarEntry = async (tarballPath, entry) =>
8797
})
8898
})
8999

90-
const readTarEntry = async (tarballPath, entry) => {
91-
const { stdout } = await execFileAsync('tar', ['-xOf', tarballPath, entry])
100+
const readTarEntry = async (tarballPath: string, entry: string): Promise<string> => {
101+
const { stdout } = await execFileAsync('tar', ['-xOf', tarballPath, entry], {
102+
encoding: 'utf8',
103+
})
92104
return stdout
93105
}
94106

95-
const main = async () => {
107+
const main = async (): Promise<void> => {
96108
const { target, out, keep, ignoreScripts } = parseArgs()
97109
const cwd = resolve(process.cwd(), target)
98110

99111
const packArgs = ['pack', '--json']
100112
if (ignoreScripts) {
101113
packArgs.push('--ignore-scripts')
102114
}
103-
const { stdout } = await execFileAsync('npm', packArgs, { cwd })
115+
const { stdout } = await execFileAsync('npm', packArgs, { cwd, encoding: 'utf8' })
104116
const packed = JSON.parse(stdout.trim())
105117
const info = Array.isArray(packed) ? packed[0] : packed
106118
if (!info?.filename) {

scripts/pack-transloadit.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ const repoRoot = resolve(dirname(filePath), '..')
1111
const legacyPackage = resolve(repoRoot, 'packages/transloadit')
1212

1313
const runPack = async () => {
14-
await execFileAsync('node', [resolve(repoRoot, 'scripts/prepare-transloadit.js')], {
15-
cwd: repoRoot,
16-
})
14+
await execFileAsync(
15+
'node',
16+
['--experimental-strip-types', resolve(repoRoot, 'scripts/prepare-transloadit.ts')],
17+
{
18+
cwd: repoRoot,
19+
},
20+
)
1721

1822
await execFileAsync('npm', ['pack', '--ignore-scripts'], {
1923
cwd: legacyPackage,

0 commit comments

Comments
 (0)