-
Notifications
You must be signed in to change notification settings - Fork 38
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: Add updatedAt field in metadata #628
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,16 @@ import { | |
updateAuthorMetadataStringByAuthorLogin, | ||
updateAuthorMetadataStringByFilePath, | ||
} from './authors'; | ||
import {getFileContributorsMetadata, getFileContributorsString} from './contributors'; | ||
import { | ||
getFileContributorsMetadata, | ||
getFileContributorsString, | ||
getFileIncludes, | ||
} from './contributors'; | ||
import {isObject} from './utils'; | ||
import {сarriage} from '../utils'; | ||
import {REGEXP_AUTHOR, metadataBorder} from '../constants'; | ||
import {dirname, relative, resolve} from 'path'; | ||
import {ArgvService} from './index'; | ||
import {ArgvService, TocService} from './index'; | ||
|
||
async function getContentWithUpdatedMetadata( | ||
fileContent: string, | ||
|
@@ -105,6 +109,11 @@ async function getContentWithUpdatedDynamicMetadata( | |
newMetadatas.push(contributorsMetaData); | ||
} | ||
|
||
const mtimeMetadata = await getModifiedTimeMetadataString(options, fileContent); | ||
if (mtimeMetadata) { | ||
newMetadatas.push(mtimeMetadata); | ||
} | ||
|
||
let authorMetadata = ''; | ||
if (fileMetadata) { | ||
const matchAuthor = fileMetadata.match(REGEXP_AUTHOR); | ||
|
@@ -188,6 +197,35 @@ async function getContributorsMetadataString( | |
return undefined; | ||
} | ||
|
||
async function getModifiedTimeMetadataString(options: MetaDataOptions, fileContent: string) { | ||
const {isContributorsEnabled, vcsConnector, fileData} = options; | ||
|
||
const {tmpInputFilePath, inputFolderPathLength} = fileData; | ||
|
||
const relativeFilePath = tmpInputFilePath.substring(inputFolderPathLength + 1); | ||
|
||
if (isContributorsEnabled && vcsConnector) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем основная логика под if, а в конце return undefined? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. да чет там было уже в функции выше, переделаю |
||
const includedFiles = await getFileIncludes({...fileData, fileContent}); | ||
includedFiles.push(relativeFilePath); | ||
|
||
const tocCopyFileMap = TocService.getCopyFileMap(); | ||
|
||
const mtimeList = includedFiles | ||
.map((path) => { | ||
const mappedPath = tocCopyFileMap.get(path) || path; | ||
return vcsConnector.getModifiedTimeByPath(mappedPath); | ||
}) | ||
.filter((v) => typeof v === 'number') as number[]; | ||
|
||
const mtime = mtimeList.length ? Math.max(...mtimeList) : undefined; | ||
if (mtime) { | ||
return `updatedAt: ${new Date(mtime * 1000).toISOString()}`; | ||
} | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
function getUpdatedMetadataString(newMetadatas: string[], defaultMetadata = ''): string { | ||
const newMetadata = newMetadatas.join(сarriage) + (newMetadatas.length ? сarriage : ''); | ||
const preparedDefaultMetadata = defaultMetadata.trimRight(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,9 +33,10 @@ const authorByPath: Map<string, Contributor | null> = new Map(); | |
const contributorsByPath: Map<string, FileContributors> = new Map(); | ||
const contributorsData: Map<string, Contributor | null> = new Map(); | ||
const loginUserMap: Map<string, Contributor | null> = new Map(); | ||
const pathMTime = new Map<string, number>(); | ||
|
||
async function getGitHubVCSConnector(): Promise<VCSConnector | undefined> { | ||
const {contributors} = ArgvService.getConfig(); | ||
const {contributors, rootInput} = ArgvService.getConfig(); | ||
|
||
const httpClientByToken = getHttpClientByToken(); | ||
if (!httpClientByToken) { | ||
|
@@ -49,6 +50,7 @@ async function getGitHubVCSConnector(): Promise<VCSConnector | undefined> { | |
authorByPath.get(path) ?? null; | ||
|
||
if (contributors) { | ||
await getFilesMTime(rootInput, pathMTime); | ||
await getAllContributorsTocFiles(httpClientByToken); | ||
addNestedContributorsForPath = (path: string, nestedContributors: Contributors) => | ||
addNestedContributorsForPathFunction(path, nestedContributors); | ||
|
@@ -60,6 +62,7 @@ async function getGitHubVCSConnector(): Promise<VCSConnector | undefined> { | |
addNestedContributorsForPath, | ||
getContributorsByPath, | ||
getUserByLogin: (login: string) => getUserByLogin(httpClientByToken, login), | ||
getModifiedTimeByPath: (filename: string) => pathMTime.get(filename), | ||
}; | ||
} | ||
|
||
|
@@ -364,4 +367,45 @@ function shouldAuthorBeIgnored({email, name}: ShouldAuthorBeIgnoredArgs) { | |
return false; | ||
} | ||
|
||
async function getFilesMTime(repoDir: string, pathMTime: Map<string, number>) { | ||
const timeFiles = await simpleGit({ | ||
baseDir: repoDir, | ||
}).raw( | ||
'log', | ||
'--reverse', | ||
'--before=now', | ||
'--diff-filter=ADMR', | ||
'--pretty=format:%ct', | ||
'--name-status', | ||
); | ||
|
||
const parts = timeFiles.split(/\n\n/); | ||
while (parts.length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Array.forEach |
||
const part = parts.shift(); | ||
if (!part) return; | ||
const lines = part.trim().split(/\n/); | ||
const committerDate = lines.shift(); | ||
const unixtime = Number(committerDate); | ||
for (let i = 0, len = lines.length; i < len; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Array.forEach There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ну.. можно и так, хотя for мне больше нравится |
||
const line = lines[i]; | ||
const [status, from, to] = line.split(/\t/); | ||
switch (status[0]) { | ||
case 'R': { | ||
pathMTime.delete(from); | ||
pathMTime.set(to, unixtime); | ||
break; | ||
} | ||
case 'D': { | ||
pathMTime.delete(from); | ||
break; | ||
} | ||
default: { | ||
pathMTime.set(from, unixtime); | ||
} | ||
} | ||
} | ||
} | ||
return pathMTime; | ||
} | ||
|
||
export default getGitHubVCSConnector; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Почему отсутствие файла в этом месте норма?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Потому что мы собираем инклуды без учета условий из var переменных, как и в случае с авторами, это допущение