Skip to content

Commit

Permalink
chore(http-codex): Switch from esbuild to tsc
Browse files Browse the repository at this point in the history
  • Loading branch information
jahands committed Feb 13, 2025
1 parent cdfeb39 commit 3433a68
Show file tree
Hide file tree
Showing 28 changed files with 533 additions and 91 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-wolves-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@repo/typescript-config': minor
---

Add lib-emit.json
5 changes: 5 additions & 0 deletions .changeset/sharp-insects-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@repo/tools': patch
---

Add / improve scripts
5 changes: 5 additions & 0 deletions .changeset/swift-files-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'http-codex': minor
---

chore: Switch from esbuild to tsc
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
],
"github-actions.workflows.pinned.refresh.enabled": true,
"files.associations": {
"**/packages/tools/bin/*": "shellscript"
"**/packages/tools/bin/*": "shellscript",
"**/packages/typescript-config/*.json": "jsonc"
},
"eslint.workingDirectories": [
{
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"changeset:new": "run-changeset-new",
"check:ci": "turbo check:ci --log-order=grouped",
"test:ci": "run-vitest-ci",
"check:deps": "syncpack lint",
"check:deps": "runx check -l",
"check:format": "prettier . --check --cache --ignore-unknown",
"chore:update:deps": "run-update-deps",
"chore:update:tools": "run-s chore:update:tools:* fix:deps",
Expand All @@ -29,6 +29,7 @@
"publish": "changeset publish"
},
"devDependencies": {
"@arethetypeswrong/cli": "0.17.3",
"@changesets/cli": "2.27.11",
"@ianvs/prettier-plugin-sort-imports": "4.4.0",
"@repo/eslint-config": "workspace:*",
Expand Down
14 changes: 7 additions & 7 deletions packages/http-codex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,33 @@
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
"default": "./dist/index.js"
}
},
"./status": {
"import": {
"types": "./dist/status.d.ts",
"default": "./dist/status.mjs"
"default": "./dist/status.js"
}
}
},
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"main": "./dist/index.js",
"module": "./dist/index.js",
"files": [
"dist"
],
"scripts": {
"build": "bun ./scripts/build.ts",
"build": "runx build tsc",
"check:exports": "runx check -e",
"check:lint": "run-eslint-workers",
"check:types": "run-tsc",
"check:types": "run-s build check:exports",
"test": "run-vitest"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "0.6.0",
"@cloudflare/workers-types": "4.20250109.0",
"@repo/tools": "workspace:*",
"@repo/typescript-config": "workspace:*",
"esbuild": "0.24.2",
"rimraf": "6.0.1",
"typescript": "5.5.4",
"vitest": "2.1.1",
Expand Down
24 changes: 0 additions & 24 deletions packages/http-codex/scripts/build-types.ts

This file was deleted.

23 changes: 0 additions & 23 deletions packages/http-codex/scripts/build.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/http-codex/scripts/entrypoints.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/http-codex/src/combined.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, expect, it } from 'vitest'
import { z } from 'zod'

import { httpStatus } from './combined'
import { httpStatus } from './combined.js'

import type { HttpStatusCodeName } from '.'
import type { HttpStatusCodeName } from './index.js'

