Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: apply v2 deps #342

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
"rules": {
"no-useless-constructor": "off",
"@typescript-eslint/no-useless-constructor": "error",
"unicorn/import-style": "off",
"unicorn/prefer-node-protocol": "off"
}
}
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/oclif/plugin-update/issues",
"dependencies": {
"@oclif/color": "^0.1.0",
"@oclif/core": "^1.0.1",
"@oclif/color": "^1.0.0",
"@oclif/core": "^1.0.7",
"@types/semver": "^7.3.4",
"cli-ux": "^5.5.1",
"cli-ux": "^6.0.3",
"cross-spawn": "^7.0.3",
"debug": "^4.3.1",
"filesize": "^6.1.0",
Expand All @@ -19,24 +19,24 @@
"tar-fs": "^2.1.1"
},
"devDependencies": {
"@oclif/plugin-help": "^5.1.0",
"@oclif/test": "^1.2.8",
"@oclif/plugin-help": "^5.1.7",
"@oclif/test": "^2.0.2",
"@types/chai": "^4.2.15",
"@types/cross-spawn": "^6.0.2",
"@types/execa": "^0.9.0",
"@types/fs-extra": "^8.0.1",
"@types/glob": "^7.1.3",
"@types/lodash": "^4.14.168",
"@types/mocha": "^8.2.2",
"@types/mocha": "^9",
"@types/node": "^14.14.31",
"@types/supports-color": "^7.2.0",
"@types/write-json-file": "^3.2.1",
"chai": "^4.3.4",
"eslint": "^7.21.0",
"eslint-config-oclif": "^3.1.0",
"eslint-config-oclif-typescript": "^0.2.0",
"eslint": "^7.32.0",
"eslint-config-oclif": "^4",
"eslint-config-oclif-typescript": "^1.0.2",
"globby": "^11.0.2",
"mocha": "^8.3.2",
"mocha": "^9",
"nock": "^13.2.1",
"oclif": "^2.0.0-main.5",
"qqjs": "^0.3.11",
Expand Down
26 changes: 14 additions & 12 deletions src/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import {touch} from './util'

const debug = require('debug')('oclif-update')

const ignore = (_: any, header: any) => {
switch (header.type) {
case 'directory':
case 'file':
if (process.env.OCLIF_DEBUG_UPDATE_FILES) debug(header.name)
return false
case 'symlink':
return true
default:
throw new Error(header.type)
}
}

export async function extract(stream: NodeJS.ReadableStream, basename: string, output: string, sha?: string) {
const getTmp = () => `${output}.partial.${Math.random().toString().split('.')[1].slice(0, 5)}`
let tmp = getTmp()
Expand Down Expand Up @@ -34,18 +47,6 @@ export async function extract(stream: NodeJS.ReadableStream, basename: string, o
})
} else shaValidated = true

