Skip to content

Commit

Permalink
fix: use context's cwd for vsce calls (#349)
Browse files Browse the repository at this point in the history
Co-authored-by: Aram Becker <becker.aram@gmail.com>
  • Loading branch information
felipecrs and 1nVitr0 committed Oct 31, 2022
1 parent 6d18e32 commit 86b499e
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 44 deletions.
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

const verifyVsce = require('./lib/verify');
const vscePublish = require('./lib/publish');
const vscePrepare = require('./lib/prepare');
Expand All @@ -6,29 +8,29 @@ let verified = false;
let prepared = false;
let packagePath;

async function verifyConditions (pluginConfig, { logger }) {
await verifyVsce(logger, pluginConfig);
async function verifyConditions (pluginConfig, { logger, cwd }) {
await verifyVsce(pluginConfig, { logger, cwd });
verified = true;
}

async function prepare (pluginConfig, { nextRelease: { version }, logger }) {
async function prepare (pluginConfig, { nextRelease: { version }, logger, cwd }) {
if (!verified) {
await verifyVsce(logger);
await verifyVsce(pluginConfig, { logger, cwd });
verified = true;
}
packagePath = await vscePrepare(version, pluginConfig.packageVsix, logger);
packagePath = await vscePrepare(version, pluginConfig.packageVsix, logger, cwd);
prepared = true;
}

async function publish (pluginConfig, { nextRelease: { version }, logger }) {
async function publish (pluginConfig, { nextRelease: { version }, logger, cwd }) {
if (!verified) {
await verifyVsce(logger);
await verifyVsce(pluginConfig, { logger, cwd });
verified = true;
}

if (!prepared) {
// BC: prior to semantic-release v15 prepare was part of publish
packagePath = await vscePrepare(version, pluginConfig.packageVsix, logger);
packagePath = await vscePrepare(version, pluginConfig.packageVsix, logger, cwd);
}

// If publishing is disabled, return early.
Expand Down
6 changes: 4 additions & 2 deletions lib/prepare.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// @ts-check

const execa = require('execa');
const { readJson } = require('fs-extra');
const { isOvsxEnabled } = require('./verify-ovsx-auth');

module.exports = async (version, packageVsix, logger) => {
module.exports = async (version, packageVsix, logger, cwd) => {
const ovsxEnabled = isOvsxEnabled();
if (packageVsix || ovsxEnabled) {
if (!packageVsix && ovsxEnabled) {
Expand All @@ -22,7 +24,7 @@ module.exports = async (version, packageVsix, logger) => {

const options = ['package', version, '--no-git-tag-version', '--out', packagePath];

await execa('vsce', options, { stdio: 'inherit', preferLocal: true });
await execa('vsce', options, { stdio: 'inherit', preferLocal: true, cwd });

return packagePath;
}
Expand Down
8 changes: 5 additions & 3 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// @ts-check

const execa = require('execa');
const { readJson } = require('fs-extra');
const { isOvsxEnabled } = require('./verify-ovsx-auth');

module.exports = async (version, packagePath, logger) => {
module.exports = async (version, packagePath, logger, cwd) => {
const { publisher, name } = await readJson('./package.json');

const options = ['publish'];
Expand All @@ -15,7 +17,7 @@ module.exports = async (version, packagePath, logger) => {
options.push(...[version, '--no-git-tag-version']);
}

await execa('vsce', options, { stdio: 'inherit', preferLocal: true });
await execa('vsce', options, { stdio: 'inherit', preferLocal: true, cwd });

const vsceUrl = `https://marketplace.visualstudio.com/items?itemName=${publisher}.${name}`;
logger.log(`The new version is available at ${vsceUrl}.`);
Expand All @@ -28,7 +30,7 @@ module.exports = async (version, packagePath, logger) => {
if (isOvsxEnabled()) {
logger.log('Now publishing to OpenVSX');

await execa('ovsx', ['publish', packagePath], { stdio: 'inherit', preferLocal: true });
await execa('ovsx', ['publish', packagePath], { stdio: 'inherit', preferLocal: true, cwd });
const ovsxUrl = `https://open-vsx.org/extension/${publisher}/${name}/${version}`;

logger.log(`The new ovsx version is available at ${ovsxUrl}`);
Expand Down
6 changes: 4 additions & 2 deletions lib/verify-auth.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// @ts-check

const execa = require('execa');
const SemanticReleaseError = require('@semantic-release/error');

module.exports = async (logger) => {
module.exports = async (logger, cwd) => {
logger.log('Verifying authentication for vsce');

if (!process.env.VSCE_PAT) {
throw new SemanticReleaseError('No vsce personal access token specified (set the `VSCE_PAT` environment variable).', 'ENOVSCEPAT');
}

try {
await execa('vsce', ['verify-pat'], { preferLocal: true });
await execa('vsce', ['verify-pat'], { preferLocal: true, cwd });
} catch (e) {
throw new SemanticReleaseError(`Invalid vsce token. Additional information:\n\n${e}`, 'EINVALIDVSCETOKEN');
}
Expand Down
4 changes: 3 additions & 1 deletion lib/verify-ovsx-auth.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

const SemanticReleaseError = require('@semantic-release/error');

const isOvsxEnabled = () => {
Expand All @@ -18,7 +20,7 @@ module.exports = async (logger) => {

// TODO: waiting for https://github.com/eclipse/openvsx/issues/313
// try {
// await execa('ovsx', ['verify-pat'], { preferLocal: true });
// await execa('ovsx', ['verify-pat'], { preferLocal: true, cwd });
// } catch (e) {
// throw new SemanticReleaseError(`Invalid ovsx personal access token. Additional information:\n\n${e}`, 'EINVALIDOVSXPAT');
// }
Expand Down
2 changes: 2 additions & 0 deletions lib/verify-pkg.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

const SemanticReleaseError = require('@semantic-release/error');
const { readJson } = require('fs-extra');
const fs = require('fs');
Expand Down
6 changes: 4 additions & 2 deletions lib/verify.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// @ts-check

const verifyPkg = require('./verify-pkg');
const verifyAuth = require('./verify-auth');
const verifyOvsxAuth = require('./verify-ovsx-auth');

module.exports = async (logger, pluginConfig) => {
module.exports = async (pluginConfig, { logger, cwd }) => {
await verifyPkg();

if (pluginConfig?.publish !== false) {
await verifyAuth(logger);
await verifyAuth(logger, cwd);
await verifyOvsxAuth(logger);
}
};
13 changes: 8 additions & 5 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const semanticReleasePayload = {
},
logger: {
log: sinon.fake()
}
},
cwd: process.cwd()
};

const pluginConfig = {
Expand Down Expand Up @@ -39,7 +40,7 @@ test('verifyConditions', async t => {

await verifyConditions(pluginConfig, semanticReleasePayload);

t.true(verifyVsceStub.calledOnceWith(semanticReleasePayload.logger));
t.true(verifyVsceStub.calledOnceWith(pluginConfig, { logger: semanticReleasePayload.logger, cwd: semanticReleasePayload.cwd }));
});

test('prepare and unverified', async t => {
Expand All @@ -53,11 +54,12 @@ test('prepare and unverified', async t => {

await prepare(pluginConfig, semanticReleasePayload);

t.true(verifyVsceStub.calledOnceWith(semanticReleasePayload.logger));
t.true(verifyVsceStub.calledOnceWith(pluginConfig, { logger: semanticReleasePayload.logger, cwd: semanticReleasePayload.cwd }));
t.deepEqual(vscePrepareStub.getCall(0).args, [
semanticReleasePayload.nextRelease.version,
pluginConfig.packageVsix,
semanticReleasePayload.logger
semanticReleasePayload.logger,
semanticReleasePayload.cwd
]);
});

Expand All @@ -76,7 +78,8 @@ test('prepare and verified', async t => {
t.deepEqual(vscePrepareStub.getCall(0).args, [
semanticReleasePayload.nextRelease.version,
pluginConfig.packageVsix,
semanticReleasePayload.logger
semanticReleasePayload.logger,
semanticReleasePayload.cwd
]);
});

Expand Down
13 changes: 7 additions & 6 deletions test/prepare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const proxyquire = require('proxyquire');
const logger = {
log: sinon.fake()
};
const cwd = process.cwd();

test.beforeEach(t => {
t.context.stubs = {
Expand Down Expand Up @@ -37,10 +38,10 @@ test('packageVsix is a string', async t => {
const version = '1.0.0';
const packageVsix = 'test.vsix';
const packagePath = packageVsix;
const result = await prepare(version, packageVsix, logger);
const result = await prepare(version, packageVsix, logger, cwd);

t.deepEqual(result, packagePath);
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['package', version, '--no-git-tag-version', '--out', packagePath], { stdio: 'inherit', preferLocal: true }]);
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['package', version, '--no-git-tag-version', '--out', packagePath], { stdio: 'inherit', preferLocal: true, cwd }]);
});

test('packageVsix is true', async t => {
Expand All @@ -60,10 +61,10 @@ test('packageVsix is true', async t => {
const packageVsix = true;
const packagePath = `${name}-${version}.vsix`;

const result = await prepare(version, packageVsix, logger);
const result = await prepare(version, packageVsix, logger, cwd);

t.deepEqual(result, packagePath);
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['package', version, '--no-git-tag-version', '--out', packagePath], { stdio: 'inherit', preferLocal: true }]);
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['package', version, '--no-git-tag-version', '--out', packagePath], { stdio: 'inherit', preferLocal: true, cwd }]);
});

test('packageVsix is not set but OVSX_PAT is', async t => {
Expand All @@ -87,8 +88,8 @@ test('packageVsix is not set but OVSX_PAT is', async t => {
const packageVsix = undefined;
const packagePath = `${name}-${version}.vsix`;

const result = await prepare(version, packageVsix, logger);
const result = await prepare(version, packageVsix, logger, cwd);

t.deepEqual(result, packagePath);
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['package', version, '--no-git-tag-version', '--out', packagePath], { stdio: 'inherit', preferLocal: true }]);
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['package', version, '--no-git-tag-version', '--out', packagePath], { stdio: 'inherit', preferLocal: true, cwd }]);
});
15 changes: 8 additions & 7 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const proxyquire = require('proxyquire');
const logger = {
log: sinon.fake()
};
const cwd = process.cwd();

test.beforeEach(t => {
t.context.stubs = {
Expand Down Expand Up @@ -35,13 +36,13 @@ test('publish', async t => {
sinon.stub(process, 'env').value({
VSCE_PAT: token
});
const result = await publish(version, undefined, logger);
const result = await publish(version, undefined, logger, cwd);

t.deepEqual(result, {
name: 'Visual Studio Marketplace',
url: `https://marketplace.visualstudio.com/items?itemName=${publisher}.${name}`
});
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['publish', version, '--no-git-tag-version'], { stdio: 'inherit', preferLocal: true }]);
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['publish', version, '--no-git-tag-version'], { stdio: 'inherit', preferLocal: true, cwd }]);
});

test('publish with packagePath', async t => {
Expand All @@ -64,13 +65,13 @@ test('publish with packagePath', async t => {
sinon.stub(process, 'env').value({
VSCE_PAT: token
});
const result = await publish(version, packagePath, logger);
const result = await publish(version, packagePath, logger, cwd);

t.deepEqual(result, {
name: 'Visual Studio Marketplace',
url: `https://marketplace.visualstudio.com/items?itemName=${publisher}.${name}`
});
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['publish', '--packagePath', packagePath], { stdio: 'inherit', preferLocal: true }]);
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['publish', '--packagePath', packagePath], { stdio: 'inherit', preferLocal: true, cwd }]);
});

test('publish to OpenVSX', async t => {
Expand All @@ -94,17 +95,17 @@ test('publish to OpenVSX', async t => {
OVSX_PAT: token,
VSCE_PAT: token
});
const result = await publish(version, packagePath, logger);
const result = await publish(version, packagePath, logger, cwd);

t.deepEqual(result, {
name: 'Visual Studio Marketplace',
url: `https://marketplace.visualstudio.com/items?itemName=${publisher}.${name}`
});
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['publish', '--packagePath', packagePath], { stdio: 'inherit', preferLocal: true }]);
t.deepEqual(execaStub.getCall(0).args, ['vsce', ['publish', '--packagePath', packagePath], { stdio: 'inherit', preferLocal: true, cwd }]);

// t.deepEqual(result[1], {
// name: 'Open VSX Registry',
// url: `https://open-vsx.org/extension/${publisher}/${name}/${version}`
// });
t.deepEqual(execaStub.getCall(1).args, ['ovsx', ['publish', packagePath], { stdio: 'inherit', preferLocal: true }]);
t.deepEqual(execaStub.getCall(1).args, ['ovsx', ['publish', packagePath], { stdio: 'inherit', preferLocal: true, cwd }]);
});
9 changes: 5 additions & 4 deletions test/verify-auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ const SemanticReleaseError = require('@semantic-release/error');
const logger = {
log: sinon.fake()
};
const cwd = process.cwd();

test('VSCE_PAT is set', async t => {
sinon.stub(process, 'env').value({
VSCE_PAT: 'abc123'
});

const verifyAuth = proxyquire('../lib/verify-auth', {
execa: sinon.stub().withArgs('vsce', ['verify-pat'], { preferLocal: true }).resolves()
execa: sinon.stub().withArgs('vsce', ['verify-pat'], { preferLocal: true, cwd }).resolves()
});

await t.notThrowsAsync(() => verifyAuth(logger));
Expand All @@ -23,7 +24,7 @@ test('VSCE_PAT is not set', async t => {
sinon.stub(process, 'env').value({});

const verifyAuth = proxyquire('../lib/verify-auth', {
execa: sinon.stub().withArgs('vsce', ['verify-pat'], { preferLocal: true }).resolves()
execa: sinon.stub().withArgs('vsce', ['verify-pat'], { preferLocal: true, cwd }).resolves()
});

await t.throwsAsync(() => verifyAuth(logger), { instanceOf: SemanticReleaseError, code: 'ENOVSCEPAT' });
Expand All @@ -35,7 +36,7 @@ test('VSCE_PAT is valid', async t => {
});

const verifyAuth = proxyquire('../lib/verify-auth', {
execa: sinon.stub().withArgs('vsce', ['verify-pat'], { preferLocal: true }).resolves()
execa: sinon.stub().withArgs('vsce', ['verify-pat'], { preferLocal: true, cwd }).resolves()
});

await t.notThrowsAsync(() => verifyAuth(logger));
Expand All @@ -47,7 +48,7 @@ test('VSCE_PAT is invalid', async t => {
});

const verifyAuth = proxyquire('../lib/verify-auth', {
execa: sinon.stub().withArgs('vsce', ['verify-pat'], { preferLocal: true }).rejects()
execa: sinon.stub().withArgs('vsce', ['verify-pat'], { preferLocal: true, cwd }).rejects()
});

await t.throwsAsync(() => verifyAuth(logger), { instanceOf: SemanticReleaseError, code: 'EINVALIDVSCETOKEN' });
Expand Down
9 changes: 5 additions & 4 deletions test/verify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ const proxyquire = require('proxyquire');
const logger = {
log: sinon.fake()
};
const cwd = process.cwd();

test('resolves', async t => {
const verify = proxyquire('../lib/verify', {
'./verify-auth': sinon.stub().resolves(),
'./verify-pkg': sinon.stub().resolves()
});

await t.notThrowsAsync(() => verify(logger));
await t.notThrowsAsync(() => verify({}, { logger, cwd }));
});

test('rejects with verify-auth', async t => {
Expand All @@ -21,7 +22,7 @@ test('rejects with verify-auth', async t => {
'./verify-pkg': sinon.stub().resolves()
});

await t.throwsAsync(() => verify(logger));
await t.throwsAsync(() => verify({}, { logger, cwd }));
});

test('rejects with verify-pkg', async t => {
Expand All @@ -30,7 +31,7 @@ test('rejects with verify-pkg', async t => {
'./verify-pkg': sinon.stub().rejects()
});

await t.throwsAsync(() => verify(logger));
await t.throwsAsync(() => verify({}, { logger, cwd }));
});

test('is does not verify the auth tokens if publishing is disabled', async t => {
Expand All @@ -45,7 +46,7 @@ test('is does not verify the auth tokens if publishing is disabled', async t =>
'./verify-ovsx-auth': stubs.verifyOvsxAuthStub
});

await verify(logger, { publish: false });
await verify({ publish: false }, { logger, cwd });

t.true(stubs.verifyAuthStub.notCalled);
t.true(stubs.verifyOvsxAuthStub.notCalled);
Expand Down

0 comments on commit 86b499e

Please sign in to comment.