version.js 1.1 KB

123456789101112131415161718192021222324
  1. const fs = require("fs");
  2. const child_process = require('child_process');
  3. module.exports = (path) => {
  4. try {
  5. const gitHEAD = fs.readFileSync(path + '/.git/HEAD', 'utf-8').trim(); // ref: refs/heads/develop
  6. const ref = gitHEAD.split(': ')[1];// refs/heads/develop
  7. const branch = gitHEAD.split('/')[2];// 环境:develop
  8. const buildVersion = fs.readFileSync(path + '/.git/' + ref, 'utf-8').trim(); // git版本号,例如:6ceb0ab5059d01fd444cf4e78467cc2dd1184a66
  9. const buildUserName = child_process.execSync('git config user.name').toString().trim();
  10. const buildUserMail = child_process.execSync('git config user.email').toString().trim();
  11. const nowDate = new Date();
  12. const buildDate = `${nowDate.getFullYear() + '-' + (nowDate.getMonth() + 1) + '-' + nowDate.getDate() + ' ' + nowDate.getHours() + ':' + nowDate.getMinutes()}`;
  13. return {
  14. branch,
  15. buildVersion,
  16. buildUserName,
  17. buildUserMail,
  18. buildDate
  19. };
  20. } catch (e) {
  21. return {}
  22. }
  23. };