Skip to content

Commit

Permalink
feat(deps): no longer depending on @semantic-release/npm
Browse files Browse the repository at this point in the history
  • Loading branch information
ext committed Mar 19, 2024
1 parent c182102 commit ea3c09d
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 413 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AggregateError from "aggregate-error";
import { temporaryFile } from "tempy";
import getPkg from "@semantic-release/npm/lib/get-pkg.js";
import verifyNpmConfig from "@semantic-release/npm/lib/verify-config.js";
import getPkg from "./lib/get-pkg.js";
import verifyNpmConfig from "./lib/verify-config.js";
import verifyNpmAuth from "./lib/verify-auth.js";
import verifyGit from "./lib/verify-git.js";
import prepareNpm from "./lib/prepare.js";
Expand Down
5 changes: 5 additions & 0 deletions lib/get-channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import semver from "semver";

export default function (channel) {
return channel ? (semver.validRange(channel) ? `release-${channel}` : channel) : "latest";
}
22 changes: 22 additions & 0 deletions lib/get-pkg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import path from "path";
import { readPackage } from "read-pkg";
import AggregateError from "aggregate-error";
import getError from "./get-error.js";

export default async function ({ pkgRoot }, { cwd }) {
try {
const pkg = await readPackage({ cwd: pkgRoot ? path.resolve(cwd, String(pkgRoot)) : cwd });

if (!pkg.name) {
throw getError("ENOPKGNAME");
}

return pkg;
} catch (error) {
if (error.code === "ENOENT") {
throw new AggregateError([getError("ENOPKG")]);
}

throw new AggregateError([error]);
}
}
18 changes: 18 additions & 0 deletions lib/get-registry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import path from "path";
import rc from "rc";
import getRegistryUrl from "registry-auth-token/registry-url.js";

export default function ({ publishConfig: { registry } = {}, name }, { cwd, env }) {
return (
registry ||
env.NPM_CONFIG_REGISTRY ||
getRegistryUrl(
name.split("/")[0],
rc(
"npm",
{ registry: "https://registry.npmjs.org/" },
{ config: env.NPM_CONFIG_USERCONFIG || path.resolve(cwd, ".npmrc") },
),
)
);
}
17 changes: 17 additions & 0 deletions lib/get-release-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import normalizeUrl from "normalize-url";

export default function (
{ name },
{ env: { DEFAULT_NPM_REGISTRY = "https://registry.npmjs.org/" }, nextRelease: { version } },
distTag,
registry,
) {
return {
name: `npm package (@${distTag} dist-tag)`,
url:
normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY)
? `https://www.npmjs.com/package/${name}/v/${version}`
: undefined,
channel: distTag,
};
}
6 changes: 3 additions & 3 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createRequire } from "node:module";
import { execa } from "execa";
import getRegistry from "@semantic-release/npm/lib/get-registry.js";
import getChannel from "@semantic-release/npm/lib/get-channel.js";
import getReleaseInfo from "@semantic-release/npm/lib/get-release-info.js";
import getRegistry from "./get-registry.js";
import getChannel from "./get-channel.js";
import getReleaseInfo from "./get-release-info.js";

/**
* @param {string} npmrc
Expand Down
42 changes: 42 additions & 0 deletions lib/set-npmrc-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import path from "path";
import rc from "rc";
import fs from "fs-extra";
import getAuthToken from "registry-auth-token";
import nerfDart from "nerf-dart";
import AggregateError from "aggregate-error";
import getError from "./get-error.js";

export default async function (
npmrc,
registry,
{ cwd, env: { NPM_TOKEN, NPM_CONFIG_USERCONFIG }, logger },
) {
logger.log("Verify authentication for registry %s", registry);
const { configs, ...rcConfig } = rc(
"npm",
{ registry: "https://registry.npmjs.org/" },
{ config: NPM_CONFIG_USERCONFIG || path.resolve(cwd, ".npmrc") },
);

if (configs) {
logger.log("Reading npm config from %s", configs.join(", "));
}

const currentConfig = configs
? (await Promise.all(configs.map((config) => fs.readFile(config)))).join("\n")
: "";

if (getAuthToken(registry, { npmrc: rcConfig })) {
await fs.outputFile(npmrc, currentConfig);
return;
}

if (NPM_TOKEN) {
const oldConfig = currentConfig ? `${currentConfig}\n` : "";
const newConfig = `${nerfDart(registry)}:_authToken = \${NPM_TOKEN}`;
await fs.outputFile(npmrc, `${oldConfig}${newConfig}`);
logger.log(`Wrote NPM_TOKEN to ${npmrc}`);
} else {
throw new AggregateError([getError("ENONPMTOKEN", { registry })]);
}
}
6 changes: 3 additions & 3 deletions lib/verify-auth.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { execa } from "execa";
import AggregateError from "aggregate-error";
import getError from "@semantic-release/npm/lib/get-error.js";
import getRegistry from "@semantic-release/npm/lib/get-registry.js";
import setNpmrcAuth from "@semantic-release/npm/lib/set-npmrc-auth.js";
import getError from "./get-error.js";
import getRegistry from "./get-registry.js";
import setNpmrcAuth from "./set-npmrc-auth.js";

export default async function (npmrc, pkg, context) {
const { cwd, env, stdout, stderr } = context;
Expand Down
20 changes: 20 additions & 0 deletions lib/verify-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isBoolean, isNil, isString } from "lodash-es";
import getError from "./get-error.js";

const isNonEmptyString = (value) => isString(value) && value.trim();

const VALIDATORS = {
npmPublish: isBoolean,
tarballDir: isNonEmptyString,
pkgRoot: isNonEmptyString,
};

export default function ({ npmPublish, tarballDir, pkgRoot }) {
return Object.entries({ npmPublish, tarballDir, pkgRoot }).reduce(
(errors, [option, value]) =>
!isNil(value) && !VALIDATORS[option](value)
? [...errors, getError(`EINVALID${option.toUpperCase()}`, { [option]: value })]
: errors,
[],
);
}
Loading

0 comments on commit ea3c09d

Please sign in to comment.