This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
/
map.ts
99 lines (84 loc) · 3.14 KB
/
map.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
/**
* 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_download from '../download';
import * as deploy_helpers from '../helpers';
import * as deploy_plugins from '../plugins';
import * as deploy_targets from '../targets';
const MergeDeep = require('merge-deep');
/**
* A 'map' target.
*/
export interface MapTarget extends deploy_targets.Target, deploy_targets.TargetProvider {
/**
* One or more sources with property values for the targets.
*/
readonly from: MapItem | MapItem[];
}
/**
* A map item.
*/
export type MapItem = string | Object;
/**
* Mappings for a target.
*/
export type TargetMappings = { [name: string]: any };
class MapPlugin extends deploy_plugins.IterablePluginBase<MapTarget> {
protected async prepareTarget(mapTarget: MapTarget, target: deploy_targets.Target): Promise<deploy_targets.Target[]> {
const ME = this;
const WORKSPACE = mapTarget.__workspace;
const CLONED_TARGETS: deploy_targets.Target[] = [];
const MAPPINGS: TargetMappings[] = [];
await deploy_helpers.forEachAsync(deploy_helpers.asArray(mapTarget.from), async (item) => {
let itemsToAdd: TargetMappings[];
if (!deploy_helpers.isObject<TargetMappings>(item)) {
const DOWNLOAD_SOURCE = ME.replaceWithValues(
mapTarget,
item
);
itemsToAdd = deploy_helpers.asArray(
JSON.parse(
(await deploy_download.download(
DOWNLOAD_SOURCE,
WORKSPACE.getSettingScopes()
)).toString('utf8')
)
);
}
else {
itemsToAdd = [ item ];
}
MAPPINGS.push
.apply(MAPPINGS, itemsToAdd);
});
for (const M of MAPPINGS) {
let ct = deploy_helpers.cloneObjectFlat(target);
ct = MergeDeep(ct, M);
CLONED_TARGETS.push(ct);
}
return CLONED_TARGETS;
}
}
/**
* Creates a new instance of that plugin.
*
* @param {deploy_plugins.PluginContext} context The context for the plugin.
*
* @return {deploy_plugins.Plugin} The new plugin.
*/
export function createPlugins(context: deploy_plugins.PluginContext) {
return new MapPlugin(context);
}