node-external-editor-2.0.4/000077500000000000000000000000001311110717400155425ustar00rootroot00000000000000node-external-editor-2.0.4/.coffeelint.json000066400000000000000000000046071311110717400206400ustar00rootroot00000000000000{ "arrow_spacing": { "level": "error" }, "braces_spacing": { "level": "error", "spaces": 0, "empty_object_spaces": 0 }, "camel_case_classes": { "level": "error" }, "coffeescript_error": { "level": "error" }, "colon_assignment_spacing": { "level": "ignore", "spacing": { "left": 0, "right": 0 } }, "cyclomatic_complexity": { "level": "ignore", "value": 10 }, "duplicate_key": { "level": "error" }, "empty_constructor_needs_parens": { "level": "ignore" }, "ensure_comprehensions": { "level": "warn" }, "eol_last": { "level": "error" }, "indentation": { "value": 2, "level": "error" }, "line_endings": { "level": "error", "value": "unix" }, "max_line_length": { "value": 80, "level": "ignore", "limitComments": true }, "missing_fat_arrows": { "level": "ignore", "is_strict": false }, "newlines_after_classes": { "value": 3, "level": "ignore" }, "no_backticks": { "level": "error" }, "no_debugger": { "level": "error", "console": false }, "no_empty_functions": { "level": "ignore" }, "no_empty_param_list": { "level": "ignore" }, "no_implicit_braces": { "level": "ignore", "strict": true }, "no_implicit_parens": { "level": "ignore", "strict": true }, "no_interpolation_in_single_quotes": { "level": "error" }, "no_nested_string_interpolation": { "level": "warn" }, "no_plusplus": { "level": "ignore" }, "no_private_function_fat_arrows": { "level": "warn" }, "no_stand_alone_at": { "level": "ignore" }, "no_tabs": { "level": "error" }, "no_this": { "level": "error" }, "no_throwing_strings": { "level": "error" }, "no_trailing_semicolons": { "level": "error" }, "no_trailing_whitespace": { "level": "error", "allowed_in_comments": false, "allowed_in_empty_lines": true }, "no_unnecessary_double_quotes": { "level": "error" }, "no_unnecessary_fat_arrows": { "level": "warn" }, "non_empty_constructor_needs_parens": { "level": "ignore" }, "prefer_english_operator": { "level": "ignore", "doubleNotLevel": "ignore" }, "space_operators": { "level": "error" }, "spacing_after_comma": { "level": "error" }, "transform_messes_up_line_numbers": { "level": "warn" } } node-external-editor-2.0.4/.editorconfig000066400000000000000000000003141311110717400202150ustar00rootroot00000000000000root = true [*] charset = utf-8 end_of_line = lf indent_style = space indent_size = 2 insert_final_newline = true trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false indent_size = 4 node-external-editor-2.0.4/.gitignore000066400000000000000000000011101311110717400175230ustar00rootroot00000000000000# Logs logs *.log npm-debug.log* # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # nyc test coverage .nyc_output # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # node-waf configuration .lock-wscript # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directories node_modules jspm_packages # Optional npm cache directory .npm # Optional REPL history .node_repl_history .ideanode-external-editor-2.0.4/.release.json000066400000000000000000000001471311110717400201350ustar00rootroot00000000000000{ "pre_commit_commands": [ "npm test" ], "post_complete_commands": [ "npm publish" ] } node-external-editor-2.0.4/.travis.yml000066400000000000000000000001301311110717400176450ustar00rootroot00000000000000sudo: false node_js: - 7 - 6 - 5 - 4 - .12 language: node_js script: npm test node-external-editor-2.0.4/LICENSE000066400000000000000000000020701311110717400165460ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Kevin Gravier Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-external-editor-2.0.4/README.md000066400000000000000000000136071311110717400170300ustar00rootroot00000000000000# External Editor [![ExternalEditor on Travis CI](https://img.shields.io/travis/mrkmg/node-external-editor.svg?style=flat-square)](https://travis-ci.org/mrkmg/node-external-editor/branches) [![ExternalEditor on NPM](https://img.shields.io/npm/v/external-editor.svg?style=flat-square)](https://www.npmjs.com/package/external-editor) [![ExternalEditor uses the MIT](https://img.shields.io/npm/l/external-editor.svg?style=flat-square)](https://opensource.org/licenses/MIT) A node module to edit a string with a users preferred text editor using $VISUAL or $ENVIRONMENT. Version: 2.0.1 As of version 2.0.0, node 0.10 is no longer support. Minimum node version is now 0.12. ##Install `npm install external-editor --save` ##Usage A simple example using the `.edit` convenience method var ExternalEditor = require('external-editor') var data = ExternalEditor.edit('\n\n# Please write your text above'); console.log(data); A full featured example var ExternalEditor = require('external-editor'); try { var editor = new ExternalEditor(); var text = editor.run() // the text is also available in editor.text } catch (err) { if (err instanceOf ExternalEditor.CreateFileError) { console.log('Failed to create the temporary file'); } else if (err instanceOf ExternalEditor.ReadFileError) { console.log('Failed to read the temporary file'); } else if (err instanceOf ExternalEditor.LaunchEditorError) { console.log('Failed to launch your editor'); } else { throw err; } } // Do things with the text // Eventually call the cleanup to remove the temporary file try { editor.cleanup(); } catch (err) { if (err instanceOf ExternalEditor.RemoveFileError) { console.log('Failed to remove the temporary file'); } else { throw err } } ####API **Static Methods** - `edit(text)` - `text` (string) *Optional* Defaults to empty string - **Returns** (string) The contents of the file - Could throw `CreateFileError`, `ReadFileError`, or `LaunchEditorError`, or `RemoveFileError` - `editAsync(text, callback)` - `text` (string) *Optional* Defaults to empty string - `callback` (function (error, text)) - `error` could be of type `CreateFileError`, `ReadFileError`, or `LaunchEditorError`, or `RemoveFileError` - `text`(string) The contents of the file **Static Properties** - `CreateFileError` Error thrown if the temporary file could not be created. - `ReadFileError` Error thrown if the temporary file could not be read. - `RemoveFileError` Error thrown if the temporary file could not be removed during cleanup. - `LaunchEditorError` Error thrown if the editor could not be launched. **Public Methods** - `new ExternalEditor(text)` - `text` (string) *Optional* Defaults to empty string - Could throw `CreateFileError` - `run()` Launches the editor. - **Returns** (string) The contents of the file - Could throw `LaunchEditorError` or `ReadFileError` - `runAsync(callback)` Launches the editor in an async way - `callback` (function (error, text)) - `error` could be of type `ReadFileError` or `LaunchEditorError` - `text`(string) The contents of the file - `cleanup()` Removes the temporary file. - Could throw `RemoveFileError` **Public Properties** - `text` (string) *readonly* The text in the temporary file. - `editor.bin` (string) The editor determined from the environment. - `editor.args` (array) Default arguments for the bin - `temp_file` (string) Path to temporary file. Can be changed, but be careful as the temporary file probably already exists and would need be removed manually. ##Errors All errors have a simple message explaining what went wrong. They all also have an `original_error` property containing the original error thrown for debugging purposes. ##Why Synchronous? Everything is synchronous to make sure the editor has complete control of the stdin and stdout. Testing has shown async launching of the editor can lead to issues when using readline or other packages which try to read from stdin or write to stdout. Seeing as this will be used in an interactive CLI environment, I made the decision to force the package to be synchronous. If you know a reliable way to force all stdin and stdout to be limited only to the child_process, please submit a PR. If async is really needed, you can use `editAsync` or `runAsync`. If you are using readline or have anything else listening to the stdin or you write to stdout, you will most likely have problem, so make sure to remove any other listeners on stdin, stdout, or stdin. ##Demo [![asciicast](https://asciinema.org/a/a1qh9lypbe65mj0ivfuoslz2s.png)](https://asciinema.org/a/a1qh9lypbe65mj0ivfuoslz2s) ##License The MIT License (MIT) Copyright (c) 2016 Kevin Gravier Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-external-editor-2.0.4/example_async.js000066400000000000000000000021121311110717400207240ustar00rootroot00000000000000var ExternalEditor = require('./main'); var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: null }); var message = '\n\n# Please Write a message\n# Any line starting with # is ignored'; process.stdout.write('Please write a message. (press enter to launch your preferred editor)'); editor = new ExternalEditor(message); rl.on('line', function () { try { rl.pause(); editor.runAsync(function (error, response) { if (error) { process.stdout.write(error.message); process.exit(1); } if (response.length === 0) { readline.moveCursor(process.stdout, 0, -1); process.stdout.write('Your message was empty, please try again. (press enter to launch your preferred editor)'); rl.resume(); } else { process.stdout.write('Your Message:\n'); process.stdout.write(response); process.stdout.write('\n'); rl.close(); } }); } catch (err) { process.stderr.write(err.message); process.stdout.write('\n'); rl.close(); } }); node-external-editor-2.0.4/example_sync.js000066400000000000000000000020411311110717400205640ustar00rootroot00000000000000var ExternalEditor = require('./main'); var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: null }); var message = '\n\n# Please Write a message\n# Any line starting with # is ignored'; process.stdout.write('Please write a message. (press enter to launch your preferred editor)'); editor = new ExternalEditor(message); rl.on('line', function () { try { // Get response, remove all lines starting with #, remove any trailing newlines. var response = editor.run().replace(/^#.*\n?/gm, '').replace(/\n+$/g, '').trim(); if (response.length === 0) { readline.moveCursor(process.stdout, 0, -1); process.stdout.write('Your message was empty, please try again. (press enter to launch your preferred editor)'); } else { process.stdout.write('Your Message:\n'); process.stdout.write(response); process.stdout.write('\n'); rl.close(); } } catch (err) { process.stderr.write(err.message); process.stdout.write('\n'); rl.close(); } }); node-external-editor-2.0.4/package.json000066400000000000000000000023161311110717400200320ustar00rootroot00000000000000{ "name": "external-editor", "version": "2.0.4", "description": "Edit a string with the users preferred text editor using $VISUAL or $ENVIRONMENT", "main": "main/index.js", "scripts": { "test": "npm run lint && npm run unit", "unit": "mocha --recursive --compilers coffee:coffee-script/register --timeout 10000 ./test/spec", "compile": "coffee --compile --output main/ src/", "lint": "coffeelint -f .coffeelint.json src" }, "repository": { "type": "git", "url": "git+https://github.com/mrkmg/node-external-editor.git" }, "keywords": [ "editor", "external", "user", "visual" ], "author": "Kevin Gravier (https://mrkmg.com)", "license": "MIT", "bugs": { "url": "https://github.com/mrkmg/node-external-editor/issues" }, "homepage": "https://github.com/mrkmg/node-external-editor#readme", "dependencies": { "iconv-lite": "^0.4.17", "jschardet": "^1.4.2", "tmp": "^0.0.31" }, "engines": { "node": ">=0.12" }, "devDependencies": { "chai": "^3.5.0", "coffee-script": "^1.10.0", "coffeelint": "^1.14.2", "mocha": "^3.2.0" }, "files": [ "main", "example_sync.js", "example_async.js" ] } node-external-editor-2.0.4/shrinkwrap.yaml000066400000000000000000000126671311110717400206320ustar00rootroot00000000000000createdWith: pnpm@0.68.0 dependencies: chai@^3.5.0: 3.5.0 coffee-script@^1.10.0: 1.12.6 coffeelint@^1.14.2: 1.16.0 iconv-lite@^0.4.17: 0.4.17 jschardet@^1.4.2: 1.4.2 mocha@^3.2.0: 3.4.1 tmp@^0.0.31: 0.0.31 packages: /assertion-error/1.0.2: 13ca515d86206da0bac66e834dd397d87581094c /balanced-match/0.4.2: cb3f3e3c732dc0f01ee70b403f302e61d7709838 /brace-expansion/1.1.7: dependencies: balanced-match: 0.4.2 concat-map: 0.0.1 resolution: 3effc3c50e000531fb720eaff80f0ae8ef23cf59 /browser-stdout/1.3.0: f351d32969d32fa5d7a5567154263d928ae3bd1f /chai/3.5.0: dependencies: assertion-error: 1.0.2 deep-eql: 0.1.3 type-detect: 1.0.0 resolution: 4d02637b067fe958bdbfdd3a40ec56fef7373247 /coffee-script/1.11.1: bf1c47ad64443a0d95d12df2b147cc0a4daad6e9 /coffee-script/1.12.6: 285a3f7115689065064d6bf9ef4572db66695cbf /coffeelint/1.16.0: dependencies: coffee-script: 1.11.1 glob: 7.1.2 ignore: 3.3.3 optimist: 0.6.1 resolve: 0.6.3 strip-json-comments: 1.0.4 resolution: 83d8ed1dafde3a677de44e7b8a18be607761e6d8 /commander/2.9.0: dependencies: graceful-readlink: 1.0.1 resolution: 9c99094176e12240cb22d6c5146098400fe0f7d4 /concat-map/0.0.1: d8a96bd77fd68df7793a73036a3ba0d5405d477b /debug/2.6.0: dependencies: ms: 0.7.2 resolution: bc596bcabe7617f11d9fa15361eded5608b8499b /deep-eql/0.1.3: dependencies: type-detect: 0.1.1 resolution: ef558acab8de25206cd713906d74e56930eb69f2 /diff/3.2.0: c9ce393a4b7cbd0b058a725c93df299027868ff9 /escape-string-regexp/1.0.5: 1b61c0562190a8dff6ae3bb2cf0200ca130b86d4 /fs.realpath/1.0.0: 1504ad2523158caa40db4a2787cb01411994ea4f /glob/7.1.1: dependencies: fs.realpath: 1.0.0 inflight: 1.0.6 inherits: 2.0.3 minimatch: 3.0.4 once: 1.4.0 path-is-absolute: 1.0.1 resolution: 805211df04faaf1c63a3600306cdf5ade50b2ec8 /glob/7.1.2: dependencies: fs.realpath: 1.0.0 inflight: 1.0.6 inherits: 2.0.3 minimatch: 3.0.4 once: 1.4.0 path-is-absolute: 1.0.1 resolution: c19c9df9a028702d678612384a6552404c636d15 /graceful-readlink/1.0.1: 4cafad76bc62f02fa039b2f94e9a3dd3a391a725 /growl/1.9.2: 0ea7743715db8d8de2c5ede1775e1b45ac85c02f /has-flag/1.0.0: 9d9e793165ce017a00f00418c43f942a7b1d11fa /iconv-lite/0.4.17: 4fdaa3b38acbc2c031b045d0edcdfe1ecab18c8d /ignore/3.3.3: 432352e57accd87ab3110e82d3fea0e47812156d /inflight/1.0.6: dependencies: once: 1.4.0 wrappy: 1.0.2 resolution: 49bd6331d7d02d0c09bc910a1075ba8165b56df9 /inherits/2.0.3: 633c2c83e3da42a502f52466022480f4208261de /jschardet/1.4.2: 2aa107f142af4121d145659d44f50830961e699a /json3/3.3.2: 3c0434743df93e2f5c42aee7b19bcb483575f4e1 /lodash._baseassign/3.2.0: dependencies: lodash._basecopy: 3.0.1 lodash.keys: 3.1.2 resolution: 8c38a099500f215ad09e59f1722fd0c52bfe0a4e /lodash._basecopy/3.0.1: 8da0e6a876cf344c0ad8a54882111dd3c5c7ca36 /lodash._basecreate/3.0.3: 1bc661614daa7fc311b7d03bf16806a0213cf821 /lodash._getnative/3.9.1: 570bc7dede46d61cdcde687d65d3eecbaa3aaff5 /lodash._isiterateecall/3.0.9: 5203ad7ba425fae842460e696db9cf3e6aac057c /lodash.create/3.1.1: dependencies: lodash._baseassign: 3.2.0 lodash._basecreate: 3.0.3 lodash._isiterateecall: 3.0.9 resolution: d7f2849f0dbda7e04682bb8cd72ab022461debe7 /lodash.isarguments/3.1.0: 2f573d85c6a24289ff00663b491c1d338ff3458a /lodash.isarray/3.0.4: 79e4eb88c36a8122af86f844aa9bcd851b5fbb55 /lodash.keys/3.1.2: dependencies: lodash._getnative: 3.9.1 lodash.isarguments: 3.1.0 lodash.isarray: 3.0.4 resolution: 4dbc0472b156be50a0b286855d1bd0b0c656098a /minimatch/3.0.4: dependencies: brace-expansion: 1.1.7 resolution: 5166e286457f03306064be5497e8dbb0c3d32083 /minimist/0.0.10: de3f98543dbf96082be48ad1a0c7cda836301dcf /minimist/0.0.8: 857fcabfc3397d2625b8228262e86aa7a011b05d /mkdirp/0.5.1: dependencies: minimist: 0.0.8 resolution: 30057438eac6cf7f8c4767f38648d6697d75c903 /mocha/3.4.1: dependencies: browser-stdout: 1.3.0 commander: 2.9.0 debug: 2.6.0 diff: 3.2.0 escape-string-regexp: 1.0.5 glob: 7.1.1 growl: 1.9.2 json3: 3.3.2 lodash.create: 3.1.1 mkdirp: 0.5.1 supports-color: 3.1.2 resolution: a3802b4aa381934cacb38de70cf771621da8f9af /ms/0.7.2: ae25cf2512b3885a1d95d7f037868d8431124765 /once/1.4.0: dependencies: wrappy: 1.0.2 resolution: 583b1aa775961d4b113ac17d9c50baef9dd76bd1 /optimist/0.6.1: dependencies: minimist: 0.0.10 wordwrap: 0.0.3 resolution: da3ea74686fa21a19a111c326e90eb15a0196686 /os-tmpdir/1.0.2: bbe67406c79aa85c5cfec766fe5734555dfa1274 /path-is-absolute/1.0.1: 174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f /resolve/0.6.3: dd957982e7e736debdf53b58a4dd91754575dd46 /strip-json-comments/1.0.4: 1e15fbcac97d3ee99bf2d73b4c656b082bbafb91 /supports-color/3.1.2: dependencies: has-flag: 1.0.0 resolution: 72a262894d9d408b956ca05ff37b2ed8a6e2a2d5 /tmp/0.0.31: dependencies: os-tmpdir: 1.0.2 resolution: 8f38ab9438e17315e5dbd8b3657e8bfb277ae4a7 /type-detect/0.1.1: 0ba5ec2a885640e470ea4e8505971900dac58822 /type-detect/1.0.0: 762217cc06db258ec48908a1298e8b95121e8ea2 /wordwrap/0.0.3: a3d5da6cd5c0bc0008d37234bbaf1bed63059107 /wrappy/1.0.2: b5243d8f3ec1aa35f1364605bc0d1036e30ab69f registry: 'https://registry.npmjs.org/' version: 2 node-external-editor-2.0.4/src/000077500000000000000000000000001311110717400163315ustar00rootroot00000000000000node-external-editor-2.0.4/src/errors/000077500000000000000000000000001311110717400176455ustar00rootroot00000000000000node-external-editor-2.0.4/src/errors/CreateFileError.coffee000066400000000000000000000003441311110717400240340ustar00rootroot00000000000000### ExternalEditor Kevin Gravier MIT ### class CreateFileError extends Error message: 'Failed to create temporary file for editor' constructor: (@original_error) -> module.exports = CreateFileError node-external-editor-2.0.4/src/errors/LaunchEditorError.coffee000066400000000000000000000003221311110717400244060ustar00rootroot00000000000000### ExternalEditor Kevin Gravier MIT ### class LaunchEditorError extends Error message: 'Failed launch editor' constructor: (@original_error) -> module.exports = LaunchEditorError node-external-editor-2.0.4/src/errors/ReadFileError.coffee000066400000000000000000000003231311110717400235010ustar00rootroot00000000000000### ExternalEditor Kevin Gravier MIT ### class ReadFileError extends Error message: 'Failed to read temporary file' constructor: (@original_error) -> module.exports = ReadFileError node-external-editor-2.0.4/src/errors/RemoveFileError.coffee000066400000000000000000000003321311110717400240630ustar00rootroot00000000000000### ExternalEditor Kevin Gravier MIT ### class RemoveFileError extends Error message: 'Failed to cleanup temporary file' constructor: (@original_error) -> module.exports = RemoveFileError node-external-editor-2.0.4/src/index.coffee000066400000000000000000000055761311110717400206260ustar00rootroot00000000000000### ExternalEditor Kevin Gravier MIT ### FS = require 'fs' Temp = require 'tmp' SpawnSync = require('child_process').spawnSync Spawn = require('child_process').spawn JSCharDet = require 'jschardet' IConvLite = require 'iconv-lite' CreateFileError = require './errors/CreateFileError' ReadFileError = require './errors/ReadFileError' RemoveFileError = require './errors/RemoveFileError' LaunchEditorError = require './errors/LaunchEditorError' JSCharDet.Constants.MINIMUM_THRESHOLD = 0 class ExternalEditor @edit: (text = '') -> editor = new ExternalEditor(text) editor.run() editor.cleanup() editor.text @editAsync: (text = '', callback) -> editor = new ExternalEditor(text) editor.runAsync (error_run, response) -> if not error_run try editor.cleanup() catch error_cleanup callback(error_cleanup) if typeof callback is 'function' callback(null, response) else callback(error_run) of typeof callback is 'function' @CreateFileError: CreateFileError @ReadFileError: ReadFileError @RemoveFileError: RemoveFileError @LaunchEditorError: LaunchEditorError text: '' temp_file: undefined editor: bin: undefined args: [] constructor: (@text = '') -> @determineEditor() @createTemporaryFile() run: => @launchEditor() @readTemporaryFile() runAsync: (callback) => try @launchEditorAsync => try @readTemporaryFile() callback(null, @text) if typeof callback is 'function' catch error_read callback(error_read) if typeof callback is 'function' catch error_launch callback(error_launch) if typeof callback is 'function' cleanup: => @removeTemporaryFile() determineEditor: => ed = if /^win/.test process.platform then 'notepad' else 'vim' editor = process.env.VISUAL or process.env.EDITOR or ed args = editor.split /\s+/ @bin = args.shift() @args = args createTemporaryFile: => try @temp_file = Temp.tmpNameSync {} FS.writeFileSync(@temp_file, @text, encoding: 'utf8') catch e throw new CreateFileError e readTemporaryFile: => try buffer = FS.readFileSync(@temp_file) encoding = JSCharDet.detect(buffer) @text = IConvLite.decode(buffer, encoding.encoding) catch e throw new ReadFileError e removeTemporaryFile: => try FS.unlinkSync(@temp_file) catch e throw new RemoveFileError e launchEditor: => try SpawnSync @bin, @args.concat([@temp_file]), stdio: 'inherit' catch e throw new LaunchEditorError e launchEditorAsync: (callback) => try child_process = Spawn @bin, @args.concat([@temp_file]), stdio: 'inherit' child_process.on 'exit', -> callback() if typeof callback is 'function' catch e throw new LaunchEditorError e module.exports = ExternalEditor node-external-editor-2.0.4/test/000077500000000000000000000000001311110717400165215ustar00rootroot00000000000000node-external-editor-2.0.4/test/spec/000077500000000000000000000000001311110717400174535ustar00rootroot00000000000000node-external-editor-2.0.4/test/spec/main.coffee000066400000000000000000000051371311110717400215560ustar00rootroot00000000000000assert = require('chai').assert readFileSync = require('fs').readFileSync writeFileSync = require('fs').writeFileSync IConvLite = require 'iconv-lite' ExternalEditor = require('../../src') describe 'main', -> before -> @previous_visual = process.env.VISUAL process.env.VISUAL = 'truncate --size 1' beforeEach -> @editor = new ExternalEditor 'XXX' afterEach -> @editor.cleanup() after -> process.env.VISUAL = @previous_visual it 'convenience method ".edit"', -> text = ExternalEditor.edit 'XXX' assert.equal text, 'X' it 'convenience method ".editAsync"', (cb) -> ExternalEditor.editAsync 'XXX', (e, text) -> assert.equal text, 'X' cb() it 'writes original text to file', -> contents = readFileSync @editor.temp_file assert.equal contents, 'XXX' it 'run() returns correctly', -> text = @editor.run() assert.equal text, 'X' it 'runAsync() callbacks correctly', (cb) -> @editor.runAsync (e, text) -> assert.equal text, 'X' cb() it 'run() returns text same as editor.text', -> text = @editor.run() assert.equal text, @editor.text it 'runAsync() callback text same as editor.text', (cb) -> @editor.runAsync (e, text) => assert.equal text, @editor.text cb() describe 'charsets', -> before -> @previous_visual = process.env.VISUAL process.env.VISUAL = 'true' beforeEach -> @editor = new ExternalEditor 'XXX' afterEach -> @editor.cleanup() after -> process.env.VISUAL = @previous_visual it 'utf8', -> writeFileSync(@editor.temp_file, IConvLite.encode('काचं शक्नोम्यत्तुम् । नोपहिनस्ति माम् ॥', 'utf8'), encoding: 'binary') text = @editor.run() assert.equal text, 'काचं शक्नोम्यत्तुम् । नोपहिनस्ति माम् ॥' it 'utf16', -> writeFileSync(@editor.temp_file, IConvLite.encode('काचं शक्नोम्यत्तुम् । नोपहिनस्ति माम् ॥', 'utf16'), encoding: 'binary') text = @editor.run() assert.equal text, 'काचं शक्नोम्यत्तुम् । नोपहिनस्ति माम् ॥' it 'win1252', -> writeFileSync(@editor.temp_file, IConvLite.encode('abc 123 ‰åþ', 'win1252'), encoding: 'binary') text = @editor.run() assert.equal text, 'abc 123 ‰åþ' it 'Big5', -> writeFileSync(@editor.temp_file, IConvLite.encode('一一一一', 'Big5'), encoding: 'binary') text = @editor.run() assert.equal text, '一一一一'