Skip to content

Commit

Permalink
feat(changelog): Add extract changelogs (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feverqwe authored Jun 27, 2023
1 parent 63a71f6 commit a8d096d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ const images = require('@doc-tools/transform/lib/plugins/images');
const monospace = require('@doc-tools/transform/lib/plugins/monospace');
const table = require('@doc-tools/transform/lib/plugins/table');
const term = require('@doc-tools/transform/lib/plugins/term');
const changelog = require('@doc-tools/transform/lib/plugins/changelog');
const mermaid = require('@diplodoc/mermaid-extension');
const openapi = require('@diplodoc/openapi-extension');

includes.collect = require('@doc-tools/transform/lib/plugins/includes/collect');
images.collect = require('@doc-tools/transform/lib/plugins/images/collect');
changelog.collect = require('@doc-tools/transform/lib/plugins/changelog/collect');

export const BUILD_FOLDER = 'build';
export const BUNDLE_FOLDER = '_bundle';
Expand Down Expand Up @@ -86,6 +88,7 @@ export const YFM_PLUGINS = [
term,
openapi.transform(),
mermaid.transform(),
changelog,
];

export const PROCESSING_FINISHED = 'Processing finished:';
Expand Down
3 changes: 3 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {LintConfig} from '@doc-tools/transform/lib/yfmlint';

import {FileContributors, VCSConnector, VCSConnectorConfig} from './vcs-connector/connector-models';
import {Lang, Stage, IncludeMode, ResourceType} from './constants';
import {ChangelogItem} from '@doc-tools/transform/lib/plugins/changelog/types';

export type VarsPreset = 'internal'|'external';

Expand Down Expand Up @@ -193,6 +194,8 @@ export interface PluginOptions {
destPath?: string;
destRoot?: string;
collectOfPlugins?: (input: string, options: PluginOptions) => string;
changelogs?: ChangelogItem[];
extractChangelogs?: boolean;
}

export interface Plugin {
Expand Down
42 changes: 39 additions & 3 deletions src/resolvers/md2md.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {readFileSync, writeFileSync} from 'fs';
import {dirname, resolve} from 'path';
import {existsSync, readFileSync, writeFileSync} from 'fs';
import {dirname, resolve, join, basename, extname} from 'path';
import shell from 'shelljs';
import log from '@doc-tools/transform/lib/log';
import liquid from '@doc-tools/transform/lib/liquid';
Expand All @@ -9,6 +9,7 @@ import {logger, getVarsPerFile} from '../utils';
import {PluginOptions, ResolveMd2MdOptions} from '../models';
import {PROCESSING_FINISHED} from '../constants';
import {getContentWithUpdatedMetadata} from '../services/metadata';
import {ChangelogItem} from '@doc-tools/transform/lib/plugins/changelog/types';

export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void> {
const {inputPath, outputPath, metadata} = options;
Expand All @@ -22,7 +23,7 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void>
vars.__system,
);

const {result} = transformMd2Md(content, {
const {result, changelogs} = transformMd2Md(content, {
path: resolvedInputPath,
destPath: outputPath,
root: resolve(input),
Expand All @@ -34,6 +35,37 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void>
});

writeFileSync(outputPath, result);

if (changelogs?.length) {
const mdFilename = basename(outputPath, extname(outputPath));
const outputDir = dirname(outputPath);
changelogs.forEach((changes, index) => {
let changesName;
const changesDate = changes.date as string | undefined;
const changesIdx = changes.index as number | undefined;
if (typeof changesIdx === 'number') {
changesName = String(changesIdx);
}
if (!changesName && changesDate && /^\d{4}/.test(changesDate)) {
changesName = Math.trunc(new Date(changesDate).getTime() / 1000);
}
if (!changesName) {
changesName = `name-${mdFilename}-${String(changelogs.length - index).padStart(3, '0')}`;
}

const changesPath = join(outputDir, `changes-${changesName}.json`);

if (existsSync(changesPath)) {
throw new Error(`Changelog ${changesPath} already exists!`);
}

writeFileSync(changesPath, JSON.stringify({
...changes,
source: mdFilename,
}));
});
}

logger.info(inputPath, PROCESSING_FINISHED);

return undefined;
Expand Down Expand Up @@ -84,6 +116,7 @@ function transformMd2Md(input: string, options: PluginOptions) {
} = options;

let output = input;
const changelogs: ChangelogItem[] = [];

if (!disableLiquid) {
const liquidResult = liquidMd2Md(input, vars, path);
Expand All @@ -101,11 +134,14 @@ function transformMd2Md(input: string, options: PluginOptions) {
log: pluginLog,
copyFile: pluginCopyFile,
collectOfPlugins,
changelogs,
extractChangelogs: true,
});
}

return {
result: output,
changelogs,
logs: pluginLog.get(),
};
}

0 comments on commit a8d096d

Please sign in to comment.