Skip to content

Commit fbe1ea9

Browse files
authoredMar 11, 2021
feat: add support for VSCE_PAT env var and deprecate VSCE_TOKEN (#109)
This adds support for using the VSCE_PAT environment variable supported in vsce. If both VSCE_PAT and VSCE_TOKEN are set in the environment then VSCE_PAT is preferred. Also deprecates the VSCE_TOKEN. Closes #84
1 parent 1bb5721 commit fbe1ea9

File tree

5 files changed

+67
-10
lines changed

5 files changed

+67
-10
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Prior to v13, you had to override `getLastRelease` to use `@semantic-release/git
5555

5656
#### Travis example
5757

58-
Secret environment variables: `VSCE_TOKEN`
58+
Secret environment variables: `VSCE_PAT`
5959

6060
Example:
6161

‎lib/publish.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ const execa = require('execa');
22
const { readJson } = require('fs-extra');
33

44
module.exports = async (version, yarn, logger) => {
5-
const { VSCE_TOKEN } = process.env;
6-
75
const { publisher, name } = await readJson('./package.json');
86

97
logger.log('Publishing version %s to vs code marketplace', version);
108

11-
const options = ['publish', '--pat', VSCE_TOKEN];
9+
const options = ['publish'];
1210

1311
if (yarn) {
1412
options.push('--yarn');

‎lib/verify-auth.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ const SemanticReleaseError = require('@semantic-release/error');
33

44
module.exports = async (logger) => {
55
logger.log('Verify authentication for vsce');
6-
const { VSCE_TOKEN } = process.env;
76

8-
if (!VSCE_TOKEN) {
9-
throw new SemanticReleaseError('No vsce personal access token specified (set the "VSCE_TOKEN" environment variable).', 'ENOVSCEPAT');
7+
if (!('VSCE_PAT' in process.env) && ('VSCE_TOKEN' in process.env)) {
8+
logger.log('VSCE_TOKEN is deprecated and may be removed in the next major release, please use VSCE_PAT instead.');
9+
process.env.VSCE_PAT = process.env.VSCE_TOKEN;
10+
}
11+
12+
if (!process.env.VSCE_PAT) {
13+
throw new SemanticReleaseError('No vsce personal access token specified (set the "VSCE_PAT" environment variable).', 'ENOVSCEPAT');
1014
}
1115

1216
try {
13-
execa.sync('vsce', ['verify-pat', '--pat', VSCE_TOKEN]);
17+
const options = ['verify-pat'];
18+
execa.sync('vsce', options);
1419
} catch (e) {
1520
throw new SemanticReleaseError(`Invalid vsce token. Additional information:\n\n${e}`, 'EINVALIDVSCETOKEN');
1621
}

‎test/publish.test.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test('publish', async t => {
3838
name: 'Visual Studio Marketplace',
3939
url: `https://marketplace.visualstudio.com/items?itemName=${publisher}.${name}`
4040
});
41-
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['publish', '--pat', token], { stdio: 'inherit' }]);
41+
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['publish'], { stdio: 'inherit' }]);
4242
});
4343

4444
test('publish when yarn is true', async t => {
@@ -64,5 +64,31 @@ test('publish when yarn is true', async t => {
6464
name: 'Visual Studio Marketplace',
6565
url: `https://marketplace.visualstudio.com/items?itemName=${publisher}.${name}`
6666
});
67-
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['publish', '--pat', token, '--yarn'], { stdio: 'inherit' }]);
67+
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['publish', '--yarn'], { stdio: 'inherit' }]);
68+
});
69+
70+
test('publish with VSCE_PAT and VSCE_TOKEN should prefer VSCE_PAT', async t => {
71+
const { execaStub } = t.context.stubs;
72+
const publisher = 'semantic-release-vsce';
73+
const name = 'Semantice Release VSCE';
74+
const publish = proxyquire('../lib/publish', {
75+
execa: execaStub,
76+
'fs-extra': {
77+
readJson: sinon.stub().returns({
78+
publisher,
79+
name
80+
})
81+
}
82+
});
83+
84+
const token = 'abc123';
85+
process.env.VSCE_TOKEN = token;
86+
process.env.VSCE_PAT = token;
87+
const result = await publish('1.0.0', undefined, logger);
88+
89+
t.deepEqual(result, {
90+
name: 'Visual Studio Marketplace',
91+
url: `https://marketplace.visualstudio.com/items?itemName=${publisher}.${name}`
92+
});
93+
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['publish'], { stdio: 'inherit' }]);
6894
});

‎test/verify-auth.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ test('VSCE_TOKEN is set', async t => {
1818

1919
test('VSCE_TOKEN is not set', async t => {
2020
delete process.env.VSCE_TOKEN;
21+
delete process.env.VSCE_PAT;
22+
23+
await t.throwsAsync(() => verifyAuth(logger), { instanceOf: SemanticReleaseError, code: 'ENOVSCEPAT' });
24+
});
25+
26+
test('VSCE_PAT is set', async t => {
27+
process.env.VSCE_PAT = 'abc123';
28+
29+
await t.notThrowsAsync(() => verifyAuth(logger));
30+
});
31+
32+
test('VSCE_PAT is not set', async t => {
33+
delete process.env.VSCE_TOKEN;
34+
delete process.env.VSCE_PAT;
2135

2236
await t.throwsAsync(() => verifyAuth(logger), { instanceOf: SemanticReleaseError, code: 'ENOVSCEPAT' });
2337
});
@@ -34,3 +48,17 @@ test('VSCE_TOKEN is invalid', async t => {
3448

3549
await t.throwsAsync(() => verifyAuth(logger), { instanceOf: SemanticReleaseError, code: 'EINVALIDVSCETOKEN' });
3650
});
51+
52+
test('VSCE_PAT is valid', async t => {
53+
process.env.VSCE_PAT = 'abc123';
54+
sinon.stub(execa, 'sync').withArgs('vsce', ['verify-pat']).returns();
55+
56+
await t.notThrowsAsync(() => verifyAuth(logger));
57+
});
58+
59+
test('VSCE_PAT is invalid', async t => {
60+
process.env.VSCE_PAT = 'abc123';
61+
execa.sync.restore();
62+
63+
await t.throwsAsync(() => verifyAuth(logger), { instanceOf: SemanticReleaseError, code: 'EINVALIDVSCETOKEN' });
64+
});

0 commit comments

Comments
 (0)
Please sign in to comment.