Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple OS #44

Merged
merged 3 commits into from
Jan 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions bin/tldr
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

var util = require('util');
var tldr = require('../lib/tldr');
var argv = require('optimist').argv;

if (process.argv.length != 3) {
if (argv._.length != 1) {
util.log('Usage: tldr <command>');
process.exit(1);
} else {
try {
tldr.get(process.argv[2]);
tldr.get(argv._[0], argv);
} catch (ex) {
util.error(ex);
process.exit(1);
Expand Down
12 changes: 5 additions & 7 deletions lib/cache.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
var fs = require('fs');
var os = require('os');
var path = require('path');
var async = require('async');
var mkdirp = require('mkdirp');
var config = require('./config');

var FOLDER = path.join(os.tmpdir ? os.tmpdir() : os.tmpDir(), 'tldr-cache');
var config = require('./config');

exports.get = function(filename, done) {
var filepath = path.join(FOLDER, filename);
var filepath = path.join(config.local.cacheFolder, filename);
async.waterfall([
function(next) { checkCache(filepath, next); },
function(next) { getContent(filepath, next); }
], done);
};

exports.save = function(filename, contents, done) {
mkdirp(FOLDER, function(err) {
var filepath = path.join(FOLDER, filename);
var filepath = path.join(config.local.cacheFolder, filename);
var dir = path.dirname(filepath);
mkdirp(dir, function(err) {
fs.writeFile(filepath, contents, done);
});
};
Expand Down
10 changes: 8 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
var ms = require('ms');
var os = require('os');
var path = require('path');

function path() {
function configPath() {
if (process.env.NODE_ENV === 'development') {
return './conf/local.json';
} else {
return './conf/prod.json';
}
}

var conf = require(path());
var conf = require(configPath());

if (typeof(conf.remote.maxAge) === 'string') {
conf.remote.maxAge = ms(conf.remote.maxAge) * 1000;
}

var CACHE_FOLDER = path.join(os.tmpdir ? os.tmpdir() : os.tmpDir(), 'tldr-cache');
conf.local = conf.local || {};
conf.local.cacheFolder = conf.local.cacheFolder || CACHE_FOLDER;

module.exports = conf;
15 changes: 0 additions & 15 deletions lib/remote.js

This file was deleted.

29 changes: 11 additions & 18 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
var request = require('request');
var cache = require('./cache');
var remote = require('./remote');
var config = require('./config');

exports.get = function(command, done) {
cache.get(command + '.md', function(err, contents) {
if (err) {
getRemote(command, done);
} else {
done(null, contents);
}
});
};

function getRemote(command, done) {
var url = remote.url(command);
request.get(url, function(err, res, body) {
exports.get = function(path, done) {
request.get(config.remote.url+"/"+path, function(err, res, body){
if (err) {
done('tldr not available (check your internet connection)');
} else if (res.statusCode != 200) {
done(command + ' command not available\n' +
done(path + ' documentation is not available\n' +
'Consider contributing Pull Request to https://github.com/rprieto/tldr');
} else {
cache.save(command + '.md', body, function(err) {
done(null, body);
});
cachePath(path, body, done);
}
});
}

function cachePath(path, contents, done) {
cache.save(path, contents, function(err) {
done(null, contents);
});
}
59 changes: 59 additions & 0 deletions lib/resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var request = require('./request');
var cache = require('./cache');
var os = require('os');
var async = require('async');

var osDirectories = {
common : "common",
darwin : 'osx',
linux : 'linux'
};

function path(command, platform) {
return (platform || osDirectories[os.platform()]) + '/' + command + '.md';
}

function getForPlatform(repository, command, platform, done) {
var pathCommon = path(command);
var pathPlatform = path(command, platform);

async.parallel([
function(callback) {
repository.get(pathCommon, function() { callback(null, arguments); });
},
function(callback) {
repository.get(pathPlatform, function() { callback(null, arguments); });
}
], function(err, results){
var errCommon = results[0][0];
var errPlatform = results[1][0];
var contentsCommon = results[0][1];
var contentsPlatform = results[1][1];
if(errCommon && errPlatform) {
done([errCommon, errPlatform].join('\n'));
} else {
done(null, contentsPlatform || contentsCommon);
}
});

}

exports.get = function(command, options, done) {
options = options || {};
var resolver = this;
resolver.getLocal(command, options.os, function(err, contents) {
if (err) {
resolver.getRemote(command, options.os, done);
} else {
done(null, contents);
}
});
};

exports.getLocal = function(command, platform, done) {
getForPlatform(cache, command, platform, done);
};

exports.getRemote = function(command, platform, done) {
getForPlatform(request, command, platform, done);
};
10 changes: 5 additions & 5 deletions lib/tldr.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var util = require('util');
var request = require('./request');
var output = require('./output');
var util = require('util');
var resolver = require('./resolver');
var output = require('./output');

exports.get = function(command) {
request.get(command, function(err, body) {
exports.get = function(command, options) {
resolver.get(command, options, function(err, body) {
if (err) {
util.error('ERROR: ' + err);
process.exit(1);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"async": "~0.2.9",
"mkdirp": "~0.3.5",
"unhtml": "~0.1.0",
"ms": "~0.6.2"
"ms": "~0.6.2",
"optimist": "~0.6.0"
},
"devDependencies": {
"mocha": "~1.15.1",
Expand Down