Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Diana Sentoso committed Jun 7, 2021
1 parent eea1b80 commit 3be6770
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 138 deletions.
115 changes: 59 additions & 56 deletions src/commands/install.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,75 @@
import cli from 'cli-ux'
import * as semver from 'semver'
import * as fs from 'fs-extra'
import cli from 'cli-ux';
import * as semver from 'semver';
import * as fs from 'fs-extra';

import UpdateCommand from './update'
import UpdateCommand from './update';

const SEMVER_REGEX = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?/
const SEMVER_REGEX =
/^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?/;
export default class InstallCommand extends UpdateCommand {
static args = [{name: 'version', optional: false}]
static args = [{ name: 'version', optional: false }];

static flags = {}
static flags = {};

async run() {
const {args} = this.parse(InstallCommand)
async run() {
const { args } = this.parse(InstallCommand);

// Check if this command is trying to update the channel. TODO: make this dynamic
const prereleaseChannels = ['alpha', 'beta', 'next']
const isExplicitVersion = SEMVER_REGEX.test(args.version || '')
const channelUpdateRequested = ['stable', ...prereleaseChannels].some(
c => args.version === c,
)
// Check if this command is trying to update the channel. TODO: make this dynamic
const prereleaseChannels = ['alpha', 'beta', 'next'];
const isExplicitVersion = SEMVER_REGEX.test(args.version || '');
const channelUpdateRequested = ['stable', ...prereleaseChannels].some(
(c) => args.version === c,
);

if (!isExplicitVersion && !channelUpdateRequested) {
throw new Error(
`Invalid argument provided: ${args.version}. Please specify either a valid channel (alpha, beta, next, stable) or an explicit version (ex. 2.68.13)`,
)
}
if (!isExplicitVersion && !channelUpdateRequested) {
throw new Error(
`Invalid argument provided: ${args.version}. Please specify either a valid channel (alpha, beta, next, stable) or an explicit version (ex. 2.68.13)`,
);
}

this.channel = channelUpdateRequested ?
args.version :
await this.determineChannel()
this.channel = channelUpdateRequested
? args.version
: await this.determineChannel();

const targetVersion = semver.clean(args.version) || args.version
// Determine if the version is from a different channel and update to account for it (ex. cli-example update 3.0.0-next.22 should update the channel to next as well.)
const versionParts = targetVersion?.split('-') || ['', '']
if (versionParts && versionParts[1]) {
this.channel = versionParts[1].substr(0, versionParts[1].indexOf('.'))
this.debug(`Flag overriden target channel: ${this.channel}`)
}
const targetVersion = semver.clean(args.version) || args.version;
// Determine if the version is from a different channel and update to account for it (ex. cli-example update 3.0.0-next.22 should update the channel to next as well.)
const versionParts = targetVersion?.split('-') || ['', ''];
if (versionParts && versionParts[1]) {
this.channel = versionParts[1].substr(0, versionParts[1].indexOf('.'));
this.debug(`Flag overriden target channel: ${this.channel}`);
}

const versions = fs
const versions = fs
.readdirSync(this.clientRoot)
.filter(dirOrFile => dirOrFile !== 'bin' && dirOrFile !== 'current')
.filter((dirOrFile) => dirOrFile !== 'bin' && dirOrFile !== 'current');

if (versions.includes(targetVersion)) {
await this.updateToExistingVersion(targetVersion)
this.currentVersion = await this.determineCurrentVersion()
this.updatedVersion = targetVersion
if (channelUpdateRequested) {
await this.setChannel()
}
} else {
const explicitVersion = isExplicitVersion ? targetVersion : null
cli.action.start(`${this.config.name}: Updating CLI`)
await this.config.runHook('preupdate', {channel: this.channel})
const manifest = await this.fetchManifest()
this.currentVersion = await this.determineCurrentVersion()
if (versions.includes(targetVersion)) {
await this.updateToExistingVersion(targetVersion);
this.currentVersion = await this.determineCurrentVersion();
this.updatedVersion = targetVersion;
if (channelUpdateRequested) {
await this.setChannel();
}
} else {
const explicitVersion = isExplicitVersion ? targetVersion : null;
cli.action.start(`${this.config.name}: Updating CLI`);
await this.config.runHook('preupdate', { channel: this.channel });
const manifest = await this.fetchManifest();
this.currentVersion = await this.determineCurrentVersion();

this.updatedVersion = (manifest as any).sha ? `${targetVersion}-${(manifest as any).sha}` : targetVersion
this.debug(`Updating to ${this.updatedVersion}`)
const reason = await this.skipUpdate()
if (reason) cli.action.stop(reason || 'done')
else await this.update(manifest, this.channel, explicitVersion)
this.debug('tidy')
await this.tidy()
await this.config.runHook('update', {channel: this.channel})
this.updatedVersion = (manifest as any).sha
? `${targetVersion}-${(manifest as any).sha}`
: targetVersion;
this.debug(`Updating to ${this.updatedVersion}`);
const reason = await this.skipUpdate();
if (reason) cli.action.stop(reason || 'done');
else await this.update(manifest, this.channel, explicitVersion);
this.debug('tidy');
await this.tidy();
await this.config.runHook('update', { channel: this.channel });

this.debug('done')
cli.action.stop()
}
this.debug('done');
cli.action.stop();
}
}
}
88 changes: 55 additions & 33 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,34 +154,46 @@ export default class UpdateCommand extends Command {
}
}

