-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
publish.js
131 lines (124 loc) · 3.55 KB
/
publish.js
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
121
122
123
124
125
126
127
128
129
130
131
const execa = require('execa')
const { getOption } = require('./util')
const path = require('path')
const fs = require('fs')
/**
* @param setupPy
* @param distDir
* @param repoUrl
* @param gpgSign
* @param gpgIdentity
*/
function publishPackage(setupPy, distDir, repoUrl, gpgSign, gpgIdentity) {
return execa(
'python3',
[
'-m',
'twine',
'upload',
'--repository-url',
repoUrl,
'--non-interactive',
'--skip-existing',
'--verbose',
gpgSign ? '--sign' : null,
gpgSign && gpgIdentity ? '--identity' : null,
gpgSign && gpgIdentity ? gpgIdentity : null,
`${distDir}/*`
].filter((arg) => arg !== null),
{
cwd: path.dirname(setupPy),
env: {
TWINE_PASSWORD: process.env.PYPI_TOKEN,
TWINE_USERNAME: process.env.PYPI_USERNAME ? process.env.PYPI_USERNAME : '__token__'
}
}
)
}
/**
* @param pluginConfig
* @param logger
* @param stdout
* @param stderr
*/
async function publishSetupCfg(pluginConfig, logger, stdout, stderr) {
const setupPy = getOption(pluginConfig, 'setupPy')
const distDir = getOption(pluginConfig, 'distDir')
const pypiPublish = getOption(pluginConfig, 'pypiPublish')
const repoUrl = process.env.PYPI_REPO_URL ? process.env.PYPI_REPO_URL : getOption(pluginConfig, 'repoUrl')
const gpgSign = getOption(pluginConfig, 'gpgSign')
const gpgIdentity = getOption(pluginConfig, 'gpgIdentity')
if (pypiPublish) {
logger.log(`Publishing package to ${repoUrl}`)
const result = publishPackage(setupPy, distDir, repoUrl, gpgSign, gpgIdentity)
result.stdout.pipe(stdout, { end: false })
result.stderr.pipe(stderr, { end: false })
await result
} else {
logger.log('Not publishing package due to requested configuration')
}
}
/**
* @param pluginConfig
* @param logger
* @param stdout
* @param stderr
*/
async function publishPoetry(pluginConfig, logger) {
const setupPy = getOption(pluginConfig, 'setupPy')
const pypiPublish = getOption(pluginConfig, 'pypiPublish')
const repoUrl = process.env.PYPI_REPO_URL ? process.env.PYPI_REPO_URL : getOption(pluginConfig, 'repoUrl')
if (pypiPublish) {
try {
logger.log(`Publishing package to ${repoUrl}`)
await execa('poetry', ['config', 'repositories.repo', repoUrl], { cwd: path.dirname(setupPy) })
} catch {
throw new Error(`Failed to run "poetry config repositories.repo ${repoUrl}"`)
}
try {
await execa(
'poetry',
[
'publish',
'--build',
'--repository',
'repo',
'--username',
'__token__',
'--password',
process.env.PYPI_TOKEN,
'--no-interaction',
'-vvv'
],
{ cwd: path.dirname(setupPy) }
)
} catch {
throw new Error(`Failed to run "poetry config repositories.repo ${repoUrl}"`)
}
} else {
logger.log('Not publishing package due to requested configuration')
}
}
/**
* @param pluginConfig
* @param root0
* @param root0.logger
* @param root0.stdout
* @param root0.stderr
*/
async function publish(pluginConfig, { logger, stdout, stderr }) {
if (fs.existsSync('./setup.cfg')) {
await publishSetupCfg(pluginConfig, logger, stdout, stderr)
} else if (fs.existsSync('./pyproject.toml')) {
await publishPoetry(pluginConfig, logger)
} else {
const pypiPublish = getOption(pluginConfig, 'pypiPublish')
if (pypiPublish !== false) {
throw new Error(`Project must have either a setup.cfg or a pyproject.toml file`)
}
}
}
module.exports = {
publish,
publishPackage
}