Add force option to download command

This commit is contained in:
trickypr 2022-11-15 12:40:51 +11:00
parent e79d4b099c
commit 77a63264b7
4 changed files with 24 additions and 12 deletions

View file

@ -64,8 +64,14 @@ export const commands: Cmd[] = [
requestController: async () => (await import('./commands/discard')).discard, requestController: async () => (await import('./commands/discard')).discard,
}, },
{ {
cmd: 'download [ffVersion]', cmd: 'download',
description: 'Download Firefox.', description: 'Download Firefox.',
options: [
{
arg: '--force',
description: 'Delete the engine directory if it already exists',
}
],
requestController: async () => requestController: async () =>
(await import('./commands/download')).download, (await import('./commands/download')).download,
}, },

View file

@ -4,12 +4,15 @@
import { bin_name, config } from '..' import { bin_name, config } from '..'
import { log } from '../log' import { log } from '../log'
import { import {
downloadInternals downloadInternals
} from './download/firefox' } from './download/firefox'
export const download = async (): Promise<void> => { type Options = {
force?: boolean
}
export const download = async (options: Options): Promise<void> => {
const version = config.version.version const version = config.version.version
// If gFFVersion isn't specified, provide legible error // If gFFVersion isn't specified, provide legible error
@ -20,7 +23,7 @@ export const download = async (): Promise<void> => {
process.exit(1) process.exit(1)
} }
await downloadInternals(version) await downloadInternals({version, force: options.force})
log.success( log.success(
`You should be ready to make changes to ${config.name}.`, `You should be ready to make changes to ${config.name}.`,

View file

@ -1,6 +1,8 @@
import execa from 'execa' import execa from 'execa'
import { existsSync, rmSync, writeFileSync } from 'node:fs' import { existsSync, rmSync, writeFileSync } from 'node:fs'
import { readdir } from 'node:fs/promises'
import { dirname, resolve } from 'node:path' import { dirname, resolve } from 'node:path'
import { bin_name } from '../..' import { bin_name } from '../..'
import { BASH_PATH, ENGINE_DIR, MELON_TMP_DIR } from '../../constants' import { BASH_PATH, ENGINE_DIR, MELON_TMP_DIR } from '../../constants'
import { log } from '../../log' import { log } from '../../log'
@ -19,7 +21,6 @@ import {
unpackAddon, unpackAddon,
} from './addon' } from './addon'
import { configPath } from '../../utils' import { configPath } from '../../utils'
import { readdir } from 'node:fs/promises'
export function shouldSetupFirefoxSource() { export function shouldSetupFirefoxSource() {
return !( return !(
@ -111,7 +112,7 @@ async function downloadFirefoxSource(version: string) {
return filename return filename
} }
export async function downloadInternals(version: string) { export async function downloadInternals({ version, force }: { version: string, force?: boolean }) {
// Provide a legible error if there is no version specified // Provide a legible error if there is no version specified
if (!version) { if (!version) {
log.error( log.error(
@ -120,6 +121,11 @@ export async function downloadInternals(version: string) {
process.exit(1) process.exit(1)
} }
if (force && existsSync(ENGINE_DIR)) {
log.info('Removing existing workspace')
rmSync(ENGINE_DIR, { recursive: true })
}
// If the engine directory is empty, we should delete it. // If the engine directory is empty, we should delete it.
const engineIsEmpty = await readdir(ENGINE_DIR).then((files) => files.length === 0) const engineIsEmpty = await readdir(ENGINE_DIR).then((files) => files.length === 0)
if (existsSync(ENGINE_DIR) && engineIsEmpty) { if (existsSync(ENGINE_DIR) && engineIsEmpty) {

View file

@ -1,7 +1,6 @@
// 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 { existsSync, rmSync } from 'node:fs'
import { bin_name, config } from '..' import { bin_name, config } from '..'
import { log } from '../log' import { log } from '../log'
@ -9,15 +8,13 @@ import {
downloadInternals downloadInternals
} from './download/firefox' } from './download/firefox'
import { getLatestFF } from '../utils' import { getLatestFF } from '../utils'
import { ENGINE_DIR } from '../constants'
export const update = async (): Promise<void> => { export const update = async (): Promise<void> => {
const version = await getLatestFF(config.version.product) const version = await getLatestFF(config.version.product)
// Delete the existing engine directory to download the new version // We are using force here to delete the engine directory if it already
if (existsSync(ENGINE_DIR)) rmSync(ENGINE_DIR, { recursive: true }) // exists to make way for the new version.
await downloadInternals({version, force: true})
await downloadInternals(version)
log.success( log.success(
`Firefox has successfully been updated to ${version}.`, `Firefox has successfully been updated to ${version}.`,