protected async downloadAndExtract(output: string, manifest: IManifest, channel: string, targetVersion?: string) {
const {version} = manifest
protected async downloadAndExtract(
output: string,
manifest: IManifest,
channel: string,
targetVersion?: string,
) {
const { version } = manifest;

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

const http: typeof HTTP = require('http-call').HTTP
const gzUrl = !targetVersion && manifest.gz ? manifest.gz : this.config.s3Url(this.config.s3Key('versioned', {
version: targetVersion,
channel,
bin: this.config.bin,
platform: this.config.platform,
arch: this.config.arch,
ext: targetVersion ? 'tar.gz' : 'gz',
}))
const {response: stream} = await http.stream(gzUrl)
stream.pause()

const baseDir = manifest.baseDir || this.config.s3Key('baseDir', {
version,
channel,
bin: this.config.bin,
platform: this.config.platform,
arch: this.config.arch,
})
const extraction = extract(stream, baseDir, output, manifest.sha256gz)
const http: typeof HTTP = require('http-call').HTTP;
const gzUrl =
!targetVersion && manifest.gz
? manifest.gz
: this.config.s3Url(
this.config.s3Key('versioned', {
version: targetVersion,
channel,
bin: this.config.bin,
platform: this.config.platform,
arch: this.config.arch,
ext: targetVersion ? 'tar.gz' : 'gz',
}),
);
const { response: stream } = await http.stream(gzUrl);
stream.pause();

const baseDir =
manifest.baseDir ||
this.config.s3Key('baseDir', {
version,
channel,
bin: this.config.bin,
platform: this.config.platform,
arch: this.config.arch,
});
const extraction = extract(stream, baseDir, output, manifest.sha256gz);

// to-do: use cli.action.type
if ((cli.action as any).frames) {
Expand All @@ -205,16 +217,26 @@ export default class UpdateCommand extends Command {
await extraction;
}

protected async update(manifest: IManifest, channel = this.channel, targetVersion?: string) {
const {channel: manifestChannel} = manifest
if (manifestChannel) channel = manifestChannel
cli.action.start(`${this.config.name}: Updating CLI from ${color.green(this.currentVersion)} to ${color.green(this.updatedVersion)}${channel === 'stable' ? '' : ' (' + color.yellow(channel) + ')'}`)
protected async update(
manifest: IManifest,
channel = this.channel,
targetVersion?: string,
) {
const { channel: manifestChannel } = manifest;
if (manifestChannel) channel = manifestChannel;
cli.action.start(
`${this.config.name}: Updating CLI from ${color.green(
this.currentVersion,
)} to ${color.green(this.updatedVersion)}${
channel === 'stable' ? '' : ' (' + color.yellow(channel) + ')'
}`,
);

await this.ensureClientDir()
const output = path.join(this.clientRoot, this.updatedVersion)
await this.ensureClientDir();
const output = path.join(this.clientRoot, this.updatedVersion);

if (!await fs.pathExists(output)) {
await this.downloadAndExtract(output, manifest, channel, targetVersion)
if (!(await fs.pathExists(output))) {
await this.downloadAndExtract(output, manifest, channel, targetVersion);
}

await this.setChannel();
Expand Down
Loading

0 comments on commit 3be6770

Please sign in to comment.