Skip to content

Commit

Permalink
Merge pull request #503 from Accenture/502-bug-mcdev-init-in-unattend…
Browse files Browse the repository at this point in the history
…ed-mode-asks-if-bus-should-be-downloaded

#501 + #502 bug mcdev init in unattended mode asks if bus should be downloaded
  • Loading branch information
JoernBerkefeld authored Nov 3, 2022
2 parents 2bb8d02 + 0ac33ba commit 62b9b39
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 231 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ Example url: `https://mcg123abcysykllg-0321cbs8bbt64.auth.marketingcloudapis.com
> You can run this command without the interactive wizard asking questions using the `--skipInteraction` (or short`--yes`/`--y`) flag. In this case, you need to provide a few values in the command:
>
> ```bash
> mcdev init --y.credentialsName "yourCustomCredentialName" --y.client_id "yourClientIdHere" --y.client_secret "yourClientSecretHere" --y.auth_url "https://yourTenantSubdomainHere.auth.marketingcloudapis.com/" --y.gitRemoteUrl "https://my.git.server.com/myrepo.git" --y.account_id 00000000
> mcdev init --y.credentialName "yourCustomCredentialName" --y.client_id "yourClientIdHere" --y.client_secret "yourClientSecretHere" --y.auth_url "https://yourTenantSubdomainHere.auth.marketingcloudapis.com/" --y.gitRemoteUrl "https://my.git.server.com/myrepo.git" --y.account_id 00000000 --y.backupBUs "yes" --y.gitPush "yes"
> ```
#### 6.1.2. upgrade
Expand Down
228 changes: 62 additions & 166 deletions docs/dist/documentation.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ yargs
handler: (argv) => {
Mcdev.setSkipInteraction(argv.skipInteraction);
Mcdev.setLoggingLevel(argv);
Mcdev.initProject(argv.credentialsName, argv.skipInteraction);
Mcdev.initProject(argv.credentialsName);
},
})
.command({
Expand Down
21 changes: 5 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ class Mcdev {
into deploy directory
* @param {string} [argv.filter] filter file paths that start with any
* @param {TYPE.DeltaPkgItem[]} [argv.diffArr] list of files to include in delta package (skips git diff when provided)
* @param {TYPE.skipInteraction} [argv.skipInteraction] allows to skip interactive wizard
* @returns {Promise.<TYPE.DeltaPkgItem[]>} list of changed items
*/
static async createDeltaPkg(argv) {
Util.logger.info('Create Delta Package ::');
Mcdev.setSkipInteraction(argv.skipInteraction);
const properties = await config.getProperties();
if (!(await config.checkProperties(properties))) {
return null;
Expand All @@ -65,12 +63,7 @@ class Mcdev {
? // get source market and source BU from config
DevOps.getDeltaList(properties, argv.range, true, argv.filter)
: // If no custom filter was provided, use deployment marketLists & templating
DevOps.buildDeltaDefinitions(
properties,
argv.range,
argv.diffArr,
argv.skipInteraction
);
DevOps.buildDeltaDefinitions(properties, argv.range, argv.diffArr);
}

/**
Expand All @@ -90,17 +83,15 @@ class Mcdev {
Cli.explainTypes();
}
/**
* @param {boolean | TYPE.skipInteraction} [skipInteraction] signals what to insert automatically for things usually asked via wizard
* @returns {Promise.<boolean>} success flag
*/
static async upgrade(skipInteraction) {
Mcdev.setSkipInteraction(skipInteraction);
static async upgrade() {
const properties = await config.getProperties();
if (!properties) {
Util.logger.error('No config found. Please run mcdev init');
return false;
}
if ((await InitGit.initGitRepo(skipInteraction)).status === 'error') {
if ((await InitGit.initGitRepo()).status === 'error') {
return false;
}

Expand Down Expand Up @@ -295,14 +286,12 @@ class Mcdev {
* Creates template file for properties.json
*
* @param {string} [credentialsName] identifying name of the installed package / project
* @param {boolean | TYPE.skipInteraction} [skipInteraction] signals what to insert automatically for things usually asked via wizard
* @returns {Promise.<void>} -
*/
static async initProject(credentialsName, skipInteraction) {
static async initProject(credentialsName) {
Util.logger.info('mcdev:: Setting up project');
Mcdev.setSkipInteraction(skipInteraction);
const properties = await config.getProperties(!!credentialsName);
await Init.initProject(properties, credentialsName, skipInteraction);
await Init.initProject(properties, credentialsName);
}

/**
Expand Down
21 changes: 10 additions & 11 deletions lib/util/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,25 @@ const Cli = {
* used when initially setting up a project.
* loads default config and adds first credential
*
* @param {TYPE.skipInteraction} [skipInteraction] signals what to insert automatically for things usually asked via wizard
* @returns {Promise.<boolean>} success of init
*/
async initMcdevConfig(skipInteraction) {
async initMcdevConfig() {
Util.logger.info('-- Initialising server connection --');
Util.logger.info('Please enter a name for your "Installed Package" credentials:');
const propertiesTemplate = await config.getDefaultProperties();
delete propertiesTemplate.credentials.default;

// wait for the interaction to finish or else an outer await will run before this is done
return this._setCredential(propertiesTemplate, null, skipInteraction);
return this._setCredential(propertiesTemplate, null);
},
/**
* Extends template file for properties.json
*
* @param {TYPE.Mcdevrc} properties config file's json
* @param {TYPE.skipInteraction} [skipInteraction] signals what to insert automatically for things usually asked via wizard
* @returns {Promise.<void>} -
*/
async addExtraCredential(properties, skipInteraction) {
async addExtraCredential(properties) {
const skipInteraction = Util.skipInteraction;
if (!(await config.checkProperties(properties))) {
// return null here to avoid seeing 2 error messages for the same issue
return null;
Expand All @@ -54,7 +53,7 @@ const Cli = {
`Credential '${skipInteraction.credentialName}' already existing. If you tried updating please provide run 'mcdev init ${skipInteraction.credentialName}'`
);
}
return this._setCredential(properties, null, skipInteraction);
return this._setCredential(properties, null);
}
},
/**
Expand All @@ -63,15 +62,15 @@ const Cli = {
*
* @param {TYPE.Mcdevrc} properties config file's json
* @param {string} credName name of credential that needs updating
* @param {TYPE.skipInteraction} [skipInteraction] signals what to insert automatically for things usually asked via wizard
* @returns {Promise.<boolean>} success of update
*/
async updateCredential(properties, credName, skipInteraction) {
async updateCredential(properties, credName) {
const skipInteraction = Util.skipInteraction;
if (credName) {
if (!skipInteraction) {
Util.logger.info(`Please enter the details for '${credName}'`);
}
return await this._setCredential(properties, credName, skipInteraction);
return await this._setCredential(properties, credName);
}
},
/**
Expand Down Expand Up @@ -226,10 +225,10 @@ const Cli = {
*
* @param {TYPE.Mcdevrc} properties from config file
* @param {string} [credName] name of credential that needs updating
* @param {TYPE.skipInteraction} [skipInteraction] signals what to insert automatically for things usually asked via wizard
* @returns {Promise.<boolean | string>} success of refresh or credential name
*/
async _setCredential(properties, credName, skipInteraction) {
async _setCredential(properties, credName) {
const skipInteraction = Util.skipInteraction;
// Get user input
let credentialsGood = null;
let inputData;
Expand Down
4 changes: 2 additions & 2 deletions lib/util/devops.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ const DevOps = {
* @param {TYPE.Mcdevrc} properties project config file
* @param {string} range git commit range
* @param {TYPE.DeltaPkgItem[]} [diffArr] instead of running git diff the method can also get a list of files to process
* @param {TYPE.SkipInteraction} [skipInteraction] allows to skip interactive wizard
* @returns {Promise.<TYPE.DeltaPkgItem[]>} -
*/
async buildDeltaDefinitions(properties, range, diffArr, skipInteraction) {
async buildDeltaDefinitions(properties, range, diffArr) {
const skipInteraction = Util.skipInteraction;
// check if sourceTargetMapping is valid
if (
!properties.options.deployment.sourceTargetMapping ||
Expand Down
25 changes: 12 additions & 13 deletions lib/util/init.git.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const TYPE = require('../../types/mcdev.d');
// const TYPE = require('../../types/mcdev.d');
const File = require('./file');
const inquirer = require('inquirer');
const Util = require('./util');
Expand All @@ -14,10 +14,9 @@ const Init = {
/**
* check if git repo exists and otherwise create one
*
* @param {TYPE.skipInteraction} [skipInteraction] signals what to insert automatically for things usually asked via wizard
* @returns {Promise.<{status: string, repoName: string}>} success flag
*/
async initGitRepo(skipInteraction) {
async initGitRepo() {
const result = { status: null, repoName: null };
// check if git is installed (https://www.npmjs.com/package/command-exists)
if (!commandExists.sync('git')) {
Expand Down Expand Up @@ -65,10 +64,10 @@ const Init = {
}

// offer to update local user.name and user.email
await this._updateGitConfigUser(skipInteraction);
await this._updateGitConfigUser();
if (newRepoInitialized) {
// offer to insert git remote url now
result.repoName = await this._addGitRemote(skipInteraction);
result.repoName = await this._addGitRemote();
}

Util.logger.info('✔️ Git initialization done.');
Expand All @@ -78,10 +77,10 @@ const Init = {
/**
* offer to push the new repo straight to the server
*
* @param {boolean | TYPE.skipInteraction} [skipInteraction] signals what to insert automatically for things usually asked via wizard
* @returns {void}
*/
async gitPush(skipInteraction) {
async gitPush() {
const skipInteraction = Util.skipInteraction;
const gitRemotes = (await git.getRemotes(true)).filter((item) => item.name === 'origin');
if (gitRemotes.length && gitRemotes[0].refs.push) {
// check if remote repo is still empty (otherwise to risky to blindly push)
Expand All @@ -101,7 +100,7 @@ const Init = {
`Your remote Git repository is still empty and ready to store your initial backup. Hint: This is the server version of the repo which you share with your team.`
);
let responses;
if (!skipInteraction) {
if (!skipInteraction || !skipInteraction.gitPush !== 'yes') {
responses = await inquirer.prompt([
{
type: 'confirm',
Expand All @@ -111,7 +110,7 @@ const Init = {
},
]);
}
if (skipInteraction || responses.gitPush) {
if (skipInteraction.gitPush === 'yes' || responses.gitPush) {
Util.execSync('git', ['push', '-u', 'origin', 'master']);
}
} else if (remoteBranchesExist === true) {
Expand All @@ -124,10 +123,10 @@ const Init = {
/**
* offers to add the git remote origin
*
* @param {TYPE.skipInteraction} [skipInteraction] signals what to insert automatically for things usually asked via wizard
* @returns {string} repo name (optionally)
*/
async _addGitRemote(skipInteraction) {
async _addGitRemote() {
const skipInteraction = Util.skipInteraction;
// #1 ask if the user wants to do it now
let responses;
if (!skipInteraction) {
Expand Down Expand Up @@ -177,10 +176,10 @@ const Init = {
/**
* checks global config and ask to config the user info and then store it locally
*
* @param {boolean | TYPE.skipInteraction} [skipInteraction] signals what to insert automatically for things usually asked via wizard
* @returns {void}
*/
async _updateGitConfigUser(skipInteraction) {
async _updateGitConfigUser() {
const skipInteraction = Util.skipInteraction;
const gitUser = (await this._getGitConfigUser()) || {};
Util.logger.info(
`Please confirm your Git user name & email. It should be in the format 'FirstName LastName' and 'your.email@accenture.com'. The current (potentially wrong) values are provided as default. If correct, confirm with ENTER, otherwise please update:`
Expand Down
34 changes: 13 additions & 21 deletions lib/util/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ const Init = {
*
* @param {TYPE.Mcdevrc} properties config file's json
* @param {string} credentialName identifying name of the installed package / project
* @param {TYPE.skipInteraction} [skipInteraction] signals what to insert automatically for things usually asked via wizard
* @returns {Promise.<void>} -
*/
async initProject(properties, credentialName, skipInteraction) {
async initProject(properties, credentialName) {
const skipInteraction = Util.skipInteraction;
if (!properties) {
// try to get cached properties because we return null in case of a crucial error
properties = config.properties;
Expand All @@ -42,11 +42,7 @@ const Init = {
do {
error = false;
try {
const success = await Cli.updateCredential(
properties,
credentialName,
skipInteraction
);
const success = await Cli.updateCredential(properties, credentialName);
if (success) {
Util.logger.info(`✔️ Credential '${credentialName}' updated.`);
} else {
Expand All @@ -72,11 +68,7 @@ const Init = {
do {
error = false;
try {
const success = await Cli.updateCredential(
properties,
badCredName,
skipInteraction
);
const success = await Cli.updateCredential(properties, badCredName);
if (success) {
Util.logger.info(`✔️ Credential '${badCredName}' updated.`);
} else {
Expand Down Expand Up @@ -128,18 +120,18 @@ const Init = {
}
let credentialName;
if (skipInteraction || responses.isAddCredential) {
credentialName = await Cli.addExtraCredential(properties, skipInteraction);
credentialName = await Cli.addExtraCredential(properties);
}
if (credentialName) {
await this._downloadAllBUs(`${credentialName}/*`, 'update', skipInteraction);
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(skipInteraction);
const initGit = await InitGit.initGitRepo();
if (initGit.status === 'error') {
return;
}
Expand All @@ -152,7 +144,7 @@ const Init = {
}

// ask for credentials and create mcdev config
status = await Cli.initMcdevConfig(skipInteraction);
status = await Cli.initMcdevConfig();
if (!status) {
return;
}
Expand All @@ -169,7 +161,7 @@ const Init = {
await this._downloadAllBUs('"*"', initGit.status);

// backup to server
await InitGit.gitPush(skipInteraction);
await InitGit.gitPush();

// all done
Util.logger.info('You are now ready to work with Accenture SFMC DevTools!');
Expand All @@ -183,12 +175,12 @@ const Init = {
*
* @param {string} bu cred/bu or cred/* or *
* @param {string} gitStatus signals what state the git repo is in
* @param {boolean | TYPE.skipInteraction} [skipInteraction] signals what to insert automatically for things usually asked via wizard
* @returns {Promise.<void>} -
*/
async _downloadAllBUs(bu, gitStatus, skipInteraction) {
async _downloadAllBUs(bu, gitStatus) {
const skipInteraction = Util.skipInteraction;
let responses;
if (!skipInteraction) {
if (!skipInteraction || skipInteraction.backupBUs !== 'yes') {
responses = await inquirer.prompt([
{
type: 'confirm',
Expand All @@ -198,7 +190,7 @@ const Init = {
},
]);
}
if (skipInteraction || responses.initialRetrieveAll) {
if (skipInteraction.backupBUs === 'yes' || responses.initialRetrieveAll) {
Util.execSync('mcdev', ['retrieve', bu]);

if (gitStatus === 'init') {
Expand Down

0 comments on commit 62b9b39

Please sign in to comment.