2020-09-04 17:22:15 +09:00
|
|
|
import { join, parse } from 'path'
|
|
|
|
|
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 { fdir } from 'fdir'
|
|
|
|
|
2020-10-15 21:16:52 +09:00
|
|
|
import { getNapiConfig } from './consts'
|
2020-09-04 17:22:15 +09:00
|
|
|
import { debugFactory } from './debug'
|
2022-12-15 19:53:26 +09:00
|
|
|
import { UniArchsByPlatform } from './parse-triple'
|
2020-09-04 17:22:15 +09:00
|
|
|
import { readFileAsync, writeFileAsync } from './utils'
|
|
|
|
|
|
|
|
const debug = debugFactory('artifacts')
|
|
|
|
|
|
|
|
export class ArtifactsCommand extends Command {
|
2020-12-23 23:43:43 +09:00
|
|
|
static usage = Command.Usage({
|
|
|
|
description: 'Copy artifacts from Github Actions into specified dir',
|
|
|
|
})
|
|
|
|
|
2021-08-07 01:22:53 +09:00
|
|
|
static paths = [['artifacts']]
|
2020-09-04 17:22:15 +09:00
|
|
|
|
2021-08-07 01:22:53 +09:00
|
|
|
sourceDir = Option.String('-d,--dir', 'artifacts')
|
2020-10-15 21:16:52 +09:00
|
|
|
|
2021-08-07 01:22:53 +09:00
|
|
|
distDir = Option.String('--dist', 'npm')
|
|
|
|
|
|
|
|
configFileName?: string = Option.String('-c,--config')
|
2020-09-04 17:22:15 +09:00
|
|
|
|
|
|
|
async execute() {
|
2020-10-15 21:16:52 +09:00
|
|
|
const { platforms, binaryName, packageJsonPath } = getNapiConfig(
|
|
|
|
this.configFileName,
|
|
|
|
)
|
|
|
|
|
|
|
|
const packageJsonDir = parse(packageJsonPath).dir
|
|
|
|
|
2020-09-04 17:22:15 +09:00
|
|
|
const sourceApi = new fdir()
|
|
|
|
.withFullPaths()
|
|
|
|
.crawl(join(process.cwd(), this.sourceDir))
|
2020-10-15 21:16:52 +09:00
|
|
|
|
|
|
|
const distDirs = platforms.map((platform) =>
|
|
|
|
join(process.cwd(), this.distDir, platform.platformArchABI),
|
2020-09-04 17:22:15 +09:00
|
|
|
)
|
|
|
|
|
2022-12-15 19:53:26 +09:00
|
|
|
const universalSourceBins = new Set(
|
|
|
|
platforms
|
|
|
|
.filter((platform) => platform.arch === 'universal')
|
|
|
|
.flatMap((p) =>
|
|
|
|
UniArchsByPlatform[p.platform].map((a) => `${p.platform}-${a}`),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2020-09-04 17:22:15 +09:00
|
|
|
await sourceApi.withPromise().then((output) =>
|
|
|
|
Promise.all(
|
|
|
|
(output as string[]).map(async (filePath) => {
|
|
|
|
debug(`Read [${chalk.yellowBright(filePath)}]`)
|
|
|
|
const sourceContent = await readFileAsync(filePath)
|
|
|
|
const parsedName = parse(filePath)
|
2020-10-15 21:16:52 +09:00
|
|
|
const [_binaryName, platformArchABI] = parsedName.name.split('.')
|
|
|
|
if (_binaryName !== binaryName) {
|
|
|
|
debug(
|
|
|
|
`[${chalk.yellowBright(
|
|
|
|
_binaryName,
|
|
|
|
)}] is not matched with [${chalk.greenBright(binaryName)}], skip`,
|
|
|
|
)
|
2022-12-15 19:57:44 +09:00
|
|
|
return
|
2020-10-15 21:16:52 +09:00
|
|
|
}
|
|
|
|
const dir = distDirs.find((dir) => dir.includes(platformArchABI))
|
2022-12-15 19:53:26 +09:00
|
|
|
if (!dir && universalSourceBins.has(platformArchABI)) {
|
|
|
|
debug(
|
|
|
|
`[${chalk.yellowBright(
|
|
|
|
platformArchABI,
|
|
|
|
)}] has no dist dir but it is source bin for universal arch, skip`,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
2020-09-04 17:22:15 +09:00
|
|
|
if (!dir) {
|
|
|
|
throw new TypeError(`No dist dir found for ${filePath}`)
|
|
|
|
}
|
|
|
|
const distFilePath = join(dir, parsedName.base)
|
|
|
|
debug(`Write file content to [${chalk.yellowBright(distFilePath)}]`)
|
|
|
|
await writeFileAsync(distFilePath, sourceContent)
|
2020-10-15 21:16:52 +09:00
|
|
|
const distFilePathLocal = join(packageJsonDir, parsedName.base)
|
|
|
|
debug(
|
|
|
|
`Write file content to [${chalk.yellowBright(distFilePathLocal)}]`,
|
|
|
|
)
|
|
|
|
await writeFileAsync(distFilePathLocal, sourceContent)
|
2020-09-04 17:22:15 +09:00
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|