🐛 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 { bin_name } from '..'
import { log } from '../log'
import { config, dispatch } from '../utils'
import { config, configDispatch, dispatch } from '../utils'
export const init = async (
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
logInfo('Initializing git, this may take some time')
await dispatch('git', ['init'], dir as string, false, logInfo)
await dispatch(
'git',
['checkout', '--orphan', version],
dir as string,
false,
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: ['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...')
await dispatch(
'git',
['commit', '-aqm', `"Firefox ${version}"`],
dir as string,
false,
logInfo
)
await dispatch(
'git',
['checkout', '-b', config.name.toLowerCase().replace(/\s/g, '_')],
dir as string,
false,
logInfo
)
await configDispatch('git', {
args: ['commit', '-aqm', `"Firefox ${version}"`],
cwd: dir,
logger: logInfo,
shell: 'unix',
})
await configDispatch('git', {
args: ['checkout', '-b', config.name.toLowerCase().replace(/\s/g, '_')],
cwd: dir,
logger: logInfo,
shell: 'unix',
})
}

View file

@ -1,6 +1,7 @@
// 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
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
import execa from 'execa'
import { BASH_PATH } from '../constants'
import { log } from '../log'
@ -8,16 +9,44 @@ import { log } from '../log'
export const removeTimestamp = (input: string): string =>
input.replace(/\s\d{1,5}:\d\d\.\d\d /g, '')
export const dispatch = (
export const configDispatch = (
cmd: string,
args?: string[],
cwd?: string,
killOnError?: boolean,
logger = (data: string) => log.info(data)
config?: {
args?: string[]
/**
* 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> => {
log.debug(
`Running dispatch with args; command: ${cmd}, args: ${args}, cwd: ${cwd}, killOnError: ${killOnError}`
)
const logger = config?.logger || ((data: string) => log.info(data))
// 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 d = data.toString()
@ -34,20 +63,38 @@ export const dispatch = (
return new Promise((resolve) => {
process.env.MACH_USE_SYSTEM_PYTHON = 'true'
const proc = execa(cmd, args, {
cwd: cwd || process.cwd(),
shell: BASH_PATH || false,
const proc = execa(cmd, config?.args, {
cwd: config?.cwd || process.cwd(),
shell: shell,
env: process.env,
})
proc.stdout?.on('data', (d) => handle(d))
proc.stderr?.on('data', (d) => handle(d))
proc.stdout?.on('error', (d) => handle(d, killOnError))
proc.stderr?.on('error', (d) => handle(d, killOnError))
proc.stdout?.on('error', (d) => handle(d, config?.killOnError || false))
proc.stderr?.on('error', (d) => handle(d, config?.killOnError || false))
proc.on('exit', () => {
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,
})
}