Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Feverqwe committed Dec 27, 2024
1 parent 6749f51 commit 6663651
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 27 deletions.
1 change: 1 addition & 0 deletions scripts/build.cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const commonConfig = {
const builds = [
[['src/index.ts'], 'build/index.js'],
[['src/workers/linter/index.ts'], 'build/linter.js'],
[['src/reCli/workers/transform.ts'], 'build/workers/transform.js'],
];

Promise.all(builds.map(([entries, outfile]) => {
Expand Down
35 changes: 27 additions & 8 deletions src/commands/build/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import 'threads/register';

import OpenapiIncluder from '@diplodoc/openapi-extension/includer';

import {ArgvService, Includers, SearchService} from '~/services';
import {processLogs} from '~/steps';
import {ArgvService, Includers} from '~/services';
import {processChangelogs, processLogs} from '~/steps';
import {getNavigationPaths, getPresetIndex} from '~/reCli/components/presets';
import {getTocIndex, transformTocForJs, transformTocForSinglePage} from '~/reCli/components/toc';
import GithubConnector from '~/reCli/components/vcs/github';
Expand All @@ -26,16 +26,17 @@ import {saveSinglePages} from '~/reCli/components/render/singlePage';
import {copyAssets} from '~/reCli/components/assets/assets';
import {saveRedirectPage} from '~/reCli/components/render/redirect';
import {LogCollector} from '~/reCli/utils/logger';
import {getMapFile} from '~/reCli/components/toc/mapFile';
import {BuildConfig} from '~/commands/build/index';

export async function handler(run: Run) {
try {
ArgvService.init(run.legacyConfig);
SearchService.init();
// TODO: Remove duplicated types from openapi-extension
// @ts-ignore
Includers.init([OpenapiIncluder]);

const {input, output, outputFormat, singlePage} = run.config;
const {input, output, outputFormat, singlePage, addMapFile} = run.config;
const {applyPresets, resolveConditions} = run.legacyConfig;

const presetIndex = await getPresetIndex(run.input, run.config, run);
Expand Down Expand Up @@ -139,14 +140,22 @@ export async function handler(run: Run) {
);
const pages = Array.from(pageSet.values());

if (addMapFile) {
const map = getMapFile(pages);
await fs.promises.writeFile(
path.join(output, 'files.json'),
JSON.stringify(map, null, '\t'),
);
}

const writeConflicts = new Map<string, string>();
const singlePageTocPagesMap = new Map<string, SinglePageResult[]>();

const workerCount = Number(WORKER_COUNT);
// eslint-disable-next-line new-cap
const transformPool = Pool(
() =>
spawn<TransformWorker>(new Worker('../../../reCli/workers/transform'), {
spawn<TransformWorker>(new Worker('./workers/transform'), {
timeout: 60000,
}),
workerCount,
Expand All @@ -165,8 +174,15 @@ export async function handler(run: Run) {
index = workerIndex++;
workerIndexMap.set(worker, index);
const threadOutput = path.join(tmpThreads, String(index));

const configClone: Record<string, unknown> = {};
// eslint-disable-next-line guard-for-in
for (const key in run.config) {
configClone[key] = run.config[key as keyof BuildConfig];
}

await worker.init({
config: run.config,
config: configClone as BuildConfig,
presetIndex,
tmpSource,
tmpDraft,
Expand Down Expand Up @@ -226,8 +242,8 @@ export async function handler(run: Run) {
}),
);
await Promise.all([
fs.promises.rm(tmpThreads, {recursive: true}),
fs.promises.rm(tmpDraft, {recursive: true}),
fs.promises.rm(tmpThreads, {recursive: true, force: true}),
fs.promises.rm(tmpDraft, {recursive: true, force: true}),
]);
} finally {
await transformPool.terminate(true);
Expand All @@ -246,6 +262,7 @@ export async function handler(run: Run) {

if (singlePage) {
await saveSinglePages({
targetCwd: output,
options: run.config,
singlePageTocPagesMap,
tocIndex,
Expand All @@ -261,6 +278,8 @@ export async function handler(run: Run) {
pages,
logger,
});

await processChangelogs();
} catch (error) {
run.logger.error(error);
} finally {
Expand Down
22 changes: 17 additions & 5 deletions src/reCli/components/render/singlePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import {DocInnerProps, DocPageData} from '@diplodoc/client/ssr';
import {CONCURRENCY} from '~/reCli/constants';
import fs from 'node:fs';
import {LogCollector} from '~/reCli/utils/logger';
import {cachedMkdir, safePath} from '~/reCli/utils';

const SINGLE_PAGE_FILENAME = 'single-page.html';
const SINGLE_PAGE_DATA_FILENAME = 'single-page.json';

export interface SaveSinglePagesProps {
targetCwd: string;
options: BuildConfig;
singlePageTocPagesMap: Map<string, SinglePageResult[]>;
tocIndex: TocIndexMap;
logger: LogCollector;
}

export async function saveSinglePages({
targetCwd,
options,
singlePageTocPagesMap,
tocIndex,
Expand All @@ -34,7 +37,9 @@ export async function saveSinglePages({
await pMap(
Array.from(singlePageTocPagesMap.entries()),
async ([tocPath, pageResults]) => {
if (!pageResults.length) return;
if (!pageResults.length) {
return;
}

const tocDir = path.dirname(tocPath.replace(/\\/g, '/').replace(/^\/?/, ''));
const singlePageBody = joinSinglePageResults(pageResults, tocDir);
Expand Down Expand Up @@ -66,8 +71,8 @@ export async function saveSinglePages({
};

// Save the full single page for viewing locally
const singlePageFn = join(tocPath, SINGLE_PAGE_FILENAME);
const singlePageDataFn = join(tocPath, SINGLE_PAGE_DATA_FILENAME);
const singlePageFn = path.join(tocDir, SINGLE_PAGE_FILENAME);
const singlePageDataFn = safePath(path.join(tocDir, SINGLE_PAGE_DATA_FILENAME));
const tocInfo = {path: join(tocDir, 'single-page-toc'), content: toc};
const singlePageContent = generateStaticMarkup(
options,
Expand All @@ -76,9 +81,16 @@ export async function saveSinglePages({
(toc.title as string) || '',
);

await cachedMkdir(path.join(targetCwd, safePath(tocDir)));
await Promise.all([
fs.promises.writeFile(singlePageFn, singlePageContent),
fs.promises.writeFile(singlePageDataFn, JSON.stringify(pageData)),
fs.promises.writeFile(
path.join(targetCwd, safePath(singlePageFn)),
singlePageContent,
),
fs.promises.writeFile(
path.join(targetCwd, safePath(singlePageDataFn)),
JSON.stringify(pageData),
),
]);
},
{concurrency: CONCURRENCY},
Expand Down
18 changes: 18 additions & 0 deletions src/reCli/components/toc/mapFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {convertBackSlashToSlash} from '~/utils';
import path from 'node:path';

export function getMapFile(pages: string[]) {
const navigationPathsWithoutExtensions = pages.map((pagePath) => {
let preparedPath = convertBackSlashToSlash(
path.dirname(pagePath) + '/' + path.basename(pagePath, path.extname(pagePath)),
);

if (preparedPath.endsWith('/index')) {
preparedPath = preparedPath.substring(0, preparedPath.length - 5);
}

return preparedPath;
});

return {files: navigationPathsWithoutExtensions.sort()};
}
8 changes: 4 additions & 4 deletions src/reCli/components/toc/toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ async function replaceIncludes(
const itemsWithIncluded = (item.items || []).concat(includeToc.items);

/* Resolve nested toc inclusions */
const baseTocPath =
mode === IncludeMode.LINK ? includeTocPath : path.dirname(tocPath);
const baseTocPath = mode === IncludeMode.LINK ? includeTocPath : tocPath;

const {items: subItems, includedTocs: subIncludedTocs} = await processTocItems(
itemsWithIncluded,
Expand Down Expand Up @@ -308,11 +307,12 @@ async function copyTocDir(
destDir: string,
copyMap: Map<string, string>,
) {
const source = path.join(cwd, safePath(tocPath));
const target = path.join(cwd, safePath(destDir));
const source = path.join(cwd, safePath(tocPath)) as AbsolutePath;
const target = path.join(cwd, safePath(destDir)) as AbsolutePath;

const files = await run.glob('**/*.*', {
ignore: ['**/toc.yaml'],
cwd: source,
});

const dirs = new Set<string>();
Expand Down
16 changes: 15 additions & 1 deletion src/reCli/components/transform/mdPageToMd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const TARGET_COPY_SET = new Set();

async function transformMd(props: TransformPageProps, pagePath: string) {
const {presetIndex, cwd, options, fileMetaMap, vcsConnector} = props;
const {vars, addSystemMeta} = options;
const {vars, addSystemMeta, allowCustomResources, resources} = options;

const combinedVars = getFilePresets(presetIndex, vars, pagePath);
const input = await fs.promises.readFile(path.join(cwd, pagePath) as AbsolutePath, 'utf8');
Expand All @@ -91,6 +91,13 @@ async function transformMd(props: TransformPageProps, pagePath: string) {
fileMetaMap.set(pagePath, {...fileMetaMap.get(pagePath), __system: combinedVars.__system});
}

if (Array.isArray(combinedVars.__metadata)) {
fileMetaMap.set(pagePath, {
...fileMetaMap.get(pagePath),
metadata: [...(meta.metadata ?? []), ...combinedVars.__metadata],
});
}

if (vcsConnector) {
const author = vcsConnector.getAuthor(pagePath);
const updatedAt = vcsConnector.getMtime(pagePath, includedPaths);
Expand All @@ -114,6 +121,13 @@ async function transformMd(props: TransformPageProps, pagePath: string) {
}
}

if (allowCustomResources && resources) {
fileMetaMap.set(pagePath, {
...fileMetaMap.get(pagePath),
...resources,
});
}

const extraMeta = fileMetaMap.get(pagePath);
if (extraMeta) {
Object.assign(meta, extraMeta);
Expand Down
12 changes: 10 additions & 2 deletions src/reCli/components/transform/pageToHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ async function getFileProps(props: PageToHtmlProps, pagePath: string) {
}

async function getFileData(props: PageToHtmlProps, pagePath: string) {
const {cwd, fileMetaMap, vcsConnector, options} = props;
const {allowCustomResources, resources} = options;
const {cwd, fileMetaMap, vcsConnector, options, presetIndex} = props;
const {allowCustomResources, resources, vars} = options;
const combinedVars = getFilePresets(presetIndex, vars, pagePath);

const pageContent: string = await fs.promises.readFile(
path.join(cwd, safePath(pagePath)) as AbsolutePath,
Expand Down Expand Up @@ -128,6 +129,13 @@ async function getFileData(props: PageToHtmlProps, pagePath: string) {
fileMeta.metadata = [fileMeta.metadata];
}

if (Array.isArray(combinedVars.__metadata)) {
fileMeta.metadata = [
...(fileMeta.metadata ?? []),
...combinedVars.__metadata.filter(Boolean),
];
}

if (allowCustomResources) {
if (resources) {
Object.entries(resources).forEach(([key, value = []]) => {
Expand Down
5 changes: 2 additions & 3 deletions src/reCli/utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import {

export function getPlugins() {
const plugins = getPluginsLegacy();
if (!plugins.length) {
if (!plugins?.length) {
setPlugins();
}
return getPluginsLegacy();
}

export function getCollectOfPlugins() {
const list = getCollectOfPluginsLegacy();
if (!list.length) {
if (!getPluginsLegacy()?.length) {
setPlugins();
}
return getCollectOfPluginsLegacy();
Expand Down
6 changes: 2 additions & 4 deletions src/reCli/workers/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {SinglePageResult} from '~/models';
import {readTransformLog} from '~/reCli/utils/legacy';
import {lintPage} from '~/reCli/components/lint/lint';
import {transformPage} from '~/reCli/components/transform/transform';
import {LogCollector} from "~/reCli/utils/logger";
import {LogCollector} from '~/reCli/utils/logger';

/*eslint-disable no-console*/

Expand Down Expand Up @@ -150,9 +150,7 @@ async function run({pages}: TransformWorkerProps) {
);
} catch (err) {
const error = err as Error;
logger.error(
`Transform page error ${pagePath}. Error: ${error.stack}`,
);
logger.error(`Transform page error ${pagePath}. Error: ${error.stack}`);
}
}

Expand Down

0 comments on commit 6663651

Please sign in to comment.