From 2a4e37f4ca483d50e313ea61dbefe82948b301b8 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Sun, 18 Nov 2018 04:47:09 +0800 Subject: [PATCH] chore($last-updated): ensure build process success if not in git repo. (close: #1007) During some CI context, the git command would not exist, this change just ensure that the build process does not crash. --- packages/@vuepress/plugin-last-updated/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/@vuepress/plugin-last-updated/index.js b/packages/@vuepress/plugin-last-updated/index.js index a4e8b137ad..d6fa880b09 100644 --- a/packages/@vuepress/plugin-last-updated/index.js +++ b/packages/@vuepress/plugin-last-updated/index.js @@ -4,11 +4,17 @@ module.exports = (options = {}, context) => ({ extendPageData ($page) { const { transformer } = options const timestamp = getGitLastUpdatedTimeStamp($page._filePath) - const lastUpdated = typeof transformer === 'function' ? transformer(timestamp) : timestamp - $page.lastUpdated = lastUpdated + if (timestamp) { + const lastUpdated = typeof transformer === 'function' ? transformer(timestamp) : timestamp + $page.lastUpdated = lastUpdated + } } }) function getGitLastUpdatedTimeStamp (filePath) { - return parseInt(spawn.sync('git', ['log', '-1', '--format=%ct', filePath]).stdout.toString('utf-8')) * 1000 + let lastUpdated + try { + lastUpdated = parseInt(spawn.sync('git', ['log', '-1', '--format=%ct', filePath]).stdout.toString('utf-8')) * 1000 + } catch (e) { /* do not handle for now */ } + return lastUpdated }