Skip to content

Commit

Permalink
fix: optional peer dependencies check
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcherGu committed Jun 2, 2022
1 parent e5635d3 commit ab4857d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
}
}
}
1 change: 1 addition & 0 deletions packages/builder/node/log.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Borrowed from https://github.com/egoist/tsup/blob/main/src/log.ts
import * as colors from 'colorette'

type LOG_TYPE = 'info' | 'success' | 'error' | 'warn'
Expand Down
32 changes: 24 additions & 8 deletions packages/builder/node/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import type { ChildProcess } from 'child_process'
import { spawn } from 'child_process'
import fs from 'fs'
import { bgCyan, bgCyanBright, bgGreen, cyan, greenBright } from 'colorette'
import { build as electronBuilder } from 'electron-builder'
import { build as tsupBuild } from 'tsup'
import electron from 'electron'
import type { Options as TsupOptions } from 'tsup'
import waitOn from 'wait-on'
import { checkPackageExists } from 'check-package-exists'
import { TAG } from './constants'
import { resolveConfig } from './config'
import type { AppType } from './config'
Expand Down Expand Up @@ -44,12 +43,12 @@ function exitMainProcess() {
process.exit(0)
}

function runMainProcess(mainFile: string, isElectron: boolean) {
function runMainProcess(mainFile: string, electron: any) {
if (!fs.existsSync(mainFile))
throw new Error(`Main file not found: ${mainFile}`)

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

/**
Expand Down Expand Up @@ -79,13 +78,22 @@ function doTsupBuild(opts: TsupOptions) {
})
}

function electronEnvCheck() {
if (!checkPackageExists('electron'))
throw new Error('"Application type: electron" is powered by "electron", please installed it via `npm i electron -D`')

return true
}

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: ${isElectron ? bgCyan(' electron ') : bgGreen(' node ')}`)

isElectron && electronEnvCheck()

const config = await resolveConfig()

getMainFileAndCheck(config.cwd, config.main)
Expand All @@ -98,6 +106,11 @@ export async function build(type: AppType) {
const { electron: electronConfig } = config

if (isElectron && electronConfig.build && electronConfig.build.disabled !== true) {
if (!checkPackageExists('electron-builder'))
throw new Error('"electronConfig.build" is powered by "electron-builder", please installed it via `npm i electron-builder -D`')

const { build: electronBuilder } = await import('electron-builder')

logger.info(TAG, 'Start electron build...\n')

await electronBuilder({
Expand All @@ -117,6 +130,10 @@ export async function dev(type: AppType) {
logger.info(TAG, `Mode: ${bgCyanBright('Development')}`)
logger.info(TAG, `Application type: ${isElectron ? bgCyan(' electron ') : bgGreen(' node ')}`)

let electron: any | undefined
if (isElectron && electronEnvCheck())
electron = await import('electron')

const config = await resolveConfig()
let child: ChildProcess

Expand All @@ -132,7 +149,7 @@ export async function dev(type: AppType) {
userOnRebuild = options.watch.onRebuild

options.watch = {
onRebuild(error, result) {
onRebuild: async (error, result) => {
userOnRebuild?.(error, result)

if (error) {
Expand All @@ -145,7 +162,7 @@ export async function dev(type: AppType) {
child.kill()
}

child = runMainProcess(mainFile!, isElectron)
child = runMainProcess(mainFile!, electron)
}
},
}
Expand All @@ -167,6 +184,5 @@ export async function dev(type: AppType) {
}
}

child = runMainProcess(mainFile, isElectron)
child = runMainProcess(mainFile, electron)
}

1 change: 1 addition & 0 deletions packages/runner/node/log.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Borrowed from https://github.com/egoist/tsup/blob/main/src/log.ts
import * as colors from 'colorette'

type LOG_TYPE = 'info' | 'success' | 'error' | 'warn'
Expand Down
11 changes: 9 additions & 2 deletions packages/runner/node/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import path from 'path'
import { performance } from 'perf_hooks'
import type { ConcurrentlyCommandInput } from 'concurrently'
import concurrently from 'concurrently'
import { build as electronBuilder } from 'electron-builder'
import { yellow } from 'colorette'
import { checkPackageExists } from 'check-package-exists'
import type { ElectronBuildConfig } from './config'
import { resolveConfig } from './config'
import { createLogger } from './log'
Expand Down Expand Up @@ -73,18 +73,25 @@ export async function run(command: string) {
logger.success(TAG, 'All commands finished successfully')
}, () => {
logger.warn(TAG, 'Some commands exit')
}).catch((e) => {
logger.error(TAG, e)
}).finally(() => {
logger.info(TAG, 'Exiting...')
process.exit(0)
})
}

async function doElectronBuild(buildConfig: ElectronBuildConfig | undefined) {
if (!checkPackageExists('electron-builder'))
throw new Error('"electronBuild" config is powered by "electron-builder", please installed it via `npm i electron-builder -D`')

const { build } = await import('electron-builder')

const logger = createLogger()
const startTime = performance.now()
try {
logger.info(`\n[${TAG}] electron-builder`, 'Start electron build...\n')
await electronBuilder({
await build({
projectDir: buildConfig?.projectDir || process.cwd(),
config: buildConfig?.config,
})
Expand Down
1 change: 1 addition & 0 deletions packages/runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"bundle-require": "^3.0.4",
"cac": "^6.7.12",
"check-package-exists": "^1.0.0",
"colorette": "^2.0.16",
"concurrently": "^7.2.1",
"esbuild": "^0.14.42",
Expand Down
19 changes: 11 additions & 8 deletions pnpm-lock.yaml

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

0 comments on commit ab4857d

Please sign in to comment.