Skip to content

Commit 45459d0

Browse files
committed
feat(config): allow reading repository configurations from file
1 parent 96111a7 commit 45459d0

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed

lib/default-options.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ export class DefaultConfig {
2828
return this.config.repoUrl ?? 'https://upload.pypi.org/legacy/';
2929
}
3030

31+
public get repoUsername() {
32+
return this.config.repoUsername ?? '__token__';
33+
}
34+
35+
public get repoToken() {
36+
return this.config.repoToken ?? '';
37+
}
38+
3139
public get pypiPublish() {
3240
return this.config.pypiPublish ?? true;
3341
}

lib/publish.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ function publishPackage(
99
srcDir: string,
1010
distDir: string,
1111
repoUrl: string,
12+
repoUsername: string,
13+
repoToken: string,
1214
gpgSign: boolean,
1315
gpgIdentity?: string,
1416
options?: Options,
@@ -37,10 +39,8 @@ function publishPackage(
3739
cwd: srcDir,
3840
env: {
3941
...options?.env,
40-
TWINE_USERNAME: process.env['PYPI_USERNAME']
41-
? process.env['PYPI_USERNAME']
42-
: '__token__',
43-
TWINE_PASSWORD: process.env['PYPI_TOKEN'],
42+
TWINE_USERNAME: repoUsername,
43+
TWINE_PASSWORD: repoToken,
4444
},
4545
},
4646
);
@@ -55,6 +55,8 @@ async function publish(pluginConfig: PluginConfig, context: Context) {
5555
gpgSign,
5656
gpgIdentity,
5757
repoUrl,
58+
repoUsername,
59+
repoToken,
5860
envDir,
5961
} = new DefaultConfig(pluginConfig);
6062

@@ -65,10 +67,17 @@ async function publish(pluginConfig: PluginConfig, context: Context) {
6567

6668
if (pypiPublish !== false) {
6769
logger.log(`Publishing package to ${repoUrl}`);
70+
if (process.env['PYPI_TOKEN'] === undefined && repoToken === '') {
71+
logger.error(
72+
'Token is not set. Either set PYPI_TOKEN environment variable or repoToken in plugin configuration',
73+
);
74+
}
6875
const result = publishPackage(
6976
srcDir,
7077
distDir,
7178
process.env['PYPI_REPO_URL'] ?? repoUrl,
79+
process.env['PYPI_USERNAME'] ?? repoUsername,
80+
process.env['PYPI_TOKEN'] ?? repoToken,
7281
gpgSign,
7382
gpgIdentity,
7483
options,

lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export interface PluginConfig {
22
srcDir?: string;
33
distDir?: string;
44
repoUrl?: string;
5+
repoUsername?: string;
6+
repoToken?: string;
57
pypiPublish?: boolean;
68
gpgSign?: boolean;
79
gpgIdentity?: string;

lib/verify.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,31 @@ function assertVersionCmd(pyproject: any, versionCmd?: string) {
9090

9191
async function verify(pluginConfig: PluginConfig, context: Context) {
9292
const { logger } = context;
93-
const { srcDir, setupPath, pypiPublish, repoUrl, versionCmd } =
94-
new DefaultConfig(pluginConfig);
93+
const {
94+
srcDir,
95+
setupPath,
96+
pypiPublish,
97+
repoUrl,
98+
repoUsername,
99+
repoToken,
100+
versionCmd,
101+
} = new DefaultConfig(pluginConfig);
95102

96103
const execaOptions: Options = pipe(context);
97104

98105
if (pypiPublish !== false) {
99-
const username = process.env['PYPI_USERNAME']
100-
? process.env['PYPI_USERNAME']
101-
: '__token__';
102-
const token = process.env['PYPI_TOKEN'];
103106
const repo = process.env['PYPI_REPO_URL'] ?? repoUrl;
107+
const username = process.env['PYPI_USERNAME'] ?? repoUsername;
108+
const token = process.env['PYPI_TOKEN'] ?? repoToken;
104109

105-
assertEnvVar('PYPI_TOKEN');
110+
if (token === '') {
111+
throw new Error(
112+
`Token is not set. Either set PYPI_TOKEN environment variable or repoToken in plugin configuration`,
113+
);
114+
}
106115

107116
logger.log(`Verify authentication for ${username}@${repo}`);
108-
await verifyAuth(repo, username, token!);
117+
await verifyAuth(repo, username, token);
109118
}
110119

111120
if (isLegacyBuildInterface(srcDir)) {

test/util.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ test('test DefaultConfig', async () => {
77
expect(new DefaultConfig({}).srcDir).toBe('.');
88
expect(new DefaultConfig({}).setupPath).toBe('setup.py');
99
expect(new DefaultConfig({}).repoUrl).toBe('https://upload.pypi.org/legacy/');
10+
expect(new DefaultConfig({}).repoUsername).toBe('__token__');
11+
expect(new DefaultConfig({}).repoToken).toBe('');
1012
expect(new DefaultConfig({ distDir: 'mydist' }).distDir).toBe('mydist');
1113
});
1214

test/util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ function genPluginArgs(config: PluginConfig) {
129129
distDir: config.distDir ?? `.tmp/${packageName}/dist`,
130130
envDir: config.envDir ?? `.tmp/${packageName}/.venv`,
131131
repoUrl: config.repoUrl ?? 'https://test.pypi.org/legacy/',
132+
repoUsername: config.repoUsername ?? '__token__',
133+
repoToken: config.repoToken ?? '',
132134
};
133135

134136
const context: Context = {

0 commit comments

Comments
 (0)