-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunScripts.js
273 lines (267 loc) · 10.4 KB
/
runScripts.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const runScripts = {
extMan: null,
scriptDir: '../scripts',
showOutput: true,
hist: [],
histFile: '',
template: `
#!/bin/zsh
#
# The following Environment Variables are created by Modal File Manager
# before running your script:
#
# $CURRENT_DIRECTORY The current directory for the cursor
# $CURRENT_FILE The current file name for the cursor
# $LEFT_PANE The directory of the left file pane
# $LEFT_PANE_SELECTED_FILE The last highlighted file in the left file pane
# $RIGHT_PANE The directory of the right file pane
# $RIGHT_PANE_SELECTED_FILE The last highlighted file in the right file pane
# $FILES_SELECTED A newline separated list of selected files
#
# After creating the script, you have to set the mode to executable for
# it to be ran. You can change the shebang to run any language on your
# computer (and then change the comments also).
#
`,
init: function(extManager) {
runScripts.extMan = extManager;
var cmds = runScripts.extMan.getCommands();
const lfs = extManager.localFS;
var extdir = extManager.getExtensionDir();
runScripts.histFile = lfs.appendPath(extdir,'../cmdhist.json');
runScripts.scriptDir = lfs.appendPath(extdir, '../scripts');
if(!lfs.dirExists(runScripts.scriptDir)) {
lfs.makeDir(runScripts.scriptDir);
runScripts.copyExampleScripts();
}
if(!lfs.fileExists(runScripts.histFile)) {
lfs.writeFile(runScripts.histFile, JSON.stringify(['ls']));
runScripts.hist = ['ls'];
} else {
runScripts.hist = JSON.parse(lfs.readFile(runScripts.histFile));
}
cmds.addCommand('Run Script', 'runScripts.runScript','Run a user created script.', runScripts.runScript);
cmds.addCommand('Run NPM Script', 'runScripts.runNpmScript','Run a npm script.', runScripts.runNpmScript);
cmds.addCommand('Run Mask Script', 'runScripts.runMaskScript','Run a Mask script.', runScripts.runMaskScript);
cmds.addCommand('Create Script', 'runScripts.createScript','Create a script.', runScripts.createScript);
cmds.addCommand('Toggle Show Output', 'runScripts.toggleShowOutput','Toggle the showing of an output from running scripts.', runScripts.toggleShowOutput);
cmds.addCommand('Run Command Line', 'runScripts.runCommandLine','Run a command line the user gives.', runScripts.runCommandLine);
cmds.addCommand('Edit Script', 'runScripts.editScript','Edit the user specified script.', runScripts.editScript);
cmds.addCommand('Go To Scripts Directory', 'runScripts.goToScript','Open the scripts directory.', runScripts.goToScript);
cmds.addCommand('Install Example Scripts', 'runScripts.copyExampleScripts','Install the example scripts. If you have changed any, they will be overwritten.', runScripts.copyExampleScripts);
},
copyExampleScripts: function() {
const lfs = runScripts.extMan.localFS;
if(lfs.dirExists(runScripts.scriptDir)) {
var orgScriptDir = lfs.appendPath(runScripts.extMan.getExtensionDir(),'runScripts-ModalFileManagerExtension/scripts');
if(lfs.dirExists(orgScriptDir)) {
const scrpts = lfs.getDirList(orgScriptDir);
runScripts.extMan.getExtCommand('copyEntriesCommand').command(scrpts, {
dir: runScripts.scriptDir,
name: ''
});
}
}
},
saveHistFile: function() {
runScripts.hist = [...new Set(runScripts.hist)];
runScripts.extMan.localFS.writeFile(runScripts.histFile, JSON.stringify(runScripts.hist));
},
unload: function() {
runScripts.saveHistFile();
},
installKeyMaps: function() {
},
goToScript: function() {
runScripts.extMan.getExtCommand('changeDir').command({
path: runScripts.scriptDir
});
},
toggleShowOutput: function() {
if(runScripts.showOutput) {
runScripts.showOutput = false;
} else {
runScripts.showOutput = true;
}
},
runCommandLine: function() {
//
// This will prompt the user for a command line to run. It shows
// past command lines to pick from as well.
//
runScripts.extMan.getExtCommand('pickItem').command('Command Line:', runScripts.hist.map(item => {
return({
name: item,
value: item
});
}), runScripts.runCommandLineReturn, true);
},
runCommandLineReturn: function(value) {
if(value !== null) {
runScripts.hist.push(value);
runScripts.saveHistFile();
runScripts.returnScript(value);
} else {
runScripts.extMan.getExtCommand('showMessage').command('Run Command Line', 'Not a proper command line. Try again.');
}
},
runNpmScript: function() {
//
// This will show all npm scripts in the current directory and prompt the
// user to select one.
//
const lfs = runScripts.extMan.localFS;
const lcursor = runScripts.extMan.getExtCommand('getCursor').command();
const npmf = lfs.appendPath(lcursor.entry.dir, 'package.json');
if(lfs.fileExists(npmf)) {
var npmFile = JSON.parse(lfs.readFile(npmf));
var scripts = [];
Object.keys(npmFile.scripts).forEach(item => {
scripts.push({
name: item,
value: item
});
});
runScripts.extMan.getExtCommand('pickItem').command('Which Npm Script?', scripts, runScripts.runNpmScriptReturn);
} else {
runScripts.extMan.getExtCommand('showMessage').command('Run User Scripts', 'No package.json file in this directory!');
}
},
runNpmScriptReturn: function(value) {
runScripts.returnScript('npm run ' + value);
},
runMaskScript: function() {
//
// This will show all Mask scripts in the current directory and prompt the
// user to select one.
//
const lfs = runScripts.extMan.localFS;
const lcursor = runScripts.extMan.getExtCommand('getCursor').command();
const maskf = lfs.appendPath(lcursor.entry.dir, 'maskfile.md');
var scripts = [];
if(lfs.fileExists(maskf)) {
var npmFile = new String(lfs.readFile(maskf)).split('\n').forEach(el => {
var mtch = el.match(/##\ ([^\ ]*)/);
console.log(mtch);
if(mtch !== null) {
scripts.push({
name: mtch[1],
value: mtch[1]
})
}
});
console.log(scripts);
runScripts.extMan.getExtCommand('pickItem').command('Which Mask Script?', scripts, runScripts.runMaskScriptReturn);
} else {
runScripts.extMan.getExtCommand('showMessage').command('Run User Scripts', 'No maskfile.md file in this directory!');
}
},
runMaskScriptReturn: function(value) {
runScripts.returnScript('mask ' + value);
},
runScript: function() {
const lfs = runScripts.extMan.localFS;
const scrpts = lfs.getDirList(runScripts.scriptDir).map(item => {
return {
name: item.name,
value: lfs.appendPath(item.dir, item.name)
}
});
if(scrpts.length < 1) {
//
// Tell the user to create some scripts.
//
runScripts.extMan.getExtCommand('showMessage').command('Run User Scripts', 'No scripts created yet. Start making some!');
} else {
//
// List the scripts for the user to pick from.
//
runScripts.extMan.getExtCommand('pickItem').command('Which Script?', scrpts, runScripts.returnScript);
}
},
returnScript: function(value) {
const lfs = runScripts.extMan.localFS;
var sEnv = [];
const lcursor = runScripts.extMan.getExtCommand('getCursor').command();
const lLeftFile = runScripts.extMan.getExtCommand('getLeftFile').command();
const lRightFile = runScripts.extMan.getExtCommand('getRightFile').command();
const lLeftDir = runScripts.extMan.getExtCommand('getLeftDir').command();
const lRightDir = runScripts.extMan.getExtCommand('getRightDir').command();
const sFileList = runScripts.extMan.getExtCommand('getSelectedFiles').command().map(item => {
return(lfs.appendPath(item.dir, item.name));
});
sEnv['CURRENT_DIRECTORY'] = lcursor.entry.dir;
sEnv['CURRENT_FILE'] = lcursor.entry.name;
sEnv['LEFT_PANE'] = lLeftDir.path;
sEnv['LEFT_PANE_SELECTED_FILE'] = lLeftFile.entry.name;
sEnv['RIGHT_PANE'] = lRightDir.path;
sEnv['RIGHT_PANE_SELECTED_FILE'] = lRightFile.entry.name;
sEnv['FILES_SELECTED'] = sFileList.join('\n');
lfs.runCommandLine(value,(err, data)=>{
if(runScripts.showOutput) {
//
// Show the user the output from the script.
//
if(err) {
runScripts.showOutputDialog('Error: ' + err);
} else {
runScripts.showOutputDialog(data);
}
}
}, sEnv, {
cwd: lcursor.entry.dir
});
},
showOutputDialog: function(msg) {
//
// Show the user the message.
//
msg = msg.replaceAll('\n','<br />');
msg = msg.replaceAll(/Error/gi,'<span style="color: red;">Error</span>');
msg = "<div style='display: flex; flex-direction: column; overflow: auto; height: 200px;'>" + msg + "</div>";
runScripts.extMan.getExtCommand('showMessage').command('Script Output', msg);
},
createScript: function() {
runScripts.extMan.getExtCommand('askQuestion').command('Create User Scripts', 'Name of the script file (with extension):', runScripts.createScriptReturn);
},
createScriptReturn: function(value) {
const lfs = runScripts.extMan.localFS;
value = value.trim();
const sptFile = lfs.appendPath(runScripts.scriptDir, value);
console.log(sptFile);
if(!lfs.fileExists(sptFile)) {
lfs.writeFile(sptFile,runScripts.template);
runScripts.returnEdit(sptFile);
} else {
runScripts.extMan.getExtCommand('showMessage').command('Create User Script', 'Script already exists!');
}
},
editScript: function() {
//
// Show the user the list of scripts and open the selected one in the
// editor.
//
const lfs = runScripts.extMan.localFS;
const scrpts = lfs.getDirList(runScripts.scriptDir).map(item => {
return {
name: item.name,
value: lfs.appendPath(item.dir, item.name)
}
});
if(scrpts.length < 1) {
//
// Tell the user to create some scripts.
//
runScripts.extMan.getExtCommand('showMessage').command('Run User Scripts', 'No scripts created yet. Start making some!');
} else {
//
// List the scripts for the user to pick from.
//
runScripts.extMan.getExtCommand('pickItem').command('Which Script?', scrpts, runScripts.returnEdit);
}
},
returnEdit: function(value) {
runScripts.extMan.getExtCommand('editEntryCommand').command(value);
}
};
return(runScripts);