This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
/
script.ts
151 lines (134 loc) · 5.3 KB
/
script.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
/**
* This file is part of the vscode-deploy-reloaded distribution.
* Copyright (c) Marcel Joachim Kloubert.
*
* vscode-deploy-reloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* vscode-deploy-reloaded is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import * as deploy_contracts from '../../contracts';
import * as deploy_helpers from '../../helpers';
import * as deploy_targets from '../../targets';
import * as i18 from '../../i18';
/**
* An operation that executes a script.
*/
export interface ScriptTargetOperation extends deploy_targets.TargetOperation {
/**
* Additional data / options for the script.
*/
readonly options?: any;
/**
* The path to the script to execute.
*/
readonly script: string;
}
/**
* Arguments for script execution.
*/
export interface ScriptTargetOperationExecutionArguments extends deploy_contracts.ScriptArguments {
/**
* The underlying operation context.
*/
readonly context: deploy_targets.TargetOperationExecutionContext<ScriptTargetOperation>;
}
/**
* A function / method that executes a script.
*
* @param {ScriptTargetOperationExecutionArguments} args The arguments.
*
* @return {any} The result,
*/
export type ScriptTargetOperationExecutor = (args: ScriptTargetOperationExecutionArguments) => any;
/**
* A script module.
*/
export interface ScriptTargetOperationModule {
/**
* Executes the script.
*/
readonly execute: ScriptTargetOperationExecutor;
}
/** @inheritdoc */
export async function execute(context: deploy_targets.TargetOperationExecutionContext<ScriptTargetOperation>) {
const OPERATION = context.operation;
const TARGET = context.target;
const WORKSPACE = TARGET.__workspace;
let scriptFile = deploy_helpers.toStringSafe(
WORKSPACE.replaceWithValues(OPERATION.script)
);
if (deploy_helpers.isEmptyString(scriptFile)) {
switch (context.event) {
case deploy_targets.TargetOperationEvent.AfterDeployed:
scriptFile = './deployed.js';
break;
case deploy_targets.TargetOperationEvent.BeforeDeploy:
scriptFile = './beforeDeploy.js';
break;
}
}
let scriptFullPath = await WORKSPACE.getExistingSettingPath(scriptFile);
if (false === scriptFullPath) {
throw new Error(i18.t('targets.operations.script.scriptNotFound',
scriptFile));
}
const SCRIPT_MODULE = deploy_helpers.loadModule<ScriptTargetOperationModule>(scriptFullPath);
if (SCRIPT_MODULE) {
const EXECUTE = SCRIPT_MODULE.execute;
if (EXECUTE) {
const ARGS: ScriptTargetOperationExecutionArguments = {
_: require('lodash'),
context: context,
events: WORKSPACE.workspaceSessionState['target_operations']['script']['events'],
extension: WORKSPACE.context.extension,
folder: WORKSPACE.folder,
globalEvents: deploy_helpers.EVENTS,
globals: WORKSPACE.globals,
globalState: WORKSPACE.workspaceSessionState['target_operations']['script']['global'],
homeDir: deploy_helpers.getExtensionDirInHome(),
logger: WORKSPACE.createLogger(),
options: deploy_helpers.cloneObject(OPERATION.options),
output: WORKSPACE.output,
replaceWithValues: (val) => {
return WORKSPACE.replaceWithValues(val);
},
require: (id) => {
return deploy_helpers.requireFromExtension(id);
},
sessionState: deploy_helpers.SESSION,
settingFolder: WORKSPACE.settingFolder,
state: undefined,
workspaceRoot: WORKSPACE.rootPath,
};
// ARGS.state
Object.defineProperty(ARGS, 'state', {
enumerable: true,
get: () => {
return WORKSPACE.workspaceSessionState['target_operations']['script']['scripts'][<string>scriptFullPath];
},
set: (newValue) => {
WORKSPACE.workspaceSessionState['target_operations']['script']['scripts'][<string>scriptFullPath] = newValue;
}
});
await Promise.resolve(
EXECUTE.apply(SCRIPT_MODULE, [ ARGS ])
);
}
else {
throw new Error(i18.t('targets.operations.script.noScriptFunction',
scriptFile));
}
}
else {
throw new Error(i18.t('targets.operations.script.noScriptModule',
scriptFile));
}
}