Permalink
Browse files

editor works

  • Loading branch information...
substack committed Jun 13, 2012
1 parent 5fffa47 commit 7889ddf88a2b7995b824af894c9cbda6d77adca6
Showing with 47 additions and 7 deletions.
  1. +10 −1 bin/cmd.js
  2. +37 −6 index.js
View
@@ -1,6 +1,7 @@
var pkginit = require('../');
-var argv = require('optimist').argv;
var fs = require('fs');
+
+var argv = require('optimist').argv;
var cmd = argv._[0];
pkginit({ context : argv }, function (err, pkg) {
@@ -31,6 +32,14 @@ pkginit({ context : argv }, function (err, pkg) {
}
});
}
+ else if (cmd === 'edit') {
+ var opts = {
+ editor : argv.editor || process.env.EDITOR || 'vim'
+ };
+ pkg.edit(argv._[1] || 'default', opts, function (err) {
+ if (err) console.error(err);
+ });
+ }
else {
var name = (cmd === 'build' ? argv._[1] : argv._[0]) || 'default';
View
@@ -1,6 +1,8 @@
var mkdirp = require('mkdirp');
var prompter = require('prompter');
var merge = require('merge');
+var spawn = require('child_process').spawn;
+var exec = require('child_process').exec;
var fs = require('fs');
var path = require('path');
@@ -33,13 +35,17 @@ function PkgInit (opts) {
this.context = opts.context;
}
+PkgInit.prototype.filename = function (name, cb) {
+ return path.join(this.configDir, name + '.json');
+};
+
PkgInit.prototype.build = function (name, cb) {
var self = this;
var opts = self.opts;
var stderr = opts.stderr || process.stderr;
var stdout = opts.stdout || process.stdout;
var stdin = opts.stdin || process.stdin;
- var file = path.join(self.configDir, name + '.json');
+ var file = self.filename(name);
fs.readFile(file, function (err, src) {
if (err) return cb(err);
@@ -54,21 +60,46 @@ PkgInit.prototype.build = function (name, cb) {
};
PkgInit.prototype.add = function (name, src, cb) {
- var file = path.join(this.configDir, name + '.json');
- fs.writeFile(file, src, cb);
+ fs.writeFile(this.filename(name), src, cb);
};
PkgInit.prototype.rm = function (name, cb) {
- var file = path.join(this.configDir, name + '.json');
- fs.unlink(file, cb);
+ fs.unlink(this.filename(name), cb);
};
PkgInit.prototype.list = function (cb) {
fs.readdir(this.configDir, function (err, files) {
if (err) cb(err)
else cb(null, files
.filter(function (x) { return /\.json$/.test(x) })
- .map(function (x) { return /\.json$/.replace('') })
+ .map(function (x) { return x.replace(/\.json$/, '') })
)
});
};
+
+PkgInit.prototype.edit = function (name, opts, cb) {
+ if (!opts) opts = {};
+
+ var editor = opts.editor || process.env.EDITOR || 'vim';
+ var file = this.filename(name);
+
+ var opts = {
+ customFds : [ 0, 1, 2 ],
+ env : process.env,
+ cwd : process.cwd()
+ };
+ setRaw(true);
+ var ps = spawn(editor, [ file ], opts);
+ process.stdin.pipe(ps);
+
+ ps.on('exit', function () {
+ setRaw(false);
+ process.stdin.pause();
+ if (cb) cb()
+ });
+};
+
+var tty = require('tty');
+function setRaw (mode) {
+ (process.stdin.setRawMode || tty.setRawMode)(mode);
+}

0 comments on commit 7889ddf

Please sign in to comment.