yarn-version 1.0.0-pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +13 -0
- package/.eslintrc.json +5 -0
- package/.yo-rc.json +12 -0
- package/LICENSE +21 -0
- package/build/index-node18.mjs +1059 -0
- package/build/index-node18.mjs.map +1 -0
- package/package.json +84 -0
- package/rollup.config.mjs +12 -0
- package/src/.eslintrc.json +31 -0
- package/src/commands/version.test.ts +88 -0
- package/src/commands/version.ts +850 -0
- package/src/index.ts +13 -0
- package/src/missing-types.d.ts +7 -0
- package/src/utils/UsageError.ts +1 -0
- package/src/utils/bumpTypeUtils.ts +113 -0
- package/src/utils/conventionalChangelogUtils.ts +77 -0
- package/src/utils/conventionalCommitConfigUtils.ts +20 -0
- package/src/utils/execCommand.ts +87 -0
- package/src/utils/generatorUtils.ts +11 -0
- package/src/utils/gitUtils.ts +87 -0
- package/src/utils/githubUtils.ts +78 -0
- package/src/utils/mapUtils.ts +7 -0
- package/src/utils/packageDependenciesUtils.test.ts +13 -0
- package/src/utils/packageDependenciesUtils.ts +67 -0
- package/src/utils/packageUtils.ts +97 -0
- package/src/utils/updateChangelog.ts +39 -0
- package/src/utils/workspaceUtils.ts +142 -0
- package/tsconfig.json +24 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { program } from "commander";
|
|
3
|
+
|
|
4
|
+
import "./commands/version";
|
|
5
|
+
|
|
6
|
+
// relative to dist directory
|
|
7
|
+
const pkg = JSON.parse(
|
|
8
|
+
// eslint-disable-next-line unicorn/prefer-json-parse-buffer
|
|
9
|
+
fs.readFileSync(new URL("../package.json", import.meta.url), "utf8"),
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
program.name(pkg.name).description(pkg.description).version(pkg.version);
|
|
13
|
+
program.parse();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const UsageError = Error;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import semver from "semver";
|
|
2
|
+
import { UsageError } from "./UsageError";
|
|
3
|
+
import type { Workspace } from "./packageUtils";
|
|
4
|
+
import { getWorkspaceName } from "./workspaceUtils";
|
|
5
|
+
|
|
6
|
+
export type BumpType = "major" | "minor" | "patch";
|
|
7
|
+
|
|
8
|
+
// https://github.com/yarnpkg/berry/blob/506ded5f5f5a89553435940c74f1d857fd685a42/packages/plugin-version/sources/versionUtils.ts#L10
|
|
9
|
+
// Basically we only support auto-upgrading the ranges that are very simple (^x.y.z, ~x.y.z, >=x.y.z, and of course x.y.z)
|
|
10
|
+
const SUPPORTED_UPGRADE_REGEXP =
|
|
11
|
+
// eslint-disable-next-line unicorn/better-regex -- note that autofix cause issues https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1626
|
|
12
|
+
/^(>=|[~^]|)(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\+[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)?$/;
|
|
13
|
+
|
|
14
|
+
const yarnWorkspaceProtocol = "workspace:";
|
|
15
|
+
|
|
16
|
+
export const calcBumpRange = (
|
|
17
|
+
workspace: Workspace,
|
|
18
|
+
range: string,
|
|
19
|
+
newVersion: string,
|
|
20
|
+
): string => {
|
|
21
|
+
if (range === "*") {
|
|
22
|
+
return range;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let useWorkspaceProtocol = false;
|
|
26
|
+
|
|
27
|
+
if (range.startsWith(yarnWorkspaceProtocol)) {
|
|
28
|
+
const slicedRange = range.slice(yarnWorkspaceProtocol.length);
|
|
29
|
+
|
|
30
|
+
// Workspaces referenced through their path never get upgraded ("workspace:packages/yarnpkg-core")
|
|
31
|
+
if (slicedRange === workspace.relativeCwd) {
|
|
32
|
+
return range;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (slicedRange === "*") {
|
|
36
|
+
return range;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
range = slicedRange;
|
|
40
|
+
useWorkspaceProtocol = true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const parsed = SUPPORTED_UPGRADE_REGEXP.exec(range);
|
|
44
|
+
if (!parsed) {
|
|
45
|
+
const workspaceName = getWorkspaceName(workspace);
|
|
46
|
+
throw new Error(`Couldn't bump range ${range} in ${workspaceName}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return `${useWorkspaceProtocol ? yarnWorkspaceProtocol : ""}${
|
|
50
|
+
parsed[1]
|
|
51
|
+
}${newVersion}`;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const getHighestBumpType = (bumpTypes: BumpType[]): BumpType => {
|
|
55
|
+
if (bumpTypes.includes("major")) {
|
|
56
|
+
return "major";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (bumpTypes.includes("minor")) {
|
|
60
|
+
return "minor";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return "patch";
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const calcBumpType = (
|
|
67
|
+
bumpType: BumpType,
|
|
68
|
+
maxBumpType: BumpType,
|
|
69
|
+
): BumpType => {
|
|
70
|
+
if (maxBumpType === "major") {
|
|
71
|
+
return bumpType;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (maxBumpType === "minor") {
|
|
75
|
+
return bumpType === "major" || bumpType === "minor" ? "minor" : "patch";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return "patch";
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const incVersion = (version: string, bumpType: BumpType): string | null => {
|
|
82
|
+
// handle breaking of 0.x
|
|
83
|
+
if (bumpType === "major" && semver.major(version) === 0) {
|
|
84
|
+
bumpType = "minor";
|
|
85
|
+
}
|
|
86
|
+
return semver.inc(version, bumpType);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const incrementVersion = (
|
|
90
|
+
workspace: Workspace,
|
|
91
|
+
currentVersion: string,
|
|
92
|
+
bumpType: BumpType,
|
|
93
|
+
): string => {
|
|
94
|
+
const newVersion = incVersion(currentVersion, bumpType);
|
|
95
|
+
|
|
96
|
+
if (!newVersion) {
|
|
97
|
+
throw new UsageError(
|
|
98
|
+
`Could not determine next version for "${getWorkspaceName(
|
|
99
|
+
workspace,
|
|
100
|
+
)}" (currentVersion: ${currentVersion}, bumpType: ${bumpType}})`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return newVersion;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// const createPrerelease = (
|
|
108
|
+
// version: string,
|
|
109
|
+
// prereleaseType: ReleaseType,
|
|
110
|
+
// prereleaseId: string,
|
|
111
|
+
// ) => {
|
|
112
|
+
// return semver.inc(version, prereleaseType, prereleaseId);
|
|
113
|
+
// };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { Readable } from "node:stream";
|
|
2
|
+
import { text } from "node:stream/consumers";
|
|
3
|
+
import conventionalChangelogCore from "conventional-changelog-core";
|
|
4
|
+
import type { Commit } from "conventional-commits-parser";
|
|
5
|
+
import type { BumperRecommendation } from "conventional-recommended-bump";
|
|
6
|
+
import type { PackageJson } from "type-fest";
|
|
7
|
+
import type { ConventionalChangelogConfig } from "./conventionalCommitConfigUtils";
|
|
8
|
+
import type { Workspace } from "./packageUtils";
|
|
9
|
+
|
|
10
|
+
const versions: BumperRecommendation["releaseType"][] = [
|
|
11
|
+
"major",
|
|
12
|
+
"minor",
|
|
13
|
+
"patch",
|
|
14
|
+
];
|
|
15
|
+
export const recommendBump = async (
|
|
16
|
+
commits: Commit[],
|
|
17
|
+
config: ConventionalChangelogConfig,
|
|
18
|
+
): Promise<BumperRecommendation> => {
|
|
19
|
+
const whatBump = config.whatBump;
|
|
20
|
+
if (!whatBump) {
|
|
21
|
+
throw new Error("whatBump method is missing in config");
|
|
22
|
+
}
|
|
23
|
+
let result: BumperRecommendation = { ...(await whatBump(commits)) };
|
|
24
|
+
if (result.level != null) {
|
|
25
|
+
result.releaseType = versions[result.level];
|
|
26
|
+
} else if (result == null) {
|
|
27
|
+
result = {};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return result;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const generateChangelog = (
|
|
34
|
+
workspace: Workspace,
|
|
35
|
+
pkg: PackageJson,
|
|
36
|
+
config: ConventionalChangelogConfig,
|
|
37
|
+
newTag: string | null,
|
|
38
|
+
{
|
|
39
|
+
previousTag = "",
|
|
40
|
+
verbose = false,
|
|
41
|
+
tagPrefix = "v",
|
|
42
|
+
path = "",
|
|
43
|
+
lernaPackage,
|
|
44
|
+
}: {
|
|
45
|
+
previousTag?: string;
|
|
46
|
+
verbose?: boolean;
|
|
47
|
+
tagPrefix?: string;
|
|
48
|
+
path?: string;
|
|
49
|
+
lernaPackage?: string;
|
|
50
|
+
} = {},
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/max-params
|
|
52
|
+
): Promise<string> => {
|
|
53
|
+
if (!newTag) throw new Error("Missing new tag");
|
|
54
|
+
const stream: Readable = conventionalChangelogCore(
|
|
55
|
+
{
|
|
56
|
+
cwd: workspace.cwd,
|
|
57
|
+
config,
|
|
58
|
+
pkg,
|
|
59
|
+
path,
|
|
60
|
+
append: !!previousTag,
|
|
61
|
+
releaseCount: !previousTag ? 0 : 1,
|
|
62
|
+
skipUnstable: true,
|
|
63
|
+
lernaPackage,
|
|
64
|
+
tagPrefix,
|
|
65
|
+
verbose,
|
|
66
|
+
previousTag,
|
|
67
|
+
currentTag: newTag,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
version: pkg.version,
|
|
71
|
+
currentTag: newTag,
|
|
72
|
+
previousTag,
|
|
73
|
+
},
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
return text(stream);
|
|
77
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// eslint-disable-next-line import/no-unresolved
|
|
2
|
+
import { loadPreset } from "conventional-changelog-preset-loader";
|
|
3
|
+
import type { Preset } from "conventional-recommended-bump";
|
|
4
|
+
import { UsageError } from "./UsageError";
|
|
5
|
+
import type { Workspace } from "./packageUtils";
|
|
6
|
+
|
|
7
|
+
export type ConventionalChangelogConfig = Preset;
|
|
8
|
+
|
|
9
|
+
export const loadConventionalCommitConfig = async (
|
|
10
|
+
rootWorkspace: Workspace,
|
|
11
|
+
preset: string,
|
|
12
|
+
): Promise<ConventionalChangelogConfig> => {
|
|
13
|
+
try {
|
|
14
|
+
return loadPreset<ConventionalChangelogConfig>(preset);
|
|
15
|
+
} catch (error: any) {
|
|
16
|
+
throw new UsageError(
|
|
17
|
+
`Failed to require preset "${preset}": ${error.message as string}`,
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import childProcess from "node:child_process";
|
|
2
|
+
import type { Workspace } from "./packageUtils";
|
|
3
|
+
|
|
4
|
+
interface ExecResultIfFailed {
|
|
5
|
+
code: number | null;
|
|
6
|
+
signal: string | null;
|
|
7
|
+
stdout: string;
|
|
8
|
+
stderr: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface ExecResultIfSuccess {
|
|
12
|
+
code: null;
|
|
13
|
+
signal: null;
|
|
14
|
+
stdout: string;
|
|
15
|
+
stderr: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type ExecResult<Strict extends boolean> = Strict extends true
|
|
19
|
+
? ExecResultIfSuccess
|
|
20
|
+
: ExecResultIfFailed;
|
|
21
|
+
|
|
22
|
+
async function execvp<const Strict extends boolean>(
|
|
23
|
+
command: string,
|
|
24
|
+
args: string[],
|
|
25
|
+
{
|
|
26
|
+
cwd = process.cwd(),
|
|
27
|
+
env = process.env,
|
|
28
|
+
encoding,
|
|
29
|
+
strict,
|
|
30
|
+
}: {
|
|
31
|
+
cwd?: string;
|
|
32
|
+
env?: typeof process.env;
|
|
33
|
+
encoding?: BufferEncoding;
|
|
34
|
+
strict?: Strict;
|
|
35
|
+
},
|
|
36
|
+
): Promise<ExecResult<Strict>> {
|
|
37
|
+
const stdoutChunks: Uint8Array[] = [];
|
|
38
|
+
const stderrChunks: Uint8Array[] = [];
|
|
39
|
+
if (env.PWD !== undefined) {
|
|
40
|
+
env = { ...env, PWD: cwd };
|
|
41
|
+
}
|
|
42
|
+
const subprocess = childProcess.spawn(command, args, {
|
|
43
|
+
cwd,
|
|
44
|
+
env,
|
|
45
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
46
|
+
});
|
|
47
|
+
subprocess.stdout.on("data", (chunk) => {
|
|
48
|
+
stdoutChunks.push(chunk);
|
|
49
|
+
});
|
|
50
|
+
subprocess.stderr.on("data", (chunk) => {
|
|
51
|
+
stderrChunks.push(chunk);
|
|
52
|
+
});
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
subprocess.on("error", (err) => {
|
|
55
|
+
reject(new Error(`Process ${command} failed to spawn`));
|
|
56
|
+
});
|
|
57
|
+
subprocess.on("close", (code, signal) => {
|
|
58
|
+
const stdout = Buffer.concat(stdoutChunks).toString(encoding ?? "utf8");
|
|
59
|
+
const stderr = Buffer.concat(stderrChunks).toString(encoding ?? "utf8");
|
|
60
|
+
if (code === 0 || !strict) {
|
|
61
|
+
resolve({
|
|
62
|
+
code,
|
|
63
|
+
signal,
|
|
64
|
+
stdout,
|
|
65
|
+
stderr,
|
|
66
|
+
} as ExecResult<Strict>);
|
|
67
|
+
} else {
|
|
68
|
+
reject(
|
|
69
|
+
new Error(
|
|
70
|
+
`Process ${[command, ...args].join(" ")} exited ${code !== null ? `with code ${code}` : `with signal ${signal || ""}`}:\nstdout: ${stdout.toString()}\nstderr: ${stderr.toString()}`,
|
|
71
|
+
),
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const execCommand = (
|
|
79
|
+
workspace: Workspace,
|
|
80
|
+
commandAndArgs: string[] = [],
|
|
81
|
+
): ReturnType<typeof execvp> => {
|
|
82
|
+
const [command, ...args] = commandAndArgs;
|
|
83
|
+
return execvp(command, args, {
|
|
84
|
+
cwd: workspace.cwd,
|
|
85
|
+
strict: true,
|
|
86
|
+
});
|
|
87
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { UsageError } from "./UsageError";
|
|
2
|
+
import { execCommand } from "./execCommand";
|
|
3
|
+
import type { Workspace } from "./packageUtils";
|
|
4
|
+
|
|
5
|
+
export const getGitCurrentBranch = async (
|
|
6
|
+
workspace: Workspace,
|
|
7
|
+
): Promise<string> => {
|
|
8
|
+
const { stdout } = await execCommand(workspace, [
|
|
9
|
+
"git",
|
|
10
|
+
"rev-parse",
|
|
11
|
+
"--abbrev-ref",
|
|
12
|
+
"HEAD",
|
|
13
|
+
]);
|
|
14
|
+
const currentBranch = stdout.trim();
|
|
15
|
+
|
|
16
|
+
if (currentBranch === "HEAD") {
|
|
17
|
+
throw new UsageError("HEAD is detached. Please checkout a branch.");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return currentBranch;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const createGitCommit = async (
|
|
24
|
+
workspace: Workspace,
|
|
25
|
+
commitMessage: string,
|
|
26
|
+
): Promise<void> => {
|
|
27
|
+
await execCommand(workspace, ["git", "add", "-A"]);
|
|
28
|
+
|
|
29
|
+
await execCommand(workspace, [
|
|
30
|
+
"git",
|
|
31
|
+
"commit",
|
|
32
|
+
"--no-verify",
|
|
33
|
+
"-m",
|
|
34
|
+
commitMessage,
|
|
35
|
+
]);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const createGitTag = async (
|
|
39
|
+
workspace: Workspace,
|
|
40
|
+
newTag: string,
|
|
41
|
+
): Promise<void> => {
|
|
42
|
+
await execCommand(workspace, ["git", "tag", newTag, "-m", newTag]);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const pushCommitsAndTags = async (
|
|
46
|
+
workspace: Workspace,
|
|
47
|
+
gitRemote: string,
|
|
48
|
+
currentBranch: string,
|
|
49
|
+
): Promise<void> => {
|
|
50
|
+
await execCommand(workspace, [
|
|
51
|
+
"git",
|
|
52
|
+
"push",
|
|
53
|
+
"--follow-tags",
|
|
54
|
+
"--no-verify",
|
|
55
|
+
"--atomic",
|
|
56
|
+
gitRemote,
|
|
57
|
+
currentBranch,
|
|
58
|
+
]);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const isBehindRemote = async (
|
|
62
|
+
workspace: Workspace,
|
|
63
|
+
gitRemote: string,
|
|
64
|
+
currentBranch: string,
|
|
65
|
+
): Promise<boolean> => {
|
|
66
|
+
await execCommand(workspace, ["git", "remote", "update", gitRemote]);
|
|
67
|
+
const { stdout } = await execCommand(workspace, [
|
|
68
|
+
"git",
|
|
69
|
+
"rev-list",
|
|
70
|
+
"--left-right",
|
|
71
|
+
"--count",
|
|
72
|
+
`${gitRemote}/${currentBranch}..${currentBranch}`,
|
|
73
|
+
]);
|
|
74
|
+
|
|
75
|
+
const [behind] = stdout.split("\t").map((val) => parseInt(val, 10));
|
|
76
|
+
|
|
77
|
+
return behind > 0;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const getDirtyFiles = async (workspace: Workspace): Promise<string> => {
|
|
81
|
+
const { stdout: dirtyFiles } = await execCommand(workspace, [
|
|
82
|
+
"git",
|
|
83
|
+
"status",
|
|
84
|
+
"--porcelain",
|
|
85
|
+
]);
|
|
86
|
+
return dirtyFiles;
|
|
87
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Octokit } from "@octokit/rest";
|
|
2
|
+
import { UsageError } from "./UsageError";
|
|
3
|
+
import type { Workspace } from "./packageUtils";
|
|
4
|
+
|
|
5
|
+
export async function createGitHubClient(): Promise<Octokit> {
|
|
6
|
+
const { GH_TOKEN, GHE_API_URL, GHE_VERSION } = process.env;
|
|
7
|
+
|
|
8
|
+
if (!GH_TOKEN) {
|
|
9
|
+
throw new UsageError(
|
|
10
|
+
'"GH_TOKEN" environment variable required when "createRelease" is set to "github"',
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (GHE_VERSION) {
|
|
15
|
+
Octokit.plugin(
|
|
16
|
+
await import(`@octokit/plugin-enterprise-rest/ghe-${GHE_VERSION}`),
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const options = {
|
|
21
|
+
auth: `token ${GH_TOKEN}`,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
if (GHE_API_URL) {
|
|
25
|
+
// TODO: refactor based on TS feedback
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
27
|
+
// @ts-expect-error
|
|
28
|
+
options.baseUrl = GHE_API_URL;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return new Octokit(options);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface ParsedGithubUrl {
|
|
35
|
+
username: string;
|
|
36
|
+
reponame: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const githubRegex = /^https?:\/\/github.com\/([^#/]+)\/([^#/]+?)(?:\.git)?$/;
|
|
40
|
+
export const parseGithubRepoUrl = (workspace: Workspace): ParsedGithubUrl => {
|
|
41
|
+
const repository = workspace.pkg.repository;
|
|
42
|
+
|
|
43
|
+
const url: string | undefined =
|
|
44
|
+
typeof repository === "string" ? repository : repository?.url;
|
|
45
|
+
|
|
46
|
+
if (!url) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
"No repository URL found in manifest. Please add one and try again. https://docs.npmjs.com/cli/v9/configuring-npm/package-json#repository",
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
const match = githubRegex.exec(url);
|
|
52
|
+
|
|
53
|
+
if (!match) {
|
|
54
|
+
throw new Error(`Invalid GitHub repository URL: "${url}"`);
|
|
55
|
+
}
|
|
56
|
+
const [, username, reponame] = match;
|
|
57
|
+
return { username, reponame };
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const createGitRelease = async (
|
|
61
|
+
githubClient: Octokit,
|
|
62
|
+
parsedRepoUrl: ParsedGithubUrl,
|
|
63
|
+
tag: string,
|
|
64
|
+
body: string,
|
|
65
|
+
prerelease: boolean,
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/max-params
|
|
67
|
+
): Promise<void> => {
|
|
68
|
+
await githubClient.repos.createRelease({
|
|
69
|
+
owner: parsedRepoUrl.username,
|
|
70
|
+
repo: parsedRepoUrl.reponame,
|
|
71
|
+
// eslint-disable-next-line camelcase
|
|
72
|
+
tag_name: tag,
|
|
73
|
+
name: tag,
|
|
74
|
+
body,
|
|
75
|
+
draft: false,
|
|
76
|
+
prerelease,
|
|
77
|
+
});
|
|
78
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PackageDependencyDescriptorUtils } from "./packageDependenciesUtils";
|
|
2
|
+
|
|
3
|
+
describe("PackageDependencyDescriptorUtils.parse", () => {
|
|
4
|
+
it.each([["name@1.1.1", { name: "name", version: "1.1.1" }]] as const)(
|
|
5
|
+
'should parse "%s" to "%s"',
|
|
6
|
+
(value, expected) => {
|
|
7
|
+
const [k, v] = value.split("@", 2);
|
|
8
|
+
expect(PackageDependencyDescriptorUtils.parse(v, k)).toBe(expected);
|
|
9
|
+
},
|
|
10
|
+
);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// describe(stringifyPackageDescriptor.name, () => {});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Combination of an optional scope and name.
|
|
3
|
+
*
|
|
4
|
+
* eg `@npm/types`
|
|
5
|
+
*/
|
|
6
|
+
export interface PackageDescriptorName {
|
|
7
|
+
scope?: string;
|
|
8
|
+
name: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface DescriptorUtils<Descriptor> {
|
|
12
|
+
parse: (value: string) => Descriptor;
|
|
13
|
+
stringify: (descriptor: Descriptor) => string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const PackageDescriptorNameUtils: DescriptorUtils<PackageDescriptorName> =
|
|
17
|
+
{
|
|
18
|
+
parse: (value) => {
|
|
19
|
+
if (value.startsWith("@")) {
|
|
20
|
+
const [scope, name] = value.split("/", 2);
|
|
21
|
+
return { scope, name };
|
|
22
|
+
}
|
|
23
|
+
return { name: value };
|
|
24
|
+
},
|
|
25
|
+
stringify: (descriptor) => {
|
|
26
|
+
return descriptor.scope === undefined
|
|
27
|
+
? descriptor.name
|
|
28
|
+
: `@${descriptor.scope}/${descriptor.name}`;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export interface PackageDependencyDescriptor {
|
|
33
|
+
key: string;
|
|
34
|
+
name: PackageDescriptorName;
|
|
35
|
+
selector: string; // can be npm tag or version or version range or git url or local folder path
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface PackageDependencyDescriptorUtils<
|
|
39
|
+
Descriptor = PackageDependencyDescriptor,
|
|
40
|
+
> {
|
|
41
|
+
make: (descriptor: Descriptor, selector: string) => Descriptor;
|
|
42
|
+
parse: (dependencyKey: string, dependencyValue: string) => Descriptor;
|
|
43
|
+
stringify: (descriptor: Descriptor) => [key: string, value: string];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const PackageDependencyDescriptorUtils: PackageDependencyDescriptorUtils =
|
|
47
|
+
{
|
|
48
|
+
make: (descriptor, selector) => {
|
|
49
|
+
return { key: descriptor.key, name: descriptor.name, selector };
|
|
50
|
+
},
|
|
51
|
+
parse: (dependencyKey, dependencyValue) => {
|
|
52
|
+
const [name, selector] = dependencyValue.startsWith("npm:")
|
|
53
|
+
? dependencyValue.slice("npm:".length).split("@", 2)
|
|
54
|
+
: [dependencyKey, dependencyValue];
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
key: dependencyKey,
|
|
58
|
+
name: PackageDescriptorNameUtils.parse(name),
|
|
59
|
+
selector,
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
stringify: (descriptor) => {
|
|
63
|
+
return [descriptor.key, descriptor.selector];
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// export const PackageDependencySelectorUtils;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import mapWorkspaces from "@npmcli/map-workspaces";
|
|
5
|
+
import prettyPkg from "@pob/pretty-pkg";
|
|
6
|
+
import type { PackageJson } from "type-fest";
|
|
7
|
+
|
|
8
|
+
export interface Workspace {
|
|
9
|
+
readonly cwd: string;
|
|
10
|
+
readonly relativeCwd?: string;
|
|
11
|
+
readonly pkg: PackageJson;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ProjectWorkspace {
|
|
15
|
+
readonly root: Workspace;
|
|
16
|
+
readonly children: Map<string, Workspace>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const createProjectWorkspace = async (
|
|
20
|
+
root: Workspace,
|
|
21
|
+
): Promise<ProjectWorkspace> => {
|
|
22
|
+
const map: Map<string, string> = root.pkg.workspaces
|
|
23
|
+
? await mapWorkspaces({ cwd: root.cwd, pkg: root.pkg })
|
|
24
|
+
: new Map();
|
|
25
|
+
|
|
26
|
+
const children = new Map<string, Workspace>(
|
|
27
|
+
await Promise.all(
|
|
28
|
+
[...map.entries()].map(
|
|
29
|
+
async ([packageName, packagePath]) =>
|
|
30
|
+
[
|
|
31
|
+
packageName,
|
|
32
|
+
{
|
|
33
|
+
...(await createWorkspace(packagePath)),
|
|
34
|
+
relativeCwd: path.relative(root.cwd, packagePath),
|
|
35
|
+
} satisfies Workspace,
|
|
36
|
+
] as const,
|
|
37
|
+
),
|
|
38
|
+
),
|
|
39
|
+
);
|
|
40
|
+
return { root, children };
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const isAccessible = (path: string): Promise<boolean> =>
|
|
44
|
+
fs.access(path).then(
|
|
45
|
+
() => true,
|
|
46
|
+
() => false,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
export const findRootWorkspace = async (
|
|
50
|
+
cwd: string,
|
|
51
|
+
): Promise<Workspace | null> => {
|
|
52
|
+
let currentPath = cwd;
|
|
53
|
+
do {
|
|
54
|
+
const isRootIfOneOfThesePathsExists = await Promise.all([
|
|
55
|
+
isAccessible(path.join(currentPath, ".yarnrc.yml")),
|
|
56
|
+
isAccessible(path.join(currentPath, "yarn.lock")),
|
|
57
|
+
]);
|
|
58
|
+
if (isRootIfOneOfThesePathsExists.some(Boolean)) {
|
|
59
|
+
return createWorkspace(currentPath);
|
|
60
|
+
}
|
|
61
|
+
currentPath = path.dirname(currentPath);
|
|
62
|
+
} while (currentPath && currentPath !== "/");
|
|
63
|
+
return null;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const getPackageJsonPath = (cwd: string): string =>
|
|
67
|
+
path.join(cwd, "package.json");
|
|
68
|
+
|
|
69
|
+
export const createWorkspace = async (path: string): Promise<Workspace> => {
|
|
70
|
+
const pkg = await readPkg(path);
|
|
71
|
+
return { cwd: path, pkg };
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export async function writePkg(
|
|
75
|
+
workspace: Workspace,
|
|
76
|
+
prettierOptions = undefined,
|
|
77
|
+
): Promise<void> {
|
|
78
|
+
const string = await prettyPkg(workspace.pkg, prettierOptions);
|
|
79
|
+
await fs.writeFile(getPackageJsonPath(workspace.cwd), string, "utf8");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function readPkg(cwd: string): Promise<PackageJson> {
|
|
83
|
+
const packagePath = getPackageJsonPath(cwd);
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/use-unknown-in-catch-callback-variable
|
|
85
|
+
const pkg = await fs.readFile(packagePath, "utf8").catch((error: Error) => {
|
|
86
|
+
throw new Error(
|
|
87
|
+
`Failed to read package.json in "${cwd}": ${error instanceof Error ? error.message : String(error)}`,
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
try {
|
|
91
|
+
return JSON.parse(pkg);
|
|
92
|
+
} catch (error: unknown) {
|
|
93
|
+
throw new Error(
|
|
94
|
+
`Failed to parse package.json in "${cwd}": ${error instanceof Error ? error.message : String(error)}`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|