From b5830e2254eef93cc7a6d736e3112afdc07a094f Mon Sep 17 00:00:00 2001 From: 3y3 <3y3@ya.ru> Date: Thu, 14 Nov 2024 21:15:09 +0300 Subject: [PATCH] chore: Small impovments --- src/commands/build/config.ts | 3 +- src/commands/build/handler.ts | 38 +++++-------------- src/commands/build/index.ts | 3 +- src/commands/build/run.ts | 2 +- src/config/index.ts | 2 +- src/logger/index.ts | 3 +- src/steps/processAssets.ts | 69 +++++++++++++---------------------- 7 files changed, 41 insertions(+), 79 deletions(-) diff --git a/src/commands/build/config.ts b/src/commands/build/config.ts index a09a1b48..804bf9e5 100644 --- a/src/commands/build/config.ts +++ b/src/commands/build/config.ts @@ -26,8 +26,7 @@ const outputFormat = option({ const langs = option({ flags: '--lang, --langs ', - desc: 'Allow loading custom resources into statically generated pages.', - // parser: toArray, + desc: 'Configure langs supported by build', }); const vars = option({ diff --git a/src/commands/build/handler.ts b/src/commands/build/handler.ts index fbf995a2..55ac4b27 100644 --- a/src/commands/build/handler.ts +++ b/src/commands/build/handler.ts @@ -3,12 +3,10 @@ import type {Run} from './run'; import 'threads/register'; import glob from 'glob'; -import {join} from 'path'; import shell from 'shelljs'; import OpenapiIncluder from '@diplodoc/openapi-extension/includer'; -import {BUNDLE_FOLDER} from '~/constants'; import {ArgvService, Includers, SearchService} from '~/services'; import { initLinterWorkers, @@ -24,9 +22,6 @@ import {prepareMapFile} from '~/steps/processMapFile'; import {copyFiles} from '~/utils'; export async function handler(run: Run) { - const tmpInputFolder = run.input; - const tmpOutputFolder = run.output; - if (typeof VERSION !== 'undefined') { console.log(`Using v${VERSION} version`); } @@ -38,15 +33,9 @@ export async function handler(run: Run) { // @ts-ignore Includers.init([OpenapiIncluder]); - const { - output: outputFolderPath, - outputFormat, - lintDisabled, - buildDisabled, - addMapFile, - } = ArgvService.getConfig(); + const {lintDisabled, buildDisabled, addMapFile} = ArgvService.getConfig(); - preparingTemporaryFolders(); + preparingTemporaryFolders(run); await processServiceFiles(); processExcludedFiles(); @@ -55,7 +44,7 @@ export async function handler(run: Run) { prepareMapFile(); } - const outputBundlePath = join(outputFolderPath, BUNDLE_FOLDER); + const outputBundlePath = run.bundlePath; if (!lintDisabled) { /* Initialize workers in advance to avoid a timeout failure due to not receiving a message from them */ @@ -71,12 +60,7 @@ export async function handler(run: Run) { if (!buildDisabled) { // process additional files - processAssets({ - run, - outputFormat, - outputBundlePath, - tmpOutputFolder, - }); + processAssets(run); await processChangelogs(); @@ -85,23 +69,21 @@ export async function handler(run: Run) { } catch (error) { run.logger.error(error); } finally { - processLogs(tmpInputFolder); + processLogs(run.input); } } -function preparingTemporaryFolders() { - const args = ArgvService.getConfig(); - +function preparingTemporaryFolders(run: Run) { copyFiles( - args.rootInput, - args.input, + run.originalInput, + run.input, glob.sync('**', { - cwd: args.rootInput, + cwd: run.originalInput, nodir: true, follow: true, ignore: ['node_modules/**', '*/node_modules/**'], }), ); - shell.chmod('-R', 'u+w', args.input); + shell.chmod('-R', 'u+w', run.input); } diff --git a/src/commands/build/index.ts b/src/commands/build/index.ts index 73f979a6..b3923c75 100644 --- a/src/commands/build/index.ts +++ b/src/commands/build/index.ts @@ -274,8 +274,6 @@ export class Build run.logger.pipe(this.logger); - shell.mkdir('-p', run.originalOutput); - // Create temporary input/output folders shell.rm('-rf', run.input, run.output); shell.mkdir('-p', run.input, run.output); @@ -287,6 +285,7 @@ export class Build await this.hooks.AfterAnyRun.promise(run); // Copy all generated files to user' output folder + shell.mkdir('-p', run.originalOutput); shell.cp('-r', join(run.output, '*'), run.originalOutput); if (glob.sync('.*', {cwd: run.output}).length) { diff --git a/src/commands/build/run.ts b/src/commands/build/run.ts index 06a909d7..327bfff5 100644 --- a/src/commands/build/run.ts +++ b/src/commands/build/run.ts @@ -32,7 +32,7 @@ export class Run { readonly config: BuildConfig; get bundlePath() { - return join(this.originalOutput, BUNDLE_FOLDER); + return join(this.output, BUNDLE_FOLDER); } get configPath() { diff --git a/src/config/index.ts b/src/config/index.ts index 6a6df7fc..987c65c1 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -161,7 +161,7 @@ export function withConfigUtils(path: string | null, conf [configPath]: { enumerable: false, value: path === null ? path : resolve(path), - } + }, }); } diff --git a/src/logger/index.ts b/src/logger/index.ts index fe1383e5..6547c1ba 100644 --- a/src/logger/index.ts +++ b/src/logger/index.ts @@ -64,7 +64,6 @@ const colors = { [LogLevel.ERROR]: red, }; - /** * Logger has three logging channels: info, warning, and error. * There are also many topics that use one of these channels. @@ -198,7 +197,7 @@ export class Logger implements LogConsumer { [LogLevel.INFO]: this[INFO].count, [LogLevel.WARN]: this[WARN].count, [LogLevel.ERROR]: this[ERROR].count, - } + }; } [Write](level: LogLevels, message: string) { diff --git a/src/steps/processAssets.ts b/src/steps/processAssets.ts index 90dada29..6cd699e9 100644 --- a/src/steps/processAssets.ts +++ b/src/steps/processAssets.ts @@ -5,7 +5,7 @@ import {load} from 'js-yaml'; import {readFileSync} from 'fs'; import {join, relative} from 'path'; -import {ArgvService, TocService} from '../services'; +import {TocService} from '../services'; import {checkPathExists, copyFiles, findAllValuesByKeys} from '../utils'; import {DocLeadingPageData, LINK_KEYS} from '@diplodoc/client/ssr'; @@ -15,54 +15,38 @@ import {ASSETS_FOLDER} from '../constants'; import {Resources} from '../models'; import {resolveRelativePath} from '@diplodoc/transform/lib/utilsFS'; -/** - * @param {Array} args - * @param {string} outputBundlePath - * @param {string} outputFormat - * @param {string} tmpOutputFolder - * @return {void} - */ - -type Props = { - run: Run; - outputBundlePath: string; - outputFormat: string; - tmpOutputFolder: string; -}; /* * Processes assets files (everything except .md files) */ -export function processAssets({run, outputFormat, outputBundlePath, tmpOutputFolder}: Props) { - switch (outputFormat) { +export function processAssets(run: Run) { + switch (run.config.outputFormat) { case 'html': - processAssetsHtmlRun({outputBundlePath}); + processAssetsHtmlRun(run); break; case 'md': - processAssetsMdRun({run, tmpOutputFolder}); + processAssetsMdRun(run); break; } } -function processAssetsHtmlRun({outputBundlePath}) { - const {input: inputFolderPath, output: outputFolderPath} = ArgvService.getConfig(); - - const documentationAssetFilePath: string[] = walkSync(inputFolderPath, { +function processAssetsHtmlRun(run: Run) { + const documentationAssetFilePath: string[] = walkSync(run.input, { directories: false, includeBasePath: false, ignore: ['**/*.yaml', '**/*.md'], }); - copyFiles(inputFolderPath, outputFolderPath, documentationAssetFilePath); + copyFiles(run.input, run.output, documentationAssetFilePath); const bundleAssetFilePath: string[] = walkSync(ASSETS_FOLDER, { directories: false, includeBasePath: false, }); - copyFiles(ASSETS_FOLDER, outputBundlePath, bundleAssetFilePath); + copyFiles(ASSETS_FOLDER, run.bundlePath, bundleAssetFilePath); } -function processAssetsMdRun({run, tmpOutputFolder}: {run: Run; tmpOutputFolder: string}) { +function processAssetsMdRun(run: Run) { const {allowCustomResources, resources} = run.config; if (resources && allowCustomResources) { @@ -78,7 +62,7 @@ function processAssetsMdRun({run, tmpOutputFolder}: {run: Run; tmpOutputFolder: }); //copy resources - copyFiles(run.originalInput, tmpOutputFolder, resourcePaths); + copyFiles(run.originalInput, run.output, resourcePaths); } const tocYamlFiles = TocService.getNavigationPaths().reduce((acc, file) => { @@ -96,21 +80,20 @@ function processAssetsMdRun({run, tmpOutputFolder}: {run: Run; tmpOutputFolder: } const contentLinks = findAllValuesByKeys(content as DocLeadingPageData, LINK_KEYS); - const localMediaLinks = contentLinks.reduce( - (acc: string[], link: string) => { - const linkHasMediaExt = new RegExp( - /^\S.*\.(svg|png|gif|jpg|jpeg|bmp|webp|ico)$/gm, - ).test(link); - - if (linkHasMediaExt && isLocalUrl(link) && checkPathExists(link, yamlFile)) { - const linkAbsolutePath = resolveRelativePath(yamlFile, link); - const linkRootPath = relative(run.input, linkAbsolutePath); - - acc.push(linkRootPath); - } - return acc; - }, [] as RelativePath[]); - - copyFiles(run.originalInput, tmpOutputFolder, localMediaLinks); + const localMediaLinks = contentLinks.reduce((acc: string[], link: string) => { + const linkHasMediaExt = new RegExp( + /^\S.*\.(svg|png|gif|jpg|jpeg|bmp|webp|ico)$/gm, + ).test(link); + + if (linkHasMediaExt && isLocalUrl(link) && checkPathExists(link, yamlFile)) { + const linkAbsolutePath = resolveRelativePath(yamlFile, link); + const linkRootPath = relative(run.input, linkAbsolutePath); + + acc.push(linkRootPath); + } + return acc; + }, [] as RelativePath[]); + + copyFiles(run.originalInput, run.output, localMediaLinks); }); }