describe('httpStatus', () => {
const all = Object.keys(httpStatus).filter((key) => (key as keyof typeof httpStatus) !== 'text')
Expand Down
4 changes: 2 additions & 2 deletions packages/http-codex/src/combined.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { httpStatus as statusCodes } from './status'
import { text } from './statusText'
import { httpStatus as statusCodes } from './status.js'
import { text } from './statusText.js'

/**
* HTTP status codes as registered with IANA.
Expand Down
6 changes: 3 additions & 3 deletions packages/http-codex/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { HttpStatusCodeName, HttpStatusCode } from './status'
export type { HttpStatusCodeName, HttpStatusCode } from './status.js'

export { isNullBodyStatus } from './status'
export { httpStatus } from './combined'
export { isNullBodyStatus } from './status.js'
export { httpStatus } from './combined.js'
2 changes: 1 addition & 1 deletion packages/http-codex/src/status.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'

import { httpStatus, isNullBodyStatus, nullBodyStatuses } from './status'
import { httpStatus, isNullBodyStatus, nullBodyStatuses } from './status.js'

describe('isNullBodyStatus()', () => {
const nonNullBodyStatuses = Object.values(httpStatus).filter(
Expand Down
4 changes: 2 additions & 2 deletions packages/http-codex/src/statusText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Commit: 55590f3a2b89f001bcadf0df6eb2dde62618302b
*/

import { httpStatus } from './status'
import { httpStatus } from './status.js'

import type { HttpStatusCode } from './status'
import type { HttpStatusCode } from './status.js'

/**
* statusText returns a text for the HTTP status code. It returns the empty
Expand Down
2 changes: 1 addition & 1 deletion packages/http-codex/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@repo/typescript-config/workers-lib.json",
"extends": "@repo/typescript-config/lib-emit.json",
"include": ["**/*.ts"],
"exclude": ["node_modules", "dist", "tsconfig.json"]
}
2 changes: 2 additions & 0 deletions packages/tools/src/bin/runx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'zx/globals'
import { program } from '@commander-js/extra-typings'

import { buildCmd } from '../cmd/build'
import { checkCmd } from '../cmd/check'
import { deployCmd } from '../cmd/deploy'
import { parseChangesetCmd } from '../cmd/parse-changeset'
import { pkgCmd } from '../cmd/pkg/pkg'
Expand All @@ -16,6 +17,7 @@ program

.addCommand(updatePnpmCmd)
.addCommand(parseChangesetCmd)
.addCommand(checkCmd)
.addCommand(buildCmd)
.addCommand(deployCmd)
.addCommand(sentryCmd)
Expand Down
49 changes: 49 additions & 0 deletions packages/tools/src/cmd/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { inspect } from 'util'
import { Command } from '@commander-js/extra-typings'
import ts from 'typescript'
import { z } from 'zod'

export const buildCmd = new Command('build').description('Build Workers/etc.')

Expand All @@ -10,3 +13,49 @@ buildCmd
await $`rm -rf ./dist` // Make sure we don't have any previous artifacts
await $`wrangler deploy --minify --outdir ./dist --dry-run`
})

buildCmd
.command('tsc')
.description('Build a library with tsc')
.option('-r, --root-dir <string>', 'Root dir for tsc to use (overrides tsconfig.json)', 'src')
.action(async ({ rootDir }) => {
await $`rm -rf ./dist` // Make sure we don't have any previous artifacts

// Read the parent config so that we don't get
// weird path issues from being in a monorepo
const parentTsConfig = z
.object({ extends: z.string() })
.parse(JSON.parse(fs.readFileSync('./tsconfig.json').toString())).extends

const tsconfig = ts.readConfigFile(`./node_modules/${parentTsConfig}`, ts.sys.readFile)
if (tsconfig.error) {
throw new Error(`failed to read tsconfig: ${inspect(tsconfig)}`)
}

const jsonCompopts = getCompilerOptionsJSONFollowExtends('tsconfig.json')
const tmp = ts.convertCompilerOptionsFromJson(jsonCompopts, '')
if (tmp.errors.length > 0) {
throw new Error(`failed parse config: ${inspect(tmp)}`)
}
const tsCompopts: ts.CompilerOptions = tmp.options

const config = {
...tsCompopts,
rootDir: rootDir,
} satisfies ts.CompilerOptions

ts.createProgram(['./src/index.ts', './src/status.ts'], config).emit()
})

