Skip to content

Commit

Permalink
fix: wait for all checkers to finish during build (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
zsparal authored Jul 18, 2021
1 parent e1a0089 commit 257358f
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions packages/vite-plugin-checker/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
UserPluginConfig,
SharedConfig,
BuildCheckBinStr,
PluginConfig,
} from './types'

export * from './types'
Expand Down Expand Up @@ -72,24 +73,12 @@ export default function Plugin(userConfig: UserPluginConfig): Plugin {
execPath: process.execPath,
})

checkers.forEach((checker) => {
const buildBin = checker.build.buildBin
const finalBin: BuildCheckBinStr =
typeof buildBin === 'function' ? buildBin(userConfig) : buildBin

const proc = spawn(...finalBin, {
cwd: process.cwd(),
stdio: 'inherit',
env: localEnv,
shell: os.platform() === 'win32',
})

proc.on('exit', (code) => {
if (code !== null && code !== 0) {
process.exit(code)
}
})
})
Promise.all(checkers.map((checker) => spawnChecker(checker, userConfig, localEnv))).then(
(exitCodes) => {
const exitCode = exitCodes.find((code) => code !== 0) || 0
process.exit(exitCode)
}
)
},
configureServer(server) {
// for dev mode (2/2)
Expand All @@ -110,3 +99,30 @@ export default function Plugin(userConfig: UserPluginConfig): Plugin {
},
}
}

function spawnChecker(
checker: ServeAndBuildChecker,
userConfig: Partial<PluginConfig>,
localEnv: npmRunPath.ProcessEnv
) {
return new Promise<number>((resolve) => {
const buildBin = checker.build.buildBin
const finalBin: BuildCheckBinStr =
typeof buildBin === 'function' ? buildBin(userConfig) : buildBin

const proc = spawn(...finalBin, {
cwd: process.cwd(),
stdio: 'inherit',
env: localEnv,
shell: os.platform() === 'win32',
})

proc.on('exit', (code) => {
if (code !== null && code !== 0) {
resolve(code)
} else {
resolve(0)
}
})
})
}

0 comments on commit 257358f

Please sign in to comment.