Skip to content

Commit

Permalink
feat: copy yaml assets (#707)
Browse files Browse the repository at this point in the history
* feat: copy yaml assets

* lint

* use PC links array

* refactor
  • Loading branch information
martyanovandrey authored Mar 29, 2024
1 parent db8f988 commit 0d3293c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 51 deletions.
60 changes: 14 additions & 46 deletions src/cmd/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import glob from 'glob';
import {Arguments, Argv} from 'yargs';
import {
BUNDLE_FOLDER,
LINT_CONFIG_FILENAME,
REDIRECTS_FILENAME,
Stage,
TMP_INPUT_FOLDER,
TMP_OUTPUT_FOLDER,
YFM_CONFIG_FILENAME,
} from '../../constants';
import {argvValidator} from '../../validator';
import {join, resolve} from 'path';
import {ArgvService, Includers} from '../../services';
import shell from 'shelljs';

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

import {BUNDLE_FOLDER, Stage, TMP_INPUT_FOLDER, TMP_OUTPUT_FOLDER} from '../../constants';
import {argvValidator} from '../../validator';
import {ArgvService, Includers} from '../../services';
import {
initLinterWorkers,
processAssets,
Expand All @@ -22,11 +18,8 @@ import {
processServiceFiles,
} from '../../steps';
import {prepareMapFile} from '../../steps/processMapFile';
import shell from 'shelljs';
import {Resources} from '../../models';
import {copyFiles, logger} from '../../utils';
import {upload as publishFilesToS3} from '../publish/upload';
import glob from 'glob';

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

preparingTemporaryFolders(userOutputFolder);
Expand All @@ -215,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 @@ -233,33 +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);
}

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} 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);
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
2 changes: 1 addition & 1 deletion src/utils/markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export function replaceDoubleToSingleQuotes(str: string): string {
export function findAllValuesByKeys(obj, keysToFind) {
return flatMapDeep(obj, (value, key) => {
if (
keysToFind.includes(key) &&
keysToFind?.includes(key) &&
(isString(value) || (isArray(value) && value.every(isString)))
) {
return [value];
Expand Down

0 comments on commit 0d3293c

Please sign in to comment.