2021-11-21 22:39:36 +09:00
|
|
|
import { createReadStream, existsSync, statSync } from 'fs'
|
2020-09-04 17:22:15 +09:00
|
|
|
import { join } from 'path'
|
|
|
|
|
|
|
|
import { Octokit } from '@octokit/rest'
|
2021-08-07 01:22:53 +09:00
|
|
|
import { Command, Option } from 'clipanion'
|
2023-01-19 01:32:52 +09:00
|
|
|
import * as chalk from 'colorette'
|
2020-09-04 17:22:15 +09:00
|
|
|
|
|
|
|
import { getNapiConfig } from './consts'
|
|
|
|
import { debugFactory } from './debug'
|
|
|
|
import { spawn } from './spawn'
|
|
|
|
import { updatePackageJson } from './update-package'
|
2020-09-08 10:21:59 +09:00
|
|
|
import { VersionCommand } from './version'
|
2020-09-04 17:22:15 +09:00
|
|
|
|
|
|
|
const debug = debugFactory('prepublish')
|
|
|
|
|
|
|
|
interface PackageInfo {
|
|
|
|
name: string
|
|
|
|
version: string
|
|
|
|
tag: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PrePublishCommand extends Command {
|
|
|
|
static usage = Command.Usage({
|
|
|
|
description:
|
|
|
|
'Update package.json and copy addons into per platform packages',
|
|
|
|
})
|
|
|
|
|
2021-08-07 01:22:53 +09:00
|
|
|
static paths = [['prepublish']]
|
2020-09-04 17:22:15 +09:00
|
|
|
|
2021-08-07 01:22:53 +09:00
|
|
|
prefix = Option.String(`-p,--prefix`, 'npm')
|
2020-09-04 17:22:15 +09:00
|
|
|
|
2021-08-07 01:22:53 +09:00
|
|
|
tagStyle: 'npm' | 'lerna' = Option.String('--tagstyle,-t', 'lerna')
|
2020-09-04 17:22:15 +09:00
|
|
|
|
2021-08-07 01:22:53 +09:00
|
|
|
configFileName?: string = Option.String('-c,--config')
|
2020-09-04 17:22:15 +09:00
|
|
|
|
2021-08-07 01:22:53 +09:00
|
|
|
isDryRun = Option.Boolean('--dry-run', false)
|
|
|
|
|
|
|
|
skipGHRelease = Option.Boolean('--skip-gh-release', false)
|
2021-06-07 12:35:26 +09:00
|
|
|
|
2022-08-04 01:10:51 +09:00
|
|
|
ghReleaseName?: string = Option.String('--gh-release-name')
|
|
|
|
|
2022-08-04 13:12:18 +09:00
|
|
|
existingReleaseId?: string = Option.String('--gh-release-id')
|
|
|
|
|
2020-09-04 17:22:15 +09:00
|
|
|
async execute() {
|
2022-07-31 21:43:25 +09:00
|
|
|
const {
|
|
|
|
packageJsonPath,
|
|
|
|
platforms,
|
|
|
|
version,
|
|
|
|
packageName,
|
|
|
|
binaryName,
|
|
|
|
npmClient,
|
|
|
|
} = getNapiConfig(this.configFileName)
|
2020-09-04 17:22:15 +09:00
|
|
|
debug(`Update optionalDependencies in [${packageJsonPath}]`)
|
|
|
|
if (!this.isDryRun) {
|
2020-09-08 10:21:59 +09:00
|
|
|
await VersionCommand.updatePackageJson(this.prefix, this.configFileName)
|
2020-09-04 17:22:15 +09:00
|
|
|
await updatePackageJson(packageJsonPath, {
|
|
|
|
optionalDependencies: platforms.reduce(
|
2020-10-15 21:16:52 +09:00
|
|
|
(acc: Record<string, string>, cur) => {
|
2021-09-19 23:38:37 +09:00
|
|
|
acc[`${packageName}-${cur.platformArchABI}`] = `${version}`
|
2020-09-04 17:22:15 +09:00
|
|
|
return acc
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-04 13:12:18 +09:00
|
|
|
const { owner, repo, pkgInfo, octokit } = this.existingReleaseId
|
|
|
|
? await this.getRepoInfo(packageName, version)
|
|
|
|
: await this.createGhRelease(packageName, version)
|
2020-09-04 17:22:15 +09:00
|
|
|
|
2020-10-15 21:16:52 +09:00
|
|
|
for (const platformDetail of platforms) {
|
|
|
|
const pkgDir = join(
|
|
|
|
process.cwd(),
|
|
|
|
this.prefix,
|
|
|
|
`${platformDetail.platformArchABI}`,
|
|
|
|
)
|
|
|
|
const filename = `${binaryName}.${platformDetail.platformArchABI}.node`
|
2020-09-04 17:22:15 +09:00
|
|
|
const dstPath = join(pkgDir, filename)
|
2020-10-15 21:16:52 +09:00
|
|
|
|
2020-09-04 17:22:15 +09:00
|
|
|
if (!this.isDryRun) {
|
2021-11-21 22:39:36 +09:00
|
|
|
if (!existsSync(dstPath)) {
|
2022-08-03 09:01:15 +09:00
|
|
|
console.warn(`[${chalk.yellowBright(dstPath)}] doesn't exist`)
|
2020-10-15 21:16:52 +09:00
|
|
|
continue
|
|
|
|
}
|
2022-07-31 21:43:25 +09:00
|
|
|
await spawn(`${npmClient} publish`, {
|
2020-10-15 21:16:52 +09:00
|
|
|
cwd: pkgDir,
|
|
|
|
env: process.env,
|
|
|
|
})
|
2022-04-01 14:34:42 +09:00
|
|
|
if (!this.skipGHRelease && repo && owner) {
|
2020-09-08 00:18:04 +09:00
|
|
|
debug(
|
2021-06-07 12:35:26 +09:00
|
|
|
`Start upload [${chalk.greenBright(
|
|
|
|
dstPath,
|
|
|
|
)}] to Github release, [${chalk.greenBright(pkgInfo.tag)}]`,
|
2020-09-08 00:18:04 +09:00
|
|
|
)
|
2021-06-07 12:35:26 +09:00
|
|
|
try {
|
2022-08-04 13:12:18 +09:00
|
|
|
const releaseId = this.existingReleaseId
|
|
|
|
? Number(this.existingReleaseId)
|
|
|
|
: (
|
|
|
|
await octokit!.repos.getReleaseByTag({
|
|
|
|
repo: repo,
|
|
|
|
owner: owner,
|
|
|
|
tag: pkgInfo.tag,
|
|
|
|
})
|
|
|
|
).data.id
|
2021-11-21 22:39:36 +09:00
|
|
|
const dstFileStats = statSync(dstPath)
|
2021-11-19 15:58:21 +09:00
|
|
|
const assetInfo = await octokit!.repos.uploadReleaseAsset({
|
2022-04-01 14:34:42 +09:00
|
|
|
owner: owner,
|
|
|
|
repo: repo,
|
2021-11-19 15:58:21 +09:00
|
|
|
name: filename,
|
2022-08-04 13:12:18 +09:00
|
|
|
release_id: releaseId,
|
2021-11-21 22:39:36 +09:00
|
|
|
mediaType: { format: 'raw' },
|
|
|
|
headers: {
|
|
|
|
'content-length': dstFileStats.size,
|
|
|
|
'content-type': 'application/octet-stream',
|
|
|
|
},
|
|
|
|
// @ts-expect-error
|
|
|
|
data: createReadStream(dstPath),
|
2021-06-07 12:35:26 +09:00
|
|
|
})
|
|
|
|
console.info(`${chalk.green(dstPath)} upload success`)
|
2021-11-19 15:58:21 +09:00
|
|
|
console.info(
|
|
|
|
`Download url: ${chalk.blueBright(
|
|
|
|
assetInfo.data.browser_download_url,
|
|
|
|
)}`,
|
|
|
|
)
|
2021-06-07 12:35:26 +09:00
|
|
|
} catch (e) {
|
|
|
|
debug(
|
|
|
|
`Param: ${JSON.stringify(
|
|
|
|
{ owner, repo, tag: pkgInfo.tag, filename: dstPath },
|
|
|
|
null,
|
|
|
|
2,
|
|
|
|
)}`,
|
|
|
|
)
|
|
|
|
console.error(e)
|
|
|
|
}
|
2020-09-08 00:18:04 +09:00
|
|
|
}
|
2020-09-04 17:22:15 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async createGhRelease(packageName: string, version: string) {
|
2021-06-07 12:35:26 +09:00
|
|
|
if (this.skipGHRelease) {
|
|
|
|
return {
|
|
|
|
owner: null,
|
|
|
|
repo: null,
|
|
|
|
pkgInfo: { name: null, version: null, tag: null },
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 13:12:18 +09:00
|
|
|
const { repo, owner, pkgInfo, octokit } = await this.getRepoInfo(
|
|
|
|
packageName,
|
|
|
|
version,
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!repo || !owner) {
|
|
|
|
return {
|
|
|
|
owner: null,
|
|
|
|
repo: null,
|
|
|
|
pkgInfo: { name: null, version: null, tag: null },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.isDryRun) {
|
|
|
|
try {
|
|
|
|
await octokit.repos.createRelease({
|
|
|
|
owner,
|
|
|
|
repo,
|
|
|
|
tag_name: pkgInfo.tag,
|
|
|
|
name: this.ghReleaseName,
|
|
|
|
prerelease:
|
|
|
|
version.includes('alpha') ||
|
|
|
|
version.includes('beta') ||
|
|
|
|
version.includes('rc'),
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
debug(
|
|
|
|
`Params: ${JSON.stringify(
|
|
|
|
{ owner, repo, tag_name: pkgInfo.tag },
|
|
|
|
null,
|
|
|
|
2,
|
|
|
|
)}`,
|
|
|
|
)
|
|
|
|
console.error(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { owner, repo, pkgInfo, octokit }
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getRepoInfo(packageName: string, version: string) {
|
2020-09-04 17:22:15 +09:00
|
|
|
const headCommit = (await spawn('git log -1 --pretty=%B'))
|
|
|
|
.toString('utf8')
|
|
|
|
.trim()
|
2022-04-01 14:34:42 +09:00
|
|
|
const { GITHUB_REPOSITORY } = process.env
|
|
|
|
if (!GITHUB_REPOSITORY) {
|
|
|
|
return {
|
|
|
|
owner: null,
|
|
|
|
repo: null,
|
|
|
|
pkgInfo: { name: null, version: null, tag: null },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug(`Github repository: ${GITHUB_REPOSITORY}`)
|
|
|
|
const [owner, repo] = GITHUB_REPOSITORY.split('/')
|
2020-09-04 17:22:15 +09:00
|
|
|
const octokit = new Octokit({
|
|
|
|
auth: process.env.GITHUB_TOKEN,
|
|
|
|
})
|
|
|
|
let pkgInfo: PackageInfo | undefined
|
|
|
|
if (this.tagStyle === 'lerna') {
|
|
|
|
const packagesToPublish = headCommit
|
|
|
|
.split('\n')
|
|
|
|
.map((line) => line.trim())
|
|
|
|
.filter((line, index) => line.length && index)
|
2021-12-27 19:36:57 +09:00
|
|
|
.map((line) => line.substring(2))
|
2020-09-04 17:22:15 +09:00
|
|
|
.map(this.parseTag)
|
|
|
|
pkgInfo = packagesToPublish.find(
|
|
|
|
(pkgInfo) => pkgInfo.name === packageName,
|
|
|
|
)
|
|
|
|
if (!pkgInfo) {
|
|
|
|
throw new TypeError(
|
|
|
|
`No release commit found with ${packageName}, original commit info: ${headCommit}`,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pkgInfo = {
|
2020-09-07 23:22:56 +09:00
|
|
|
tag: `v${version}`,
|
2020-09-04 17:22:15 +09:00
|
|
|
version,
|
|
|
|
name: packageName,
|
|
|
|
}
|
|
|
|
}
|
2021-11-19 15:58:21 +09:00
|
|
|
return { owner, repo, pkgInfo, octokit }
|
2020-09-04 17:22:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
private parseTag(tag: string) {
|
|
|
|
const segments = tag.split('@')
|
|
|
|
const version = segments.pop()!
|
|
|
|
const name = segments.join('@')
|
|
|
|
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
version,
|
|
|
|
tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|