Skip to content

Commit ec8e69e

Browse files
committed
fix: Don't throw an error if version doesn't start with `v´
Closes #743
1 parent 84225d7 commit ec8e69e

File tree

9 files changed

+65
-48
lines changed

9 files changed

+65
-48
lines changed

src/builder.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getRepositoryInfo } from "./repositoryInfo"
1111
import { DIR_TARGET } from "./targets/targetFactory"
1212
import { BintrayPublisher } from "./publish/BintrayPublisher"
1313
import { BintrayOptions } from "./publish/bintray"
14+
import { PublishConfiguration, GithubPublishConfiguration } from "./options/publishOptions"
1415

1516
//noinspection JSUnusedLocalSymbols
1617
const __awaiter = require("./util/awaiter")
@@ -269,18 +270,23 @@ function publishManager(packager: Packager, publishTasks: Array<BluebirdPromise<
269270
}
270271

271272
if (publishers == null && options.githubToken != null) {
272-
publishers = ["github"]
273+
publishers = {provider: "github"}
273274
}
274275
// if both tokens are set — still publish to github (because default publisher is github)
275276
if (publishers == null && options.bintrayToken != null) {
276-
publishers = ["bintray"]
277+
publishers = {provider: "bintray"}
277278
}
278279
}
279280

280-
for (let publisherName of asArray(publishers)) {
281+
for (let publishConfig of asArray(publishers)) {
282+
if (typeof publishConfig === "string") {
283+
publishConfig = {provider: publishConfig}
284+
}
285+
286+
const publisherName = publishConfig.provider
281287
let publisher = nameToPublisher.get(publisherName)
282288
if (publisher == null) {
283-
publisher = createPublisher(packager, options, publisherName, isPublishOptionGuessed)
289+
publisher = createPublisher(packager, options, publishConfig, isPublishOptionGuessed)
284290
nameToPublisher.set(publisherName, publisher)
285291
}
286292

@@ -292,9 +298,13 @@ function publishManager(packager: Packager, publishTasks: Array<BluebirdPromise<
292298
})
293299
}
294300

295-
export async function createPublisher(packager: Packager, options: PublishOptions, publisherName: string, isPublishOptionGuessed: boolean = false): Promise<Publisher | null> {
296-
const info = await getRepositoryInfo(packager.metadata, packager.devMetadata)
297-
if (info == null) {
301+
export async function createPublisher(packager: Packager, options: PublishOptions, publishConfig: PublishConfiguration | GithubPublishConfiguration, isPublishOptionGuessed: boolean = false): Promise<Publisher | null> {
302+
async function getInfo() {
303+
const info = await getRepositoryInfo(packager.metadata, packager.devMetadata)
304+
if (info != null) {
305+
return info
306+
}
307+
298308
if (isPublishOptionGuessed) {
299309
return null
300310
}
@@ -303,12 +313,30 @@ export async function createPublisher(packager: Packager, options: PublishOption
303313
throw new Error(`Please specify 'repository' in the dev package.json ('${packager.devPackageFile}')`)
304314
}
305315

306-
if (publisherName === "github") {
316+
if (publishConfig.provider === "github") {
317+
const config = <GithubPublishConfiguration>publishConfig
318+
let user = config.owner
319+
let repo = config.repo
320+
if (!user || !repo) {
321+
const info = await getInfo()
322+
if (info == null) {
323+
return null
324+
}
325+
326+
user = info.user
327+
repo = info.project
328+
}
329+
307330
const version = packager.metadata.version!
308-
log(`Creating Github Publisher — user: ${info.user}, project: ${info.project}, version: ${version}`)
309-
return new GitHubPublisher(info.user, info.project, version, options, isPublishOptionGuessed)
331+
log(`Creating Github Publisher — user: ${user}, project: ${repo}, version: ${version}`)
332+
return new GitHubPublisher(user, repo, version, options, isPublishOptionGuessed, config)
310333
}
311-
if (publisherName === "bintray") {
334+
if (publishConfig.provider === "bintray") {
335+
const info = await getInfo()
336+
if (info == null) {
337+
return null
338+
}
339+
312340
const version = packager.metadata.version!
313341
//noinspection ReservedWordAsName
314342
const bintrayInfo: BintrayOptions = {user: info.user, package: info.project, repo: "generic"}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ export { DIR_TARGET, DEFAULT_TARGET } from "./targets/targetFactory"
44
export { BuildOptions, build, CliOptions, createTargets } from "./builder"
55
export { PublishOptions, Publisher } from "./publish/publisher"
66
export { AppMetadata, DevMetadata, Platform, Arch, archFromString, BuildMetadata, WinBuildOptions, LinuxBuildOptions, CompressionLevel } from "./metadata"
7-
export { MacOptions, DmgOptions, MasBuildOptions } from "./macOptions"
7+
export { MacOptions, DmgOptions, MasBuildOptions } from "./options/macOptions"

src/macPackager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PlatformPackager, BuildInfo, Target } from "./platformPackager"
22
import { Platform, Arch } from "./metadata"
3-
import { MasBuildOptions, MacOptions } from "./macOptions"
3+
import { MasBuildOptions, MacOptions } from "./options/macOptions"
44
import * as path from "path"
55
import { Promise as BluebirdPromise } from "bluebird"
66
import { log, warn, task } from "./util/log"

src/metadata.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AsarOptions } from "asar-electron-builder"
22
import { PlatformPackager } from "./platformPackager"
3-
import { MacOptions, DmgOptions, MasBuildOptions } from "./macOptions"
3+
import { MacOptions, DmgOptions, MasBuildOptions } from "./options/macOptions"
4+
import { PublishConfiguration } from "./options/publishOptions"
45

56
export interface Metadata {
67
readonly repository?: string | RepositoryInfo | null
@@ -206,7 +207,7 @@ export interface BuildMetadata {
206207

207208
readonly dereference?: boolean
208209

209-
readonly publish?: string | Array<string> | null
210+
readonly publish?: string | Array<string> | null | PublishConfiguration
210211
}
211212

212213
export interface AfterPackContext {

src/macOptions.ts renamed to src/options/macOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PlatformSpecificBuildOptions } from "./metadata"
1+
import { PlatformSpecificBuildOptions } from "../metadata"
22

33
/*
44
### `.build.mac`

src/options/publishOptions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export type PublishProvider = "github" | "bintray"
2+
3+
export interface PublishConfiguration {
4+
provider: PublishProvider
5+
}
6+
7+
export interface GithubPublishConfiguration extends PublishConfiguration {
8+
repo: string
9+
version: string
10+
owner: string
11+
12+
vPrefixedTagName?: boolean
13+
}

src/publish/gitHubPublisher.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { githubRequest, HttpError, doApiRequest } from "./restApiRequest"
99
import { Promise as BluebirdPromise } from "bluebird"
1010
import { PublishPolicy, PublishOptions, Publisher } from "./publisher"
1111
import { uploadFile } from "./uploader"
12+
import { GithubPublishConfiguration } from "../options/publishOptions"
1213

1314
//noinspection JSUnusedLocalSymbols
1415
const __awaiter = require("../util/awaiter")
@@ -38,7 +39,7 @@ export class GitHubPublisher implements Publisher {
3839
return this._releasePromise
3940
}
4041

41-
constructor(private owner: string, private repo: string, private version: string, private options: PublishOptions, private isPublishOptionGuessed: boolean = false) {
42+
constructor(private owner: string, private repo: string, private version: string, private options: PublishOptions, private isPublishOptionGuessed: boolean = false, config?: GithubPublishConfiguration | null) {
4243
if (isEmptyOrSpaces(options.githubToken)) {
4344
throw new Error("GitHub Personal Access Token is not specified")
4445
}
@@ -50,7 +51,7 @@ export class GitHubPublisher implements Publisher {
5051
throw new Error(`Version must not starts with "v": ${version}`)
5152
}
5253

53-
this.tag = `v${version}`
54+
this.tag = config != null && config.vPrefixedTagName ? `v${version}` : version
5455
this._releasePromise = this.token === "__test__" ? BluebirdPromise.resolve(<any>null) : <BluebirdPromise<Release>>this.init()
5556
}
5657

@@ -59,7 +60,7 @@ export class GitHubPublisher implements Publisher {
5960
// we don't use "Get a release by tag name" because "tag name" means existing git tag, but we draft release and don't create git tag
6061
const releases = await githubRequest<Array<Release>>(`/repos/${this.owner}/${this.repo}/releases`, this.token)
6162
for (let release of releases) {
62-
if (release.tag_name === this.tag) {
63+
if (release.tag_name === this.tag || release.tag_name === this.version) {
6364
if (release.draft) {
6465
return release
6566
}
@@ -77,9 +78,6 @@ export class GitHubPublisher implements Publisher {
7778
}
7879
return null
7980
}
80-
else if (release.tag_name === this.version) {
81-
throw new Error(`Tag name must starts with "v": ${release.tag_name}`)
82-
}
8381
}
8482

8583
if (createReleaseIfNotExists) {

src/targets/dmg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { deepAssign } from "../util/deepAssign"
22
import * as path from "path"
33
import { log } from "../util/log"
44
import { Target, PlatformPackager } from "../platformPackager"
5-
import { MacOptions, DmgOptions } from "../macOptions"
5+
import { MacOptions, DmgOptions } from "../options/macOptions"
66
import { Promise as BluebirdPromise } from "bluebird"
77
import { debug, use } from "../util/util"
88

test/src/ArtifactPublisherTest.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,30 +98,6 @@ testAndIgnoreApiRate("prerelease", async () => {
9898
}
9999
})
100100

101-
testAndIgnoreApiRate("incorrect tag name", async () => {
102-
const publisher = new GitHubPublisher("actperepo", "ecb2", "5.0", {
103-
githubToken: token,
104-
draft: false,
105-
prerelease: true,
106-
publish: "onTagOrDraft",
107-
})
108-
109-
// await publisher.deleteOldReleases()
110-
try {
111-
await publisher.releasePromise
112-
//noinspection ExceptionCaughtLocallyJS
113-
throw new Error("No expected error")
114-
}
115-
catch (e) {
116-
if (e.message !== 'Tag name must starts with "v": 5.0') {
117-
throw e
118-
}
119-
}
120-
finally {
121-
await publisher.deleteRelease()
122-
}
123-
})
124-
125101
testAndIgnoreApiRate("GitHub upload org", async () => {
126102
//noinspection SpellCheckingInspection
127103
const publisher = new GitHubPublisher("builder-gh-test", "darpa", versionNumber(), {
@@ -146,12 +122,13 @@ test("create publisher", async () => {
146122
}
147123
const publisher = await createPublisher(packager, {
148124
githubToken: "__test__",
149-
}, "github")
125+
}, {provider: "github", vPrefixedTagName: false})
150126

151127
assertThat(publisher).hasProperties({
152128
"owner": "develar",
153129
"repo": "test",
154130
"token": "__test__",
155131
"version": "2.0.0",
132+
"tag": "2.0.0",
156133
})
157134
})

0 commit comments

Comments
 (0)