Skip to content

Commit dff1b43

Browse files
committed
Merge branch 'igorshubovych-os-type'
2 parents 940475a + 33cda88 commit dff1b43

File tree

4 files changed

+122
-18
lines changed

4 files changed

+122
-18
lines changed

bin/tldr

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ program
1515
.option('-r, --random', 'Show a random command')
1616
.option('-e, --random-example', 'Show a random example')
1717
.option('-f, --render [file]', 'Render a specific markdown [file]')
18+
.option('-o, --os [type]', 'Override the operating system [linux, osx, sunos]')
1819
//
1920
// CACHE MANAGEMENT
2021
//
2122
.option('-u, --update', 'Update the local cache')
22-
.option('-c, --clear-cache', 'Clear the local cache')
23-
//
24-
// DEPRECATED SOON
25-
//
26-
.option('-o, --os [type]', 'Override the operating system [linux, osx, sunos]');
23+
.option('-c, --clear-cache', 'Clear the local cache');
2724

2825
program.on('--help', function() {
2926
console.log('Examples');
@@ -46,6 +43,14 @@ program.on('--help', function() {
4643

4744
program.parse(process.argv);
4845

46+
if (program.os) {
47+
var config = require('../lib/config');
48+
var platform = require('../lib/platform');
49+
if (platform.isSupported(program.os)) {
50+
config.get().platform = program.os;
51+
}
52+
}
53+
4954
if (program.list) {
5055
tldr.list();
5156
} else if (program.random) {

lib/config.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ var fs = require('fs');
33
var path = require('path');
44
var extend = require('deep-extend');
55

6-
var DEFAULT = path.join(__dirname, '..', 'config.json');
7-
var CUSTOM = path.join(process.env['HOME'], '.tldrrc');
8-
96
var config = null;
107

118
exports.reset = function() {
@@ -18,6 +15,8 @@ exports.get = function() {
1815
};
1916

2017
function load() {
18+
var DEFAULT = path.join(__dirname, '..', 'config.json');
19+
var CUSTOM = path.join(process.env['HOME'], '.tldrrc');
2120
var defaultConfig = JSON.parse(fs.readFileSync(DEFAULT));
2221
defaultConfig.cache = path.join(process.env['HOME'], '.tldr');
2322
defaultConfig.proxy = process.env.HTTP_PROXY || process.env.http_proxy;
@@ -32,14 +31,23 @@ function load() {
3231

3332
var merged = extend(defaultConfig, customConfig);
3433
var errors = _.map(merged.colors, validateColor);
34+
errors.push(validatePlatform(merged.platform));
35+
errors = _.compact(errors);
3536

36-
if (_.compact(errors).length === 0) {
37+
if (errors.length === 0) {
3738
return merged;
3839
} else {
3940
throw new Error('Error in .tldrrc configuration:\n' + errors.join('\n'));
4041
}
4142
}
4243

44+
function validatePlatform(os) {
45+
var platform = require('./platform');
46+
if (os && !platform.isSupported(os)) {
47+
return 'Unsupported platform : ' + os;
48+
}
49+
}
50+
4351
function validateColor(colorName, key) {
4452
var ansi = ['white','black','blue','cyan','green','magenta','red','yellow'];
4553
if (ansi.indexOf(colorName) === -1) {

lib/platform.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
11
var os = require('os');
2+
var config = require('./config');
23

34
var folders = {
4-
darwin: 'osx',
5-
linux : 'linux',
6-
sunos : 'sunos'
5+
'osx': 'osx',
6+
'darwin': 'osx',
7+
'linux': 'linux',
8+
'sunos': 'sunos'
79
};
810

9-
exports.resolve = function(command) {
10-
return [
11-
specific(command, folders[os.platform()]),
12-
specific(command, 'common')
13-
];
14-
};
11+
function isSupported(platform) {
12+
return folders.hasOwnProperty(platform);
13+
}
14+
15+
function getPreferredPlatform() {
16+
var platform = config.get().platform;
17+
if (isSupported(platform)) {
18+
return platform;
19+
}
20+
return os.platform();
21+
}
1522

1623
function specific(command, os) {
1724
return 'pages/' + os + '/' + command + '.md';
1825
}
26+
27+
function resolve(command) {
28+
var platform = getPreferredPlatform();
29+
return [
30+
specific(command, folders[platform]),
31+
specific(command, 'common')
32+
];
33+
}
34+
35+
module.exports = {
36+
isSupported: isSupported,
37+
getPreferredPlatform: getPreferredPlatform,
38+
resolve: resolve
39+
};

test/platform.spec.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var os = require('os');
2+
var config = require('../lib/config');
3+
var sinon = require('sinon');
4+
var should = require('should');
5+
var platform = require('../lib/platform')
6+
7+
describe('Platform', function() {
8+
9+
describe('getPreferredPlatform', function() {
10+
beforeEach(function() {
11+
sinon.stub(os, 'platform');
12+
sinon.stub(config, 'get');
13+
});
14+
15+
afterEach(function() {
16+
os.platform.restore();
17+
config.get.restore();
18+
});
19+
20+
it('should return the running platform with no configuration', function() {
21+
os.platform.onCall(0).returns('darwin');
22+
config.get.onCall(0).returns({});
23+
platform.getPreferredPlatform().should.eql('darwin');
24+
});
25+
26+
it('should overwrite the running platform if configured', function() {
27+
os.platform.onCall(0).returns('darwin');
28+
config.get.onCall(0).returns({
29+
platform: 'linux'
30+
});
31+
platform.getPreferredPlatform().should.eql('linux');
32+
});
33+
34+
it('should return current system platform if configuration is wrong', function() {
35+
os.platform.onCall(0).returns('darwin');
36+
config.get.onCall(0).returns({
37+
platform: 'there_is_no_such_platform'
38+
});
39+
platform.getPreferredPlatform().should.eql('darwin');
40+
});
41+
});
42+
43+
describe('isSupported', function() {
44+
it('should tell that Linux, OSX and SunOS are supported', function() {
45+
platform.isSupported('osx').should.eql(true);
46+
platform.isSupported('linux').should.eql(true);
47+
platform.isSupported('sunos').should.eql(true);
48+
platform.isSupported('windows').should.eql(false); // Yet
49+
platform.isSupported('ios').should.eql(false);
50+
});
51+
});
52+
53+
describe('resolve', function() {
54+
beforeEach(function() {
55+
sinon.stub(os, 'platform');
56+
});
57+
58+
afterEach(function() {
59+
os.platform.restore();
60+
});
61+
62+
it('should resolve tar command with specific OS and common folder', function() {
63+
os.platform.onCall(0).returns('linux');
64+
platform.resolve('tar').should.eql([
65+
'pages/linux/tar.md',
66+
'pages/common/tar.md',
67+
]);
68+
});
69+
});
70+
});

0 commit comments

Comments
 (0)