Skip to content

feat: migrate to esm #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ jobs:

```js
// package.release.config.js
module.exports = {
/**
* @type {import('semantic-release').GlobalConfig}
*/
export default {
plugins: [
'@semantic-release/commit-analyzer',
'@semantic-release/release-notes-generator',
Expand All @@ -214,7 +217,10 @@ jobs:

```js
// publish.release.config.js
module.exports = {
/**
* @type {import('semantic-release').GlobalConfig}
*/
export default {
plugins: [
'@semantic-release/commit-analyzer',
'@semantic-release/release-notes-generator',
Expand Down
File renamed without changes.
34 changes: 13 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// @ts-check

const path = require('path');
const verifyVsce = require('./lib/verify');
const vscePublish = require('./lib/publish');
const vscePrepare = require('./lib/prepare');
import { join } from 'node:path';
import { prepare as vscePrepare } from './lib/prepare.js';
import { publish as vscePublish } from './lib/publish.js';
import { verify as vsceVerify } from './lib/verify.js';

let verified = false;
let prepared = false;
let packagePath;

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

async function prepare(
export async function prepare(
pluginConfig,
{ nextRelease: { version }, logger, cwd },
) {
cwd = getPackageRoot(pluginConfig, cwd);
if (!verified) {
await verifyVsce(pluginConfig, { logger, cwd });
await vsceVerify(pluginConfig, { logger, cwd });
verified = true;
}
packagePath = await vscePrepare(
Expand All @@ -33,13 +33,13 @@ async function prepare(
prepared = true;
}

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

Expand All @@ -60,21 +60,13 @@ async function publish(

if (pluginConfig?.publishPackagePath) {
// Expand glob
const globSync = require('glob').glob.sync;
packagePath = globSync(pluginConfig.publishPackagePath, { cwd });
const glob = (await import('glob')).glob;
packagePath = await glob(pluginConfig.publishPackagePath, { cwd });
}

return vscePublish(version, packagePath, logger, cwd);
}

function getPackageRoot(pluginConfig, cwd) {
return pluginConfig.packageRoot
? path.join(cwd, pluginConfig.packageRoot)
: cwd;
return pluginConfig.packageRoot ? join(cwd, pluginConfig.packageRoot) : cwd;
}

module.exports = {
verifyConditions,
prepare,
publish,
};
15 changes: 8 additions & 7 deletions lib/prepare.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// @ts-check

const execa = require('execa');
const { readJson } = require('fs-extra');
const path = require('path');
const { isOvsxPublishEnabled, isTargetEnabled } = require('./utils');
import { execa } from 'execa';
import { readJson } from 'fs-extra/esm';
import { join } from 'node:path';
import process from 'node:process';
import { isOvsxPublishEnabled, isTargetEnabled } from './utils.js';

module.exports = async (version, packageVsix, logger, cwd) => {
export async function prepare(version, packageVsix, logger, cwd) {
if (packageVsix === false) {
return;
}
Expand All @@ -24,7 +25,7 @@ module.exports = async (version, packageVsix, logger, cwd) => {
if (typeof packageVsix === 'string') {
packagePath = packageVsix;
} else {
const { name } = await readJson(path.join(cwd, './package.json'));
const { name } = await readJson(join(cwd, './package.json'));
if (isTargetEnabled()) {
packagePath = `${name}-${process.env.VSCE_TARGET}-${version}.vsix`;
} else {
Expand All @@ -49,4 +50,4 @@ module.exports = async (version, packageVsix, logger, cwd) => {

return packagePath;
}
};
}
19 changes: 10 additions & 9 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// @ts-check

const execa = require('execa');
const { readJson } = require('fs-extra');
const path = require('path');
const {
import { execa } from 'execa';
import { readJson } from 'fs-extra/esm';
import { join } from 'node:path';
import process from 'node:process';
import {
isAzureCredentialEnabled,
isOvsxPublishEnabled,
isTargetEnabled,
isVscePublishEnabled,
isAzureCredentialEnabled,
} = require('./utils');
} from './utils.js';

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

const options = ['publish'];

Expand Down Expand Up @@ -69,4 +70,4 @@ module.exports = async (version, packagePath, logger, cwd) => {
// TODO: uncomment after https://github.com/semantic-release/semantic-release/issues/2123
// return releases;
return releases.shift();
};
}
29 changes: 12 additions & 17 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
// @ts-check

const envToBoolean = (name) => {
import process from 'node:process';

function envToBoolean(name) {
return process.env[name] === 'true' || process.env[name] === '1';
};
}

const isOvsxPublishEnabled = () => {
export function isOvsxPublishEnabled() {
return 'OVSX_PAT' in process.env;
};
}

const isAzureCredentialEnabled = () => {
export function isAzureCredentialEnabled() {
return envToBoolean('VSCE_AZURE_CREDENTIAL');
};
}

const isVscePublishEnabled = () => {
export function isVscePublishEnabled() {
return 'VSCE_PAT' in process.env || isAzureCredentialEnabled();
};
}

const isTargetEnabled = () => {
export function isTargetEnabled() {
return (
'VSCE_TARGET' in process.env && process.env.VSCE_TARGET !== 'universal'
);
};

module.exports = {
isTargetEnabled,
isOvsxPublishEnabled,
isVscePublishEnabled,
isAzureCredentialEnabled,
};
}
9 changes: 5 additions & 4 deletions lib/verify-ovsx-auth.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// @ts-check

const SemanticReleaseError = require('@semantic-release/error');
const execa = require('execa');
import SemanticReleaseError from '@semantic-release/error';
import { execa } from 'execa';
import process from 'node:process';

module.exports = async (logger, cwd) => {
export async function verifyOvsxAuth(logger, cwd) {
logger.log('Verifying authentication for ovsx');

if (!process.env.OVSX_PAT) {
Expand All @@ -21,4 +22,4 @@ module.exports = async (logger, cwd) => {
'EINVALIDOVSXPAT',
);
}
};
}
15 changes: 7 additions & 8 deletions lib/verify-pkg.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// @ts-check

const SemanticReleaseError = require('@semantic-release/error');
const { readJson } = require('fs-extra');
const fs = require('fs');
const path = require('path');
import SemanticReleaseError from '@semantic-release/error';
import { pathExists, readJson } from 'fs-extra/esm';
import { join } from 'node:path';

module.exports = async (cwd) => {
const packagePath = path.join(cwd, './package.json');
export async function verifyPkg(cwd) {
const packagePath = join(cwd, './package.json');

if (!fs.existsSync(packagePath)) {
if (!(await pathExists(packagePath))) {
throw new SemanticReleaseError(
`${packagePath} was not found. A \`package.json\` is required to release with vsce.`,
'ENOPKG',
Expand Down Expand Up @@ -40,4 +39,4 @@ module.exports = async (cwd) => {
'ENOPUBLISHER',
);
}
};
}
11 changes: 6 additions & 5 deletions lib/verify-target.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// @ts-check

const SemanticReleaseError = require('@semantic-release/error');
const { isTargetEnabled } = require('./utils');
import SemanticReleaseError from '@semantic-release/error';
import process from 'node:process';
import { isTargetEnabled } from './utils.js';

module.exports = async () => {
export async function verifyTarget() {
if (!isTargetEnabled()) {
return;
}
Expand All @@ -16,7 +17,7 @@ module.exports = async () => {
}

if (process.env.VSCE_TARGET !== 'universal') {
const targets = require('@vscode/vsce/out/package').Targets;
const targets = (await import('@vscode/vsce/out/package.js')).Targets;

// Throw if the target is not supported
if (!targets.has(process.env.VSCE_TARGET)) {
Expand All @@ -28,4 +29,4 @@ module.exports = async () => {
);
}
}
};
}
11 changes: 6 additions & 5 deletions lib/verify-vsce-auth.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// @ts-check

const SemanticReleaseError = require('@semantic-release/error');
const execa = require('execa');
const { isAzureCredentialEnabled } = require('./utils');
import SemanticReleaseError from '@semantic-release/error';
import { execa } from 'execa';
import process from 'node:process';
import { isAzureCredentialEnabled } from './utils.js';

module.exports = async (logger, cwd) => {
export async function verifyVsceAuth(logger, cwd) {
const pat = 'VSCE_PAT' in process.env && process.env.VSCE_PAT;
const azureCredential = isAzureCredentialEnabled();

Expand Down Expand Up @@ -35,4 +36,4 @@ module.exports = async (logger, cwd) => {
'EINVALIDVSCEPAT',
);
}
};
}
16 changes: 8 additions & 8 deletions lib/verify.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// @ts-check

const SemanticReleaseError = require('@semantic-release/error');
const verifyPkg = require('./verify-pkg');
const verifyVsceAuth = require('./verify-vsce-auth');
const verifyOvsxAuth = require('./verify-ovsx-auth');
const verifyTarget = require('./verify-target');
const { isOvsxPublishEnabled, isVscePublishEnabled } = require('./utils');
import SemanticReleaseError from '@semantic-release/error';
import { isOvsxPublishEnabled, isVscePublishEnabled } from './utils.js';
import { verifyOvsxAuth } from './verify-ovsx-auth.js';
import { verifyPkg } from './verify-pkg.js';
import { verifyTarget } from './verify-target.js';
import { verifyVsceAuth } from './verify-vsce-auth.js';

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

Expand Down Expand Up @@ -35,4 +35,4 @@ module.exports = async (pluginConfig, { logger, cwd }) => {
);
}
}
};
}
Loading
Loading