-
Notifications
You must be signed in to change notification settings - Fork 4
/
publish.ts
116 lines (99 loc) · 3.75 KB
/
publish.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { ios } from '../fastlane'
import { android, gradle, GradleArgs, supply } from '../fastlane/android'
import { gym } from '../fastlane/ios/gym'
import { match } from '../fastlane/ios/match'
import { pilot } from '../fastlane/ios/pilot'
import { IOSPlatform, Platform, PlatformActions, platformTypes } from '../model/platform'
import { AndroidPlatform } from '../model/platform/AndroidPlatform'
import { ui } from '../ui'
import { env, git } from '../utils'
import { FastpushResult } from './fastpush'
import { Hooks } from './hooks'
import { assertPlatforms, incrementPackageJson, Version } from './utils'
import shell from 'shelljs'
export const defaultHooks: Hooks = {
onFinish: null,
onStart: async (options: FastpushResult) => {
env.add(options.env)
if (options.silent) {
// don't assert clean
// don't assert environment
} else {
await git.assertClean()
await env.assert()
}
},
provideAndroidLanes: (options: FastpushResult) => {
const buildPayload: GradleArgs = {
build_type: 'Release',
}
if (options.flavor) {
// const flavorCapitalized = options.flavor.charAt(0).toUpperCase() + options.flavor.substring(1)
buildPayload.flavor = options.flavor
}
return [
gradle('clean'),
// todo: validate GradleArgTask
gradle(options.build, buildPayload),
supply({ track: options.track }),
]
},
provideIOSLanes: (options: FastpushResult) => {
return [match('appstore'), gym(), pilot()]
},
onPostPublish: async (platform: PlatformActions, versions: [Version, Version], buildNumbers: [number, number]) => {
const [oldVersion, version] = versions
const [oldBuild, build] = buildNumbers
const tag = `${platform.type}/${version}-${build}`
const whoami = process.env.USER || ''
git.commit(`Up version ${tag}`)
git.tag(tag, `Up version by ${whoami}`)
git.push()
},
}
export async function publish(options: FastpushResult, passedHooks?: Hooks) {
const hooks = {
...defaultHooks,
...passedHooks,
}
let platforms: Platform[] = []
if (options.android) {
platforms.push('android')
}
if (options.ios) {
platforms.push('ios')
}
if (options.silent) {
// don't apply changes to platforms
if (!platforms || platforms.length <= 0) {
throw 'You should specify at least 1 platform for processing: ' + platformTypes
}
} else {
platforms = await assertPlatforms(platforms)
}
await hooks.onStart?.(options)
const [oldVersion, newVersion] = await incrementPackageJson(`${options.project}/package.json`, options.increment)
ui.success(`Up package.json version from [${oldVersion}] -> [${newVersion}]`)
if (platforms.find(it => it === 'android')) {
const androidPlatform = new AndroidPlatform(options.project)
await distribute(options, androidPlatform, newVersion, hooks)
}
if (platforms.find(it => it === 'ios')) {
await distribute(options, new IOSPlatform(options.project), newVersion, hooks)
}
await hooks.onFinish?.()
}
async function distribute(options: FastpushResult, platform: PlatformActions, version: Version, hooks: Hooks) {
const [oldBuildNumber, newBuildNumber] = await platform.incrementBuildNumber()
ui.success(`Update ${platform.type} versionCode [${oldBuildNumber}] -> [${newBuildNumber}]`)
const [oldVersion, newVersion] = await platform.setVersionName(version)
ui.success(`Update ${platform.type} versionName [${oldVersion}] -> [${newVersion}]`)
if (platform.type === 'ios') {
ios(hooks.provideIOSLanes(options))
} else if (platform.type === 'android') {
android(hooks.provideAndroidLanes(options))
} else {
throw `Unexpected platform type ${platform.type}`
}
await hooks.onPostPublish?.(platform, [oldVersion, newVersion], [oldBuildNumber, newBuildNumber])
}