Skip to content

Commit

Permalink
fix: no error when using --force
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed May 13, 2024
1 parent 8ca7a04 commit 45d56be
Show file tree
Hide file tree
Showing 5 changed files with 829 additions and 780 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"chalk": "^5",
"debug": "^4.3.1",
"filesize": "^6.1.0",
"http-call": "^5.3.0",
"lodash.throttle": "^4.1.1",
"got": "^14.2.1",
"semver": "^7.6.0",
"tar-fs": "^2.1.1"
},
Expand All @@ -22,7 +21,6 @@
"@types/chai": "^4.3.11",
"@types/debug": "^4.1.12",
"@types/execa": "^0.9.0",
"@types/lodash.throttle": "^4.1.9",
"@types/mocha": "^10",
"@types/node": "^18",
"@types/semver": "^7.5.8",
Expand All @@ -33,7 +31,6 @@
"eslint-config-oclif": "^5.2.0",
"eslint-config-oclif-typescript": "^3.1.7",
"eslint-config-prettier": "^9.1.0",
"got": "^13.0.0",
"husky": "^9",
"lint-staged": "^15",
"mocha": "^10.4.0",
Expand Down
6 changes: 2 additions & 4 deletions src/tar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import makeDebug from 'debug'
import {existsSync} from 'node:fs'
import {cp, rename, rm} from 'node:fs/promises'
import {rename, rm} from 'node:fs/promises'
import {join} from 'node:path'

