-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrun.ts
151 lines (96 loc) · 3.89 KB
/
run.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* IMPORT */
import * as _ from 'lodash';
import * as fs from 'fs';
import * as path from 'path';
import * as untildify from 'untildify';
import * as vscode from 'vscode';
import Config from './config';
import Substitutions from './substitutions';
import Utils from './utils';
/* TERMINALS CACHE */ // Used for the `recycle` and `target` options
const cache = {}; // Mapping terminal name => terminal instance
function onTerminalClose () {
vscode.window.onDidCloseTerminal ( term => {
delete cache[term['__id']];
});
}
onTerminalClose ();
/* ROOTS CACHE */ // Used for the `autokill` option
const cacheRoots = {}; // Mapping configPath => terminals instances
function onRootRemove () {
vscode.workspace.onDidChangeWorkspaceFolders ( ({ removed }) => {
removed.forEach ( async root => {
const rootPath = root.uri.fsPath,
config = await Config.get ( rootPath ),
{configPath} = config;
if ( !cacheRoots[configPath] ) return;
if ( config.autokill ) {
cacheRoots[configPath].forEach ( term => {
delete cache[term.name];
term.dispose ();
});
delete cacheRoots[configPath];
}
});
});
}
onRootRemove ();
/* RUN */
async function run ( terminal, config, rootPath?, substitutions? ) {
rootPath = rootPath || Utils.folder.getActiveRootPath ();
const { name, color, icon, target, split, cwd: terminalCwd, command, commands, execute, persistent, recycle, substitution, dynamicTitle, shellPath, env: terminalEnv, envInherit } = terminal,
configPath = _.get ( config, 'configPath' ) as string,
configEnv = _.get ( config, 'env' );
let {shellArgs} = terminal,
cwd = terminalCwd || rootPath,
texts = commands || [],
env = envInherit !== false ? _.merge ( {}, configEnv, terminalEnv ) : terminalEnv;
if ( command ) texts.unshift ( command );
if ( substitution !== false ) {
substitutions = substitutions || Substitutions.get ();
shellArgs = Substitutions.apply ( shellArgs, substitutions );
cwd = Substitutions.apply ( cwd, substitutions );
texts = Substitutions.apply ( texts, substitutions );
env = Substitutions.apply ( env, substitutions );
}
if ( cwd ) {
cwd = path.resolve ( rootPath, untildify ( cwd ) );
if ( !fs.existsSync ( cwd ) ) {
vscode.window.showErrorMessage ( `The provided cwd path doesn\'t exist: "${cwd}"` );
return;
}
}
if ( persistent ) {
const reattach = Utils.multiplexer.reattach ( config.multiplexer, persistent );
texts.unshift ( reattach );
}
const cacheTarget = target || name,
cacheTerm = recycle !== false && cache[cacheTarget],
cacheParentTerm = split && cache[split],
location = cacheParentTerm ? { parentTerminal: cacheParentTerm } : undefined,
title = dynamicTitle ? undefined : cacheTarget,
colorPath = color ? new vscode.ThemeColor ( color ) : null,
iconPath = icon ? new vscode.ThemeIcon ( icon ) : null,
isCached = !!cacheTerm,
term = cacheTerm || vscode.window.createTerminal ({ cwd, env, name: title, color: colorPath, iconPath, shellPath, shellArgs, location });
cache[cacheTarget] = term;
if ( configPath && !isCached ) {
if ( !cacheRoots[configPath] ) cacheRoots[configPath] = [];
cacheRoots[configPath].push ( term );
}
term['__id'] = cacheTarget;
term['__config'] = config;
term['__terminal'] = terminal;
await term.processId;
await Utils.delay ( 150 );
if ( persistent ) term.show ( false );
for ( let i = 0, l = texts.length; i < l; i++ ) {
if ( persistent && i === 1 ) await Utils.delay ( 1500 ); // It may take a while to start the multiplexer
await Utils.delay ( 50 );
const doExecute = execute !== false || i < l - 1; // The last of multiple commands won't be executed
term.sendText ( texts[i], doExecute );
}
return term;
}
/* EXPORT */
export default run;