Skip to content

Commit

Permalink
feat: add changelogs
Browse files Browse the repository at this point in the history
  • Loading branch information
v8tenko committed Apr 8, 2024
1 parent 4d0df1c commit 38733c8
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/cmd/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {ArgvService, Includers} from '../../services';
import {
initLinterWorkers,
processAssets,
processChangelogs,
processExcludedFiles,
processLinter,
processLogs,
Expand Down Expand Up @@ -229,6 +230,8 @@ async function handler(args: Arguments<any>) {
userOutputFolder,
});

await processChangelogs();

// Copy all generated files to user' output folder
shell.cp(
'-r',
Expand Down
1 change: 1 addition & 0 deletions src/steps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './processLogs';
export * from './processPages';
export * from './processLinter';
export * from './processServiceFiles';
export * from './processChangelogs';
13 changes: 11 additions & 2 deletions src/steps/processAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@ import {Resources} from '../models';
import {resolveRelativePath} from '@diplodoc/transform/lib/utilsFS';

/**
* Processes assets files (everything except .md files)
* @param {Array} args
* @param {string} outputBundlePath
* @param {string} outputFormat
* @param {string} tmpOutputFolder
* @return {void}
*/
export function processAssets({args, outputFormat, outputBundlePath, tmpOutputFolder}) {

type Props = {
args: string[];
outputBundlePath: string;
outputFormat: string;
tmpOutputFolder: string;
};
/*
* Processes assets files (everything except .md files)
*/
export function processAssets({args, outputFormat, outputBundlePath, tmpOutputFolder}: Props) {
switch (outputFormat) {
case 'html':
processAssetsHtmlRun({outputBundlePath});
Expand Down
109 changes: 109 additions & 0 deletions src/steps/processChangelogs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import {glob} from '../utils/glob';
import {join} from 'node:path';
import {ArgvService} from '../services';
import {readFile, unlink, writeFile} from 'node:fs/promises';

type Language = string;
type MergedChangelogs =
| {
[language: Language]: Record<string, Record<string, unknown>>;
}
| Record<string, Record<string, unknown>>;

/*
{
"ru": {
"/": {
"12314": <changelog>
},
"/plugins": {
"213312": <changelog>
}
}
}
or if single language
{
"/": {
"12314": <changelog>
},
"/plugins": {
"213312": <changelog>
}
}
*/

export async function processChangelogs() {
const {output: outputFolderPath, langs} = ArgvService.getConfig();

const result = await glob('**/**/changes-*', {
cwd: outputFolderPath,
});

const files = result.state.found;

if (!files.length) {
return;
}

const merged: MergedChangelogs = {};

const changes = await Promise.all(
files.map((path) => {
const filePath = join(outputFolderPath, path);

return readFile(filePath).then(
(buffer) => [path, JSON.parse(buffer.toString())] as [string, unknown],
);
}),
);

changes.forEach(([path, value]) => {
if (!langs?.length) {
const parts = path.split('/');
const [, hash] = parts.pop().split(/[-.]/);

const fullPath = '/' + parts.join('/');

if (!merged[fullPath]) {
merged[fullPath] = {};
}

Object.assign(merged[fullPath], {
[hash]: value,
});

return;
}

const [lang, ...rest] = path.split('/');
const [, hash] = rest.pop().split(/[-.]/);

const fullPath = '/' + rest.join('/');

if (!merged[lang]) {
merged[lang] = {};
}

if (!merged[lang][fullPath]) {
merged[lang][fullPath] = {};
}

Object.assign(merged[lang][fullPath], {
[hash]: value,
});
});

await Promise.all(
files.map((path) => {
const filePath = join(outputFolderPath, path);

return unlink(filePath);
}),
);

const changelogPath = join(outputFolderPath, 'changelog.json');

await writeFile(changelogPath, JSON.stringify(merged, null, 4));
}
8 changes: 4 additions & 4 deletions src/utils/markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ export function replaceDoubleToSingleQuotes(str: string): string {
return str.replace(/"/g, "'");
}

export function findAllValuesByKeys(obj, keysToFind) {
return flatMapDeep(obj, (value, key) => {
export function findAllValuesByKeys(obj, keysToFind: string[]) {
return flatMapDeep(obj, (value: string | string[], key: string) => {
if (
keysToFind?.includes(key) &&
(isString(value) || (isArray(value) && value.every(isString)))
Expand All @@ -188,15 +188,15 @@ export function findAllValuesByKeys(obj, keysToFind) {
});
}

export function getLinksWithExtension(link) {
export function getLinksWithExtension(link: string) {
const oneLineWithExtension = new RegExp(
/^\S.*\.(md|html|yaml|svg|png|gif|jpg|jpeg|bmp|webp|ico)$/gm,
);

return oneLineWithExtension.test(link);
}

export function checkPathExists(path, parentFilePath) {
export function checkPathExists(path: string, parentFilePath: string) {
const includePath = resolveRelativePath(parentFilePath, path);

return isFileExists(includePath);
Expand Down

0 comments on commit 38733c8

Please sign in to comment.