Skip to content

Commit

Permalink
feat(version): show distro version
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Aug 23, 2020
1 parent dc7010e commit 6153d91
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
21 changes: 17 additions & 4 deletions lib/console/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,37 @@
const os = require('os');
const pkg = require('../../package.json');
const Promise = require('bluebird');
const { spawn } = require('hexo-util');

function versionConsole(args) {
const versions = process.versions;
async function versionConsole(args) {
const { versions, platform } = process;
const keys = Object.keys(versions);

if (this.version) {
console.log('hexo:', this.version);
}

console.log('hexo-cli:', pkg.version);
console.log('os:', os.type(), os.release(), os.platform(), os.arch());

let osInfo = '';
if (platform === 'darwin') osInfo = await spawn('sw_vers', '-productVersion');
else if (platform === 'linux') {
const v = await spawn('cat', '/etc/os-release');
const distro = (v || '').match(/NAME="(.+)"/);
const versionInfo = (v || '').match(/VERSION="(.+)"/) || ['', ''];
const versionStr = versionInfo !== null ? versionInfo[1] : '';
osInfo = `${distro[1]} ${versionStr}`.trim() || '';
}

osInfo = `${os.platform()} ${os.release()} ${osInfo}`;
console.log('os:', osInfo);

for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
console.log('%s: %s', key, versions[key]);
}

return Promise.resolve();
await Promise.resolve();
}

module.exports = versionConsole;
23 changes: 17 additions & 6 deletions test/scripts/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
require('chai').should();
const Context = require('../../lib/context');
const sinon = require('sinon');
const { arch, platform, release, type } = require('os');
const { platform, release } = require('os');
const { format } = require('util');
const cliVersion = require('../../package.json').version;
const rewire = require('rewire');
const { spawn } = require('hexo-util')

function getConsoleLog({ args }) {
return args.map(arr => format.apply(null, arr)).join('\n');
Expand All @@ -26,16 +27,26 @@ describe('version', () => {
})(async () => {
await versionModule.call(hexo, {_: []});
const output = getConsoleLog(spy);
const expected = [
`hexo-cli: ${cliVersion}`,
`os: ${type()} ${release()} ${platform()} ${arch()}`
];
const expected = [];

Object.keys(process.versions).forEach(key => {
expected.push(`${key}: ${process.versions[key]}`);
});

output.should.eql(expected.join('\n'));
output.should.contain(`hexo-cli: ${cliVersion}`);
output.should.contain(`os: ${platform()} ${release()}`);
output.should.contain(expected.join('\n'));

if (process.env.CI === 'true') {
if (process.platform === 'darwin') {
const osInfo = await spawn('sw_vers', '-productVersion')
output.should.contain(`os: ${platform()} ${release()} ${osInfo}`);
} else if (process.platform === 'linux') {
const v = await spawn('cat', '/etc/os-release');
const distro = (v || '').match(/NAME="(.+)"/);
output.should.contain(`os: ${platform()} ${release()} ${distro[1]}`);
}
}
});
});

Expand Down

0 comments on commit 6153d91

Please sign in to comment.