-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
179 lines (152 loc) · 6.76 KB
/
app.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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const sfcc = require('sfcc-ci');
const fs = require('fs');
const path = require("path");
const archiver = require('archiver');
function deleteFile(path) {
fs.unlinkSync(path);
}
function archiveFiles(archiveName, targetDirectory, files) {
return new Promise(async function (resolve, reject) {
var output = fs.createWriteStream(path.join(targetDirectory, archiveName + '.zip'));
var archive = archiver('zip');
archive.on('error', function(err){
reject('Error while archiving meta data ' + err);
});
archive.pipe(output);
files
.flatMap(file => file)
.forEach(filepath => {
let fileName = filepath.replace(/^.*[\\\/]/, '');
archive.file(filepath, { name: 'site_template/meta/' + fileName })
});
archive.finalize();
resolve(output.path);
})
}
function authenticate(instance, clientId, clientSecret) {
return new Promise(async function (resolve, reject) {
sfcc.auth.auth(clientId, clientSecret, function (err, token) {
if (err) {
reject('Fortuneteller: Authentication to sandbox failed ' + err);
return;
}
resolve(token);
})
})
}
function upload(instance, archivedFilepath, token) {
return new Promise(async function (resolve, reject) {
sfcc.instance.upload(instance, archivedFilepath, token, {}, function(err) {
if (err) {
reject('Fortuneteller: Meta data upload error: ' + err);
return;
}
// import data into sandbox
let archivedFileName = archivedFilepath.replace(/^.*[\\\/]/, '');
resolve(archivedFileName);
});
});
}
function importMetadata(instance, archivedFileName, token) {
return new Promise (async function (resolve, reject) {
sfcc.instance.import(instance, archivedFileName, token, function(err, jobExecutionDetail) {
if (err) {
reject('Fortuneteller: Meta data import error: ' + err);
return;
}
resolve(jobExecutionDetail.log_file_name);
});
})
}
function readConfigurationFile(configurationFilename) {
return new Promise (async function (resolve, reject) {
vscode.workspace.findFiles(configurationFilename).then(files => {
if (!files || files.length === 0) {
reject('Fortuneteller: No ' + configurationFilename + ' file found');
return;
}
return vscode.workspace.openTextDocument(files[0].path)
.then((document) => {
const text = document.getText();
resolve(JSON.parse(text));
});
})
})
}
function findMetadataFiles (configuration) {
return new Promise(async function (resolve, reject) {
const metadataFiles = configuration['metadata-files'];
if (!metadataFiles || !metadataFiles.length) {
reject('Meta data are empty in dw.json');
return;
}
Promise.all(metadataFiles.map(metadataFile => {
return vscode.workspace.findFiles(metadataFile).then(files => {
if (!files || files.length === 0) {
reject('Fortuneteller: File ' + metadataFile + ' is not found');
return;
}
return files.map(file => file.path);
})
}))
.then(foundFiles => resolve({ targetDirectory: vscode.workspace.workspaceFolders[0].uri.path, metadataFiles: foundFiles }))
.catch(err => reject('Fortuneteller: Error while looking for meta data files: ' + err));
})
}
function dispose() {
const configurationFilename = 'dw.json';
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
return vscode.commands.registerCommand('fortuneteller.uploadMetaData', function () {
// The code you place here will be executed every time your command is executed
let _configuration = {}, _archivedFilepath = '', _token = '';
readConfigurationFile(configurationFilename)
.then(configuration =>
{
_configuration = configuration;
findMetadataFiles(_configuration)
.then(({ targetDirectory, metadataFiles }) =>
{
archiveFiles('site_template', targetDirectory, metadataFiles)
.then(archivedFilepath =>
{
_archivedFilepath = archivedFilepath;
authenticate(_configuration.hostname, _configuration['client-id'], _configuration['client-secret'],)
.then(token =>
{
showInformationMessage('Fortuneteller: Uploading meta data ...');
_token = token;
upload(_configuration.hostname, _archivedFilepath, _token)
.then(archivedFileName =>
{
showInformationMessage('Fortuneteller: Meta data are uploaded');
// Once the archive is uploaded, remove it from the computer
deleteFile(_archivedFilepath);
importMetadata(_configuration.hostname, archivedFileName, _token)
.then(logFileName =>
{
showInformationMessage('Fortuneteller: Meta data are being imported (' + logFileName + ')');
}
).catch(showInformationMessage)
}
).catch(showInformationMessage)
}
).catch(showInformationMessage)
}
).catch(showInformationMessage)
}
).catch(showInformationMessage)
}
).catch(showInformationMessage)
});
}
function showInformationMessage(message) {
vscode.window.showInformationMessage(message);
}
module.exports = {
dispose
}