Skip to content

Commit

Permalink
add assets, async fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Feverqwe committed Sep 20, 2023
1 parent 1e36404 commit c37411e
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 72 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export const GETTING_ALL_CONTRIBUTORS = 'Getting all contributors.';
export const ALL_CONTRIBUTORS_RECEIVED = 'All contributors received.';
export const getMsgСonfigurationMustBeProvided =
(repo: string) => `Сonfiguration must be provided for ${repo} like env variables or in .yfm file`;
export const CACHE_HIT = 'Cache hit:';
export const LINT_CACHE_HIT = 'Lint cache hit:';

export const FIRST_COMMIT_FROM_ROBOT_IN_GITHUB = '2dce14271359cd20d7e874956d604de087560cf4';

Expand Down
4 changes: 3 additions & 1 deletion src/resolvers/lintPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import {readFileSync} from 'fs';
import {bold} from 'chalk';

import {ArgvService, PluginService} from '../services';
import {getVarsPerFileWithHash, getVarsPerRelativeFile} from '../utils';
import {getVarsPerFileWithHash, getVarsPerRelativeFile, logger} from '../utils';
import {liquidMd2Html} from './md2html';
import {liquidMd2Md} from './md2md';
import {cacheServiceLint} from '../services/cache';
import PluginEnvApi from '../utils/pluginEnvApi';
import {checkLogWithoutProblems, getLogState} from '../services/utils';
import {LINT_CACHE_HIT} from '../constants';

interface FileTransformOptions {
path: string;
Expand Down Expand Up @@ -76,6 +77,7 @@ function MdFileLinter(content: string, lintOptions: FileTransformOptions): void

const cachedFile = cacheServiceLint.checkFile(cacheKey);
if (cachedFile) {
logger.info(filePath, LINT_CACHE_HIT);
return;
}

Expand Down
5 changes: 4 additions & 1 deletion src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getVarsPerRelativeFile,
getVarsPerFileWithHash,
} from '../utils';
import {PROCESSING_FINISHED, Lang} from '../constants';
import {PROCESSING_FINISHED, Lang, CACHE_HIT} from '../constants';
import {getAssetsPublicPath, getUpdatedMetadata} from '../services/metadata';
import {MarkdownItPluginCb} from '@doc-tools/transform/lib/plugins/typings';
import PluginEnvApi from '../utils/pluginEnvApi';
Expand Down Expand Up @@ -141,6 +141,7 @@ async function MdFileTransformer(content: string, transformOptions: FileTransfor

const cachedFile = await cacheServiceMdToHtml.checkFileAsync(cacheKey);
if (cachedFile) {
logger.info(filePath, CACHE_HIT);
await cachedFile.extractCacheAsync();
return cachedFile.getResult<Output>();
}
Expand All @@ -165,6 +166,8 @@ async function MdFileTransformer(content: string, transformOptions: FileTransfor
envApi,
});

await envApi.executeActionsAsync();