import {touch} from './util.js'
Expand Down Expand Up @@ -68,9 +68,7 @@ async function extract(stream: NodeJS.ReadableStream, basename: string, output:

if (existsSync(output)) {
try {
const tmp = getTmp()
await cp(output, tmp)
await rm(tmp, {force: true, recursive: true}).catch(debug)
await rm(output, {force: true, recursive: true}).catch(debug)
} catch (error: unknown) {
debug(error)
await rm(tmp, {force: true, recursive: true}).catch(debug)
Expand Down
31 changes: 11 additions & 20 deletions src/update.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {Config, Interfaces, ux} from '@oclif/core'
import chalk from 'chalk'
import fileSize from 'filesize'
import {HTTP} from 'http-call'
import throttle from 'lodash.throttle'
import {got} from 'got'
import {Stats, existsSync} from 'node:fs'
import {mkdir, readFile, readdir, rm, stat, symlink, utimes, writeFile} from 'node:fs/promises'
import {basename, dirname, join} from 'node:path'
Expand Down Expand Up @@ -37,7 +36,7 @@ export class Updater {
ux.action.status = 'fetching version index'
const newIndexUrl = this.config.s3Url(s3VersionIndexKey(this.config))
try {
const {body} = await HTTP.get<VersionIndex>(newIndexUrl)
const {body} = await got.get<VersionIndex>(newIndexUrl)
return typeof body === 'string' ? JSON.parse(body) : body
} catch {
throw new Error(`No version indices exist for ${this.config.name}.`)
Expand Down Expand Up @@ -265,7 +264,6 @@ const ensureClientDir = async (clientRoot: string): Promise<void> => {
}
}

// eslint-disable-next-line unicorn/no-await-expression-member
const mtime = async (f: string): Promise<Date> => (await stat(f)).mtime

const notUpdatable = (config: Config): boolean => {
Expand Down Expand Up @@ -294,7 +292,7 @@ const fetchManifest = async (s3Key: string, config: Config): Promise<Interfaces.
ux.action.status = 'fetching manifest'

const url = config.s3Url(s3Key)
const {body} = await HTTP.get<Interfaces.S3Manifest | string>(url)
const {body} = await got.get<Interfaces.S3Manifest | string>(url)
if (typeof body === 'string') {
return JSON.parse(body)
}
Expand Down Expand Up @@ -385,7 +383,8 @@ const downloadAndExtract = async (
version,
}),
)
const {response: stream} = await HTTP.stream(gzUrl)
const stream = got.stream(gzUrl)

stream.pause()

const baseDir =
Expand All @@ -400,18 +399,11 @@ const downloadAndExtract = async (
const extraction = Extractor.extract(stream, baseDir, output, sha256gz)

if (ux.action.type === 'spinner') {
const total = Number.parseInt(stream.headers['content-length']!, 10)
let current = 0
const updateStatus = throttle(
(newStatus: string) => {
ux.action.status = newStatus
},
250,
{leading: true, trailing: false},
)
stream.on('data', (data) => {
current += data.length
updateStatus(`${filesize(current)}/${filesize(total)}`)
stream.on('downloadProgress', (progress) => {
ux.action.status =
progress.percent === 1
? `${filesize(progress.transferred)}/${filesize(progress.total)} - Extracting`
: `${filesize(progress.transferred)}/${filesize(progress.total)}`
})
}

Expand All @@ -422,15 +414,14 @@ const downloadAndExtract = async (
const determineChannel = async ({config, version}: {config: Config; version?: string}): Promise<string> => {
const channelPath = join(config.dataDir, 'channel')

// eslint-disable-next-line unicorn/no-await-expression-member
const channel = existsSync(channelPath) ? (await readFile(channelPath, 'utf8')).trim() : 'stable'

if (config.pjson.oclif.update?.disableNpmLookup ?? false) {
return channel
}

try {
const {body} = await HTTP.get<{'dist-tags': Record<string, string>}>(
const {body} = await got.get<{'dist-tags': Record<string, string>}>(
`${config.npmRegistry ?? 'https://registry.npmjs.org'}/${config.pjson.name}`,
)
const tags = body['dist-tags']
Expand Down
15 changes: 5 additions & 10 deletions test/update.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Config, Interfaces, ux} from '@oclif/core'
import {expect} from 'chai'
import {HTTP} from 'http-call'
import {got} from 'got'
import nock from 'nock'
import {existsSync} from 'node:fs'
import {mkdir, rm, symlink, writeFile} from 'node:fs/promises'
Expand All @@ -16,7 +16,7 @@ type OutputCollectors = {
stderr: string[]
stdout: string[]
}
async function loadConfig(options: {root: string}): Promise<Interfaces.Config> {
async function loadConfig(options: {root: string}): Promise<Config> {
return Config.load(options.root)
}

Expand Down Expand Up @@ -51,7 +51,7 @@ describe('update plugin', () => {
const sandbox = createSandbox()

beforeEach(async () => {
config = (await loadConfig({root: path.join(process.cwd(), 'examples', 's3-update-example-cli')})) as Config
config = await loadConfig({root: path.join(process.cwd(), 'examples', 's3-update-example-cli')})
config.binPath = config.binPath || config.bin
collector = {stderr: [], stdout: []}
sandbox.stub(ux, 'log').callsFake((line) => collector.stdout.push(line || ''))
Expand Down Expand Up @@ -100,7 +100,6 @@ describe('update plugin', () => {
await writeFile(path.join(`${newVersionPath}.partial.11111`, 'bin', 'example-cli'), '../2.0.1/bin', 'utf8')

sandbox.stub(Extractor, 'extract').resolves()
sandbox.stub(zlib, 'gzipSync').returns(Buffer.alloc(1, ' '))

const gzContents = zlib.gzipSync(' ')

Expand Down Expand Up @@ -136,7 +135,6 @@ describe('update plugin', () => {
const indexRegex = new RegExp(`example-cli-${config.platform}-${config.arch}-tar-gz.json`)

sandbox.stub(Extractor, 'extract').resolves()
sandbox.stub(zlib, 'gzipSync').returns(Buffer.alloc(1, ' '))

const gzContents = zlib.gzipSync(' ')

Expand Down Expand Up @@ -165,7 +163,7 @@ describe('update plugin', () => {
})

it('will get the correct channel and use default registry', async () => {
const request = sandbox.spy(HTTP, 'get')
const request = sandbox.spy(got, 'get')
const hash = 'f289627'
config.pjson.name = '@oclif/plugin-update'
clientRoot = await setupClientRoot({config})
Expand All @@ -180,7 +178,6 @@ describe('update plugin', () => {
const indexRegex = new RegExp(`example-cli-${config.platform}-${config.arch}-tar-gz.json`)

sandbox.stub(Extractor, 'extract').resolves()
sandbox.stub(zlib, 'gzipSync').returns(Buffer.alloc(1, ' '))

const gzContents = zlib.gzipSync(' ')

Expand Down Expand Up @@ -208,7 +205,7 @@ describe('update plugin', () => {
expect(request.firstCall.args[0]).to.include('https://registry.npmjs.org/@oclif/plugin-update')
})
it('will get the correct channel and use a custom registry', async () => {
const request = sandbox.spy(HTTP, 'get')
const request = sandbox.spy(got, 'get')
const hash = 'f289627'
config.pjson.name = '@oclif/plugin-update'
config.npmRegistry = 'https://myCustomRegistry.com'
Expand All @@ -224,7 +221,6 @@ describe('update plugin', () => {
const indexRegex = new RegExp(`example-cli-${config.platform}-${config.arch}-tar-gz.json`)

sandbox.stub(Extractor, 'extract').resolves()
sandbox.stub(zlib, 'gzipSync').returns(Buffer.alloc(1, ' '))

const gzContents = zlib.gzipSync(' ')

Expand Down Expand Up @@ -281,7 +277,6 @@ describe('update plugin', () => {
await writeFile(path.join(`${newVersionPath}.partial.11111`, 'bin', 'example-cli'), '../2.0.1/bin', 'utf8')
await writeFile(path.join(newVersionPath, 'bin', 'example-cli'), '../2.0.1/bin', 'utf8')
sandbox.stub(Extractor, 'extract').resolves()
sandbox.stub(zlib, 'gzipSync').returns(Buffer.alloc(1, ' '))

const gzContents = zlib.gzipSync(' ')

Expand Down

0 comments on commit 45d56be

Please sign in to comment.