-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathindex.js
32 lines (29 loc) · 939 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const path = require('path')
const spawn = require('cross-spawn')
module.exports = (options = {}, context) => ({
extendPageData ($page) {
const { transformer } = options
const timestamp = getGitLastUpdatedTimeStamp($page._filePath)
const $lang = $page._computed.$lang
if (timestamp) {
const lastUpdated = typeof transformer === 'function'
? transformer(timestamp, $lang)
: defaultTransformer(timestamp, $lang)
$page.lastUpdated = lastUpdated
}
}
})
function defaultTransformer (timestamp, lang) {
return new Date(timestamp).toLocaleString(lang)
}
function getGitLastUpdatedTimeStamp (filePath) {
let lastUpdated
try {
lastUpdated = parseInt(spawn.sync(
'git',
['log', '-1', '--format=%at', path.basename(filePath)],
{ cwd: path.dirname(filePath) }
).stdout.toString('utf-8')) * 1000
} catch (e) { /* do not handle for now */ }
return lastUpdated
}