Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace execa with tinyexec #1127

Merged
merged 9 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@
"consola": "^3.2.3",
"debug": "^4.3.7",
"esbuild": "^0.23.1",
"execa": "^5.1.1",
"joycon": "^3.1.1",
"picocolors": "^1.1.0",
"postcss-load-config": "^6.0.1",
"resolve-from": "^5.0.0",
"rollup": "^4.21.3",
"source-map": "0.8.0-beta.0",
"sucrase": "^3.35.0",
"tinyexec": "^0.3.0",
"tinyglobby": "^0.2.6",
"tree-kill": "^1.2.2"
},
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isMainThread, parentPort } from 'node:worker_threads'
import * as colors from 'picocolors'
import colors from 'picocolors'

export class PrettyError extends Error {
constructor(message: string) {
Expand Down
23 changes: 12 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'node:path'
import fs from 'node:fs'
import { Worker } from 'node:worker_threads'
import { loadTsConfig } from 'bundle-require'
import execa from 'execa'
import { exec, type Result as ExecChild } from 'tinyexec'
import { glob } from 'tinyglobby'
import kill from 'tree-kill'
import { version } from '../package.json'
Expand All @@ -28,7 +28,6 @@ import { terserPlugin } from './plugins/terser'
import { runTypeScriptCompiler } from './tsc'
import { runDtsRollup } from './api-extractor'
import { cjsInterop } from './plugins/cjs-interop'
import type { ChildProcess } from 'node:child_process'
import type { Format, KILL_SIGNAL, NormalizedOptions, Options } from './options'

export type { Format, Options, NormalizedOptions }
Expand Down Expand Up @@ -272,7 +271,7 @@ export async function build(_options: Options) {

const mainTasks = async () => {
if (!options.dts?.only) {
let onSuccessProcess: ChildProcess | undefined
let onSuccessProcess: ExecChild | undefined
let onSuccessCleanup: (() => any) | undefined | void
/** Files imported by the entry */
const buildDependencies: Set<string> = new Set()
Expand Down Expand Up @@ -365,15 +364,17 @@ export async function build(_options: Options) {
if (typeof options.onSuccess === 'function') {
onSuccessCleanup = await options.onSuccess()
} else {
onSuccessProcess = execa(options.onSuccess, {
shell: true,
stdio: 'inherit',
})
onSuccessProcess.on('exit', (code) => {
if (code && code !== 0) {
process.exitCode = code
}
onSuccessProcess = exec(options.onSuccess, [], {
nodeOptions: { shell: true, stdio: 'inherit' },
})

await onSuccessProcess
if (
onSuccessProcess.exitCode &&
onSuccessProcess.exitCode !== 0
) {
process.exitCode = onSuccessProcess.exitCode
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/report-size.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from 'picocolors'
import colors from 'picocolors'
import type { Logger } from '../log'

const prettyBytes = (bytes: number) => {
Expand Down
2 changes: 1 addition & 1 deletion src/log.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import util from 'node:util'
import { isMainThread, parentPort } from 'node:worker_threads'
import * as colors from 'picocolors'
import colors from 'picocolors'

type LOG_TYPE = 'info' | 'success' | 'error' | 'warn'

Expand Down
14 changes: 7 additions & 7 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fsp from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { expect } from 'vitest'
import execa from 'execa'
import { exec } from 'tinyexec'
import { glob } from 'tinyglobby'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
Expand Down Expand Up @@ -48,16 +48,16 @@ export async function run(
const entry = options.entry || ['input.ts']

// Run tsup cli
const { exitCode, stdout, stderr } = await execa(
bin,
[...entry, ...(options.flags || [])],
{
const processPromise = exec(bin, [...entry, ...(options.flags || [])], {
nodeOptions: {
cwd: testDir,
env: { ...process.env, ...options.env },
},
)
})
const { stdout, stderr } = await processPromise

const logs = stdout + stderr
if (exitCode !== 0) {
if (processPromise.exitCode !== 0) {
throw new Error(logs)
}

Expand Down
4 changes: 2 additions & 2 deletions vitest-global.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import path from 'node:path'
import fs from 'node:fs/promises'
import execa from 'execa'
import { exec } from 'tinyexec'

export default async function setup() {
const testDir = path.resolve(__dirname, 'test')
const cacheDir = path.resolve(testDir, '.cache')
await fs.rm(cacheDir, { recursive: true, force: true })
console.log(`Installing dependencies in ./test folder`)
await execa('pnpm', ['i'], { cwd: testDir })
await exec('pnpm', ['i'], { nodeOptions: { cwd: testDir } })
console.log(`Done... start testing..`)
}