Skip to content

Commit

Permalink
feat: resolve vcsPath correctly when sourcePath was supplied befo…
Browse files Browse the repository at this point in the history
…rehand, refactor metadata generation procedures
  • Loading branch information
brotheroftux authored and 3y3 committed Jul 11, 2024
1 parent ad1b879 commit 568f43b
Show file tree
Hide file tree
Showing 23 changed files with 833 additions and 783 deletions.
1 change: 0 additions & 1 deletion src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ export interface MetaDataOptions {
isContributorsEnabled?: boolean;
vcsConnector?: VCSConnector;
addSystemMeta?: boolean;
addSourcePath?: boolean;
resources?: Resources;
shouldAlwaysAddVCSPath?: boolean;
}
Expand Down
18 changes: 10 additions & 8 deletions src/resolvers/md2md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,32 @@ import {ArgvService, PluginService} from '../services';
import {getVarsPerFile, logger} from '../utils';
import {PluginOptions, ResolveMd2MdOptions} from '../models';
import {PROCESSING_FINISHED} from '../constants';
import {getContentWithUpdatedMetadata} from '../services/metadata';
import {ChangelogItem} from '@diplodoc/transform/lib/plugins/changelog/types';
import {enrichWithFrontMatter} from '../services/metadata';

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

const varsPreset = getVarsPerFile(inputPath);
const vars = getVarsPerFile(inputPath);

const content = await getContentWithUpdatedMetadata(
readFileSync(resolvedInputPath, 'utf8'),
const content = await enrichWithFrontMatter({
fileContent: readFileSync(resolvedInputPath, 'utf8'),
metadataOptions,
varsPreset.__system as unknown,
varsPreset.__metadata,
);
resolvedFrontMatterVars: {
systemVars: vars.__system as unknown,
metadataVars: vars.__metadata,
},
});

const {result, changelogs} = transformMd2Md(content, {
path: resolvedInputPath,
destPath: outputPath,
root: resolve(input),
destRoot: resolve(output),
collectOfPlugins: PluginService.getCollectOfPlugins(),
vars: varsPreset,
vars: vars,
log,
copyFile,
});
Expand Down
33 changes: 15 additions & 18 deletions src/services/authors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {replaceDoubleToSingleQuotes} from '../utils';
import {Contributor} from '../models';
import {VCSConnector} from '../vcs-connector/connector-models';

async function updateAuthorMetadataStringByAuthorLogin(
authorLogin: string,
async function updateAuthorMetadataStringByAuthorData(
authorLogin: string | object,
vcsConnector?: VCSConnector,
): Promise<string> {
): Promise<Contributor | null> {
if (!vcsConnector) {
return '';
return null;
}

const user = await getAuthorDetails(vcsConnector, authorLogin);
Expand All @@ -15,52 +15,49 @@ async function updateAuthorMetadataStringByAuthorLogin(
return user;
}

return '';
return null;
}

async function updateAuthorMetadataStringByFilePath(
filePath: string,
vcsConnector?: VCSConnector,
): Promise<string> {
): Promise<Contributor | null> {
if (!vcsConnector) {
return '';
return null;
}

const user = vcsConnector.getExternalAuthorByPath(filePath);

if (user) {
const author = replaceDoubleToSingleQuotes(JSON.stringify(user));
return author;
return user;
}

return '';
return null;
}

async function getAuthorDetails(
vcsConnector: VCSConnector,
author: string | object,
): Promise<string | null> {
): Promise<Contributor | null> {
if (typeof author === 'object') {
// Avoiding problems when adding to html markup
return replaceDoubleToSingleQuotes(JSON.stringify(author));
return author as Contributor;
}

try {
JSON.parse(author);
return replaceDoubleToSingleQuotes(author);
return JSON.parse(author);
} catch {
const user = await vcsConnector.getUserByLogin(author);

if (user) {
return replaceDoubleToSingleQuotes(JSON.stringify(user));
return user;
}

return null;
}
}

export {
updateAuthorMetadataStringByAuthorLogin,
updateAuthorMetadataStringByAuthorData as updateAuthorMetadataStringByAuthorLogin,
updateAuthorMetadataStringByFilePath,
getAuthorDetails,
};
21 changes: 4 additions & 17 deletions src/services/contributors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {readFile} from 'fs/promises';
import {dirname, join} from 'path';

import {replaceDoubleToSingleQuotes} from '../utils';
import {REGEXP_INCLUDE_CONTENTS, REGEXP_INCLUDE_FILE_PATH} from '../constants';
import {Contributor, Contributors} from '../models';
import {FileContributors, VCSConnector} from '../vcs-connector/connector-models';
Expand All @@ -12,19 +10,10 @@ export interface ContributorsServiceFileData {
fileContent: string;
}

async function getFileContributorsMetadata(
export async function getFileContributors(
fileData: ContributorsServiceFileData,
vcsConnector: VCSConnector,
): Promise<string> {
const contributors = await getFileContributorsString(fileData, vcsConnector);

return `contributors: ${contributors}`;
}

async function getFileContributorsString(
fileData: ContributorsServiceFileData,
vcsConnector: VCSConnector,
): Promise<string> {
): Promise<Contributor[]> {
const {resolvedFilePath, inputFolderPathLength} = fileData;

const relativeFilePath = resolvedFilePath.substring(inputFolderPathLength);
Expand All @@ -46,7 +35,7 @@ async function getFileContributorsString(
fileContributorsWithContributorsIncludedFiles,
).map(([, contributor]) => contributor);

return replaceDoubleToSingleQuotes(JSON.stringify(contributorsArray));
return contributorsArray;
}

async function getContributorsForNestedFiles(
Expand Down Expand Up @@ -123,7 +112,7 @@ function getRelativeIncludeFilePaths(
return relativeIncludeFilePaths;
}

async function getFileIncludes(fileData: ContributorsServiceFileData) {
export async function getFileIncludes(fileData: ContributorsServiceFileData) {
const {fileContent, inputFolderPathLength} = fileData;

const results = new Set<string>();
Expand Down Expand Up @@ -161,5 +150,3 @@ async function getFileIncludes(fileData: ContributorsServiceFileData) {

return Array.from(results.values());
}

export {getFileContributorsMetadata, getFileContributorsString, getFileIncludes};
Loading

0 comments on commit 568f43b

Please sign in to comment.