Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
martyanovandrey committed Mar 27, 2024
1 parent 347cdd2 commit 1e3bd4e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 82 deletions.
87 changes: 9 additions & 78 deletions src/cmd/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import glob from 'glob';
import walkSync from 'walk-sync';
import {load} from 'js-yaml';
import {readFileSync} from 'fs';
import {Arguments, Argv} from 'yargs';
import {join, resolve} from 'path';
import shell from 'shelljs';

import {LINK_KEYS_PAGE_CONSTRUCTOR_CONFIG} from '@diplodoc/client/ssr';
import {isLocalUrl} from '@diplodoc/transform/lib/utils';
import OpenapiIncluder from '@diplodoc/openapi-extension/includer';

import {
BUNDLE_FOLDER,
LINT_CONFIG_FILENAME,
REDIRECTS_FILENAME,
Stage,
TMP_INPUT_FOLDER,
TMP_OUTPUT_FOLDER,
YFM_CONFIG_FILENAME,
} from '../../constants';
import {BUNDLE_FOLDER, Stage, TMP_INPUT_FOLDER, TMP_OUTPUT_FOLDER} from '../../constants';
import {argvValidator} from '../../validator';
import {ArgvService, Includers} from '../../services';
import {
Expand All @@ -31,10 +18,8 @@ import {
processServiceFiles,
} from '../../steps';
import {prepareMapFile} from '../../steps/processMapFile';
import {Resources} from '../../models';
import {checkPathExists, copyFiles, findAllValuesByKeys, logger} from '../../utils';
import {copyFiles, logger} from '../../utils';
import {upload as publishFilesToS3} from '../publish/upload';
import {LINK_KEYS_PAGE_CONSTRUCTOR_CONFIG} from "@diplodoc/client/build/constants";

export const build = {
command: ['build', '$0'],
Expand Down Expand Up @@ -209,8 +194,6 @@ async function handler(args: Arguments<any>) {
lintDisabled,
buildDisabled,
addMapFile,
allowCustomResources,
resources,
} = ArgvService.getConfig();

preparingTemporaryFolders(userOutputFolder);
Expand All @@ -223,9 +206,6 @@ async function handler(args: Arguments<any>) {
}

const outputBundlePath = join(outputFolderPath, BUNDLE_FOLDER);
const pathToConfig = args.config || join(args.input, YFM_CONFIG_FILENAME);
const pathToRedirects = join(args.input, REDIRECTS_FILENAME);
const pathToLintConfig = join(args.input, LINT_CONFIG_FILENAME);

if (!lintDisabled) {
/* Initialize workers in advance to avoid a timeout failure due to not receiving a message from them */
Expand All @@ -241,62 +221,13 @@ async function handler(args: Arguments<any>) {

if (!buildDisabled) {
// process additional files
switch (outputFormat) {
case 'html':
processAssets(outputBundlePath);
break;
case 'md': {
shell.cp(resolve(pathToConfig), tmpOutputFolder);
shell.cp(resolve(pathToRedirects), tmpOutputFolder);
shell.cp(resolve(pathToLintConfig), tmpOutputFolder);

if (resources && allowCustomResources) {
const resourcePaths: string[] = [];

// collect paths of all resources
Object.keys(resources).forEach(
(type) =>
resources[type as keyof Resources]?.forEach((path: string) =>
resourcePaths.push(path),
),
);

//copy resources
copyFiles(args.input, tmpOutputFolder, resourcePaths);
}

const yamlFiles: string[] = walkSync(args.input, {
globs: ['**/*.yaml'],
directories: false,
includeBasePath: true,
ignore: ['**/toc.yaml', resolve(pathToRedirects)],
});

yamlFiles.forEach((yamlFile) => {
const content = load(readFileSync(yamlFile, 'utf8'));

if (!Object.prototype.hasOwnProperty.call(content, 'blocks')) {
return;
}

const contentLinks = findAllValuesByKeys(content, LINK_KEYS_PAGE_CONSTRUCTOR_CONFIG);
const localMediaLinks = contentLinks.filter(
(link) =>
new RegExp(/^\S.*\.(svg|png|gif|jpg|jpeg|bmp|webp|ico)$/gm).test(
link,
) && isLocalUrl(link),
);

copyFiles(
args.input,
tmpOutputFolder,
localMediaLinks.filter((link) => checkPathExists(link, yamlFile)),
);
});

break;
}
}
processAssets({
args,
outputFormat,
outputBundlePath,
tmpOutputFolder,
userOutputFolder,
});

// Copy all generated files to user' output folder
shell.cp(
Expand Down
91 changes: 87 additions & 4 deletions src/steps/processAssets.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
import walkSync from 'walk-sync';
import {load} from 'js-yaml';
import {readFileSync} from 'fs';
import shell from 'shelljs';
import {join, resolve} from 'path';

import {ArgvService} from '../services';
import {copyFiles} from '../utils';
import {checkPathExists, copyFiles, findAllValuesByKeys} from '../utils';

import {ASSETS_FOLDER, RTL_LANGS} from '../constants';
import {LINK_KEYS_PAGE_CONSTRUCTOR_CONFIG} from '@diplodoc/client/ssr';
import {isLocalUrl} from '@diplodoc/transform/lib/utils';

import {
ASSETS_FOLDER,
LINT_CONFIG_FILENAME,
REDIRECTS_FILENAME,
RTL_LANGS,
YFM_CONFIG_FILENAME,
} from '../constants';
import {Resources} from '../models';

/**
* Processes assets files (everything except .yaml and .md files)
* Processes assets files (everything except .md files)
* @param {Array} args
* @param {string} outputBundlePath
* @param {string} outputFormat
* @param {string} tmpOutputFolder
* @return {void}
*/
export function processAssets(outputBundlePath: string) {
export function processAssets({args, outputFormat, outputBundlePath, tmpOutputFolder}) {
switch (outputFormat) {
case 'html':
processAssetsHtmlRun({outputBundlePath});
break;
case 'md':
processAssetsMdRun({args, tmpOutputFolder});
break;
}
}

function processAssetsHtmlRun({outputBundlePath}) {
const {input: inputFolderPath, output: outputFolderPath, langs} = ArgvService.getConfig();

const documentationAssetFilePath: string[] = walkSync(inputFolderPath, {
Expand All @@ -31,6 +59,61 @@ export function processAssets(outputBundlePath: string) {
copyFiles(ASSETS_FOLDER, outputBundlePath, bundleAssetFilePath);
}

function processAssetsMdRun({args, tmpOutputFolder}) {
const {allowCustomResources, resources} = ArgvService.getConfig();

const pathToConfig = args.config || join(args.input, YFM_CONFIG_FILENAME);
const pathToRedirects = join(args.input, REDIRECTS_FILENAME);
const pathToLintConfig = join(args.input, LINT_CONFIG_FILENAME);

shell.cp(resolve(pathToConfig), tmpOutputFolder);
shell.cp(resolve(pathToRedirects), tmpOutputFolder);
shell.cp(resolve(pathToLintConfig), tmpOutputFolder);

if (resources && allowCustomResources) {
const resourcePaths: string[] = [];

// collect paths of all resources
Object.keys(resources).forEach(
(type) =>
resources[type as keyof Resources]?.forEach((path: string) =>
resourcePaths.push(path),
),
);

//copy resources
copyFiles(args.input, tmpOutputFolder, resourcePaths);
}

const yamlFiles: string[] = walkSync(args.input, {
globs: ['**/*.yaml'],
directories: false,
includeBasePath: true,
ignore: ['**/toc.yaml', resolve(pathToRedirects)],
});

yamlFiles.forEach((yamlFile) => {
const content = load(readFileSync(yamlFile, 'utf8'));

if (!Object.prototype.hasOwnProperty.call(content, 'blocks')) {
return;
}

const contentLinks = findAllValuesByKeys(content, LINK_KEYS_PAGE_CONSTRUCTOR_CONFIG);
const localMediaLinks = contentLinks.filter(
(link) =>
new RegExp(/^\S.*\.(svg|png|gif|jpg|jpeg|bmp|webp|ico)$/gm).test(link) &&
isLocalUrl(link),
);

copyFiles(
args.input,
tmpOutputFolder,
localMediaLinks.filter((link) => checkPathExists(link, yamlFile)),
);
});
}

function hasIntersection(array1, array2) {
const set1 = new Set(array1);
return array2.some((element) => set1.has(element));
Expand Down

0 comments on commit 1e3bd4e

Please sign in to comment.