Skip to content

Commit

Permalink
feat: Make last-updated plugin extendPageData function asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
kefranabg committed Feb 9, 2020
1 parent 59b20bb commit e86e470
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/@vuepress/plugin-last-updated/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const path = require('path')
const spawn = require('cross-spawn')

module.exports = (options = {}, context) => ({
extendPageData ($page) {
async extendPageData ($page) {
const { transformer } = options
const timestamp = getGitLastUpdatedTimeStamp($page._filePath)
const timestamp = await getGitLastUpdatedTimeStamp($page._filePath)
const $lang = $page._computed.$lang
if (timestamp) {
const lastUpdated = typeof transformer === 'function'
Expand All @@ -19,14 +19,16 @@ function defaultTransformer (timestamp, lang) {
return new Date(timestamp).toLocaleString(lang)
}

function getGitLastUpdatedTimeStamp (filePath) {
async function getGitLastUpdatedTimeStamp (filePath) {
let lastUpdated
try {
lastUpdated = parseInt(spawn.sync(
const commandResult = await spawn(
'git',
['log', '-1', '--format=%at', path.basename(filePath)],
{ cwd: path.dirname(filePath) }
).stdout.toString('utf-8')) * 1000
)
const formattedResult = commandResult.stdout.toString('utf-8')
lastUpdated = parseInt(formattedResult) * 1000
} catch (e) { /* do not handle for now */ }
return lastUpdated
}

0 comments on commit e86e470

Please sign in to comment.