feat(cli): support --noSandbox option for dev and preview command

This commit is contained in:
alex8088 2023-11-08 23:03:50 +08:00
parent ac47dacc1b
commit 7369960b99
2 changed files with 21 additions and 1 deletions

View file

@ -31,9 +31,15 @@ interface DevCLIOptions {
inspect?: boolean | string inspect?: boolean | string
inspectBrk?: boolean | string inspectBrk?: boolean | string
remoteDebuggingPort?: string remoteDebuggingPort?: string
noSandbox?: boolean
rendererOnly?: boolean rendererOnly?: boolean
} }
interface PreviewCLIOptions {
noSandbox?: boolean
skipBuild?: boolean
}
function createInlineConfig(root: string, options: GlobalCLIOptions): InlineConfig { function createInlineConfig(root: string, options: GlobalCLIOptions): InlineConfig {
return { return {
root, root,
@ -71,6 +77,7 @@ cli
.option('--inspect [port]', `[boolean | number] enable V8 inspector on the specified port`) .option('--inspect [port]', `[boolean | number] enable V8 inspector on the specified port`)
.option('--inspectBrk [port]', `[boolean | number] enable V8 inspector on the specified port`) .option('--inspectBrk [port]', `[boolean | number] enable V8 inspector on the specified port`)
.option('--remoteDebuggingPort <port>', `[string] port for remote debugging`) .option('--remoteDebuggingPort <port>', `[string] port for remote debugging`)
.option('--noSandbox', `[boolean] forces renderer process to run un-sandboxed`)
.option('--rendererOnly', `[boolean] only dev server for the renderer`) .option('--rendererOnly', `[boolean] only dev server for the renderer`)
.action(async (root: string, options: DevCLIOptions & GlobalCLIOptions) => { .action(async (root: string, options: DevCLIOptions & GlobalCLIOptions) => {
if (options.remoteDebuggingPort) { if (options.remoteDebuggingPort) {
@ -85,6 +92,10 @@ cli
process.env.V8_INSPECTOR_BRK_PORT = typeof options.inspectBrk === 'number' ? `${options.inspectBrk}` : '5858' process.env.V8_INSPECTOR_BRK_PORT = typeof options.inspectBrk === 'number' ? `${options.inspectBrk}` : '5858'
} }
if (options.noSandbox) {
process.env.NO_SANDBOX = '1'
}
if (options.entry) { if (options.entry) {
process.env.ELECTRON_ENTRY = options.entry process.env.ELECTRON_ENTRY = options.entry
} }
@ -125,11 +136,16 @@ cli.command('build [root]', 'build for production').action(async (root: string,
// preview // preview
cli cli
.command('preview [root]', 'start electron app to preview production build') .command('preview [root]', 'start electron app to preview production build')
.option('--noSandbox', `[boolean] forces renderer process to run un-sandboxed`)
.option('--skipBuild', `[boolean] skip build`) .option('--skipBuild', `[boolean] skip build`)
.action(async (root: string, options: { skipBuild?: boolean } & GlobalCLIOptions) => { .action(async (root: string, options: PreviewCLIOptions & GlobalCLIOptions) => {
const { preview } = await import('./preview') const { preview } = await import('./preview')
const inlineConfig = createInlineConfig(root, options) const inlineConfig = createInlineConfig(root, options)
if (options.noSandbox) {
process.env.NO_SANDBOX = '1'
}
if (options.entry) { if (options.entry) {
process.env.ELECTRON_ENTRY = options.entry process.env.ELECTRON_ENTRY = options.entry
} }

View file

@ -136,6 +136,10 @@ export function startElectron(root: string | undefined): ChildProcess {
args.push(`--inspect-brk=${process.env.V8_INSPECTOR_BRK_PORT}`) args.push(`--inspect-brk=${process.env.V8_INSPECTOR_BRK_PORT}`)
} }
if (process.env.NO_SANDBOX === '1') {
args.push('--no-sandbox')
}
const entry = process.env.ELECTRON_ENTRY || '.' const entry = process.env.ELECTRON_ENTRY || '.'
const ps = spawn(electronPath, [entry].concat(args), { stdio: 'inherit' }) const ps = spawn(electronPath, [entry].concat(args), { stdio: 'inherit' })