Skip to content

Commit

Permalink
feat(CLI Onboarding): Add deploy step [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Jun 1, 2021
1 parent 3bac0f3 commit dc3ba46
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/Serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Serverless {
let configObject = config;
configObject = configObject || {};
this._isInvokedByGlobalInstallation = Boolean(configObject._isInvokedByGlobalInstallation);
this.isInvokedByInteractive = Boolean(configObject.isInvokedByInteractive);

if (configObject.serviceDir != null) {
// Modern intialization way, to be the only supported way with v3
Expand Down Expand Up @@ -115,6 +116,7 @@ class Serverless {
delete configObject.hasResolvedCommandsExternally;
delete configObject.isTelemetryReportedExternally;
delete configObject.options;
delete configObject.isInvokedByInteractive;

this.providers = {};

Expand Down Expand Up @@ -290,6 +292,11 @@ class Serverless {
}
this.cli.suppressLogIfPrintCommand(this.processedInput);

// TODO: TESTS
if (this.isInvokedByInteractive) {
this.cli.suppressLog();
}

// make sure the command exists before doing anything else
this.pluginManager.validateCommand(this.processedInput.commands);

Expand Down
6 changes: 6 additions & 0 deletions lib/classes/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class CLI {
this.log = function () {};
}

suppressLog() {
this.log = function () {};
this.printDot = function () {};
this.consoleLog = function () {};
}

displayHelp() {
if (!resolveCliInput().isHelpRequest) return false;
renderHelp(this.serverless.pluginManager.externalPlugins);
Expand Down
3 changes: 3 additions & 0 deletions lib/classes/PluginManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ class PluginManager {
.forEach((Plugin) => this.addPlugin(Plugin));
isRegisteringExternalPlugins = false;
if (EnterprisePlugin) this.dashboardPlugin = this.addPlugin(EnterprisePlugin);
if (this.serverless.isInvokedByInteractive) {
this.addPlugin(require('../plugins/deploy-interactive'));
}
isRegisteringExternalPlugins = true;
return this.asyncPluginInit();
}
Expand Down
82 changes: 82 additions & 0 deletions lib/cli/interactive-setup/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
'use strict';

const Serverless = require('../../Serverless');
const chalk = require('chalk');
const { confirm } = require('./utils');
const _ = require('lodash');
const AWS = require('aws-sdk');

const printMessage = (serviceName, hasBeenDeployed) => {
if (hasBeenDeployed) {
process.stdout.write(
`\n${chalk.green('Your new project is live and available in ')}${chalk.white.bold(
serviceName
)}\n`
);
} else {
process.stdout.write(
`\n${chalk.green('Your new project is available in ')}${chalk.white.bold(serviceName)}\n`
);
}

process.stdout.write('\nInside that directory, you can run serveral commands:\n');
process.stdout.write(`\n ${chalk.blue('serverless info')}\n`);
process.stdout.write(' View deployment and configuration details.\n');
process.stdout.write(`\n ${chalk.blue('serverless deploy')}\n`);
if (hasBeenDeployed) {
process.stdout.write(' Deploy your service again.\n');
} else {
process.stdout.write(' Deploy your service.\n');
}
process.stdout.write(`\n ${chalk.blue('serverless dashboard')}\n`);
process.stdout.write(' Open the dashboard to view metrics, logs and alerts.\n\n');
};

module.exports = {
isApplicable({ configuration, serviceDir, history }) {
if (!serviceDir) {
return false;
}

if (!history.has('service')) {
return false;
}

if (
_.get(configuration, 'provider') !== 'aws' &&
_.get(configuration, 'provider.name') !== 'aws'
) {
return false;
}

if (new AWS.S3().config.credentials) return false;

// TODO: CHECK IF PROVIDER HAS BEEN SETUP DURING AWS CREDETIALS STEP

return true;
},
async run({ configuration, configurationFilename, serviceDir }) {
const serviceName = configuration.service;
if (!(await confirm('Do you want to deploy your project?', { name: 'shouldDeploy' }))) {
printMessage(serviceName, false);
return;
}

// TODO: TESTS
const serverless = new Serverless({
configuration,
serviceDir,
configurationFilename,
isConfigurationResolved: true,
hasResolvedCommandsExternally: true,
isTelemetryReportedExternally: true,
isInvokedByInteractive: true,
commands: ['deploy'],
options: {}, // Empty options as the original ones are for interactive command
});
await serverless.init();

await serverless.run();
printMessage(serviceName, true);
},
};
1 change: 1 addition & 0 deletions lib/cli/interactive-setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const steps = {
dashboardSetOrg: require('@serverless/dashboard-plugin/lib/cli/interactive-setup/dashboard-set-org'),
awsCredentials: require('./aws-credentials'),
autoUpdate: require('./auto-update'),
deploy: require('./deploy'),
};

module.exports = async (context) => {
Expand Down
29 changes: 29 additions & 0 deletions lib/plugins/deploy-interactive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const ora = require('ora');

class InteractiveDeployProgress {
constructor(serverless) {
this.serverless = serverless;
this.deployProgress = ora('Deploying your project...');
this.packageProgress = ora('Packaging your project...');

this.hooks = {
'before:deploy:deploy': async () => {
this.deployProgress.start();
},
'deploy:finalize': async () => {
this.deployProgress.succeed('Deployment succesful!');
},

'package:initialize': async () => {
this.packageProgress.start();
},
'package:finalize': async () => {
this.packageProgress.succeed('Packaging succesful!');
},
};
}
}

module.exports = InteractiveDeployProgress;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"ncjsm": "^4.2.0",
"node-fetch": "^2.6.1",
"object-hash": "^2.1.1",
"ora": "^5.4.0",
"path2": "^0.1.0",
"promise-queue": "^2.2.5",
"replaceall": "^0.1.6",
Expand Down

0 comments on commit dc3ba46

Please sign in to comment.