@mxpicture/build-api 0.2.15 โ†’ 0.2.17

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.
@@ -1,8 +1,8 @@
1
1
  import { basename, dirname, join, relative } from "node:path";
2
2
  import { readdir, rm, writeFile } from "node:fs/promises";
3
- import { Pkg } from "../pkg/Pkg.js";
4
3
  import { logInfo, logSuccess } from "../logger/Logger.js";
5
4
  import { WorkspacePaths } from "../workspace/WorkspacePaths.js";
5
+ import { readPackageJsonThrow, writePackageJson, } from "../workspace/workspace.pkg.js";
6
6
  // /** File patterns to exclude from barrel exports */
7
7
  export const DEFAULT_EXCLUDED_PATTERNS = [
8
8
  /\.test\.ts$/,
@@ -94,16 +94,15 @@ export class Barrel {
94
94
  }
95
95
  async updatePackageExports(packageDir, packageJsonPath, exports) {
96
96
  try {
97
- const pkgJson = new Pkg(packageJsonPath);
98
97
  const pkgName = relative(this.paths.workspacesDir, packageDir);
99
98
  logInfo(`๐Ÿ“ฆ updating ${pkgName} package.json ...`);
100
- const pkg = await pkgJson.readJson();
99
+ const pkg = await readPackageJsonThrow(packageJsonPath);
101
100
  if (!this.hasExportsChanged(exports, pkg.exports)) {
102
101
  logInfo(`๐Ÿงน No changes in ${pkgName}, nothing to do`);
103
102
  return;
104
103
  }
105
104
  pkg.exports = { ...exports };
106
- await pkgJson.writeJson(pkg);
105
+ await writePackageJson(packageJsonPath, pkg);
107
106
  logSuccess(`โœ… ${pkgName} update successful`);
108
107
  }
109
108
  catch (error) {
@@ -1,6 +1,6 @@
1
1
  import { logInfo, logSuccess } from "../logger/Logger.js";
2
2
  import { DepsReplacementMode, } from "../types/types.deps.js";
3
- import { buildMapEntries, readPackageJsons, writePackageJsons, } from "../workspace/workspace.common.js";
3
+ import { buildMapEntries, readPackageEntries, writePackageEntries, } from "../workspace/workspace.pkg.js";
4
4
  export const runFixDeps = async (p) => instanceFixDeps(p).run();
5
5
  export const instanceFixDeps = (p) => p.replacement === DepsReplacementMode.version
6
6
  ? new FixWorkspaceDepsVersion(p.repoRoot, p.mode)
@@ -17,7 +17,7 @@ export class IFixWorkspaceDeps {
17
17
  logInfo("๐Ÿ”ง Fixing workspace dependencies...\n");
18
18
  else
19
19
  logInfo("๐Ÿ”ง Restoring workspace dependencies...\n");
20
- const pkgs = await readPackageJsons(this.repoRoot);
20
+ const pkgs = await readPackageEntries(this.repoRoot);
21
21
  const mapEntries = buildMapEntries(pkgs);
22
22
  const versionMap = new Map();
23
23
  const workspacePackages = new Set();
@@ -44,7 +44,7 @@ export class IFixWorkspaceDeps {
44
44
  throw err;
45
45
  }
46
46
  }
47
- await writePackageJsons(pkgs);
47
+ await writePackageEntries(pkgs);
48
48
  if (this.mode === "fix")
49
49
  logSuccess("โœ… Done fixing workspace dependencies!");
50
50
  else
@@ -1,5 +1,5 @@
1
1
  import { SourceFile } from "../types/types.code.js";
2
2
  export declare const formatCode: (...lines: string[]) => Promise<string>;
3
3
  export declare const formatJson: (...lines: string[]) => Promise<string>;
4
- export declare const formatJson2Spaces: (...lines: string[]) => Promise<string>;
4
+ export declare const formatJsonStringify: (...lines: string[]) => Promise<string>;
5
5
  export declare const createSourceFiles: (filePaths: string[]) => SourceFile[];
@@ -2,7 +2,7 @@ import ts from "typescript";
2
2
  import { format } from "prettier";
3
3
  export const formatCode = async (...lines) => format(lines.join("\n"), { parser: "typescript" });
4
4
  export const formatJson = async (...lines) => format(lines.join("\n"), { parser: "json" });
5
- export const formatJson2Spaces = async (...lines) => format(lines.join("\n"), { parser: "json", tabWidth: 2, useTabs: false });
5
+ export const formatJsonStringify = async (...lines) => format(lines.join("\n"), { parser: "json-stringify" });
6
6
  export const createSourceFiles = (filePaths) => {
7
7
  const program = ts.createProgram(filePaths, {
8
8
  target: ts.ScriptTarget.ES2023,
@@ -1,8 +1,8 @@
1
1
  import { readdir } from "node:fs/promises";
2
2
  import { join } from "node:path";
3
- import { readPackageJson } from "../workspace/workspace.common.js";
4
3
  import { logError, logInfo, logSuccess } from "../logger/Logger.js";
5
4
  import { execAsync } from "../common/common.fs.js";
5
+ import { readPackageJson } from "../workspace/workspace.pkg.js";
6
6
  export const runNpmPublisher = async (params) => new NpmPublisher(params.packagesDir).run();
7
7
  export class NpmPublisher {
8
8
  packagesDir;
@@ -0,0 +1,13 @@
1
+ import { BumpParams, PackageEntry, SyncPkgVersionParams } from "../types/types.package.js";
2
+ import { WorkspacePaths } from "../workspace/WorkspacePaths.js";
3
+ export declare const runSyncPkgVersion: (params: SyncPkgVersionParams) => Promise<void>;
4
+ export declare class SyncPkgVersion {
5
+ protected readonly paths: WorkspacePaths;
6
+ protected readonly bump?: BumpParams | undefined;
7
+ constructor(paths: WorkspacePaths, bump?: BumpParams | undefined);
8
+ run(): Promise<void>;
9
+ protected read(): Promise<PackageEntry[]>;
10
+ protected readRoot(): Promise<PackageEntry>;
11
+ protected write(entries: PackageEntry[]): Promise<void>;
12
+ protected writeRoot(entry: PackageEntry): Promise<void>;
13
+ }
@@ -0,0 +1,45 @@
1
+ import { join } from "node:path";
2
+ import { readPackageEntries, readPackageEntryThrow, writePackageEntries, writePackageEntry, } from "../workspace/workspace.pkg.js";
3
+ import { WorkspacePaths } from "../workspace/WorkspacePaths.js";
4
+ import { bumpVersion, compareVersionsGT, concatVersion, splitVersion, } from "./pkg.common.js";
5
+ export const runSyncPkgVersion = async (params) => new SyncPkgVersion(WorkspacePaths.instance(params.repoRoot, params.workspacesName), params.bump).run();
6
+ export class SyncPkgVersion {
7
+ paths;
8
+ bump;
9
+ constructor(paths, bump) {
10
+ this.paths = paths;
11
+ this.bump = bump;
12
+ }
13
+ async run() {
14
+ const [entries, root] = await Promise.all([this.read(), this.readRoot()]);
15
+ let maxVersion = splitVersion(root.content.version);
16
+ for (const entry of entries) {
17
+ const version = splitVersion(entry.content.version);
18
+ if (compareVersionsGT(version, maxVersion))
19
+ maxVersion = version;
20
+ }
21
+ // bump
22
+ maxVersion = bumpVersion(maxVersion, this.bump ?? { patch: true });
23
+ const newVersion = concatVersion(maxVersion);
24
+ // set/update
25
+ root.content.version = newVersion;
26
+ root.modified = true;
27
+ for (const entry of entries) {
28
+ entry.content.version = newVersion;
29
+ entry.modified = true;
30
+ }
31
+ await Promise.all([this.write(entries), this.writeRoot(root)]);
32
+ }
33
+ async read() {
34
+ return await readPackageEntries(this.paths.repoRoot);
35
+ }
36
+ async readRoot() {
37
+ return readPackageEntryThrow(join(this.paths.repoRoot, "package.json"));
38
+ }
39
+ async write(entries) {
40
+ await writePackageEntries(entries);
41
+ }
42
+ async writeRoot(entry) {
43
+ await writePackageEntry(entry);
44
+ }
45
+ }
@@ -1,2 +1,2 @@
1
- export * from "./Pkg.js";
1
+ export * from "./SyncPkgVersion.js";
2
2
  export * from "./pkg.common.js";
package/dist/pkg/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  // This file is auto-generated. Do not edit manually.
2
- export * from "./Pkg.js";
2
+ export * from "./SyncPkgVersion.js";
3
3
  export * from "./pkg.common.js";
@@ -1,5 +1,6 @@
1
- import { PackageVersion } from "../types/types.package.js";
1
+ import { BumpParams, PackageVersion } from "../types/types.package.js";
2
2
  export declare const splitVersion: (version: string) => PackageVersion;
3
3
  export declare const concatVersion: (version: PackageVersion) => string;
4
4
  export declare const compareVersions: (a: PackageVersion | string, b: PackageVersion | string) => number;
5
5
  export declare const compareVersionsGT: (a: PackageVersion | string, b: PackageVersion | string) => boolean;
6
+ export declare const bumpVersion: (version: PackageVersion, bump: BumpParams) => PackageVersion;
@@ -39,3 +39,19 @@ export const compareVersions = (a, b) => {
39
39
  return 0;
40
40
  };
41
41
  export const compareVersionsGT = (a, b) => compareVersions(a, b) > 0;
42
+ export const bumpVersion = (version, bump) => {
43
+ const result = { ...version };
44
+ if (bump.major) {
45
+ result.major = result.major ? result.major + 1 : 1;
46
+ result.minor = 0;
47
+ result.patch = 0;
48
+ }
49
+ else if (bump.minor) {
50
+ result.minor = result.minor ? result.minor + 1 : 1;
51
+ result.patch = 0;
52
+ }
53
+ else if (bump.patch) {
54
+ result.patch = result.patch ? result.patch + 1 : 1;
55
+ }
56
+ return result;
57
+ };
@@ -1,3 +1,4 @@
1
+ import { RunRepoRootWsParams } from "./types.run.js";
1
2
  export interface PackageVersion {
2
3
  major: number;
3
4
  minor: number | null;
@@ -16,7 +17,7 @@ export interface PackageJson {
16
17
  }
17
18
  export interface PackageEntry {
18
19
  jsonPath: string;
19
- repoPath: string;
20
+ repoPath: string | null;
20
21
  wsName: string;
21
22
  content: PackageJson;
22
23
  modified: boolean;
@@ -26,3 +27,11 @@ export interface MapEntry {
26
27
  toPkg: PackageEntry;
27
28
  relPath: string;
28
29
  }
30
+ export interface BumpParams {
31
+ major?: boolean;
32
+ minor?: boolean;
33
+ patch?: boolean;
34
+ }
35
+ export interface SyncPkgVersionParams extends RunRepoRootWsParams {
36
+ bump?: BumpParams;
37
+ }
@@ -1,2 +1,3 @@
1
1
  export * from "./WorkspacePaths.js";
2
2
  export * from "./workspace.common.js";
3
+ export * from "./workspace.pkg.js";
@@ -1,3 +1,4 @@
1
1
  // This file is auto-generated. Do not edit manually.
2
2
  export * from "./WorkspacePaths.js";
3
3
  export * from "./workspace.common.js";
4
+ export * from "./workspace.pkg.js";
@@ -1,14 +1,5 @@
1
1
  import { PnpmWorkspace, WorkspaceFile, WorkspaceFileType } from "../types/types.workspace.js";
2
- import { PackageJson, PackageEntry, MapEntry } from "../types/types.package.js";
3
2
  export declare const DEFAULT_WORKSPACE_FILE_ENDING = ".code-workspace";
4
3
  export declare const getFileType: (filename: string) => WorkspaceFileType;
5
4
  export declare const findWorkspaceRoot: (startPath: string) => Promise<WorkspaceFile>;
6
5
  export declare const readWorkspaceYaml: (repoRoot: string) => Promise<PnpmWorkspace>;
7
- export declare const readPackageJson: (dirOrFilepath: string) => Promise<PackageJson | null>;
8
- export declare const readPackageJsonThrow: (dirOrFilepath: string) => Promise<PackageJson>;
9
- export declare const readPackageJsonSync: (dirOrFilepath: string) => PackageJson;
10
- export declare const readPackageJsons: (repoRoot: string) => Promise<PackageEntry[]>;
11
- export declare const writePackageJsons: (entries: PackageEntry[]) => Promise<void[]>;
12
- export declare const buildMapEntries: (pkgEntries: PackageEntry[]) => MapEntry[];
13
- export declare const readPackageVersions: (rootDir: string) => Promise<Record<string, string>>;
14
- export declare const readMinPackageVersion: (rootDir: string) => Promise<string | null>;
@@ -1,11 +1,7 @@
1
- import { readdir, readFile, writeFile } from "node:fs/promises";
1
+ import { readdir, readFile } from "node:fs/promises";
2
2
  import { WorkspaceFileType, } from "../types/types.workspace.js";
3
- import { join, relative, resolve } from "node:path";
4
- import { splitVersion, compareVersions, concatVersion, } from "../pkg/pkg.common.js";
5
- import json5 from "json5";
3
+ import { join, resolve } from "node:path";
6
4
  import { parse } from "yaml";
7
- import { logInfo, logSuccess } from "../logger/Logger.js";
8
- import { readFileSync } from "node:fs";
9
5
  export const DEFAULT_WORKSPACE_FILE_ENDING = ".code-workspace";
10
6
  export const getFileType = (filename) => filename.endsWith(DEFAULT_WORKSPACE_FILE_ENDING)
11
7
  ? WorkspaceFileType.workspace
@@ -82,81 +78,3 @@ export const readWorkspaceYaml = async (repoRoot) => {
82
78
  packages.push(...packageColletion);
83
79
  return { packages };
84
80
  };
85
- export const readPackageJson = async (dirOrFilepath) => {
86
- try {
87
- return readPackageJsonThrow(dirOrFilepath);
88
- }
89
- catch {
90
- return null;
91
- }
92
- };
93
- export const readPackageJsonThrow = async (dirOrFilepath) => {
94
- const pkgPath = dirOrFilepath.endsWith("/package.json")
95
- ? dirOrFilepath
96
- : join(dirOrFilepath, "package.json");
97
- return json5.parse(await readFile(pkgPath, "utf8"));
98
- };
99
- export const readPackageJsonSync = (dirOrFilepath) => {
100
- const pkgPath = dirOrFilepath.endsWith("/package.json")
101
- ? dirOrFilepath
102
- : join(dirOrFilepath, "package.json");
103
- return json5.parse(readFileSync(pkgPath, "utf8"));
104
- };
105
- export const readPackageJsons = async (repoRoot) => {
106
- try {
107
- const pnpmWS = await readWorkspaceYaml(repoRoot);
108
- return Promise.all(pnpmWS.packages.map(async (repoPath) => {
109
- const jsonPath = join(repoRoot, repoPath, "package.json");
110
- const content = await readPackageJsonThrow(jsonPath);
111
- return {
112
- jsonPath,
113
- repoPath,
114
- content,
115
- modified: false,
116
- wsName: repoPath.startsWith("packages/")
117
- ? repoPath.substring(9)
118
- : repoPath,
119
- };
120
- }));
121
- }
122
- catch {
123
- return [];
124
- }
125
- };
126
- export const writePackageJsons = async (entries) => Promise.all(entries.map(async (entry) => {
127
- if (entry.modified) {
128
- await writeFile(entry.jsonPath, JSON.stringify(entry.content, null, 2) + "\n", "utf8");
129
- logSuccess(` โœ… Updated ${entry.content.name}\n`);
130
- }
131
- else {
132
- logInfo(` โญ๏ธ No workspace dependencies to fix. ${entry.content.name}\n`);
133
- }
134
- }));
135
- // Build a map of package names to their versions
136
- export const buildMapEntries = (pkgEntries) => {
137
- const versionEntries = [];
138
- for (const fromPkg of pkgEntries)
139
- for (const toPkg of pkgEntries)
140
- if (fromPkg.content.name !== toPkg.content.name)
141
- versionEntries.push({
142
- fromPkg,
143
- toPkg,
144
- relPath: relative(fromPkg.repoPath, toPkg.repoPath),
145
- });
146
- return versionEntries;
147
- };
148
- export const readPackageVersions = async (rootDir) => {
149
- const versions = {};
150
- const packageJsons = await readPackageJsons(rootDir);
151
- for (const packageJson of packageJsons)
152
- versions[packageJson.repoPath] = packageJson.content.version;
153
- return versions;
154
- };
155
- export const readMinPackageVersion = async (rootDir) => {
156
- const versions = await readPackageVersions(rootDir);
157
- const splitVersions = Object.values(versions).map(splitVersion);
158
- if (splitVersions.length === 0)
159
- return null;
160
- splitVersions.sort(compareVersions);
161
- return concatVersion(splitVersions[0]);
162
- };
@@ -0,0 +1,15 @@
1
+ import { PackageJson, PackageEntry, MapEntry } from "../types/types.package.js";
2
+ export declare const readPackageJson: (dirOrFilepath: string) => Promise<PackageJson | null>;
3
+ export declare const readPackageEntry: (dirOrFilepath: string) => Promise<PackageEntry | null>;
4
+ export declare const readPackageJsonThrow: (dirOrFilepath: string) => Promise<PackageJson>;
5
+ export declare const readPackageEntryThrow: (dirOrFilepath: string) => Promise<PackageEntry>;
6
+ export declare const readPackageJsonSync: (dirOrFilepath: string) => PackageJson;
7
+ export declare const readPackageEntrySync: (dirOrFilepath: string) => PackageEntry;
8
+ export declare const readPackageJsons: (repoRoot: string) => Promise<PackageJson[]>;
9
+ export declare const readPackageEntries: (repoRoot: string) => Promise<PackageEntry[]>;
10
+ export declare const writePackageEntries: (entries: PackageEntry[]) => Promise<void[]>;
11
+ export declare const writePackageJson: (jsonPath: string, content: PackageJson) => Promise<void>;
12
+ export declare const writePackageEntry: (entry: PackageEntry) => Promise<void>;
13
+ export declare const buildMapEntries: (pkgEntries: PackageEntry[]) => MapEntry[];
14
+ export declare const readPackageVersions: (rootDir: string) => Promise<Record<string, string>>;
15
+ export declare const readMinPackageVersion: (rootDir: string) => Promise<string | null>;
@@ -0,0 +1,123 @@
1
+ import { readFile, writeFile } from "node:fs/promises";
2
+ import { join, relative, sep } from "node:path";
3
+ import { splitVersion, compareVersions, concatVersion, } from "../pkg/pkg.common.js";
4
+ import json5 from "json5";
5
+ import { logInfo, logSuccess } from "../logger/Logger.js";
6
+ import { readFileSync } from "node:fs";
7
+ import { readWorkspaceYaml } from "./workspace.common.js";
8
+ import { formatJsonStringify } from "../format/format.code.js";
9
+ const ensurePkgJsonPath = (dirOrFilepath) => dirOrFilepath.endsWith("/package.json")
10
+ ? dirOrFilepath
11
+ : join(dirOrFilepath, "package.json");
12
+ export const readPackageJson = async (dirOrFilepath) => {
13
+ try {
14
+ return readPackageJsonThrow(dirOrFilepath);
15
+ }
16
+ catch {
17
+ return null;
18
+ }
19
+ };
20
+ export const readPackageEntry = async (dirOrFilepath) => {
21
+ try {
22
+ return readPackageEntryThrow(dirOrFilepath);
23
+ }
24
+ catch {
25
+ return null;
26
+ }
27
+ };
28
+ export const readPackageJsonThrow = async (dirOrFilepath) => json5.parse(await readFile(ensurePkgJsonPath(dirOrFilepath), "utf8"));
29
+ export const readPackageEntryThrow = async (dirOrFilepath) => {
30
+ const pkgPath = ensurePkgJsonPath(dirOrFilepath);
31
+ const parts = pkgPath.split(sep);
32
+ parts.pop();
33
+ const wsName = parts.pop();
34
+ if (!wsName)
35
+ throw new Error(`Path ${pkgPath} does not conatin any workspace`);
36
+ return {
37
+ jsonPath: pkgPath,
38
+ repoPath: null,
39
+ content: await readPackageJsonThrow(pkgPath),
40
+ modified: false,
41
+ wsName,
42
+ };
43
+ };
44
+ export const readPackageJsonSync = (dirOrFilepath) => json5.parse(readFileSync(ensurePkgJsonPath(dirOrFilepath), "utf8"));
45
+ export const readPackageEntrySync = (dirOrFilepath) => {
46
+ const pkgPath = ensurePkgJsonPath(dirOrFilepath);
47
+ const parts = pkgPath.split(sep);
48
+ parts.pop();
49
+ const wsName = parts.pop();
50
+ if (!wsName)
51
+ throw new Error(`Path ${pkgPath} does not conatin any workspace`);
52
+ return {
53
+ jsonPath: pkgPath,
54
+ repoPath: null,
55
+ content: readPackageJsonSync(pkgPath),
56
+ modified: false,
57
+ wsName,
58
+ };
59
+ };
60
+ export const readPackageJsons = async (repoRoot) => (await readPackageEntries(repoRoot)).map((entry) => entry.content);
61
+ export const readPackageEntries = async (repoRoot) => {
62
+ try {
63
+ const pnpmWS = await readWorkspaceYaml(repoRoot);
64
+ return Promise.all(pnpmWS.packages.map(async (repoPath) => {
65
+ const jsonPath = join(repoRoot, repoPath, "package.json");
66
+ const content = await readPackageJsonThrow(jsonPath);
67
+ return {
68
+ jsonPath,
69
+ repoPath,
70
+ content,
71
+ modified: false,
72
+ wsName: repoPath.startsWith("packages/")
73
+ ? repoPath.substring(9)
74
+ : repoPath,
75
+ };
76
+ }));
77
+ }
78
+ catch {
79
+ return [];
80
+ }
81
+ };
82
+ export const writePackageEntries = async (entries) => Promise.all(entries.map(writePackageEntry));
83
+ export const writePackageJson = async (jsonPath, content) => writeFile(jsonPath, await formatJsonStringify(json5.stringify(content)));
84
+ export const writePackageEntry = async (entry) => {
85
+ if (entry.modified) {
86
+ await writePackageJson(entry.jsonPath, entry.content);
87
+ logSuccess(` โœ… package.json updated ${entry.content.name}\n`);
88
+ }
89
+ else {
90
+ logInfo(` โญ๏ธ No changes in package.json. ${entry.content.name}\n`);
91
+ }
92
+ };
93
+ // Build a map of package names to their versions
94
+ export const buildMapEntries = (pkgEntries) => {
95
+ const versionEntries = [];
96
+ for (const fromPkg of pkgEntries)
97
+ for (const toPkg of pkgEntries)
98
+ if (fromPkg.content.name !== toPkg.content.name)
99
+ versionEntries.push({
100
+ fromPkg,
101
+ toPkg,
102
+ relPath: relative(fromPkg.jsonPath, toPkg.jsonPath),
103
+ });
104
+ return versionEntries;
105
+ };
106
+ export const readPackageVersions = async (rootDir) => {
107
+ const versions = {};
108
+ const packageEntries = await readPackageEntries(rootDir);
109
+ for (const packageEntry of packageEntries) {
110
+ if (!packageEntry.repoPath)
111
+ continue;
112
+ versions[packageEntry.repoPath] = packageEntry.content.version;
113
+ }
114
+ return versions;
115
+ };
116
+ export const readMinPackageVersion = async (rootDir) => {
117
+ const versions = await readPackageVersions(rootDir);
118
+ const splitVersions = Object.values(versions).map(splitVersion);
119
+ if (splitVersions.length === 0)
120
+ return null;
121
+ splitVersions.sort(compareVersions);
122
+ return concatVersion(splitVersions[0]);
123
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mxpicture/build-api",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
4
4
  "description": "Build utilities API",
5
5
  "type": "module",
6
6
  "author": "MXPicture",
@@ -11,7 +11,6 @@
11
11
  },
12
12
  "exports": {
13
13
  "./barrel": "./dist/barrel/index.js",
14
- "./changeset": "./dist/changeset/index.js",
15
14
  "./cleanup": "./dist/cleanup/index.js",
16
15
  "./common": "./dist/common/index.js",
17
16
  "./deps": "./dist/deps/index.js",
@@ -1,14 +0,0 @@
1
- import { WorkspacePaths } from "../workspace/WorkspacePaths.js";
2
- import { ChangesetParams } from "../types/types.changeset.js";
3
- import { PackageJson } from "../types/types.package.js";
4
- export declare const runChangeset: (params: ChangesetParams) => Promise<void>;
5
- export declare class Changeset {
6
- protected readonly paths: WorkspacePaths;
7
- constructor(paths: WorkspacePaths);
8
- run(): Promise<void>;
9
- protected readPackages(): Promise<PackageJson[]>;
10
- protected range4Compare(): Promise<string>;
11
- protected findChangedPackages(range: string, packages: PackageJson[]): Promise<string[]>;
12
- protected findChangedInPackage(range: string, pkg: PackageJson): Promise<string[]>;
13
- protected write(changed: string[]): Promise<void>;
14
- }
@@ -1,84 +0,0 @@
1
- import { join } from "node:path";
2
- import { readPackageJsons } from "../workspace/workspace.common.js";
3
- import { WorkspacePaths } from "../workspace/WorkspacePaths.js";
4
- import { execAsync } from "../common/common.fs.js";
5
- import { mkdir, writeFile } from "node:fs/promises";
6
- export const runChangeset = async (params) => new Changeset(WorkspacePaths.instance(params.repoRoot, params.workspacesName)).run();
7
- export class Changeset {
8
- paths;
9
- constructor(paths) {
10
- this.paths = paths;
11
- }
12
- async run() {
13
- const [range, packages] = await Promise.all([
14
- this.range4Compare(),
15
- this.readPackages(),
16
- ]);
17
- const changed = await this.findChangedPackages(range, packages);
18
- // 4) If nothing changed, exit quietly
19
- if (changed.length === 0) {
20
- console.log("No publishable package changes detected.");
21
- return;
22
- }
23
- return this.write(changed);
24
- }
25
- // 1) Discover workspace packages (assuming packages/*)
26
- async readPackages() {
27
- return (await readPackageJsons(this.paths.repoRoot)).map((p) => p.content);
28
- }
29
- // 2) Determine range to compare: last Changesets tag if available, else initial commit
30
- async range4Compare() {
31
- let lastTag = "";
32
- try {
33
- // Tags like @your-scope/pkg-a@0.1.1 or pkg-a@0.1.1 are created by Changesets on publish
34
- lastTag = (await execAsync("git tag --list --sort=-creatordate | head -n 1")).stdout
35
- .toString()
36
- .trim();
37
- }
38
- catch { }
39
- return lastTag ? `${lastTag}..HEAD` : "";
40
- }
41
- // 3) Find changed packages
42
- async findChangedPackages(range, packages) {
43
- const changed = [];
44
- const results = await Promise.all(packages.map((pkg) => this.findChangedInPackage(range, pkg)));
45
- for (const result of results)
46
- changed.push(...result);
47
- return changed;
48
- }
49
- async findChangedInPackage(range, pkg) {
50
- const changed = [];
51
- if (pkg.private)
52
- return []; // ignore private packages
53
- const cmd = range
54
- ? `git diff --name-only ${range} -- "${pkg.dir}"`
55
- : `git diff --name-only HEAD^ -- "${pkg.dir}" || true`;
56
- const diff = (await execAsync(cmd, {
57
- cwd: this.paths.repoRoot,
58
- }))
59
- .toString()
60
- .split("\n")
61
- .filter(Boolean)
62
- // optionally ignore docs/tests-only changes:
63
- .filter((f) => !/\b(README\.md|CHANGELOG\.md|\.md|\.spec\.(t|j)s|\.test\.(t|j)s)\b/i.test(f));
64
- if (diff.length > 0)
65
- changed.push(pkg.name);
66
- return changed;
67
- }
68
- // 5) Write a Changeset file marking every changed package as 'patch'
69
- async write(changed) {
70
- const changesetDir = join(this.paths.repoRoot, ".changeset");
71
- await mkdir(changesetDir, { recursive: true });
72
- const sha = execAsync("git rev-parse --short HEAD").toString().trim();
73
- const header = changed.map((name) => `"${name}": patch`).join("\n");
74
- const body = `---
75
- ${header}
76
- ---
77
-
78
- Auto-generated patch release for changed packages in ${sha}.
79
- `;
80
- const filePath = join(changesetDir, `auto-${sha}.md`);
81
- await writeFile(filePath, body, "utf8");
82
- console.log(`Created ${filePath} for packages:\n - ${changed.join("\n - ")}`);
83
- }
84
- }
@@ -1 +0,0 @@
1
- export * from "./Changeset.js";
@@ -1,2 +0,0 @@
1
- // This file is auto-generated. Do not edit manually.
2
- export * from "./Changeset.js";
package/dist/pkg/Pkg.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import { PackageJson } from "../types/types.package.js";
2
- export declare class Pkg {
3
- readonly packageJsonPath: string;
4
- constructor(packageJsonPath: string);
5
- readJson(): Promise<PackageJson>;
6
- writeJson(pkg: PackageJson): Promise<void>;
7
- }
package/dist/pkg/Pkg.js DELETED
@@ -1,13 +0,0 @@
1
- import { readFile, writeFile } from "node:fs/promises";
2
- export class Pkg {
3
- packageJsonPath;
4
- constructor(packageJsonPath) {
5
- this.packageJsonPath = packageJsonPath;
6
- }
7
- async readJson() {
8
- return JSON.parse(await readFile(this.packageJsonPath, "utf8"));
9
- }
10
- async writeJson(pkg) {
11
- return writeFile(this.packageJsonPath, JSON.stringify(pkg, null, 2) + "\n", "utf8");
12
- }
13
- }