UNPKG

1.47 kBJavaScriptView Raw
1'use strict';
2const childProcess = require('child_process');
3const envEditor = require('env-editor');
4const lineColumnPath = require('line-column-path');
5const opn = require('opn');
6
7const make = (files, opts) => {
8 if (!Array.isArray(files)) {
9 throw new TypeError(`Expected an \`Array\`, got ${typeof files}`);
10 }
11
12 opts = Object.assign({}, opts);
13
14 const editor = opts.editor ? envEditor.get(opts.editor) : envEditor.default();
15 const args = [];
16
17 if (editor.id === 'vscode') {
18 args.push('--goto');
19 }
20
21 for (const file of files) {
22 const parsed = lineColumnPath.parse(file);
23
24 if (['sublime', 'atom', 'vscode'].indexOf(editor.id) !== -1) {
25 args.push(lineColumnPath.stringify(parsed));
26 continue;
27 }
28
29 if (editor.id === 'webstorm') {
30 args.push(lineColumnPath.stringify(parsed, {column: false}));
31 continue;
32 }
33
34 if (editor.id === 'textmate') {
35 args.push('--line', lineColumnPath.stringify(parsed, {
36 file: false,
37 column: false
38 }), parsed.file);
39 continue;
40 }
41
42 args.push(parsed.file);
43 }
44
45 return {
46 bin: editor.bin,
47 args
48 };
49};
50
51module.exports = (files, opts) => {
52 const result = make(files, opts);
53
54 const cp = childProcess.spawn(result.bin, result.args, {
55 detached: true,
56 stdio: 'ignore'
57 });
58
59 // Fallback
60 cp.on('error', () => {
61 const result = make(files, Object.assign({}, opts, {editor: ''}));
62
63 for (const file of result.args) {
64 opn(file, {wait: false});
65 }
66 });
67
68 cp.unref();
69};
70
71module.exports.make = make;