const ignore = (_: any, header: any) => {
switch (header.type) {
case 'directory':
case 'file':
if (process.env.OCLIF_DEBUG_UPDATE_FILES) debug(header.name)
return false
case 'symlink':
return true
default:
throw new Error(header.type)
}
}
const extract = tar.extract(tmp, {ignore})
extract.on('error', reject)
extract.on('finish', () => {
Expand All @@ -69,6 +70,7 @@ export async function extract(stream: NodeJS.ReadableStream, basename: string, o
await fs.remove(output)
}
}

const from = path.join(tmp, basename)
debug('moving %s to %s', from, output)
await fs.rename(from, output)
Expand Down
24 changes: 16 additions & 8 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default class UpdateCli {
if (!await fs.pathExists(path.join(this.clientRoot, pinToVersion))) {
throw new Error(`Version ${pinToVersion} is not already installed at ${this.clientRoot}.`)
}

cli.action.start(`${this.options.config.name}: Updating CLI`)
cli.debug(`switching to existing version ${pinToVersion}`)
this.updateToExistingVersion(pinToVersion)
Expand Down Expand Up @@ -100,6 +101,7 @@ export default class UpdateCli {
if (typeof body === 'string') {
return JSON.parse(body)
}

return body
} catch (error: any) {
if (error.statusCode === 403) throw new Error(`HTTP 403: Invalid channel ${this.channel}`)
Expand All @@ -108,15 +110,15 @@ export default class UpdateCli {
}

private async downloadAndExtract(output: string, manifest: IManifest, channel: string) {
const {version} = manifest
const {version, gz, sha256gz} = manifest

const filesize = (n: number): string => {
const [num, suffix] = require('filesize')(n, {output: 'array'})
return num.toFixed(1) + ` ${suffix}`
}

const http: typeof HTTP = require('http-call').HTTP
const gzUrl = manifest.gz || this.options.config.s3Url(this.options.config.s3Key('versioned', {
const gzUrl = gz || this.options.config.s3Url(this.options.config.s3Key('versioned', {
version,
channel,
bin: this.options.config.bin,
Expand All @@ -134,12 +136,12 @@ export default class UpdateCli {
platform: this.options.config.platform,
arch: this.options.config.arch,
})
const extraction = extract(stream, baseDir, output, manifest.sha256gz)
const extraction = extract(stream, baseDir, output, sha256gz)

// to-do: use cli.action.type
if ((cli.action as any).frames) {
// if spinner action
const total = parseInt(stream.headers['content-length']!, 10)
const total = Number.parseInt(stream.headers['content-length']!, 10)
let current = 0
const updateStatus = _.throttle(
(newStatus: string) => {
Expand Down Expand Up @@ -185,10 +187,12 @@ export default class UpdateCli {
if (instructions) cli.warn(instructions)
return 'not updatable'
}

if (this.currentVersion === this.updatedVersion) {
if (this.options.config.scopedEnvVar('HIDE_UPDATED_MESSAGE')) return 'done'
return `already on latest version: ${this.currentVersion}`
}

return false
}

Expand All @@ -198,21 +202,23 @@ export default class UpdateCli {
const channel = await fs.readFile(channelPath, 'utf8')
return String(channel).trim()
}

return this.options.config.channel || 'stable'
}

private async determineCurrentVersion(): Promise<string|undefined> {
try {
const currentVersion = await fs.readFile(this.clientBin, 'utf8')
const matches = currentVersion.match(/\.\.[/|\\](.+)[/|\\]bin/)
const matches = currentVersion.match(/\.\.[/\\|](.+)[/\\|]bin/)
return matches ? matches[1] : this.options.config.version
} catch (error: any) {
cli.debug(error)
}

return this.options.config.version
}

private s3ChannelManifestKey(bin: string, platform: string, arch: string, folder?: string): string {
private s3ChannelManifestKey(bin: string, platform: string, arch: string, folder = ''): string {
let s3SubDir = folder || ''
if (s3SubDir !== '' && s3SubDir.slice(-1) !== '/') s3SubDir = `${s3SubDir}/`
return path.join(s3SubDir, 'channels', this.channel, `${bin}-${platform}-${arch}-buildmanifest`)
Expand Down Expand Up @@ -252,9 +258,11 @@ export default class UpdateCli {
await cli.log(msg)
output = true
}

await wait(60 * 1000) // wait 1 minute
return this.debounce()
}

cli.log('time to update')
}

Expand Down Expand Up @@ -312,10 +320,10 @@ export default class UpdateCli {

private async createBin(version: string) {
const dst = this.clientBin
const {bin} = this.options.config
const {bin, windows} = this.options.config
const binPathEnvVar = this.options.config.scopedEnvVarKey('BINPATH')
const redirectedEnvVar = this.options.config.scopedEnvVarKey('REDIRECTED')
if (this.options.config.windows) {
if (windows) {
const body = `@echo off
setlocal enableextensions
set ${redirectedEnvVar}=1
Expand Down
1 change: 1 addition & 0 deletions test/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('update plugin', () => {
if (fs.pathExistsSync(clientRoot)) {
fs.removeSync(clientRoot)
}

sandbox.restore()
})
it('should not update - already on same version', async () => {
Expand Down
Loading