Skip to content

Commit

Permalink
feat(builder): support wait renderer url(dev mode)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcherGu committed Jun 1, 2022
1 parent 0390523 commit 7b71b30
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 274 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
],
"author": "",
"scripts": {
"build": "pnpm -r --filter=./packages/* run build",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
},
"devDependencies": {
"@antfu/eslint-config": "^0.23.1",
"@types/node": "^17.0.35",
"esbuild": "^0.14.39",
"eslint": "^8.16.0",
"lint-staged": "^12.4.1",
"rimraf": "^3.0.2",
Expand Down
12 changes: 8 additions & 4 deletions packages/builder/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { TAG } from './constants'
const logger = createLogger()

export async function build(type: AppType) {
const isElectron = type === 'electron'
const startTime = performance.now()

logger.info(TAG, `Mode: ${bgCyanBright('Production')}`)
logger.info(TAG, `Application type: ${type === 'electron' ? bgCyan(' electron ') : bgGreen(' node ')}`)
logger.info(TAG, `Application type: ${isElectron ? bgCyan(' electron ') : bgGreen(' node ')}`)

const config = await resolveConfig()

Expand All @@ -39,14 +41,16 @@ export async function build(type: AppType) {

await config.afterBuild?.()

if (type === 'electron' && config.electronBuild && config.electronBuild.disabled !== true) {
const { electron: electronConfig } = config

if (isElectron && electronConfig.build && electronConfig.build.disabled !== true) {
logger.info(TAG, 'Start electron build...\n')

await electronBuilder({
config: config.electronBuild.config,
config: electronConfig.build.config,
})

await config.electronBuild.afterBuild?.()
await electronConfig.build.afterBuild?.()
}

const endTime = performance.now() - startTime
Expand Down
25 changes: 18 additions & 7 deletions packages/builder/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import JoyCon from 'joycon'
import { bundleRequire } from 'bundle-require'
import type { Configuration as ElectronBuilderConfiguration } from 'electron-builder'
import { greenBright } from 'colorette'
import type { Options as TsupOptions } from 'tsup'
import { merge, normalizePath } from './utils'
import { createLogger } from './log'
Expand All @@ -24,23 +25,28 @@ export type TsupBuildConfig = Pick<TsupOptions, 'entry' | 'outDir' | 'tsconfig'
tsupConfig?: string | TsupOptions
}

export interface ElectronConfig {
preload?: TsupBuildConfig
build?: ElectronBuildConfig
rendererUrl?: string
waitForRenderer?: boolean
waitTimeout?: number
}

export interface DoubleShotBuilderConfig extends TsupBuildConfig {
/**
* @default 'package.json'.main
*/
main?: string
electron?: {
preload?: TsupBuildConfig
build?: ElectronBuildConfig
}
electron?: ElectronConfig
afterBuild?: () => Promise<void>
}

export type ResolvedConfig = Readonly<{
cwd: string
configFile: string | undefined
tsupConfigs: TsupOptions[]
electronBuild?: ElectronBuildConfig
electron: Omit<ElectronConfig, 'preload'>
} & Pick<DoubleShotBuilderConfig, 'main' | 'afterBuild'>>

export function defineConfig(config: DoubleShotBuilderConfigExport): DoubleShotBuilderConfigExport {
Expand All @@ -63,7 +69,7 @@ export async function resolveConfig(): Promise<ResolvedConfig> {
})

if (configPath) {
logger.info(TAG, `Using doubleshot builder config: ${configPath}\n`)
logger.info(TAG, `Using config: ${greenBright(configPath)}\n`)

const { mod } = await bundleRequire({
filepath: configPath,
Expand Down Expand Up @@ -115,7 +121,12 @@ export async function resolveConfig(): Promise<ResolvedConfig> {
main: config.main ? normalizePath(path.resolve(cwd, config.main)) : undefined,
configFile: normalizePath(configPath),
tsupConfigs: tsupConfigArr,
electronBuild: resoleElectronBuilderConfig(config.electron?.build, cwd),
electron: {
build: resoleElectronBuilderConfig(config.electron?.build, cwd),
rendererUrl: config.electron?.rendererUrl,
waitForRenderer: config.electron?.waitForRenderer,
waitTimeout: config.electron?.waitTimeout,
},
afterBuild: config.afterBuild,
}
}
Expand Down
52 changes: 44 additions & 8 deletions packages/builder/node/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import fs from 'fs'
import type { Options as TsupOptions } from 'tsup'
import { build as tsupBuild } from 'tsup'
import electron from 'electron'
import { bgCyan, bgCyanBright, bgGreen } from 'colorette'
import { bgCyan, bgCyanBright, bgGreen, cyan, greenBright } from 'colorette'
import waitOn from 'wait-on'
import { TAG } from './constants'
import { resolveConfig } from './config'
import type { AppType } from './config'
import { createLogger } from './log'
import { TAG } from './constants'

const logger = createLogger()

Expand All @@ -18,17 +19,19 @@ function exitMainProcess() {
process.exit(0)
}

function runMainProcess(mainFile: string, type: AppType = 'node') {
function runMainProcess(mainFile: string, isElectron: boolean) {
if (!fs.existsSync(mainFile))
throw new Error(`Main file not found: ${mainFile}`)

logger.success(TAG, `⚡ Run main file: ${path.basename(mainFile)}`)
return spawn(type === 'electron' ? electron as any : 'node', [mainFile], { stdio: 'inherit' }).on('exit', exitMainProcess)
logger.success(TAG, `⚡ Run main file: ${greenBright(mainFile)}`)
return spawn(isElectron ? electron as any : 'node', [mainFile], { stdio: 'inherit' }).on('exit', exitMainProcess)
}

export async function dev(type: AppType) {
const isElectron = type === 'electron'

logger.info(TAG, `Mode: ${bgCyanBright('Development')}`)
logger.info(TAG, `Application type: ${type === 'electron' ? bgCyan(' electron ') : bgGreen(' node ')}`)
logger.info(TAG, `Application type: ${isElectron ? bgCyan(' electron ') : bgGreen(' node ')}`)

const config = await resolveConfig()
let child: ChildProcess
Expand Down Expand Up @@ -69,7 +72,7 @@ export async function dev(type: AppType) {
child.kill()
}

child = runMainProcess(mainFile!, type)
child = runMainProcess(mainFile!, isElectron)
}
},
}
Expand All @@ -83,5 +86,38 @@ export async function dev(type: AppType) {
})
}

child = runMainProcess(mainFile, type)
const { electron: electronConfig } = config

if (isElectron && electronConfig.rendererUrl && electronConfig.waitForRenderer !== false) {
const url = electronConfig.rendererUrl
if (url.startsWith('http://') || url.startsWith('https://') || url.startsWith('file://')) {
logger.info(TAG, `🚦 Wait for renderer: ${cyan(url)}`)
await waitOn(createWaitOnOpts(url, electronConfig.waitTimeout))
}
else {
logger.warn(TAG, `Invalid renderer url: ${url}, ignored.\n`)
}
}

child = runMainProcess(mainFile, isElectron)
}

/**
* See: https://github.com/jeffbski/wait-on/issues/78
*
* @param {string} url
* @param {(number | undefined)} timeout
* @returns
*/
function createWaitOnOpts(url: string, timeout: number | undefined) {
if (url.startsWith('http://') || url.startsWith('https://'))
url = url.startsWith('http://') ? url.replace('http://', 'http-get://') : url.replace('https://', 'https-get://')

return {
resources: [url],
timeout: timeout || 5000,
headers: {
accept: '*/*',
},
}
}
11 changes: 7 additions & 4 deletions packages/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
"colorette": "^2.0.16",
"joycon": "^3.1.1",
"resolve-from": "^5.0.0",
"rimraf": "^3.0.2",
"tsup": "^6.0.1"
"tsup": "^6.0.1",
"wait-on": "^6.0.1"
},
"devDependencies": {
"@types/wait-on": "^5.3.1",
"electron": "^19.0.1",
"electron-builder": "^23.0.3",
"typescript": "^4.7.2"
"electron-builder": "^23.0.3"
},
"engines": {
"node": ">=14.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/nest-electron-ipc-transport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"devDependencies": {
"@nestjs/common": "^8.4.5",
"@nestjs/core": "^8.4.5",
"@nestjs/microservices": "^8.4.5",
"@nestjs/microservices": "8.4.5",
"electron": "^19.0.0",
"reflect-metadata": "^0.1.13"
}
Expand Down
3 changes: 2 additions & 1 deletion packages/runner/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import JoyCon from 'joycon'
import { bundleRequire } from 'bundle-require'
import type { Configuration as ElectronBuilderConfiguration } from 'electron-builder'
import type { ConcurrentlyCommandInput } from 'concurrently'
import { greenBright } from 'colorette'
import { normalizePath } from './utils'
import { createLogger } from './log'
import { CONFIG_FILE, TAG } from './constants'
Expand Down Expand Up @@ -70,7 +71,7 @@ export async function resolveConfig(): Promise<ResolvedConfig> {
})

if (configPath) {
logger.info(TAG, `Using doubleshot runner config: ${configPath}\n`)
logger.info(TAG, `Using config: ${greenBright(configPath)}\n`)

const { mod } = await bundleRequire({
filepath: configPath,
Expand Down
7 changes: 2 additions & 5 deletions packages/runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@
"cac": "^6.7.12",
"colorette": "^2.0.16",
"concurrently": "^7.2.1",
"esbuild": "^0.14.39",
"joycon": "^3.1.1",
"resolve-from": "^5.0.0",
"rimraf": "^3.0.2"
"resolve-from": "^5.0.0"
},
"devDependencies": {
"electron-builder": "^23.0.3",
"typescript": "^4.7.2"
"electron-builder": "^23.0.3"
},
"engines": {
"node": ">=14.0.0"
Expand Down
Loading

0 comments on commit 7b71b30

Please sign in to comment.