Skip to content

Commit

Permalink
chore: Small impovments
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Nov 27, 2024
1 parent 77a12a3 commit b5830e2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 79 deletions.
3 changes: 1 addition & 2 deletions src/commands/build/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ const outputFormat = option({

const langs = option({
flags: '--lang, --langs <value...>',
desc: 'Allow loading custom resources into statically generated pages.',
// parser: toArray,
desc: 'Configure langs supported by build',
});

const vars = option({
Expand Down
38 changes: 10 additions & 28 deletions src/commands/build/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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`);
}
Expand All @@ -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();
Expand All @@ -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 */
Expand All @@ -71,12 +60,7 @@ export async function handler(run: Run) {

if (!buildDisabled) {
// process additional files
processAssets({
run,
outputFormat,
outputBundlePath,
tmpOutputFolder,
});
processAssets(run);

await processChangelogs();

Expand All @@ -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);
}
3 changes: 1 addition & 2 deletions src/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/build/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export function withConfigUtils<T extends Hash = Hash>(path: string | null, conf
[configPath]: {
enumerable: false,
value: path === null ? path : resolve(path),
}
},
});
}

Expand Down
3 changes: 1 addition & 2 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
69 changes: 26 additions & 43 deletions src/steps/processAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand All @@ -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<string[]>((acc, file) => {
Expand All @@ -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);
});
}

0 comments on commit b5830e2

Please sign in to comment.