const logIsOk = checkLogWithoutProblems(log, logState);
if (logIsOk) {
cacheFile.setResult(result);
Expand Down
39 changes: 22 additions & 17 deletions src/resolvers/md2md.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import {existsSync, readFileSync, writeFileSync} from 'fs';
import {dirname, resolve, join, basename, extname, relative} from 'path';
import shell from 'shelljs';
import log, {LogLevels} from '@doc-tools/transform/lib/log';
import liquid from '@doc-tools/transform/lib/liquid';

import {ArgvService, PluginService} from '../services';
import {logger, getVarsPerFileWithHash} from '../utils';
import {logger, getVarsPerFileWithHash, fileExists} from '../utils';
import {PluginOptions, ResolveMd2MdOptions} from '../models';
import {PROCESSING_FINISHED} from '../constants';
import {CACHE_HIT, PROCESSING_FINISHED} from '../constants';
import {getContentWithUpdatedMetadata} from '../services/metadata';
import {ChangelogItem} from '@doc-tools/transform/lib/plugins/changelog/types';
import {cacheServiceBuildMd} from '../services/cache';
import PluginEnvApi from '../utils/pluginEnvApi';
import {checkLogWithoutProblems, getLogState} from '../services/utils';
import * as fs from 'fs';

export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void> {
const {inputPath, outputPath, metadata} = options;
const {input, output} = ArgvService.getConfig();
const resolvedInputPath = resolve(input, inputPath);
const {vars, varsHashList} = getVarsPerFileWithHash(inputPath);

const rawContent = readFileSync(resolvedInputPath, 'utf8');
const rawContent = await fs.promises.readFile(resolvedInputPath, 'utf8');

const cacheKey = cacheServiceBuildMd.getHashKey({filename: inputPath, content: rawContent, varsHashList});

Expand All @@ -29,6 +29,7 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void>

const cachedFile = await cacheServiceBuildMd.checkFileAsync(cacheKey);
if (cachedFile) {
logger.info(inputPath, CACHE_HIT);
await cachedFile.extractCacheAsync();
const results = cachedFile.getResult<{result: string; changelogs: ChangelogItem[]; logs: Record<LogLevels, string[]>}>();
result = results.result;
Expand Down Expand Up @@ -63,20 +64,23 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void>
result = transformResult.result;
changelogs = transformResult.changelogs;

await envApi.executeActionsAsync();

const logIsOk = checkLogWithoutProblems(log, logState);
if (logIsOk) {
cacheFile.setResult(transformResult);
// not async cause race condition
cacheServiceBuildMd.addFile(cacheFile);
await cacheServiceBuildMd.addFileAsync(cacheFile);
}
}

writeFileSync(outputPath, result);
await fs.promises.writeFile(outputPath, result);

if (changelogs?.length) {
const mdFilename = basename(outputPath, extname(outputPath));
const outputDir = dirname(outputPath);
changelogs.forEach((changes, index) => {
for (let index = 0, len = changelogs.length; index < len; index++) {
const changes = changelogs[index];

let changesName;
const changesDate = changes.date as string | undefined;
const changesIdx = changes.index as number | undefined;
Expand All @@ -92,15 +96,16 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void>

const changesPath = join(outputDir, `changes-${changesName}.json`);

if (existsSync(changesPath)) {
const isExists = await fileExists(changesPath);
if (isExists) {
throw new Error(`Changelog ${changesPath} already exists!`);
}

writeFileSync(changesPath, JSON.stringify({
await fs.promises.writeFile(changesPath, JSON.stringify({
...changes,
source: mdFilename,
}));
});
}
}

logger.info(inputPath, PROCESSING_FINISHED);
Expand All @@ -109,25 +114,25 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void>
}

function copyFile(targetPath: string, targetDestPath: string, options?: PluginOptions) {
shell.mkdir('-p', dirname(targetDestPath));

if (options) {
const {envApi} = options;
let sourceIncludeContent: string;
if (envApi) {
sourceIncludeContent = envApi.readFileSync(relative(envApi.root, targetPath), 'utf-8');
sourceIncludeContent = envApi.readFile(relative(envApi.root, targetPath), 'utf-8');
} else {
sourceIncludeContent = readFileSync(targetPath, 'utf8');
sourceIncludeContent = fs.readFileSync(targetPath, 'utf8');
}

const {result} = transformMd2Md(sourceIncludeContent, options);

if (envApi) {
envApi.writeFileSync(relative(envApi.distRoot, targetDestPath), result);
envApi.writeFileAsync(relative(envApi.distRoot, targetDestPath), result);
} else {
writeFileSync(targetDestPath, result);
fs.mkdirSync(dirname(targetDestPath), {recursive: true});
fs.writeFileSync(targetDestPath, result);
}
} else {
fs.mkdirSync(dirname(targetDestPath), {recursive: true});
shell.cp(targetPath, targetDestPath);
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/services/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class CacheService {
try {
const dataJson = fs.readFileSync(filepath, 'utf-8');
const data = JSON.parse(dataJson);
file = CacheFile.from(data, this.disabled);
file = CacheFile.from(data, this.disabled, this.getAssetsDir());
} catch (err) {
return;
}
Expand All @@ -108,7 +108,7 @@ export class CacheService {
try {
const dataJson = await fs.promises.readFile(filepath, 'utf-8');
const data = JSON.parse(dataJson);
file = CacheFile.from(data, this.disabled);
file = CacheFile.from(data, this.disabled, this.getAssetsDir());
} catch (err) {
return;
}
Expand All @@ -117,7 +117,7 @@ export class CacheService {
}

createFile(key: HashKey) {
return new CacheFile(key, this.disabled);
return new CacheFile(key, this.disabled, this.getAssetsDir());
}

addFile(file: CacheFile) {
Expand All @@ -129,6 +129,7 @@ export class CacheService {
fs.mkdirSync(place, {recursive: true});
existsDir.add(place);
}
file.writeAssets();
fs.writeFileSync(filepath, JSON.stringify(file.toJSON()));
}

Expand All @@ -141,7 +142,10 @@ export class CacheService {
await fs.promises.mkdir(place, {recursive: true});
existsDir.add(place);
}
await fs.promises.writeFile(filepath, JSON.stringify(file.toJSON()));
await Promise.all([
file.writeAssetsAsync(),
fs.promises.writeFile(filepath, JSON.stringify(file.toJSON())),
]);
}

getHashKey(props: GetHashKeyProps) {
Expand All @@ -161,6 +165,10 @@ export class CacheService {
private getCacheFilepath(key: string) {
return path.join(this.cacheDir, this.storeName, key.slice(0, 2), key);
}

private getAssetsDir() {
return path.join(this.cacheDir, 'assets');
}
}

export const cacheServiceLint = new CacheService('lint');
Expand Down
Loading

0 comments on commit c37411e

Please sign in to comment.