-
Notifications
You must be signed in to change notification settings - Fork 36
/
init.js
531 lines (499 loc) · 22 KB
/
init.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
'use strict';
import Cli from './cli.js';
import File from './file.js';
import config from './config.js';
import InitGit from './init.git.js';
import InitNpm from './init.npm.js';
import InitConfig from './init.config.js';
import inquirer from 'inquirer';
import { Util } from './util.js';
import fs from 'node:fs';
import path from 'node:path';
/**
* @typedef {import('../../types/mcdev.d.js').AuthObject} AuthObject
* @typedef {import('../../types/mcdev.d.js').BuObject} BuObject
* @typedef {import('../../types/mcdev.d.js').Cache} Cache
* @typedef {import('../../types/mcdev.d.js').CodeExtract} CodeExtract
* @typedef {import('../../types/mcdev.d.js').CodeExtractItem} CodeExtractItem
* @typedef {import('../../types/mcdev.d.js').DeltaPkgItem} DeltaPkgItem
* @typedef {import('../../types/mcdev.d.js').Mcdevrc} Mcdevrc
* @typedef {import('../../types/mcdev.d.js').MetadataTypeItem} MetadataTypeItem
* @typedef {import('../../types/mcdev.d.js').MetadataTypeItemDiff} MetadataTypeItemDiff
* @typedef {import('../../types/mcdev.d.js').MetadataTypeItemObj} MetadataTypeItemObj
* @typedef {import('../../types/mcdev.d.js').MetadataTypeMap} MetadataTypeMap
* @typedef {import('../../types/mcdev.d.js').MetadataTypeMapObj} MetadataTypeMapObj
* @typedef {import('../../types/mcdev.d.js').MultiMetadataTypeList} MultiMetadataTypeList
* @typedef {import('../../types/mcdev.d.js').MultiMetadataTypeMap} MultiMetadataTypeMap
* @typedef {import('../../types/mcdev.d.js').SoapRequestParams} SoapRequestParams
* @typedef {import('../../types/mcdev.d.js').TemplateMap} TemplateMap
* @typedef {import('../../types/mcdev.d.js').TypeKeyCombo} TypeKeyCombo
*/
/**
* CLI helper class
*/
const Init = {
/**
* Creates template file for properties.json
*
* @param {Mcdevrc} properties config file's json
* @param {string} [credentialName] identifying name of the installed package / project; if set, will update this credential
* @param {boolean} [refreshBUs] if this was triggered by mcdev join, do not refresh BUs
* @returns {Promise.<void>} -
*/
async initProject(properties, credentialName, refreshBUs = true) {
if (!(await Init._checkPathForCloud())) {
return;
}
const skipInteraction = Util.skipInteraction;
if (!properties) {
// try to get cached properties because we return null in case of a crucial error
properties = config.properties;
}
const missingCredentials = this._getMissingCredentials(properties);
if ((await File.pathExists(Util.configFileName)) && properties) {
// config exists
if (credentialName) {
// update-credential mode
if (!properties.credentials[credentialName]) {
Util.logger.error(
`Could not update credential '${credentialName}' because it was not found in your config. Please check your spelling and try again.`
);
Cli.logExistingCredentials(properties);
if (skipInteraction) {
return;
}
const response = await Cli._selectBU(properties, null, true);
credentialName = response.credential;
}
Util.logger.info(`Updating existing credential '${credentialName}'`);
let error;
do {
error = false;
try {
const success = await Cli.updateCredential(properties, credentialName);
if (success) {
Util.logger.info(`✔️ Credential '${credentialName}' updated.`);
} else {
error = true;
}
} catch (ex) {
if (skipInteraction) {
Util.logger.error(ex.message);
return;
} else {
// retry
error = true;
}
}
} while (error && !skipInteraction);
Util.logger.debug('reloading config');
properties = await config.getProperties(true);
} else if (missingCredentials.length) {
// forced update-credential mode - user likely cloned repo and is missing mcdev-auth.json
Util.logger.warn(
`We found ${missingCredentials.length} credential${
missingCredentials.length > 1 ? 's' : ''
} in your ${Util.configFileName} that ${
missingCredentials.length > 1 ? 'are' : 'is'
} missing details.`
);
for (const badCredName of missingCredentials) {
let error;
do {
error = false;
try {
const success = await Cli.updateCredential(
properties,
badCredName,
refreshBUs
);
if (success) {
Util.logger.info(`✔️ Credential '${badCredName}' updated.`);
} else {
error = true;
}
} catch {
error = true;
}
} while (error);
Util.logger.debug('reloading config');
properties = await config.getProperties(true);
}
Util.logger.info('✔️ All credentials updated.');
// assume node dependencies are not installed
Util.execSync('npm', ['install']);
Util.logger.info('✔️ Dependencies installed.');
Util.logger.info('You can now start using Accenture SFMC DevTools.');
} else if (!missingCredentials.length) {
// add-credential mode
Util.logger.warn(Util.configFileName + ' found in root');
let responses;
if (skipInteraction) {
if (
skipInteraction.client_id &&
skipInteraction.client_secret &&
skipInteraction.auth_url &&
skipInteraction.account_id &&
skipInteraction.credentialName
) {
// assume automated input; only option here is to add a new credential
// requires skipInteraction=={client_id,client_secret,auth_url,account_id,credentialName}
// will be checked inside of Cli.addExtraCredential()
Util.logger.info('Adding another credential');
} else {
throw new Error(
'--skipInteraction flag found but missing required input for client_id,client_secret,auth_url,account_id,credentialName'
);
}
} else {
responses = await inquirer.prompt([
{
type: 'confirm',
name: 'isAddCredential',
message: 'Do you want to add another credential instead?',
default: false,
},
]);
}
let credentialName;
if (skipInteraction || responses.isAddCredential) {
credentialName = await Cli.addExtraCredential(properties);
}
if (credentialName) {
await this._downloadAllBUs(`${credentialName}/*`, 'update');
}
}
} else {
// config does not exist
// assuming it's the first time this command is run for this project
// initialize git repo
const initGit = await InitGit.initGitRepo();
if (initGit.status === 'error') {
return;
}
// set up IDE files and load npm dependencies
if (!(await this.upgradeProject(properties, true, initGit.repoName))) {
return;
}
// ask for credentials and create mcdev config
if (!(await Cli.initMcdevConfig())) {
return;
}
// set up markets and market lists initially
await Init._initMarkets();
// create first commit to backup the project configuration
if (initGit.status === 'init') {
Util.logger.info(`Committing initial setup to Git:`);
Util.execSync('git', ['add', '.']);
Util.execSync('git', ['commit', '-n', '-m', '"Initial commit"', '--quiet']);
Util.logger.info(`✔️ Configuration committed`);
}
// do initial retrieve *
await this._downloadAllBUs('"*"', initGit.status);
// backup to server
await InitGit.gitPush();
// all done
Util.logger.info('You are now ready to work with Accenture SFMC DevTools!');
Util.logger.warn(
'If you use VSCode, please restart it now to install recommended extensions.'
);
}
},
/**
* Creates template file for properties.json
*
* @returns {Promise.<void>} -
*/
async joinProject() {
if (!(await Init._checkPathForCloud())) {
return;
}
const responses = await inquirer.prompt([
{
type: 'confirm',
name: 'isJoin',
message:
'Do you want to join an existing project for which you have a Git-Repository URL?',
default: true,
},
]);
if (responses.isJoin) {
const gitRepoQs = await inquirer.prompt([
{
type: 'input',
name: 'gitRepoUrl',
message: 'Please enter the Git-Repository URL',
},
{
type: 'input',
name: 'gitBranch',
message:
'If you were asked to work on a specific branch, please enter it now (or leave empty for default)',
},
]);
const repoName = gitRepoQs.gitRepoUrl.split('/').pop().replace('.git', '');
// clone repo into current folder
Util.logger.info(
'Cloning initiated. You might be asked for your Git credentials in a pop-up window in a few seconds.'
);
Util.execSync(
'git',
[
'clone',
gitRepoQs.gitBranch ? `--branch ${gitRepoQs.gitBranch}` : null,
'--config core.longpaths=true',
'--config core.autocrlf=input',
gitRepoQs.gitRepoUrl,
].filter(Boolean)
);
if (!fs.existsSync(repoName)) {
Util.logger.error(
'Could not clone repository. Please check your Git-Repository URL as well as your credentials and try again.'
);
Util.logger.info(
'Check if you need an "API-Token" instead of your normal user password to authenticate'
);
return;
}
// make sure we switch to the new subfolder or else the rest will fail
process.chdir(repoName);
// check if the branch looks good
const properties = await config.getProperties(true, true);
if (!properties) {
Util.logger.error(
'Could not find .mcdevrc.json file in project folder. Please check your Git repository and branch.'
);
return;
}
// get name and email that's to be used for git commits
await InitGit._updateGitConfigUser();
// ask the user to enter the server credentials
await this.initProject(properties, null, false);
} else {
return;
}
},
/**
* helper for @initProject that optionally creates markets and market lists for all BUs
*/
async _initMarkets() {
const skipInteraction = Util.skipInteraction;
const properties = await config.getProperties(true);
// get list of business units
const firstCredentialName = Object.keys(properties.credentials)[0];
const businessUnits = Object.keys(
properties.credentials[firstCredentialName].businessUnits
);
// set up empty markets for them
const markets = {};
for (const bu of businessUnits) {
markets[bu] = { suffix: '_' + bu };
}
properties.markets = markets;
let sourceBuName;
// set up default deployment market lists
if (skipInteraction) {
// don't ask, list all BUs in deployment-target and set deployment-source to ???
if (!businessUnits.includes(skipInteraction.developmentBu)) {
Util.logger.warn(
`Could not find developmentBu=${skipInteraction.developmentBu} in business units. Skipping.`
);
delete skipInteraction.developmentBu;
}
sourceBuName = skipInteraction.developmentBu || '???';
if (!skipInteraction.developmentBu) {
Util.logger.info(
'Market List "deployment-source" will need to be set up manually. Marking all BUs as target BUs in "deployment-target".'
);
}
} else {
const responses = await inquirer.prompt([
{
type: 'list',
name: 'developmentBu',
message: 'Please select your development business unit:',
choices: businessUnits.map((bu) => ({ name: bu, value: bu })),
},
]);
sourceBuName = responses.developmentBu;
}
// set source list
properties.marketList['deployment-source'][firstCredentialName + '/' + sourceBuName] =
sourceBuName;
// set target list
for (const bu of businessUnits) {
// filter out source BU & parent BU to ensure they dont get deployed to automatically
if (bu !== sourceBuName && bu !== '_ParentBU_') {
properties.marketList['deployment-target'][firstCredentialName + '/' + bu] = bu;
}
}
await File.saveConfigFile(properties);
},
/**
* helper for {@link Init.initProject}
*
* @param {string} bu cred/bu or cred/* or *
* @param {string} gitStatus signals what state the git repo is in
* @returns {Promise.<void>} -
*/
async _downloadAllBUs(bu, gitStatus) {
const skipInteraction = Util.skipInteraction;
let responses;
if (!skipInteraction) {
responses = await inquirer.prompt([
{
type: 'confirm',
name: 'initialRetrieveAll',
message: 'Do you want to start downloading all Business Units (recommended)?',
default: true,
},
]);
}
if (skipInteraction?.downloadBUs === 'true' || responses?.initialRetrieveAll) {
Util.execSync('mcdev', ['retrieve', bu]);
if (gitStatus === 'init') {
Util.logger.info(`Committing first backup of your SFMC instance:`);
Util.execSync('git', ['add', '.']);
Util.execSync('git', ['commit', '-n', '-m', '"First instance backup"', '--quiet']);
Util.logger.info(`✔️ SFMC instance backed up`);
} else if (gitStatus === 'update') {
Util.logger.warn(
'Please manually commit this backup according to your projects guidelines.'
);
// TODO create guided commit:
// 1. ask if commit with all changes shall be created
// 2. ask if that should be done to current branch (show which one we are on) or a new branch
// a. if new: ask off of which we shall branch off of (show list) and then auto-create new branch and switch to it
// 3. create commit
}
}
},
/**
* wrapper around npm dependency & configuration file setup
*
* @param {Mcdevrc} properties config file's json
* @param {boolean} [initial] print message if not part of initial setup
* @param {string} [repoName] if git URL was provided earlier, the repo name was extracted to use it for npm init
* @returns {Promise.<boolean>} success flag
*/
async upgradeProject(properties, initial, repoName) {
if (!(await Init._checkPathForCloud())) {
return;
}
let status;
const versionBeforeUpgrade = properties?.version || '0.0.0';
if (!initial) {
Util.logger.info(
'Upgrading project with newest configuration, npm dependencies & other project configurations:'
);
// ensure an existing config is up to current specs
status = await InitConfig.fixMcdevConfig(properties);
if (!status) {
return false;
}
// version 4 release to simplify auth
status = await InitConfig.upgradeAuthFile();
if (!status) {
return false;
}
}
// create files before installing dependencies to ensure .gitignore is properly set up
status = await InitConfig.createIdeConfigFiles(versionBeforeUpgrade);
if (!status) {
return false;
}
// install node dependencies
status = await InitNpm.installDependencies(repoName);
if (!status) {
return false;
}
return true;
},
/**
* check if git repo is being saved on a cloud service and warn the user
*
* @private
* @returns {Promise.<boolean>} true if path is good; false if project seems to be in a cloud service folder
*/
async _checkPathForCloud() {
const absolutePath = path.resolve('');
// popular cloud services and their respective default name for the absolute path
// * CloudDocs is the default folder name for iCloud
const cloudServices = ['Dropbox', 'OneDrive', 'Google Drive', 'iCloud', 'CloudDocs'];
let cloudServiceFound = false;
for (const variable in cloudServices) {
if (absolutePath.includes(cloudServices[variable])) {
Util.logger.warn(
`It seems your project folder will be synchronized via '${
cloudServices[variable] === 'CloudDocs' ? 'iCloud' : cloudServices[variable]
}'. This can reduce the overall performance of your computer due to conflicts with Git.`
);
Util.logger.warn(
`We strongly recommend moving your project folder outside of the '${
cloudServices[variable] === 'CloudDocs' ? 'iCloud' : cloudServices[variable]
}' folder.`
);
cloudServiceFound = true;
}
}
if (!cloudServiceFound && absolutePath.includes(process.env.USERPROFILE)) {
// warn user to not place project folder into user profile folder
Util.logger.warn(
`It seems your project folder is located in your user profile's default folder which is often synchronized to webservices like ${cloudServices.join(
', '
)}. This can reduce the overall performance of your computer due to conflicts between with Git.`
);
Util.logger.warn(
`We strongly recommend moving your project folder outside of this folder.`
);
cloudServiceFound = true;
}
if (cloudServiceFound) {
const responses = await inquirer.prompt([
{
type: 'confirm',
name: 'ignoreCloudWarning',
message: 'Do you want to continue anyways?',
default: false,
},
]);
if (!responses.ignoreCloudWarning) {
Util.logger.error('Exiting due to cloud service warning');
return false;
}
}
return true;
},
/**
* finds credentials that are set up in config but not in auth file
*
* @private
* @param {Mcdevrc} properties javascript object in .mcdevrc.json
* @returns {string[]} list of credential names
*/
_getMissingCredentials(properties) {
let missingCredentials;
if (properties?.credentials) {
// reload auth file because for some reason we didnt want that in our main properties object
let auth;
try {
auth = File.readJsonSync(Util.authFileName);
} catch {
// file not found
auth = [];
}
// walk through config credentials and check if the matching credential in the auth file is missing something
missingCredentials = Object.keys(properties.credentials).filter(
(cred) =>
!auth[cred] ||
!auth[cred].account_id ||
properties.credentials[cred].eid != auth[cred].account_id ||
!auth[cred].client_id ||
!auth[cred].client_secret ||
!auth[cred].auth_url
);
}
return missingCredentials || [];
},
};
export default Init;