Skip to content

Commit

Permalink
refactor(CLI): Modern logs for plugin install command
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Sep 28, 2021
1 parent b0d854a commit 8c5f22c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
31 changes: 25 additions & 6 deletions commands/plugin-install.js
Expand Up @@ -8,7 +8,7 @@ const _ = require('lodash');
const isPlainObject = require('type/plain-object/is');
const yaml = require('js-yaml');
const cloudformationSchema = require('@serverless/utils/cloudformation-schema');
const log = require('@serverless/utils/log');
const { legacy, log, progress, style } = require('@serverless/utils/log');
const ServerlessError = require('../lib/serverless-error');
const yamlAstParser = require('../lib/utils/yamlAstParser');
const npmCommandDeferred = require('../lib/utils/npm-command-deferred');
Expand All @@ -18,7 +18,10 @@ const {
validate,
} = require('../lib/commands/plugin-management');

const mainProgress = progress.get('main');

module.exports = async ({ configuration, serviceDir, configurationFilename, options }) => {
const commandRunStartTime = Date.now();
validate({ serviceDir });

const pluginInfo = getPluginInfo(options.name);
Expand All @@ -27,15 +30,24 @@ module.exports = async ({ configuration, serviceDir, configurationFilename, opti
const configurationFilePath = getServerlessFilePath({ serviceDir, configurationFilename });

const context = { configuration, serviceDir, configurationFilePath, pluginName, pluginVersion };
mainProgress.notice(
`Installing plugin "${pluginName}${pluginVersion === 'latest' ? '' : `@${pluginVersion}`}"`,
{ isMainEvent: true }
);
await installPlugin(context);
await addPluginToServerlessFile(context);

log(`Successfully installed "${pluginName}@${pluginVersion}"`);
legacy.log(`Successfully installed "${pluginName}@${pluginVersion}"`);
log.notice.success(
`Plugin "${pluginName}${
pluginVersion === 'latest' ? '' : `@${pluginVersion}`
}" installed ${style.aside(`(${Math.floor((Date.now() - commandRunStartTime) / 1000)}s)`)}`
);
};

const installPlugin = async ({ serviceDir, pluginName, pluginVersion }) => {
const pluginFullName = `${pluginName}@${pluginVersion}`;
log(`Installing plugin "${pluginFullName}" (this might take a few seconds...)`);
legacy.log(`Installing plugin "${pluginFullName}" (this might take a few seconds...)`);
await npmInstall(pluginFullName, { serviceDir });
};

Expand Down Expand Up @@ -115,13 +127,20 @@ const npmInstall = async (name, { serviceDir }) => {
shell: true,
});
} catch (error) {
process.stdout.write(error.stderrBuffer);
legacy.write(error.stderrBuffer);
log.error(String(error.stderrBuffer));
throw error;
}
};

const requestManualUpdate = (configurationFilePath) =>
log(`
const requestManualUpdate = (configurationFilePath) => {
legacy.log(`
Can't automatically add plugin into "${path.basename(configurationFilePath)}" file.
Please make it manually.
`);
log.notice.skip(
`Can't automatically add plugin into "${path.basename(
configurationFilePath
)}" file. Please add it manually.`
);
};
38 changes: 31 additions & 7 deletions lib/plugins/plugin/install.js
Expand Up @@ -11,19 +11,27 @@ const _ = require('lodash');
const isPlainObject = require('type/plain-object/is');
const yaml = require('js-yaml');
const cloudformationSchema = require('@serverless/utils/cloudformation-schema');
const log = require('@serverless/utils/log');
const { legacy, log, progress, style } = require('@serverless/utils/log');
const ServerlessError = require('../../serverless-error');
const cliCommandsSchema = require('../../cli/commands-schema');
const yamlAstParser = require('../../utils/yamlAstParser');
const fileExists = require('../../utils/fs/fileExists');
const pluginUtils = require('./lib/utils');
const npmCommandDeferred = require('../../utils/npm-command-deferred');

const requestManualUpdate = (serverlessFilePath) =>
log(`
const mainProgress = progress.get('main');

const requestManualUpdate = (serverlessFilePath) => {
legacy.log(`
Can't automatically add plugin into "${path.basename(serverlessFilePath)}" file.
Please make it manually.
`);
log.notice.skip(
`Can't automatically add plugin into "${path.basename(
serverlessFilePath
)}" file. Please add it manually.`
);
};

class PluginInstall {
constructor(serverless, options) {
Expand Down Expand Up @@ -57,7 +65,7 @@ class PluginInstall {
.then((plugins) => {
const plugin = plugins.find((item) => item.name === this.options.pluginName);
if (!plugin) {
this.serverless.cli.log('Plugin not found in serverless registry, continuing to install');
legacy.log('Plugin not found in serverless registry, continuing to install');
}
return BbPromise.bind(this)
.then(this.pluginInstall)
Expand All @@ -68,7 +76,16 @@ class PluginInstall {
'Successfully installed',
` "${this.options.pluginName}@${this.options.pluginVersion}"`,
].join('');
this.serverless.cli.log(message);
legacy.log(message);
log.notice.success(
`Plugin "${this.options.pluginName}${
this.options.pluginVersion === 'latest' ? '' : `@${this.options.pluginVersion}`
}" installed ${style.aside(
`(${Math.floor(
(Date.now() - this.serverless.pluginManager.commandRunStartTime) / 1000
)}s)`
)}`
);
});
});
}
Expand All @@ -81,7 +98,8 @@ class PluginInstall {
.then((exists) => {
// check if package.json is already present. Otherwise create one
if (!exists) {
this.serverless.cli.log('Creating an empty package.json file in your service directory');
mainProgress.notice('Creating an empty package.json file', { isMainEvent: true });
legacy.log('Creating an empty package.json file in your service directory');

const packageJsonFileContent = {
name: this.serverless.service.service,
Expand All @@ -101,7 +119,13 @@ class PluginInstall {
`Installing plugin "${pluginFullName}"`,
' (this might take a few seconds...)',
].join('');
this.serverless.cli.log(message);
legacy.log(message);
mainProgress.notice(
`Installing plugin "${this.options.pluginName}${
this.options.pluginVersion === 'latest' ? '' : `@${this.options.pluginVersion}`
}"`,
{ isMainEvent: true }
);
return this.npmInstall(pluginFullName);
});
}
Expand Down

0 comments on commit 8c5f22c

Please sign in to comment.