From 69bf3a00ca9e184ab62fd02472cdf0d8de705707 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 | 32 ++++++++-------------------- src/commands/build/index.ts | 3 +-- src/commands/build/run.ts | 2 +- src/steps/processAssets.ts | 40 +++++++++++------------------------ 5 files changed, 24 insertions(+), 56 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..ac03cc30 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`); } @@ -39,14 +34,12 @@ export async function handler(run: Run) { Includers.init([OpenapiIncluder]); const { - output: outputFolderPath, - outputFormat, lintDisabled, buildDisabled, addMapFile, } = ArgvService.getConfig(); - preparingTemporaryFolders(); + preparingTemporaryFolders(run); await processServiceFiles(); processExcludedFiles(); @@ -55,7 +48,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 +64,7 @@ export async function handler(run: Run) { if (!buildDisabled) { // process additional files - processAssets({ - run, - outputFormat, - outputBundlePath, - tmpOutputFolder, - }); + processAssets(run); await processChangelogs(); @@ -85,23 +73,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/steps/processAssets.ts b/src/steps/processAssets.ts index 90dada29..1d547031 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) => { @@ -111,6 +95,6 @@ function processAssetsMdRun({run, tmpOutputFolder}: {run: Run; tmpOutputFolder: return acc; }, [] as RelativePath[]); - copyFiles(run.originalInput, tmpOutputFolder, localMediaLinks); + copyFiles(run.originalInput, run.output, localMediaLinks); }); }