Skip to content

Commit aa8f7be

Browse files
committed
refactor: Do not rely on serverless.yamlParser
1 parent bab39c6 commit aa8f7be

2 files changed

Lines changed: 62 additions & 10 deletions

File tree

lib/plugins/plugin/install.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@
22

33
const BbPromise = require('bluebird');
44
const childProcess = BbPromise.promisifyAll(require('child_process'));
5+
const fsp = require('fs').promises;
56
const fse = require('fs-extra');
67
const path = require('path');
78
const _ = require('lodash');
9+
const isPlainObject = require('type/plain-object/is');
10+
const yaml = require('js-yaml');
11+
const cloudformationSchema = require('@serverless/utils/cloudformation-schema');
12+
const log = require('@serverless/utils/log');
813
const ServerlessError = require('../../serverless-error');
914
const cliCommandsSchema = require('../../cli/commands-schema');
1015
const yamlAstParser = require('../../utils/yamlAstParser');
1116
const fileExists = require('../../utils/fs/fileExists');
1217
const pluginUtils = require('./lib/utils');
1318
const npmCommandDeferred = require('../../utils/npm-command-deferred');
1419

20+
const requestManualUpdate = (serverlessFilePath) =>
21+
log(`
22+
Can't automatically add plugin into "${path.basename(serverlessFilePath)}" file.
23+
Please make it manually.
24+
`);
25+
1526
class PluginInstall {
1627
constructor(serverless, options) {
1728
this.serverless = serverless;
@@ -97,10 +108,7 @@ class PluginInstall {
97108
const serverlessFilePath = this.getServerlessFilePath();
98109
const fileExtension = path.extname(serverlessFilePath);
99110
if (fileExtension === '.js' || fileExtension === '.ts') {
100-
this.serverless.cli.log(`
101-
Can't automatically add plugin into "${path.basename(serverlessFilePath)}" file.
102-
Please make it manually.
103-
`);
111+
requestManualUpdate(serverlessFilePath);
104112
return;
105113
}
106114

@@ -136,7 +144,25 @@ class PluginInstall {
136144
return;
137145
}
138146

139-
const serverlessFileObj = await this.serverless.yamlParser.parse(serverlessFilePath);
147+
const serverlessFileObj = yaml.load(await fsp.readFile(serverlessFilePath, 'utf8'), {
148+
filename: serverlessFilePath,
149+
schema: cloudformationSchema,
150+
});
151+
if (serverlessFileObj.plugins != null) {
152+
// Plugins section can be behind veriables, opt-out in such case
153+
if (isPlainObject(serverlessFileObj.plugins)) {
154+
if (
155+
serverlessFileObj.plugins.modules != null &&
156+
!Array.isArray(serverlessFileObj.plugins.modules)
157+
) {
158+
requestManualUpdate(serverlessFilePath);
159+
return;
160+
}
161+
} else if (!Array.isArray(serverlessFileObj.plugins)) {
162+
requestManualUpdate(serverlessFilePath);
163+
return;
164+
}
165+
}
140166
await yamlAstParser.addNewArrayItem(
141167
serverlessFilePath,
142168
checkIsArrayPluginsObject(serverlessFileObj.plugins) ? 'plugins' : 'plugins.modules',

lib/plugins/plugin/uninstall.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@
22

33
const BbPromise = require('bluebird');
44
const childProcess = BbPromise.promisifyAll(require('child_process'));
5+
const fsp = require('fs').promises;
56
const fse = require('fs-extra');
67
const path = require('path');
78
const _ = require('lodash');
9+
const isPlainObject = require('type/plain-object/is');
10+
const yaml = require('js-yaml');
11+
const cloudformationSchema = require('@serverless/utils/cloudformation-schema');
12+
const log = require('@serverless/utils/log');
813
const cliCommandsSchema = require('../../cli/commands-schema');
914
const yamlAstParser = require('../../utils/yamlAstParser');
1015
const pluginUtils = require('./lib/utils');
1116

17+
const requestManualUpdate = (serverlessFilePath) =>
18+
log(`
19+
Can't automatically add plugin into "${path.basename(serverlessFilePath)}" file.
20+
Please make it manually.
21+
`);
22+
1223
class PluginUninstall {
1324
constructor(serverless, options) {
1425
this.serverless = serverless;
@@ -67,10 +78,7 @@ class PluginUninstall {
6778
const serverlessFilePath = this.getServerlessFilePath();
6879
const fileExtension = path.extname(serverlessFilePath);
6980
if (fileExtension === '.js' || fileExtension === '.ts') {
70-
this.serverless.cli.log(`
71-
Can't automatically remove plugin from "serverless.js" file.
72-
Please make it manually.
73-
`);
81+
requestManualUpdate(serverlessFilePath);
7482
return;
7583
}
7684

@@ -95,7 +103,25 @@ class PluginUninstall {
95103
return;
96104
}
97105

98-
const serverlessFileObj = await this.serverless.yamlParser.parse(serverlessFilePath);
106+
const serverlessFileObj = yaml.load(await fsp.readFile(serverlessFilePath, 'utf8'), {
107+
filename: serverlessFilePath,
108+
schema: cloudformationSchema,
109+
});
110+
if (serverlessFileObj.plugins != null) {
111+
// Plugins section can be behind veriables, opt-out in such case
112+
if (isPlainObject(serverlessFileObj.plugins)) {
113+
if (
114+
serverlessFileObj.plugins.modules != null &&
115+
!Array.isArray(serverlessFileObj.plugins.modules)
116+
) {
117+
requestManualUpdate(serverlessFilePath);
118+
return;
119+
}
120+
} else if (!Array.isArray(serverlessFileObj.plugins)) {
121+
requestManualUpdate(serverlessFilePath);
122+
return;
123+
}
124+
}
99125
await yamlAstParser.removeExistingArrayItem(
100126
serverlessFilePath,
101127
Array.isArray(serverlessFileObj.plugins) ? 'plugins' : 'plugins.modules',

0 commit comments

Comments
 (0)