Skip to content

Commit

Permalink
chore: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 10, 2023
1 parent 84d12a3 commit a884b36
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 49 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"lint-staged": "^14.0.1",
"mocha": "^10.2.0",
"nock": "^13.3.2",
"oclif": "^3.11.3",
"oclif": "^4",
"prettier": "^3.0.3",
"qqjs": "^0.3.11",
"quibble": "^0.8.0",
Expand Down Expand Up @@ -80,16 +80,16 @@
},
"repository": "oclif/plugin-update",
"scripts": {
"build": "rm -rf lib && tsc",
"lint": "eslint . --ext .ts",
"pretest": "yarn build --noEmit && tsc -p test --noEmit",
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
"test:integration:sf": "mocha --forbid-only \"test/integration/sf.integration.ts\" --timeout 900000",
"postpack": "rm -f oclif.manifest.json",
"posttest": "yarn lint",
"prepack": "yarn build && oclif manifest . && oclif lock",
"prepack": "yarn build && oclif lock && oclif manifest . && oclif lock",
"prepare": "husky install",
"postpack": "rm -f oclif.manifest.json",
"version": "oclif readme && git add README.md",
"build": "rm -rf lib && tsc"
"pretest": "yarn build --noEmit && tsc -p test --noEmit",
"test:integration:sf": "mocha --forbid-only \"test/integration/sf.integration.ts\" --timeout 900000",
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
"version": "oclif readme && git add README.md"
},
"exports": "./dist/index.js",
"type": "module"
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export const init: Interfaces.Hook<'init'> = async function (opts) {
if (opts.config.scopedEnvVarTrue('DISABLE_AUTOUPDATE')) return

const {config, error: throwError} = this
const binPath = config.binPath || config.bin
const binPath = config.binPath ?? config.bin
const lastrunfile = join(config.cacheDir, 'lastrun')
const autoupdatefile = join(config.cacheDir, 'autoupdate')
const autoupdatelogfile = join(config.cacheDir, 'autoupdate.log')
const clientRoot = config.scopedEnvVar('OCLIF_CLIENT_HOME') || join(config.dataDir, 'client')
const clientRoot = config.scopedEnvVar('OCLIF_CLIENT_HOME') ?? join(config.dataDir, 'client')

const autoupdateEnv = {
...process.env,
Expand All @@ -44,7 +44,6 @@ export const init: Interfaces.Hook<'init'> = async function (opts) {
} catch (error: unknown) {
const err = error as {code: string; stack: string}
if (err.code !== 'ENOENT') throwError(err.stack)
if ((global as unknown as {testing: boolean}).testing) return false
debug('autoupdate ENOENT')
return true
}
Expand Down
8 changes: 2 additions & 6 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Updater {
private readonly clientRoot: string

constructor(private config: Config) {
this.clientRoot = config.scopedEnvVar('OCLIF_CLIENT_HOME') || join(config.dataDir, 'client')
this.clientRoot = config.scopedEnvVar('OCLIF_CLIENT_HOME') ?? join(config.dataDir, 'client')
this.clientBin = join(this.clientRoot, 'bin', config.windows ? `${config.bin}.cmd` : config.bin)
}

Expand All @@ -38,11 +38,7 @@ export class Updater {
const newIndexUrl = this.config.s3Url(s3VersionIndexKey(this.config))
try {
const {body} = await HTTP.get<VersionIndex>(newIndexUrl)
if (typeof body === 'string') {
return JSON.parse(body)
}

return body
return typeof body === 'string' ? JSON.parse(body) : body
} catch {
throw new Error(`No version indices exist for ${this.config.name}.`)
}
Expand Down
42 changes: 20 additions & 22 deletions test/integration/sf.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {expect} from 'chai'
import {default as got} from 'got'
import {ExecOptions, exec as cpExec} from 'node:child_process'
import {createWriteStream} from 'node:fs'
import {mkdir, readFile, readdir, rm, writeFile} from 'node:fs/promises'
import {mkdir, readFile, readdir, rm} from 'node:fs/promises'
import {tmpdir} from 'node:os'
import {join} from 'node:path'
import {rsort} from 'semver'

const makeTestDir = async (): Promise<string> => {
const tmpDir = join(tmpdir(), 'sf-update-test')
Expand Down Expand Up @@ -71,22 +72,29 @@ describe('sf integration', () => {
let stableVersion: string
let sf: string

const versionToUpdateTo = '2.12.7-esm.0'
let versionToUpdateTo: string
const channel = 'nightly'

const tarball =
process.platform === 'win32'
? `https://developer.salesforce.com/media/salesforce-cli/sf/channels/${channel}/sf-win32-x64.tar.gz`
: `https://developer.salesforce.com/media/salesforce-cli/sf/channels/${channel}/sf-linux-x64.tar.gz`
const platform = process.platform === 'win32' ? 'win32' : 'linux'
const tarball = `https://developer.salesforce.com/media/salesforce-cli/sf/channels/${channel}/sf-${platform}-x64.tar.gz`

before(async () => {
console.log('Setting up test environment...')

const {stdout} = await exec('npm view @salesforce/cli --json')
const distTags = JSON.parse(stdout)['dist-tags']
const result = JSON.parse(stdout)
const distTags = result['dist-tags']
const sortedVersions = rsort(result.versions)
const channelIndex = sortedVersions.indexOf(distTags[channel])

versionToUpdateTo = sortedVersions[channelIndex + 1].toString()
stableVersion = distTags.latest

console.log('Testing with:')
console.log(`• channel: ${channel} (${distTags[channel]})`)
console.log(`• version to update to: ${versionToUpdateTo}`)
console.log(`• stable version: ${stableVersion}`)

testDir = await makeTestDir()
console.log(`Test directory: ${testDir}`)

Expand All @@ -112,7 +120,7 @@ describe('sf integration', () => {

await download(tarball, tarLocation)
const cmd =
process.platform === 'win32'
platform === 'win32'
? `tar -xf ${tarLocation} -C ${extractedLocation} --strip-components 1 --exclude node_modules/.bin`
: `tar -xf ${tarLocation} -C ${extractedLocation} --strip-components 1`

Expand All @@ -130,7 +138,7 @@ describe('sf integration', () => {
// but since we're using bin/run.js to avoid the global sf, we need to set it manually
process.env.SF_BINPATH = sf

if (process.platform === 'win32') {
if (platform === 'win32') {
// append cmd /c to the command so that it can run on windows
sf = `cmd /c "node ${sf}"`
}
Expand All @@ -142,17 +150,7 @@ describe('sf integration', () => {
console.log('Success!')

console.log('Linking plugin-update...')
// Running `plugins link` is very slow on github-action's windows runners. Writing this file
// directly is much faster and accomplishes the same thing (except for re-installing deps)
const userPjson = {
dependencies: {},
oclif: {
plugins: [{name: '@oclif/plugin-update', root: process.cwd(), type: 'link'}],
},
private: true,
}
await writeFile(join(dataDir, 'package.json'), JSON.stringify(userPjson, null, 2))

await exec(`${sf} plugins link ${process.cwd()} --no-install`, {cwd: testDir})
const pluginsResults = await exec(`${sf} plugins`, {cwd: testDir})
console.log(pluginsResults.stdout)
expect(pluginsResults.code).to.equal(0)
Expand All @@ -173,7 +171,7 @@ describe('sf integration', () => {
'new version to be added to client directory',
).to.be.true

if (process.platform === 'win32') {
if (platform === 'win32') {
const {stdout} = await exec(`${join(clientDir, 'bin', 'sf.cmd')} version --json`)
const version = JSON.parse(stdout).cliVersion.replace('@salesforce/cli/', '')
expect(version, 'version in SF_DATA_DIR\\bin\\sf.cmd to be the updated version').to.equal(versionToUpdateTo)
Expand All @@ -196,7 +194,7 @@ describe('sf integration', () => {
'new version to be added to client directory',
).to.be.true

if (process.platform === 'win32') {
if (platform === 'win32') {
const {stdout} = await exec(`${join(clientDir, 'bin', 'sf.cmd')} version --json`)
const version = JSON.parse(stdout).cliVersion.replace('@salesforce/cli/', '')
expect(version, 'version in SF_DATA_DIR\\bin\\sf.cmd to be the updated version').to.equal(stableVersion)
Expand Down
Loading

0 comments on commit a884b36

Please sign in to comment.