Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Commit

Permalink
fix: refactor to use s3Key/s3Url from @oclif/config
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Apr 10, 2018
1 parent ccbc9bf commit 8c96067
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 62 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -9,7 +9,7 @@
"bugs": "https://github.com/oclif/dev-cli/issues",
"dependencies": {
"@oclif/command": "^1.4.13",
"@oclif/config": "^1.4.13",
"@oclif/config": "^1.6.0",
"@oclif/errors": "^1.0.4",
"@oclif/plugin-help": "^1.2.3",
"@oclif/plugin-warn-if-update-available": "^1.3.0",
Expand Down
15 changes: 8 additions & 7 deletions src/commands/publish/index.ts
@@ -1,4 +1,5 @@
import {Command, flags} from '@oclif/command'
import {ArchTypes, PlatformTypes} from '@oclif/config'
import * as qq from 'qqjs'

import aws from '../../aws'
Expand All @@ -20,25 +21,25 @@ export default class Publish extends Command {
const {flags} = this.parse(Publish)
if (process.platform === 'win32') throw new Error('publish does not function on windows')
this.buildConfig = await Tarballs.buildConfig(flags.root)
const {s3Config, targets, dist, version} = this.buildConfig
if (!await qq.exists(dist(this.buildConfig.path('versioned', {ext: '.tar.gz'})))) this.error('run "oclif-dev pack" before publishing')
const {s3Config, targets, dist, version, config} = this.buildConfig
if (!await qq.exists(dist(config.s3Key('versioned', {ext: '.tar.gz'})))) this.error('run "oclif-dev pack" before publishing')
const S3Options = {
Bucket: s3Config.bucket!,
ACL: 'public-read',
}
// for (let target of targets) await this.uploadNodeBinary(target)
const ManifestS3Options = {...S3Options, CacheControl: 'max-age=86400', ContentType: 'application/json'}
const uploadTarball = async (options?: {platform: string, arch: string}) => {
const uploadTarball = async (options?: {platform: PlatformTypes, arch: ArchTypes}) => {
const TarballS3Options = {...S3Options, CacheControl: 'max-age=604800'}
const releaseTarballs = async (ext: string) => {
const versioned = this.buildConfig.path('versioned', {...options, ext})
const unversioned = this.buildConfig.path('unversioned', {...options, ext})
const releaseTarballs = async (ext: '.tar.gz' | '.tar.xz') => {
const versioned = config.s3Key('versioned', ext, options)
const unversioned = config.s3Key('unversioned', ext, options)
await aws.s3.uploadFile(dist(versioned), {...TarballS3Options, ContentType: 'application/gzip', Key: versioned})
await aws.s3.uploadFile(dist(versioned), {...TarballS3Options, ContentType: 'application/gzip', Key: unversioned})
}
await releaseTarballs('.tar.gz')
if (this.buildConfig.xz) await releaseTarballs('.tar.xz')
const manifest = this.buildConfig.path('manifest', options)
const manifest = config.s3Key('manifest', options)
await aws.s3.uploadFile(dist(manifest), {...ManifestS3Options, Key: manifest})
}
if (targets.length) log('uploading targets')
Expand Down
48 changes: 25 additions & 23 deletions src/tarballs/build.ts
@@ -1,3 +1,4 @@
import {ArchTypes, PlatformTypes} from '@oclif/config'
import * as path from 'path'
import * as qq from 'qqjs'

Expand All @@ -19,6 +20,7 @@ const pack = async (from: string, to: string) => {
}

export async function build(c: IConfig) {
const {xz, config} = c
const prevCwd = qq.cwd()
const packCLI = async () => {
const stdout = await qq.x.stdout('npm', ['pack'], {cwd: c.root})
Expand Down Expand Up @@ -63,9 +65,9 @@ export async function build(c: IConfig) {
await qq.rm(...toRemove)
await qq.rmIfEmpty('.')
}
const buildTarget = async (target: {platform: string, arch: string}) => {
const buildTarget = async (target: {platform: PlatformTypes, arch: ArchTypes}) => {
const workspace = c.workspace(target)
const key = c.path('versioned', {ext: '.tar.gz', ...target})
const key = config.s3Key('versioned', '.tar.gz', target)
const base = path.basename(key)
log(`building target ${base}`)
await qq.rm(workspace)
Expand All @@ -75,51 +77,51 @@ export async function build(c: IConfig) {
output: path.join(workspace, 'bin', 'node'),
platform: target.platform,
arch: target.arch,
tmp: qq.join(c.config.root, 'tmp'),
tmp: qq.join(config.root, 'tmp'),
})
await pack(workspace, c.dist(key))
if (c.xz) await pack(workspace, c.dist(c.path('versioned', {ext: '.tar.xz', ...target})))
if (xz) await pack(workspace, c.dist(config.s3Key('versioned', '.tar.xz', target)))
const manifest: IManifest = {
rollout: (typeof c.updateConfig.autoupdate === 'object' && c.updateConfig.autoupdate.rollout) as number,
version: c.version,
channel: c.channel,
baseDir: c.path('baseDir', target),
gz: c.s3Url(c.path('versioned', {ext: '.tar.gz', ...target})),
xz: c.xz ? c.s3Url(c.path('versioned', {ext: '.tar.xz', ...target})) : undefined,
sha256gz: await qq.hash('sha256', c.dist(c.path('versioned', {ext: '.tar.gz', ...target}))),
sha256xz: c.xz ? await qq.hash('sha256', c.dist(c.path('versioned', {ext: '.tar.xz', ...target}))) : undefined,
baseDir: config.s3Key('baseDir', target),
gz: config.s3Url(config.s3Key('versioned', '.tar.gz', target)),
xz: xz ? config.s3Url(config.s3Key('versioned', '.tar.xz', target)) : undefined,
sha256gz: await qq.hash('sha256', c.dist(config.s3Key('versioned', '.tar.gz', target))),
sha256xz: xz ? await qq.hash('sha256', c.dist(config.s3Key('versioned', '.tar.xz', target))) : undefined,
node: {
compatible: c.config.pjson.engines.node,
compatible: config.pjson.engines.node,
recommended: c.nodeVersion,
}
}
await qq.writeJSON(c.dist(c.path('manifest', target)), manifest)
await qq.writeJSON(c.dist(config.s3Key('manifest', target)), manifest)
}
const buildBaseTarball = async () => {
await pack(c.workspace(), c.dist(c.path('versioned', {ext: '.tar.gz'})))
if (c.xz) await pack(c.workspace(), c.dist(c.path('versioned', {ext: '.tar.xz'})))
await pack(c.workspace(), c.dist(config.s3Key('versioned', '.tar.gz')))
if (xz) await pack(c.workspace(), c.dist(config.s3Key('versioned', '.tar.xz')))
const manifest: IManifest = {
version: c.version,
baseDir: c.path('baseDir'),
channel: c.config.channel,
gz: c.s3Url(c.path('versioned', {ext: '.tar.gz'})),
xz: c.s3Url(c.path('versioned', {ext: '.tar.xz'})),
sha256gz: await qq.hash('sha256', c.dist(c.path('versioned', {ext: '.tar.gz'}))),
sha256xz: c.xz ? await qq.hash('sha256', c.dist(c.path('versioned', {ext: '.tar.xz'}))) : undefined,
baseDir: config.s3Key('baseDir'),
channel: config.channel,
gz: config.s3Url(config.s3Key('versioned', '.tar.gz')),
xz: config.s3Url(config.s3Key('versioned', '.tar.xz')),
sha256gz: await qq.hash('sha256', c.dist(config.s3Key('versioned', '.tar.gz'))),
sha256xz: xz ? await qq.hash('sha256', c.dist(config.s3Key('versioned', '.tar.xz'))) : undefined,
rollout: (typeof c.updateConfig.autoupdate === 'object' && c.updateConfig.autoupdate.rollout) as number,
node: {
compatible: c.config.pjson.engines.node,
compatible: config.pjson.engines.node,
recommended: c.nodeVersion,
},
}
await qq.writeJSON(c.dist(c.path('manifest')), manifest)
await qq.writeJSON(c.dist(config.s3Key('manifest')), manifest)
}
log(`gathering workspace for ${c.config.bin} to ${c.workspace()}`)
log(`gathering workspace for ${config.bin} to ${c.workspace()}`)
await extractCLI(await packCLI())
await updatePJSON()
await addDependencies()
await prune()
await writeBinScripts({config: c.config, baseWorkspace: c.workspace(), nodeVersion: c.nodeVersion})
await writeBinScripts({config, baseWorkspace: c.workspace(), nodeVersion: c.nodeVersion})
await buildBaseTarball()
for (let target of c.targets) await buildTarget(target)
qq.cd(prevCwd)
Expand Down
35 changes: 5 additions & 30 deletions src/tarballs/config.ts
@@ -1,18 +1,6 @@
import * as Config from '@oclif/config'
import {CLIError} from '@oclif/errors'
import * as _ from 'lodash'
import * as path from 'path'
import * as qq from 'qqjs'
import {URL} from 'url'

export namespace IConfig {
export type PathTypes = 'manifest' | 'baseDir' | 'versioned' | 'unversioned'
export type PathOptions = {
platform?: string
arch?: string
[key: string]: any
}
}

export interface IConfig {
root: string
Expand All @@ -25,10 +13,8 @@ export interface IConfig {
s3Config: IConfig['updateConfig']['s3']
channel: string
xz: boolean
targets: {platform: string, arch: string}[]
workspace(target?: {platform: string, arch: string}): string
path(type: IConfig.PathTypes, options?: IConfig.PathOptions): string
s3Url(key: string): string
targets: {platform: Config.PlatformTypes, arch: Config.ArchTypes}[]
workspace(target?: {platform: Config.PlatformTypes, arch: Config.ArchTypes}): string
dist(input: string): string
}

Expand Down Expand Up @@ -66,8 +52,6 @@ export async function buildConfig(root: string): Promise<IConfig> {
const version = config.version.includes('-') ? `${config.version}.${_gitSha}` : config.version
const tmp = await Tmp(config)
const updateConfig = config.pjson.oclif.update
const s3Host = updateConfig.s3.host!
if (!s3Host) throw new CLIError('must set oclif.update.s3.bucket in package.json')
return {
root,
gitSha: _gitSha,
Expand All @@ -82,20 +66,11 @@ export async function buildConfig(root: string): Promise<IConfig> {
nodeVersion: updateConfig.node.version || process.versions.node,
workspace(target) {
const base = qq.join(config.root, 'tmp')
if (target && target.platform) return qq.join(base, [target.platform, target.arch].join('-'), this.path('baseDir', target))
return qq.join(base, this.path('baseDir', target))
},
path(type, options = {}) {
const t = this.s3Config.templates[options.platform ? 'target' : 'vanilla'][type]
return _.template(t)({...config, version, ...options})
},
s3Url(key) {
const url = new URL(s3Host)
url.pathname = path.join(url.pathname, key)
return url.toString()
if (target && target.platform) return qq.join(base, [target.platform, target.arch].join('-'), config.s3Key('baseDir', target))
return qq.join(base, config.s3Key('baseDir', target))
},
targets: (updateConfig.node.targets || []).map(t => {
const [platform, arch] = t.split('-')
const [platform, arch] = t.split('-') as [Config.PlatformTypes, Config.ArchTypes]
return {platform, arch}
}),
}
Expand Down
8 changes: 7 additions & 1 deletion yarn.lock
Expand Up @@ -54,12 +54,18 @@
debug "^3.1.0"
semver "^5.5.0"

"@oclif/config@^1.4.0", "@oclif/config@^1.4.13":
"@oclif/config@^1.4.0":
version "1.4.14"
resolved "https://registry.yarnpkg.com/@oclif/config/-/config-1.4.14.tgz#ef792b5f4b4d44540f3cebc577eca5a77d17f96a"
dependencies:
debug "^3.1.0"

"@oclif/config@^1.6.0":
version "1.6.0"
resolved "https://registry.yarnpkg.com/@oclif/config/-/config-1.6.0.tgz#0d9a034a3364d0ff24368e039599f89e4c00f775"
dependencies:
debug "^3.1.0"

"@oclif/errors@^1.0.3", "@oclif/errors@^1.0.4":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@oclif/errors/-/errors-1.0.4.tgz#8e34386ede530484ae3c98bc21d818c416214c66"
Expand Down

0 comments on commit 8c96067

Please sign in to comment.