apply this functionality to serverless.json

This commit is contained in:
horike37 2017-08-24 09:11:49 +09:00
parent 75bced3a0c
commit 80cccd01e8

View File

@ -137,7 +137,7 @@ class Plugin {
).devDependencies[pluginName];
if (pluginInstalled) {
yamlAstParser.addNewArrayItem(this.getServerlessFilePath(), 'plugins', pluginName);
this.addPluginToServerlessFile(pluginName);
this.serverless.cli.log(`Successfully installed "${pluginName}@${pluginVersion}"`);
} else {
const message = 'An error occurred while installing your plugin. Please try again...';
@ -177,8 +177,7 @@ class Plugin {
).devDependencies[pluginName];
if (!pluginStillAvailable) {
yamlAstParser
.removeExistingArrayItem(this.getServerlessFilePath(), 'plugins', pluginName);
this.removePluginFromServerlessFile(pluginName);
this.serverless.cli.log(`Successfully uninstalled "${pluginName}"`);
} else {
const message = 'An error occurred while uninstalling your plugin. Please try again...';
@ -235,12 +234,15 @@ class Plugin {
const servicePath = this.serverless.config.servicePath;
const serverlessYmlFilePath = path.join(servicePath, 'serverless.yml');
const serverlessYamlFilePath = path.join(servicePath, 'serverless.yaml');
const serverlessJsonFilePath = path.join(servicePath, 'serverless.json');
let serverlessFilePath;
if (fs.existsSync(serverlessYmlFilePath)) {
serverlessFilePath = serverlessYmlFilePath;
} else {
} else if (fs.existsSync(serverlessYamlFilePath)) {
serverlessFilePath = serverlessYamlFilePath;
} else {
serverlessFilePath = serverlessJsonFilePath;
}
return serverlessFilePath;
@ -252,6 +254,38 @@ class Plugin {
return fetch(endpoint).then((result) => result.json()).then((json) => json);
}
addPluginToServerlessFile(pluginName) {
const serverlessFilePath = this.getServerlessFilePath();
if (_.last(_.split(serverlessFilePath, '.')) === 'json') {
const serverlessFileObj = fse.readJsonSync(serverlessFilePath);
if (serverlessFileObj.plugins) {
serverlessFileObj.plugins.push(pluginName);
} else {
serverlessFileObj.plugins = [pluginName];
}
serverlessFileObj.plugins = _.sortedUniq(serverlessFileObj.plugins);
fse.writeJsonSync(serverlessFilePath, serverlessFileObj);
} else {
yamlAstParser.addNewArrayItem(serverlessFilePath, 'plugins', pluginName);
}
}
removePluginFromServerlessFile(pluginName) {
const serverlessFilePath = this.getServerlessFilePath();
if (_.last(_.split(serverlessFilePath, '.')) === 'json') {
const serverlessFileObj = fse.readJsonSync(serverlessFilePath);
if (serverlessFileObj.plugins) {
serverlessFileObj.plugins.pop(pluginName);
if (_.isEmpty(serverlessFileObj.plugins)) {
_.unset(serverlessFileObj, 'plugins');
}
}
fse.writeJsonSync(serverlessFilePath, serverlessFileObj);
} else {
yamlAstParser.removeExistingArrayItem(this.getServerlessFilePath(), 'plugins', pluginName);
}
}
display(plugins) {
let message = '';
if (plugins && plugins.length) {