@monoweave/changelog 0.0.2

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/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ <!-- MONOWEAVE:BELOW -->
4
+
5
+ ## [0.0.2](https://github.com/monoweave/monoweave/compare/@monoweave/changelog@0.0.1...@monoweave/changelog@0.0.2) "@monoweave/changelog" (2024-02-29)<a name="0.0.2"></a>
6
+
7
+ ### Bug Fixes
8
+
9
+ * initial commit, forked from tophat/monodeploy under Apache 2.0 license (#1) ([2410d1c](https://github.com/monoweave/monoweave/commits/2410d1c))
10
+
11
+
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # Monoweave Changelog
2
+
3
+ This package is responsible for generating changelog entries, updating changelog files in place, and parsing conventional changelog configs.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ yarn add @monoweave/changelog
9
+ ```
@@ -0,0 +1,10 @@
1
+ import type { CommitMessage, MonoweaveConfiguration, YarnContext } from '@monoweave/types';
2
+ declare const generateChangelogEntry: ({ config, context, packageName, previousVersion, newVersion, commits, }: {
3
+ config: MonoweaveConfiguration;
4
+ context: YarnContext;
5
+ packageName: string;
6
+ previousVersion: string | null;
7
+ newVersion: string;
8
+ commits: CommitMessage[];
9
+ }) => Promise<string | null>;
10
+ export default generateChangelogEntry;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const stream_1 = require("stream");
7
+ const git_1 = require("@monoweave/git");
8
+ const io_1 = require("@monoweave/io");
9
+ const core_1 = require("@yarnpkg/core");
10
+ const conventional_changelog_writer_1 = __importDefault(require("conventional-changelog-writer"));
11
+ const conventional_commits_parser_1 = __importDefault(require("conventional-commits-parser"));
12
+ const resolveConventionalConfig_1 = __importDefault(require("./resolveConventionalConfig"));
13
+ const generateChangelogEntry = async ({ config, context, packageName, previousVersion, newVersion, commits, }) => {
14
+ if (!config.conventionalChangelogConfig) {
15
+ return null;
16
+ }
17
+ if (!commits.length) {
18
+ return null;
19
+ }
20
+ const ident = core_1.structUtils.parseIdent(packageName);
21
+ const workspace = context.project.getWorkspaceByIdent(ident);
22
+ const conventionalConfig = await (0, resolveConventionalConfig_1.default)({ config });
23
+ const commitsStream = stream_1.Readable.from(commits.map((commit) => `${commit.body}\n-hash-\n${commit.sha}`)).pipe((0, conventional_commits_parser_1.default)(conventionalConfig.parserOpts));
24
+ const conventionalCommits = await (0, io_1.readStream)(commitsStream);
25
+ const { host, owner, repository, repoUrl } = await (0, git_1.parseRepositoryProperty)(workspace);
26
+ const templateContext = {
27
+ version: newVersion,
28
+ title: packageName,
29
+ host: host ?? '',
30
+ owner: owner ?? workspace.manifest.raw?.author ?? '',
31
+ repository: repository ?? '',
32
+ repoUrl: repoUrl ?? '',
33
+ currentTag: `${packageName}@${newVersion}`,
34
+ previousTag: previousVersion ? `${packageName}@${previousVersion}` : undefined,
35
+ linkCompare: Boolean(previousVersion),
36
+ };
37
+ const changelogWriter = (0, conventional_changelog_writer_1.default)(templateContext, conventionalConfig.writerOpts);
38
+ async function* transformedCommits() {
39
+ for (const commit of conventionalCommits) {
40
+ // NOTE: This mutates the commit.
41
+ const mutableCommit = JSON.parse(JSON.stringify(commit));
42
+ if (!mutableCommit.hash && config.git.commitSha) {
43
+ mutableCommit.hash = config.git.commitSha;
44
+ }
45
+ yield mutableCommit;
46
+ }
47
+ }
48
+ const pipeline = stream_1.Readable.from(transformedCommits()).pipe(changelogWriter);
49
+ return (0, io_1.readStreamString)(pipeline);
50
+ };
51
+ exports.default = generateChangelogEntry;
package/lib/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import generateChangelogEntry from './changelog';
2
+ import prependChangelogFile from './prependChangelogFile';
3
+ import resolveConventionalConfig from './resolveConventionalConfig';
4
+ export { prependChangelogFile, resolveConventionalConfig, generateChangelogEntry };
package/lib/index.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.generateChangelogEntry = exports.resolveConventionalConfig = exports.prependChangelogFile = void 0;
7
+ const changelog_1 = __importDefault(require("./changelog"));
8
+ exports.generateChangelogEntry = changelog_1.default;
9
+ const prependChangelogFile_1 = __importDefault(require("./prependChangelogFile"));
10
+ exports.prependChangelogFile = prependChangelogFile_1.default;
11
+ const resolveConventionalConfig_1 = __importDefault(require("./resolveConventionalConfig"));
12
+ exports.resolveConventionalConfig = resolveConventionalConfig_1.default;
@@ -0,0 +1,10 @@
1
+ import { type ChangesetSchema, type MonoweaveConfiguration, type YarnContext } from '@monoweave/types';
2
+ import { type Workspace } from '@yarnpkg/core';
3
+ declare const prependChangelogFile: ({ config, context, changeset, workspaces, forceRefreshChangelogs, }: {
4
+ config: MonoweaveConfiguration;
5
+ context: YarnContext;
6
+ changeset: ChangesetSchema;
7
+ workspaces: Set<Workspace>;
8
+ forceRefreshChangelogs?: boolean | undefined;
9
+ }) => Promise<void>;
10
+ export default prependChangelogFile;
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fs_1 = require("fs");
7
+ const path_1 = __importDefault(require("path"));
8
+ const git_1 = require("@monoweave/git");
9
+ const logging_1 = __importDefault(require("@monoweave/logging"));
10
+ const types_1 = require("@monoweave/types");
11
+ const core_1 = require("@yarnpkg/core");
12
+ const fslib_1 = require("@yarnpkg/fslib");
13
+ const p_limit_1 = __importDefault(require("p-limit"));
14
+ const MARKER = '<!-- MONOWEAVE:BELOW -->';
15
+ const TOKEN_PACKAGE_DIR = '<packageDir>';
16
+ const prependEntry = async ({ config, context, filename, entry, }) => {
17
+ let changelogContents = [];
18
+ try {
19
+ changelogContents = (await fs_1.promises.readFile(filename, { encoding: 'utf-8' })).split('\n');
20
+ }
21
+ catch (err) {
22
+ if ((0, types_1.isNodeError)(err) && err.code === 'ENOENT') {
23
+ logging_1.default.info(`[Changelog] Changelog ${filename} does not exist, creating.`, {
24
+ report: context.report,
25
+ });
26
+ changelogContents = ['# Changelog', '', MARKER];
27
+ }
28
+ else {
29
+ logging_1.default.error(`[Changelog] Unable to read changelog contents at ${filename}.`, {
30
+ report: context.report,
31
+ });
32
+ throw err;
33
+ }
34
+ }
35
+ const changelogOffset = changelogContents.findIndex((value) => value.trim() === MARKER);
36
+ if (changelogOffset === -1) {
37
+ logging_1.default.error(`[Changelog] Missing changelog marker: '${MARKER}'`, {
38
+ report: context.report,
39
+ });
40
+ throw new Error('Unable to prepend changelog.');
41
+ }
42
+ changelogContents.splice(changelogOffset + 1, 0, `\n${entry}\n`);
43
+ const dataToWrite = changelogContents.join('\n');
44
+ if (config.dryRun && !config.forceWriteChangeFiles) {
45
+ logging_1.default.debug('[Changelog] Skipping changelog update.', {
46
+ report: context.report,
47
+ });
48
+ }
49
+ else {
50
+ await fs_1.promises.writeFile(filename, dataToWrite, {
51
+ encoding: 'utf-8',
52
+ });
53
+ }
54
+ logging_1.default.info(`[Changelog] Updated ${filename}`, {
55
+ report: context.report,
56
+ });
57
+ };
58
+ const prependChangelogFile = async ({ config, context, changeset, workspaces, forceRefreshChangelogs = false, }) => {
59
+ if (!config.changelogFilename)
60
+ return;
61
+ // Make sure the changelogs are up to date with the remote
62
+ if (!config.dryRun && forceRefreshChangelogs) {
63
+ const changelogGlob = config.changelogFilename.replace('<packageDir>', '**');
64
+ if (changelogGlob) {
65
+ try {
66
+ await (0, git_1.gitCheckout)({ files: [changelogGlob] }, { cwd: config.cwd, context });
67
+ }
68
+ catch {
69
+ logging_1.default.debug('Force refreshing changelogs failed. Ignoring.', {
70
+ report: context.report,
71
+ });
72
+ }
73
+ }
74
+ }
75
+ if (config.changelogFilename.includes(TOKEN_PACKAGE_DIR)) {
76
+ const prependForWorkspace = async (workspace) => {
77
+ const filename = fslib_1.npath.fromPortablePath(config.changelogFilename.replace(TOKEN_PACKAGE_DIR, fslib_1.npath.fromPortablePath(workspace.cwd)));
78
+ const packageName = core_1.structUtils.stringifyIdent(workspace.manifest.name);
79
+ const entry = changeset[packageName]?.changelog;
80
+ if (entry)
81
+ await prependEntry({ config, context, filename, entry });
82
+ };
83
+ const limit = (0, p_limit_1.default)(config.jobs || Infinity);
84
+ await Promise.all([...workspaces].map((workspace) => limit(() => prependForWorkspace(workspace))));
85
+ return;
86
+ }
87
+ const filename = path_1.default.resolve(config.cwd, config.changelogFilename);
88
+ const entry = Object.entries(changeset)
89
+ .sort(([pkgNameA], [pkgNameB]) => pkgNameA.localeCompare(pkgNameB))
90
+ .map(([, changesetValue]) => changesetValue.changelog)
91
+ .filter((value) => value)
92
+ .join('\n')
93
+ .trim();
94
+ if (entry)
95
+ await prependEntry({ config, context, filename, entry });
96
+ };
97
+ exports.default = prependChangelogFile;
@@ -0,0 +1,17 @@
1
+ import type { MonoweaveConfiguration } from '@monoweave/types';
2
+ import type { Options as ConventionalCommitsWriterOptions } from 'conventional-changelog-writer';
3
+ import type { Commit, Options as ConventionalCommitsParserOptions } from 'conventional-commits-parser';
4
+ interface ConventionalStrategy {
5
+ level?: number | null;
6
+ }
7
+ interface ConventionalChangelogConfig {
8
+ parserOpts: ConventionalCommitsParserOptions;
9
+ writerOpts: ConventionalCommitsWriterOptions;
10
+ recommendedBumpOpts: {
11
+ whatBump: (commits: Commit[]) => ConventionalStrategy;
12
+ };
13
+ }
14
+ declare const resolveConventionalConfig: ({ config, }: {
15
+ config: MonoweaveConfiguration;
16
+ }) => Promise<ConventionalChangelogConfig>;
17
+ export default resolveConventionalConfig;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const coerceConventionalConfig = (config) => {
4
+ if (typeof config === 'string') {
5
+ return {
6
+ name: config,
7
+ };
8
+ }
9
+ return config;
10
+ };
11
+ const resolveConventionalConfig = async ({ config, }) => {
12
+ const conventionalChangelogConfig = config.conventionalChangelogConfig;
13
+ if (!conventionalChangelogConfig) {
14
+ throw new Error('No conventional changelog config provided');
15
+ }
16
+ const conventionalConfig = coerceConventionalConfig(conventionalChangelogConfig);
17
+ // ghost-imports-ignore-next-line
18
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
19
+ const conventionalConfigModule = require(require.resolve(conventionalConfig.name, {
20
+ paths: [config.cwd],
21
+ }));
22
+ return await (typeof conventionalConfigModule === 'function'
23
+ ? conventionalConfigModule(conventionalConfig)
24
+ : conventionalConfigModule);
25
+ };
26
+ exports.default = resolveConventionalConfig;
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@monoweave/changelog",
3
+ "version": "0.0.2",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/monoweave/monoweave.git",
7
+ "directory": "packages/changelog"
8
+ },
9
+ "author": {
10
+ "name": "noahnu",
11
+ "url": "https://monoweave.github.io"
12
+ },
13
+ "license": "BSD-3",
14
+ "publishConfig": {
15
+ "main": "./lib/index.js",
16
+ "registry": "https://registry.npmjs.org/",
17
+ "types": "./lib/index.d.ts",
18
+ "access": "public"
19
+ },
20
+ "group": "monoweave",
21
+ "files": [
22
+ "lib"
23
+ ],
24
+ "main": "./lib/index.js",
25
+ "scripts": {
26
+ "clean": "run workspace:clean \"$(pwd)\"",
27
+ "types:watch": "run workspace:types:watch \"$(pwd)\"",
28
+ "test:watch": "run workspace:test:watch \"$(pwd)\"",
29
+ "build:watch": "run workspace:build:watch \"$(pwd)\"",
30
+ "prepack": "run workspace:build \"$(pwd)\""
31
+ },
32
+ "peerDependencies": {
33
+ "@monoweave/git": "^0.0.2",
34
+ "@monoweave/io": "^0.0.2",
35
+ "@monoweave/logging": "^0.0.2",
36
+ "@monoweave/types": "^0.0.2",
37
+ "@yarnpkg/core": "^4.0.3",
38
+ "@yarnpkg/fslib": "^3.0.2"
39
+ },
40
+ "dependencies": {
41
+ "conventional-changelog-writer": "^7.0.1",
42
+ "conventional-commits-parser": "^5.0.0",
43
+ "p-limit": "^3.1.0"
44
+ },
45
+ "devDependencies": {
46
+ "@monoweave/git": "0.0.2",
47
+ "@monoweave/io": "0.0.2",
48
+ "@monoweave/logging": "0.0.2",
49
+ "@monoweave/test-utils": "link:../../testUtils",
50
+ "@monoweave/types": "0.0.2",
51
+ "@types/conventional-changelog-writer": "^4.0.7",
52
+ "@types/conventional-commits-parser": "^3.0.5",
53
+ "@types/jest": "^29.5.6",
54
+ "@types/node": "^18.18.6",
55
+ "@yarnpkg/core": "^4.0.3",
56
+ "@yarnpkg/fslib": "^3.0.2"
57
+ },
58
+ "types": "./lib/index.d.ts"
59
+ }