|
| 1 | +#! /usr/bin/env node |
| 2 | + |
| 3 | +import { AppInfo, CancellationToken, Packager, PackagerOptions, PublishManager, PublishOptions, UploadTask, checkBuildRequestOptions } from "app-builder-lib" |
| 4 | +import { Publish } from "app-builder-lib/out/core" |
| 5 | +import { computeSafeArtifactNameIfNeeded } from "app-builder-lib/out/platformPackager" |
| 6 | +import { getConfig } from "app-builder-lib/out/util/config" |
| 7 | +import { InvalidConfigurationError, archFromString, log } from "builder-util" |
| 8 | +import { printErrorAndExit } from "builder-util/out/promise" |
| 9 | +import * as chalk from "chalk" |
| 10 | +import * as path from "path" |
| 11 | +import * as yargs from "yargs" |
| 12 | +import { BuildOptions, normalizeOptions } from "./builder" |
| 13 | + |
| 14 | +/** @internal */ |
| 15 | +export function configurePublishCommand(yargs: yargs.Argv): yargs.Argv { |
| 16 | + // https://github.com/yargs/yargs/issues/760 |
| 17 | + // demandOption is required to be set |
| 18 | + return yargs |
| 19 | + .parserConfiguration({ |
| 20 | + "camel-case-expansion": false, |
| 21 | + }) |
| 22 | + .option("files", { |
| 23 | + alias: "f", |
| 24 | + string: true, |
| 25 | + type: "array", |
| 26 | + requiresArg: true, |
| 27 | + description: "The file(s) to upload to your publisher", |
| 28 | + }) |
| 29 | + .option("version", { |
| 30 | + alias: ["v"], |
| 31 | + type: "string", |
| 32 | + description: "The app/build version used when searching for an upload release (used by some Publishers)", |
| 33 | + }) |
| 34 | + .option("config", { |
| 35 | + alias: ["c"], |
| 36 | + type: "string", |
| 37 | + description: |
| 38 | + "The path to an electron-builder config. Defaults to `electron-builder.yml` (or `json`, or `json5`, or `js`, or `ts`), see " + chalk.underline("https://goo.gl/YFRJOM"), |
| 39 | + }) |
| 40 | + .demandOption("files") |
| 41 | +} |
| 42 | + |
| 43 | +export async function publish(args: { files: string[]; version: string | undefined; config: string | undefined }) { |
| 44 | + const uploadTasks = args.files.map(f => { |
| 45 | + return { |
| 46 | + file: path.resolve(f), |
| 47 | + arch: null, |
| 48 | + } |
| 49 | + }) |
| 50 | + return publishArtifactsWithOptions(uploadTasks, args.version, args.config) |
| 51 | +} |
| 52 | + |
| 53 | +export async function publishArtifactsWithOptions( |
| 54 | + uploadOptions: { file: string; arch: string | null }[], |
| 55 | + buildVersion?: string, |
| 56 | + configurationFilePath?: string, |
| 57 | + publishConfiguration?: Publish |
| 58 | +) { |
| 59 | + const projectDir = process.cwd() |
| 60 | + const config = await getConfig(projectDir, configurationFilePath || null, { publish: publishConfiguration, detectUpdateChannel: false }) |
| 61 | + |
| 62 | + const buildOptions: BuildOptions = normalizeOptions({ config }) |
| 63 | + checkBuildRequestOptions(buildOptions) |
| 64 | + |
| 65 | + const uniqueUploads = Array.from(new Set(uploadOptions)) |
| 66 | + const tasks: UploadTask[] = uniqueUploads.map(({ file, arch }) => { |
| 67 | + const filename = path.basename(file) |
| 68 | + return { file, arch: arch ? archFromString(arch) : null, safeArtifactName: computeSafeArtifactNameIfNeeded(filename, () => filename) } |
| 69 | + }) |
| 70 | + |
| 71 | + return publishPackageWithTasks(buildOptions, tasks, buildVersion) |
| 72 | +} |
| 73 | + |
| 74 | +async function publishPackageWithTasks( |
| 75 | + options: PackagerOptions & PublishOptions, |
| 76 | + uploadTasks: UploadTask[], |
| 77 | + buildVersion?: string, |
| 78 | + cancellationToken: CancellationToken = new CancellationToken(), |
| 79 | + packager: Packager = new Packager(options, cancellationToken) |
| 80 | +) { |
| 81 | + await packager.validateConfig() |
| 82 | + const appInfo = new AppInfo(packager, buildVersion) |
| 83 | + const publishManager = new PublishManager(packager, options, cancellationToken) |
| 84 | + |
| 85 | + const sigIntHandler = () => { |
| 86 | + log.warn("cancelled by SIGINT") |
| 87 | + packager.cancellationToken.cancel() |
| 88 | + publishManager.cancelTasks() |
| 89 | + } |
| 90 | + process.once("SIGINT", sigIntHandler) |
| 91 | + |
| 92 | + try { |
| 93 | + const publishConfigurations = await publishManager.getGlobalPublishConfigurations() |
| 94 | + if (publishConfigurations == null || publishConfigurations.length === 0) { |
| 95 | + throw new InvalidConfigurationError("unable to find any publish configuration") |
| 96 | + } |
| 97 | + |
| 98 | + for (const newArtifact of uploadTasks) { |
| 99 | + for (const publishConfiguration of publishConfigurations) { |
| 100 | + publishManager.scheduleUpload(publishConfiguration, newArtifact, appInfo) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + await publishManager.awaitTasks() |
| 105 | + return uploadTasks |
| 106 | + } catch (error: any) { |
| 107 | + packager.cancellationToken.cancel() |
| 108 | + publishManager.cancelTasks() |
| 109 | + process.removeListener("SIGINT", sigIntHandler) |
| 110 | + log.error({ message: (error.stack || error.message || error).toString() }, "error publishing") |
| 111 | + } |
| 112 | + return null |
| 113 | +} |
| 114 | + |
| 115 | +function main() { |
| 116 | + return publish(configurePublishCommand(yargs).argv as any) |
| 117 | +} |
| 118 | + |
| 119 | +if (require.main === module) { |
| 120 | + log.warn("please use as subcommand: electron-builder publish") |
| 121 | + main().catch(printErrorAndExit) |
| 122 | +} |
0 commit comments