-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmain.ts
120 lines (105 loc) · 3.35 KB
/
main.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
117
118
119
120
import { URL } from 'node:url'
import { getInput, setFailed, setOutput, exportVariable } from '@actions/core'
import { exec } from '@actions/exec'
import fs from 'fs-extra'
import { env } from './env.js'
import { setupUser } from './gitUtils.js'
import readChangesetState from './readChangesetState.js'
import { runPublish, runVersion } from './run.js'
import type { MainCommandOptions } from './types.js'
import { execSync, getOptionalInput, getUsername } from './utils.js'
import { createApi } from './index.js'
export const main = async ({
published,
onlyChangesets,
}: MainCommandOptions = {}) => {
const {
CI,
GITLAB_HOST,
GITLAB_TOKEN,
HOME,
NPM_TOKEN,
DEBUG_GITLAB_CREDENTIAL = 'false',
} = env
setOutput('published', false)
setOutput('publishedPackages', [])
if (CI) {
console.log('setting git user')
await setupUser()
const url = new URL(GITLAB_HOST)
console.log('setting GitLab credentials')
const username = await getUsername(createApi())
await exec(
'git',
[
'remote',
'set-url',
'origin',
`${url.protocol}//${username}:${GITLAB_TOKEN}@${
url.host
}${url.pathname.replace(/\/$/, '')}/${env.CI_PROJECT_PATH}.git`, // eslint-disable-line unicorn/consistent-destructuring
],
{ silent: !['true', '1'].includes(DEBUG_GITLAB_CREDENTIAL) },
)
}
const { changesets } = await readChangesetState()
const publishScript = getInput('publish')
const hasChangesets = changesets.length > 0
const hasPublishScript = !!publishScript
switch (true) {
case !hasChangesets && !hasPublishScript: {
console.log('No changesets found')
return
}
case !hasChangesets && hasPublishScript: {
console.log(
'No changesets found, attempting to publish any unpublished packages to npm',
)
const npmrcPath = `${HOME}/.npmrc`
if (fs.existsSync(npmrcPath)) {
console.log('Found existing .npmrc file')
} else if (NPM_TOKEN) {
console.log('No .npmrc file found, creating one')
fs.writeFileSync(
npmrcPath,
`//registry.npmjs.org/:_authToken=${NPM_TOKEN}`,
)
} else {
setFailed(
'No `.npmrc` found nor `NPM_TOKEN` provided, unable to publish packages',
)
return
}
const result = await runPublish({
script: publishScript,
gitlabToken: GITLAB_TOKEN,
createGitlabReleases: getInput('create_gitlab_releases') !== 'false',
pushAllTags: getInput('push_all_tags') !== 'false',
})
if (result.published) {
setOutput('published', true)
setOutput('publishedPackages', result.publishedPackages)
exportVariable('PUBLISHED', true)
exportVariable('PUBLISHED_PACKAGES', result.publishedPackages)
if (published) {
execSync(published)
}
}
return
}
case hasChangesets: {
await runVersion({
script: getOptionalInput('version'),
gitlabToken: GITLAB_TOKEN,
mrTitle: getOptionalInput('title'),
mrTargetBranch: getOptionalInput('target_branch'),
commitMessage: getOptionalInput('commit'),
removeSourceBranch: getInput('remove_source_branch') === 'true',
hasPublishScript,
})
if (onlyChangesets) {
execSync(onlyChangesets)
}
}
}
}