🐛 Only use bash shell for git init

This commit is contained in:
trickypr 2022-04-10 16:30:02 +10:00
parent 5bf2b5ac87
commit d0e6248fd2
2 changed files with 104 additions and 37 deletions

View file

@ -7,7 +7,7 @@ import Listr from 'listr'
import { resolve } from 'path' import { resolve } from 'path'
import { bin_name } from '..' import { bin_name } from '..'
import { log } from '../log' import { log } from '../log'
import { config, dispatch } from '../utils' import { config, configDispatch, dispatch } from '../utils'
export const init = async ( export const init = async (
directory: Command | string, directory: Command | string,
@ -51,28 +51,48 @@ export const init = async (
// TODO: Use bash on windows, this may significantly improve performance. Still needs testing though // TODO: Use bash on windows, this may significantly improve performance. Still needs testing though
logInfo('Initializing git, this may take some time') logInfo('Initializing git, this may take some time')
await dispatch('git', ['init'], dir as string, false, logInfo)
await dispatch( await configDispatch('git', {
'git', args: ['init'],
['checkout', '--orphan', version], cwd: dir,
dir as string, logger: logInfo,
false, shell: 'unix',
logInfo })
)
await dispatch('git', ['add', '-f', '.'], dir as string, false, logInfo) await configDispatch('git', {
args: ['init'],
cwd: dir,
logger: logInfo,
shell: 'unix',
})
await configDispatch('git', {
args: ['checkout', '--orphan', version],
cwd: dir,
logger: logInfo,
shell: 'unix',
})
await configDispatch('git', {
args: ['add', '-f', '.'],
cwd: dir,
logger: logInfo,
shell: 'unix',
})
logInfo('Committing...') logInfo('Committing...')
await dispatch(
'git', await configDispatch('git', {
['commit', '-aqm', `"Firefox ${version}"`], args: ['commit', '-aqm', `"Firefox ${version}"`],
dir as string, cwd: dir,
false, logger: logInfo,
logInfo shell: 'unix',
) })
await dispatch(
'git', await configDispatch('git', {
['checkout', '-b', config.name.toLowerCase().replace(/\s/g, '_')], args: ['checkout', '-b', config.name.toLowerCase().replace(/\s/g, '_')],
dir as string, cwd: dir,
false, logger: logInfo,
logInfo shell: 'unix',
) })
} }

View file

@ -1,6 +1,7 @@
// This Source Code Form is subject to the terms of the Mozilla Public // This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
import execa from 'execa' import execa from 'execa'
import { BASH_PATH } from '../constants' import { BASH_PATH } from '../constants'
import { log } from '../log' import { log } from '../log'
@ -8,16 +9,44 @@ import { log } from '../log'
export const removeTimestamp = (input: string): string => export const removeTimestamp = (input: string): string =>
input.replace(/\s\d{1,5}:\d\d\.\d\d /g, '') input.replace(/\s\d{1,5}:\d\d\.\d\d /g, '')
export const dispatch = ( export const configDispatch = (
cmd: string, cmd: string,
args?: string[], config?: {
cwd?: string, args?: string[]
killOnError?: boolean, /**
logger = (data: string) => log.info(data) * The current working directory this should be run in. Defaults to
* `process.cwd()`
*/
cwd?: string
killOnError?: boolean
logger?: (data: string) => void
/**
* Chose what shell you should be using for the operating system
*/
shell?: 'default' | 'unix'
}
): Promise<boolean> => { ): Promise<boolean> => {
log.debug( const logger = config?.logger || ((data: string) => log.info(data))
`Running dispatch with args; command: ${cmd}, args: ${args}, cwd: ${cwd}, killOnError: ${killOnError}`
) // Decide what shell we should be using. False will use the system default
let shell: string | boolean = false
if (config?.shell) {
switch (config.shell) {
// Don't change anything if we are using the default shell
case 'default':
break
case 'unix':
// Bash path provides a unix shell on windows
shell = BASH_PATH || false
break
default:
log.error(`dispatch() does not understand the shell '${shell}'`)
break
}
}
const handle = (data: string | Error, killOnError?: boolean) => { const handle = (data: string | Error, killOnError?: boolean) => {
const d = data.toString() const d = data.toString()
@ -34,20 +63,38 @@ export const dispatch = (
return new Promise((resolve) => { return new Promise((resolve) => {
process.env.MACH_USE_SYSTEM_PYTHON = 'true' process.env.MACH_USE_SYSTEM_PYTHON = 'true'
const proc = execa(cmd, args, { const proc = execa(cmd, config?.args, {
cwd: cwd || process.cwd(), cwd: config?.cwd || process.cwd(),
shell: BASH_PATH || false, shell: shell,
env: process.env, env: process.env,
}) })
proc.stdout?.on('data', (d) => handle(d)) proc.stdout?.on('data', (d) => handle(d))
proc.stderr?.on('data', (d) => handle(d)) proc.stderr?.on('data', (d) => handle(d))
proc.stdout?.on('error', (d) => handle(d, killOnError)) proc.stdout?.on('error', (d) => handle(d, config?.killOnError || false))
proc.stderr?.on('error', (d) => handle(d, killOnError)) proc.stderr?.on('error', (d) => handle(d, config?.killOnError || false))
proc.on('exit', () => { proc.on('exit', () => {
resolve(true) resolve(true)
}) })
}) })
} }
/**
* @deprecated Use configDispatch instead
*/
export const dispatch = (
cmd: string,
args?: string[],
cwd?: string,
killOnError?: boolean,
logger = (data: string) => log.info(data)
): Promise<boolean> => {
return configDispatch(cmd, {
args: args,
cwd: cwd,
killOnError: killOnError,
logger: logger,
})
}