function getCompilerOptionsJSONFollowExtends(filename: string): { [key: string]: unknown } {
let compopts = {}
const config = ts.readConfigFile(filename, ts.sys.readFile).config
if (config.extends !== undefined) {
const rqrpath = require.resolve(config.extends)
compopts = getCompilerOptionsJSONFollowExtends(rqrpath)
}
return {
...compopts,
...config.compilerOptions,
}
}
116 changes: 116 additions & 0 deletions packages/tools/src/cmd/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Command } from '@commander-js/extra-typings'
import Table from 'cli-table3'

import { getRepoRoot } from '../path'
import { getOutcome } from '../proc'

export const checkCmd = new Command('check')
.description(
'Check for issues with deps/lint/types/format. If no options are provided, all default checks are run.'
)

.option('-r, --root', 'Run checks from root of repo. Defaults to cwd', false)

// Default checks
.option('-d, --deps', 'Check for dependency issues with Syncpack')
.option('-l, --lint', 'Check for eslint issues')
.option('-t, --types', 'Check for TypeScript issues')
.option('-f, --format', 'Check for formatting issues with prettier')

// Non-default checks
.option('-e, --exports', 'Checks package exports')

.action(async ({ root, deps, lint, types, format, exports }) => {
const repoRoot = getRepoRoot()
if (root) {
cd(repoRoot)
}
// Run all default checks if none are selected
if (!deps && !lint && !types && !format && !exports) {
deps = true
lint = true
types = true
format = true
}

const cwd = process.cwd()
const runFromRoot = cwd === repoRoot
const cwdName = path.basename(cwd)

const checks = {
deps: ['syncpack', 'lint'],
lint: ['turbo', 'check:lint'],
format: ['runx', 'prettier', 'check'],
types: ['turbo', 'check:types'],
exports: ['attw', '--pack', '.', '--profile=esm-only'],
} as const satisfies { [key: string]: string[] }

type TableRow = [string, string, string, string]
const table = new Table({
head: [
chalk.whiteBright('Name'),
chalk.whiteBright('Command'),
chalk.whiteBright('Outcome'),
chalk.whiteBright('Ran From'),
] satisfies TableRow,
})

$.stdio = 'inherit'
$.verbose = true
$.nothrow = true

if (deps) {
await within(async () => {
cd(repoRoot) // Must be run from root
const exitCode = await $`${checks.deps}`.exitCode
table.push(['deps', `${checks.deps}`, getOutcome(exitCode), 'Root'] satisfies TableRow)
})
}

if (lint) {
const exitCode = await $`${checks.lint}`.exitCode
table.push([
'lint',
checks.lint.join(' '),
getOutcome(exitCode),
runFromRoot ? 'Root' : `cwd (${cwdName})`,
] satisfies TableRow)
}

if (types) {
const exitCode = await $`${checks.types}`.exitCode
table.push([
'types',
checks.types.join(' '),
getOutcome(exitCode),
runFromRoot ? 'Root' : `cwd (${cwdName})`,
] satisfies TableRow)
}

if (format) {
await within(async () => {
cd(repoRoot) // Must be run from root
const exitCode = await $`${checks.format}`.exitCode
table.push([
'format',
checks.format.join(' '),
getOutcome(exitCode),
'Root',
] satisfies TableRow)
})
}

if (exports) {
await within(async () => {
const exitCode = await $`${checks.exports}`.exitCode
table.push([
'exports',
checks.exports.join(' '),
getOutcome(exitCode),
'Root',
] satisfies TableRow)
})
}

echo(table.toString())
})
2 changes: 1 addition & 1 deletion packages/tools/src/cmd/parse-changeset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const parseChangesetCmd = new Command('parse-changeset')
.option('--strip-packages', 'Strip packages from changeset')
.option('-v, --verbose', 'Verbose output')
.action(async (path, { stripPackages, verbose }) => {
const repoRoot = await getRepoRoot()
const repoRoot = getRepoRoot()
cd(repoRoot)
verbose && echo(chalk.blue(`Parsing changeset from ${path}`))
let changeset = (await fs.readFile(path)).toString()
Expand Down
Loading

0 comments on commit 3433a68

Please sign in to comment.