Skip to content

Commit

Permalink
test: yarn (#9)
Browse files Browse the repository at this point in the history
Use Yarn instead of NPM. Rewritten all tests. Pending changes to use `yarn npm publish`.
  • Loading branch information
hongaar authored Dec 4, 2022
1 parent 409a24d commit 8decd74
Show file tree
Hide file tree
Showing 56 changed files with 3,020 additions and 2,595 deletions.
26 changes: 26 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/typescript-node
{
"name": "Node.js & TypeScript",
"image": "mcr.microsoft.com/devcontainers/typescript-node:16-bullseye",
"customizations": {
"vscode": {
"extensions": ["esbenp.prettier-vscode"]
}
}

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn install",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ node_modules
# artifacts
/dist
/types

# tmp
/.nyc_output
/coverage
550 changes: 550 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-version.cjs

Large diffs are not rendered by default.

Empty file added .yarn/versions/3049df3a.yml
Empty file.
2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ plugins:
spec: "@yarnpkg/plugin-interactive-tools"
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
spec: "@yarnpkg/plugin-typescript"
- path: .yarn/plugins/@yarnpkg/plugin-version.cjs
spec: "@yarnpkg/plugin-version"

yarnPath: .yarn/releases/yarn-3.3.0.cjs
15 changes: 15 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Migration log

## Yarn conversion

- Is DEFAULT_NPM_REGISTRY still needed?
- ESM dependencies (in node_modules) cause problems with jest. Configuration for
js/ts esm/cjs jest/ts-jest/babel is infuriatingly complex 😤
- We need version plugin. Any side-effects of installing it on the fly?
- Possibly unneeded deps:
- ~~tempy (only in --dev)~~
- ~~rc~~
- ~~normalize-url~~
- codecov
- nyc
- ava
- sinon

## TypeScript conversion

- `definitions/errors.ts` removed dynamic import of `package.json` (due to TS
Expand Down
48 changes: 29 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
plugin to publish a [npm](https://www.npmjs.com) package with
[yarn](https://yarnpkg.com).

> ⚠️
> Please note this plugin only works with **Yarn 2** and higher.
| Step | Description |
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `verifyConditions` | Verify Yarn 2 or higher is installed, verify the presence of the `NPM_TOKEN` environment variable or an `.yarnrc.yml` file, and verify the authentication method is valid. |
| `prepare` | Update the `package.json` version and [create](https://yarnpkg.com/cli/pack) the package tarball. |
| `addChannel` | [Add a tag](https://yarnpkg.com/cli/npm/tag/add) for the release. |
| `publish` | [Publish](https://yarnpkg.com/cli/npm/publish) to the npm registry. |
| Step | Description |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `verifyConditions` | Verify Yarn 2 or higher is installed, verify the presence of an NPM auth token (either in an environment variable or an `.yarnrc.yml` file) and verify the authentication method is valid. |
| `prepare` | Update the `package.json` version and [create](https://yarnpkg.com/cli/pack) the package tarball. |
| `addChannel` | [Add a tag](https://yarnpkg.com/cli/npm/tag/add) for the release. |
| `publish` | [Publish](https://yarnpkg.com/cli/npm/publish) to the npm registry. |

## Intended audience

Expand All @@ -36,6 +33,9 @@ As an added bonus, this plugin will also publish some simple monorepo patterns
yarn add --dev semantic-release-yarn
```

> ⚠️
> Please note this plugin only works with **Yarn 2** and higher.
## Usage

The plugin must be added in the
Expand All @@ -61,8 +61,9 @@ The NPM authentication configuration is **required** and can be set either via
[environment variables](#environment-variables) or the
[`.yarnrc.yml`](#yarn-configuration) file.

The options set in an existing `.yarnrc.yml` file will take precedence over
environment variables.
The configuration set by environment variables will take precedence over
configuration set in an existing `.yarnrc.yml` file as detailed in
[Yarnrc files](https://yarnpkg.com/configuration/yarnrc)

> ⚠️
> When
Expand All @@ -80,11 +81,20 @@ environment variables.
### Environment variables

| Variable | Description |
| ----------------------- | ---------------------------------------------------------------------- |
| `NPM_TOKEN` | [NPM token](https://docs.npmjs.com/creating-and-viewing-access-tokens) |
| `NPM_CONFIG_REGISTRY` | NPM registry to use |
| `NPM_CONFIG_USERCONFIG` | Path to non-default `.yarnrc.yml` file |
| Variable | Description |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `YARN_NPM_AUTH_TOKEN` | [NPM token](https://docs.npmjs.com/creating-and-viewing-access-tokens). Translates to the [npmAuthToken](https://yarnpkg.com/configuration/yarnrc#npmAuthToken) `.yarnrc.yml` option. |
| `YARN_NPM_PUBLISH_REGISTRY` | NPM registry to use. Translates to the [npmPublishRegistry](https://yarnpkg.com/configuration/yarnrc#npmPublishRegistry) `.yarnrc.yml` option. |

Other valid `.yarnrc.yml` options could be specified as environment variables as
mentioned in the [Yarnrc files](https://yarnpkg.com/configuration/yarnrc)
documentation:

> Finally, note that most settings can also be defined through environment
> variables (at least for the simpler ones; arrays and objects aren't supported
> yet). To do this, just prefix the names and write them in snake case:
> YARN_CACHE_FOLDER will set the cache folder (such values will overwrite any
> that might have been defined in the RC files - use them sparingly).
### Yarn configuration

Expand All @@ -93,13 +103,13 @@ configuration from a `.yarnrc.yml` file if present. See
[Yarnrc files](https://yarnpkg.com/configuration/yarnrc) for the option list.

The NPM registry to publish to can be configured via the
[environment variable](#environment-variables) `NPM_CONFIG_REGISTRY` and will
take precedence over the configuration in `.yarnrc.yml`.
[environment variable](#environment-variables) `YARN_NPM_PUBLISH_REGISTRY` and
will take precedence over the configuration in `.yarnrc.yml`.

The
[`registry`](https://yarnpkg.com/configuration/manifest#publishConfig.registry)
can be configured in the `package.json` and will take precedence over the
configuration in `.yarnrc.yml` and `NPM_CONFIG_REGISTRY`:
configuration in `.yarnrc.yml` and `YARN_NPM_PUBLISH_REGISTRY`:

```json
{
Expand Down
9 changes: 9 additions & 0 deletions ava.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
files: ["test/*.test.ts"],
timeout: "1m",
workerThreads: false,
extensions: {
ts: "module",
},
nodeArguments: ["--loader=ts-node/esm"],
};
34 changes: 13 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,68 +36,60 @@
"prepublish": "yarn build",
"release": "semantic-release",
"_comment": "remove 👇 these lines",
"test": "nyc ava -v",
"pretest": "npm run lint",
"codecov": "codecov -f coverage/coverage-final.json"
"test": "ava",
"test:watch": "ava --watch"
},
"engines": {
"node": ">=16 || ^14.17"
},
"dependencies": {
"@semantic-release/error": "^3.0.0",
"aggregate-error": "^3.0.0",
"cosmiconfig": "^8.0.0",
"execa": "^5.0.0",
"fs-extra": "^10.0.0",
"js-yaml": "^4.1.0",
"lodash": "^4.17.15",
"nerf-dart": "^1.0.0",
"normalize-url": "^8.0.0",
"npm": "^8.3.0",
"rc": "^1.2.8",
"read-pkg": "^5.0.0",
"registry-auth-token": "^5.0.1",
"semver": "^7.1.2",
"tempy": "^1.0.0"
"semver": "^7.1.2"
},
"devDependencies": {
"@semantic-release/changelog": "6.0.1",
"@semantic-release/git": "10.0.1",
"@types/dockerode": "3.3.14",
"@types/env-ci": "3.1.1",
"@types/fs-extra": "9.0.13",
"@types/js-yaml": "^4",
"@types/lodash": "4.14.190",
"@types/node": "18.11.9",
"@types/prettier": "2",
"@types/rc": "1.2.1",
"@types/registry-auth-token": "4.2.1",
"@types/semantic-release": "17.2.4",
"@types/semver": "7.3.13",
"ava": "4.2.0",
"codecov": "3.8.3",
"@types/sinon": "10.0.13",
"@types/stream-buffers": "3.0.4",
"ava": "5.1.0",
"delay": "5.0.0",
"dockerode": "3.3.4",
"get-stream": "6.0.1",
"got": "11.8.5",
"husky": "8.0.2",
"lint-staged": "13.0.4",
"moker": "0.4.4",
"nyc": "15.1.0",
"p-retry": "4.6.2",
"prettier": "2.8.0",
"semantic-release": "19.0.5",
"sinon": "14.0.0",
"sinon": "15.0.0",
"stream-buffers": "3.0.2",
"tmp": "^0.2.1",
"tempy": "1.0.1",
"ts-node": "10.9.1",
"typescript": "4.9.3"
},
"peerDependencies": {
"semantic-release": ">=19.0.0"
},
"ava": {
"files": [
"test/**/*.test.js"
],
"timeout": "2m",
"workerThreads": false
},
"nyc": {
"include": [
"lib/**/*.js",
Expand Down
9 changes: 4 additions & 5 deletions src/add-channel.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import execa from "execa";
import type { PackageJson } from "read-pkg";
import type { AddChannelContext } from "./definitions/context.js";
import type { PluginConfig } from "./definitions/pluginConfig.js";
import { getChannel } from "./get-channel.js";
import { getRegistry } from "./get-registry.js";
import { getReleaseInfo } from "./get-release-info.js";
import type { PluginConfig } from "./index.js";
import { getYarnConfig } from "./get-yarn-config.js";

export async function addChannel(
npmrc: string,
{ npmPublish }: PluginConfig,
pkg: PackageJson,
context: AddChannelContext
Expand All @@ -22,7 +22,8 @@ export async function addChannel(
} = context;

if (npmPublish !== false && pkg.private !== true) {
const registry = getRegistry(pkg, context);
const yarnrc = await getYarnConfig(context);
const registry = getRegistry(pkg, yarnrc, context);
const distTag = getChannel(channel!);

logger.log(
Expand All @@ -35,8 +36,6 @@ export async function addChannel(
"add",
`${pkg.name}@${version}`,
distTag,
"--userconfig",
npmrc,
"--registry",
registry,
],
Expand Down
12 changes: 12 additions & 0 deletions src/definitions/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
// @ts-ignore
import toNerfDart from "nerf-dart";

export const PLUGIN_NAME = "semantic-release-yarn";
export const PLUGIN_HOMEPAGE =
"https://github.com/hongaar/semantic-release-yarn";
export const PLUGIN_GIT_BRANCH = "main";

export const DEFAULT_NPM_REGISTRY = "https://registry.npmjs.org";
export const DEFAULT_YARN_REGISTRY = "https://registry.yarnpkg.com";

export function isDefaultRegistry(registry: string) {
return [DEFAULT_NPM_REGISTRY, DEFAULT_YARN_REGISTRY].some(
(defaultRegistry) => toNerfDart(defaultRegistry) === toNerfDart(registry)
);
}
25 changes: 13 additions & 12 deletions src/definitions/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CiEnv } from "env-ci";
import type { Commit, Config, Options } from "semantic-release";
import type stream from "node:stream";
import type { Commit, Options } from "semantic-release";

// @todo these types need testing

Expand All @@ -14,8 +15,8 @@ export type CommonContext = {
// https://github.com/semantic-release/semantic-release/blob/27b105337b16dfdffb0dfa36d1178015e7ba68a3/index.js#L256-L260
cwd: string;
env: typeof process.env;
stdout: NonNullable<Config["stdout"]>;
stderr: NonNullable<Config["stderr"]>;
stdout: stream.Writable;
stderr: stream.Writable;
envCi: CiEnv;

// https://github.com/semantic-release/semantic-release/blob/27b105337b16dfdffb0dfa36d1178015e7ba68a3/index.js#L262
Expand Down Expand Up @@ -112,23 +113,24 @@ export type FailContext = CommonContext & {
};

// @todo infer return type from https://github.com/semantic-release/semantic-release/blob/27b105337b16dfdffb0dfa36d1178015e7ba68a3/lib/branches/index.js#L70
type BranchSpec = {
export type BranchSpec = {
name: string;
tags?: Tag[];
};

// https://github.com/semantic-release/semantic-release/blob/27b105337b16dfdffb0dfa36d1178015e7ba68a3/index.js#L133
type Tag = {
export type Tag = {
version?: string;
channel?: string;
gitTag?: string;
gitHead?: string;
};

// https://github.com/semantic-release/semantic-release/blob/27b105337b16dfdffb0dfa36d1178015e7ba68a3/lib/get-release-to-add.js#LL51C9-L56C25
type Release = {
export type Release = {
version: string;
// https://github.com/sindresorhus/semver-diff#semverdiffversiona-versionb
type:
type?:
| "major"
| "premajor"
| "minor"
Expand All @@ -138,9 +140,8 @@ type Release = {
| "prerelease"
| "build"
| undefined;
version: string;
channel: string | null;
gitTag: string;
name: string;
gitHead: string;
channel?: string | null;
gitTag?: string;
name?: string;
gitHead?: string;
};
19 changes: 15 additions & 4 deletions src/definitions/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export function ENONPMTOKEN({ registry }: { registry: string }) {
message: "No npm token specified.",
details: `An [npm token](${linkify(
"README.md#npm-registry-authentication"
)}) must be created and set in the \`NPM_TOKEN\` environment variable on your CI environment.
)}) must be created and set in the \`YARN_NPM_AUTH_TOKEN\` environment variable on your CI environment.
Please make sure to create an [npm token](https://docs.npmjs.com/getting-started/working_with_tokens#how-to-create-new-tokens) and to set it in the \`NPM_TOKEN\` environment variable on your CI environment. The token must allow to publish to the registry \`${registry}\`.`,
Please make sure to create an [npm token](https://docs.npmjs.com/getting-started/working_with_tokens#how-to-create-new-tokens) and to set it in the \`YARN_NPM_AUTH_TOKEN\` environment variable on your CI environment. The token must allow to publish to the registry \`${registry}\`.`,
};
}

Expand All @@ -53,12 +53,12 @@ export function EINVALIDNPMTOKEN({ registry }: { registry: string }) {
message: "Invalid npm token.",
details: `The [npm token](${linkify(
"README.md#npm-registry-authentication"
)}) configured in the \`NPM_TOKEN\` environment variable must be a valid [token](https://docs.npmjs.com/getting-started/working_with_tokens) allowing to publish to the registry \`${registry}\`.
)}) configured in the \`YARN_NPM_AUTH_TOKEN\` environment variable must be a valid [token](https://docs.npmjs.com/getting-started/working_with_tokens) allowing to publish to the registry \`${registry}\`.
If you are using Two Factor Authentication for your account, set its level to ["Authorization only"](https://docs.npmjs.com/getting-started/using-two-factor-authentication#levels-of-authentication) in your account settings. **semantic-release** cannot publish with the default "
Authorization and writes" level.
Please make sure to set the \`NPM_TOKEN\` environment variable in your CI with the exact value of the npm token.`,
Please make sure to set the \`YARN_NPM_AUTH_TOKEN\` environment variable in your CI with the exact value of the npm token.`,
};
}

Expand All @@ -79,3 +79,14 @@ export function ENOPKG() {
Please follow the [npm guideline](https://docs.npmjs.com/getting-started/creating-node-modules) to create a valid \`package.json\` file.`,
};
}

export function EINVALIDYARN({ version }: { version: string }) {
return {
message: "Incompatible Yarn version detected.",
details: `The version of Yarn that you are using is not compatible. Please refer to [the README](${linkify(
"README.md#install"
)}) to review which versions of Yarn are currently supported
Your version of Yarn is \`${version}\`.`,
};
}
Loading

0 comments on commit 8decd74

Please sign in to comment.