Skip to content

Commit

Permalink
build: reuse "spawned" function
Browse files Browse the repository at this point in the history
  • Loading branch information
elevatebart committed Dec 12, 2021
1 parent bc3aeae commit 67289c2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 27 deletions.
9 changes: 4 additions & 5 deletions scripts/gulp/tasks/gulpGraphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { minifyIntrospectionQuery } from '@urql/introspection'

import { nexusTypegen, watchNexusTypegen } from '../utils/nexusTypegenUtil'
import { monorepoPaths } from '../monorepoPaths'
import { spawned, winSpawn } from '../utils/childProcessUtils'
import { spawn } from 'child_process'
import { spawned } from '../utils/childProcessUtils'
import { DEFAULT_INTERNAL_CLOUD_ENV } from '../gulpConstants'

export async function nexusCodegen () {
Expand Down Expand Up @@ -43,13 +42,13 @@ export async function graphqlCodegen () {
}

export async function graphqlCodegenWatch () {
const spawned = spawn(winSpawn('graphql-codegen'), ['--watch', '--config', 'graphql-codegen.yml'], {
const spawnedProcess = await spawned('gql-codegen', 'yarn graphql-codegen --watch --config graphql-codegen.yml', {
cwd: monorepoPaths.root,
})
const dfd = pDefer()
let hasResolved = false

spawned.stdout.on('data', (chunk) => {
spawnedProcess.stdout?.on('data', (chunk) => {
const strs = `${chunk}`.split('\n').filter((f) => f)
const timestampRegex = /\[\d{2}:\d{2}:\d{2}\]/
const isFailureBlock = strs.some((s) => s.includes('[failed]'))
Expand All @@ -75,7 +74,7 @@ export async function graphqlCodegenWatch () {
})
})

spawned.stderr.on('data', (data) => {
spawnedProcess.stderr?.on('data', (data) => {
console.error(chalk.red(String(data)))
})

Expand Down
19 changes: 8 additions & 11 deletions scripts/gulp/tasks/gulpWebpack.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import chalk from 'chalk'
import { spawn } from 'child_process'
import pDefer from 'p-defer'
import { monorepoPaths } from '../monorepoPaths'
import { winSpawn } from '../utils/childProcessUtils'
import { spawned } from '../utils/childProcessUtils'
import { addChildProcess } from './gulpRegistry'

export function webpackRunner () {
Expand All @@ -24,23 +23,21 @@ type RunWebpackCfg = {
export async function runWebpack (cfg: RunWebpackCfg) {
const { cwd, args = [], env = process.env, devServer = false, prefix } = cfg
const dfd = pDefer()
const spawned = spawn(
winSpawn(devServer
const spawnedProcess = await spawned('webpack-watch',
[devServer
? 'webpack-dev-server'
: 'webpack'),
args,
: 'webpack', ...args].join(' '),
{
cwd,
env: {
...(env || process.env),
FORCE_COLOR: '1',
},
},
)
})

addChildProcess(spawned)
addChildProcess(spawnedProcess)

spawned.stdout.on('data', (chunk) => {
spawnedProcess.stdout?.on('data', (chunk) => {
process.stdout.write('\n')
String(chunk)
.split('\n')
Expand All @@ -59,7 +56,7 @@ export async function runWebpack (cfg: RunWebpackCfg) {
})
})

spawned.stderr.on('data', (chunk) => {
spawnedProcess.stderr?.on('data', (chunk) => {
process.stderr.write('\n')
String(chunk)
.split('\n')
Expand Down
12 changes: 1 addition & 11 deletions scripts/gulp/utils/childProcessUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { prefixLog, prefixStream } from './prefixStream'
import { addChildProcess } from '../tasks/gulpRegistry'

export type AllSpawnableApps =
| `webpack-${string}`
| `cmd-${string}`
| `vite-${string}`
| `vite:build-${string}`
Expand Down Expand Up @@ -269,14 +270,3 @@ function streamHandler (cp: ChildProcess, config: StreamHandlerConfig) {

return dfd.promise
}

const isWin = process.platform === 'win32'

/**
* Pretreat commands to make them compatible with windows
* @param command
* @returns
*/
export function winSpawn (command: string): string {
return `${(`./node_modules/.bin/${command}`).replace(/\//g, '\\')}${isWin ? '.cmd' : ''}`
}

0 comments on commit 67289c2

Please sign in to comment.