├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # string-editor 2 | 3 | Edit a string using $EDITOR from within your node app. 4 | 5 | npm install string-editor 6 | 7 | ## Usage 8 | 9 | ``` js 10 | var edit = require('string-editor'); 11 | 12 | // this launches your $EDITOR with a tmp file containing "hello world" 13 | edit('hello world', function(err, result) { 14 | // when you are done editing result will contain the string 15 | console.log(result); 16 | }); 17 | ``` 18 | 19 | Sometimes it can be useful to set an filename to help your editor to enable highlighting etc. 20 | 21 | ``` js 22 | // we pass app.js as a filename to help with highlighting 23 | edit('var a = 42;', 'app.js', function(err, result) { 24 | console.log(result); 25 | }) 26 | ``` 27 | 28 | Note that `app.js` will still be a tmp file 29 | 30 | ## License 31 | 32 | MIT -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var editor = require('editor'); 2 | var os = require('os'); 3 | var fs = require('fs'); 4 | var path = require('path'); 5 | 6 | var edit = function(str, filename, cb) { 7 | if (typeof filename === 'function') return edit(str, null, filename); 8 | if (!filename) filename = Date.now()+''; 9 | 10 | filename = path.join(os.tmpDir(), filename); 11 | fs.writeFile(filename, str, function(err) { 12 | if (err) return cb(err); 13 | editor(filename, function(code) { 14 | if (code) return cb(new Error('non-zero exit code ('+code+')')); 15 | fs.readFile(filename, 'utf-8', function(err, result) { 16 | if (err) return cb(err); 17 | fs.unlink(filename, function() { 18 | cb(null, result); 19 | }); 20 | }); 21 | }); 22 | }); 23 | }; 24 | 25 | module.exports = edit; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "string-editor", 3 | "version": "0.1.2", 4 | "description": "Edit a string using $EDITOR from within your node app.", 5 | "repository": "git://github.com:mafintosh/string-editor.git", 6 | "dependencies": { 7 | "editor": "^1.0.0" 8 | }, 9 | "keywords": [ 10 | "string", 11 | "editor", 12 | "edit", 13 | "env", 14 | "cli", 15 | "app" 16 | ] 17 | } 18 | --------------------------------------------------------------------------------