mirror of
https://github.com/slap-editor/slap.git
synced 2024-09-11 04:45:47 +03:00
Merge pull request #241 from slap-editor/issue-193
@tsun1215's Issue #193 (merged master)
This commit is contained in:
commit
dcc663cc32
29
lib/cli.js
29
lib/cli.js
@ -3,6 +3,7 @@ var path = require('path');
|
||||
var rc = require('rc');
|
||||
|
||||
var util = require('slap-util');
|
||||
var ttys = require('ttys');
|
||||
var package = require('../package');
|
||||
var baseDir = path.join(__dirname, '..');
|
||||
var configFile = path.join(baseDir, package.name + '.ini');
|
||||
@ -58,6 +59,20 @@ if (opts.perf.profile && process.execArgv.indexOf('--prof') === -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
function readAsync (stream) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if(!stream.isTTY) { // Checks if there is a pipe
|
||||
var chunks = [];
|
||||
stream
|
||||
.on('error', reject)
|
||||
.on('data', chunks.push.bind(chunks))
|
||||
.on('end', function () { resolve(Buffer.concat(chunks)); });
|
||||
}else{
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var Promise = require('bluebird');
|
||||
var fs = Promise.promisifyAll(require('fs'));
|
||||
|
||||
@ -67,10 +82,13 @@ var Pane = require('./ui/Pane');
|
||||
|
||||
module.exports = function (options) {
|
||||
opts = _.merge(opts, options);
|
||||
return Slap.getUserDir().catch(Promise.resolve()).then(function (userDir) {
|
||||
|
||||
return Promise.all([
|
||||
Slap.getUserDir().catch(Promise.resolve()),
|
||||
readAsync(process.stdin)
|
||||
]).spread(function (userDir, stdin) {
|
||||
if (userDir) opts = _.merge({logger: {file: path.resolve(userDir, package.name+'.log')}}, opts);
|
||||
opts = _.merge({editor: {logger: opts.logger}}, opts);
|
||||
opts.screenOpts.input = ttys.stdin; // Uses ttys to read from /dev/tty
|
||||
util.logger(opts.logger);
|
||||
util.logger.info("loading...");
|
||||
util.logger.verbose("configuration:", opts);
|
||||
@ -82,9 +100,12 @@ module.exports = function (options) {
|
||||
Promise.all(opts._.map(function (path, i) {
|
||||
return slap.open(path.toString(), !i);
|
||||
})).done();
|
||||
|
||||
|
||||
if (!opts._.length) { // if no files are passed
|
||||
new Pane({parent: slap}).setCurrent(); // open a new empty file
|
||||
var pane = new Pane({parent: slap});
|
||||
// Sets stdin from pipe if it exists
|
||||
if (stdin) pane.editor.textBuf.setText(stdin.toString());
|
||||
pane.setCurrent();
|
||||
if (!opts.config) { // first run without a file passed
|
||||
slap.help().done();
|
||||
fs.createReadStream(path.join(__dirname, '..', 'default-config.ini')).pipe(fs.createWriteStream(path.join(userDir, 'config')));
|
||||
|
@ -30,6 +30,7 @@
|
||||
"node-clap": "0.0.5",
|
||||
"rc": "1.1.5",
|
||||
"slap-util": "1.0.5",
|
||||
"ttys": "0.0.3",
|
||||
"update-notifier": "0.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
Loading…
Reference in New Issue
Block a user