-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathScriptExecutor.ts
More file actions
96 lines (83 loc) · 2.91 KB
/
Copy pathScriptExecutor.ts
File metadata and controls
96 lines (83 loc) · 2.91 KB
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
import { ProgressLocation, Uri, window } from 'vscode';
import { CommandType } from '../models';
import { Notifications } from './Notifications';
import { StateKeys } from '../constants';
import { evaluateCommand, fileExists, getPlatform } from '../utils';
import { Extension } from './Extension';
import { execFile } from 'child_process';
import { Logger } from './Logger';
import { StateManager } from './StateManager';
import { Config, Action, Step } from '@demotime/common';
export class ScriptExecutor {
public static async run(step: Step): Promise<void> {
if (!step.id) {
Notifications.error(`Step ID is missing from the "${Action.ExecuteScript}" action.`);
return;
}
if (!step.path) {
Notifications.error(`Path is missing from the "${Action.ExecuteScript}" action.`);
return;
}
if (!step.command) {
Notifications.error(`Command is missing from the "${Action.ExecuteScript}" action.`);
return;
}
await window.withProgress(
{
location: ProgressLocation.Notification,
title: `${Config.title}: Executing script`,
cancellable: false,
},
async () => {
let command = step.command as string;
const path = step.path as string;
const id = step.id as string;
const platform = getPlatform();
if (step.command === CommandType.Node && platform !== 'windows') {
step.command = await evaluateCommand(CommandType.Node);
}
const wsPath = Extension.getInstance().workspaceFolder;
if (!wsPath) {
Notifications.error('Workspace folder not found.');
return;
}
let scriptPath = Uri.joinPath(wsPath.uri, path as string);
if (!(await fileExists(scriptPath))) {
Notifications.error(`Script file not found at path: ${scriptPath.fsPath}`);
return;
}
if (platform === 'windows' && command.toLowerCase() === 'powershell') {
command = `${command} -File`;
}
const args = [scriptPath.fsPath];
const output = await ScriptExecutor.executeScriptAsync(command, args, wsPath.uri.fsPath);
Logger.info(`Step ID: ${id} - Output: ${output}`);
if (output) {
await StateManager.update(`${StateKeys.prefix.script}${id}`, output);
}
},
);
}
/**
* Execute script async
* @param fullScript
* @param wsPath
* @returns
*/
public static async executeScriptAsync(fullScript: string, wsPath: string): Promise<string> {
return new Promise((resolve, reject) => {
execFile(command, args, { cwd: wsPath }, (error, stdout) => {
if (error) {
Logger.error(error.message);
reject(error.message);
return;
}
if (stdout && stdout.endsWith(`\n`)) {
// Remove empty line at the end of the string
stdout = stdout.slice(0, -1);
}
resolve(stdout);
});
});
}
}