Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: copy yaml assets #707

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@ -180,7 +173,7 @@
);
}

async function handler(args: Arguments<any>) {

Check warning on line 176 in src/cmd/build/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
const userOutputFolder = resolve(args.output);
const tmpInputFolder = resolve(args.output, TMP_INPUT_FOLDER);
const tmpOutputFolder = resolve(args.output, TMP_OUTPUT_FOLDER);
Expand All @@ -192,7 +185,7 @@
input: tmpInputFolder,
output: tmpOutputFolder,
});
Includers.init([OpenapiIncluder as any]);

Check warning on line 188 in src/cmd/build/index.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type

const {
output: outputFolderPath,
Expand All @@ -201,8 +194,6 @@
lintDisabled,
buildDisabled,
addMapFile,
allowCustomResources,
resources,
} = ArgvService.getConfig();

preparingTemporaryFolders(userOutputFolder);
Expand All @@ -215,9 +206,6 @@
}

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 @@

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
Loading