|
2 | 2 |
|
3 | 3 | const BbPromise = require('bluebird'); |
4 | 4 | const childProcess = BbPromise.promisifyAll(require('child_process')); |
| 5 | +const fsp = require('fs').promises; |
5 | 6 | const fse = require('fs-extra'); |
6 | 7 | const path = require('path'); |
7 | 8 | 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'); |
8 | 13 | const ServerlessError = require('../../serverless-error'); |
9 | 14 | const cliCommandsSchema = require('../../cli/commands-schema'); |
10 | 15 | const yamlAstParser = require('../../utils/yamlAstParser'); |
11 | 16 | const fileExists = require('../../utils/fs/fileExists'); |
12 | 17 | const pluginUtils = require('./lib/utils'); |
13 | 18 | const npmCommandDeferred = require('../../utils/npm-command-deferred'); |
14 | 19 |
|
| 20 | +const requestManualUpdate = (serverlessFilePath) => |
| 21 | + log(` |
| 22 | + Can't automatically add plugin into "${path.basename(serverlessFilePath)}" file. |
| 23 | + Please make it manually. |
| 24 | +`); |
| 25 | + |
15 | 26 | class PluginInstall { |
16 | 27 | constructor(serverless, options) { |
17 | 28 | this.serverless = serverless; |
@@ -97,10 +108,7 @@ class PluginInstall { |
97 | 108 | const serverlessFilePath = this.getServerlessFilePath(); |
98 | 109 | const fileExtension = path.extname(serverlessFilePath); |
99 | 110 | 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); |
104 | 112 | return; |
105 | 113 | } |
106 | 114 |
|
@@ -136,7 +144,25 @@ class PluginInstall { |
136 | 144 | return; |
137 | 145 | } |
138 | 146 |
|
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 | + } |
140 | 166 | await yamlAstParser.addNewArrayItem( |
141 | 167 | serverlessFilePath, |
142 | 168 | checkIsArrayPluginsObject(serverlessFileObj.plugins) ? 'plugins' : 'plugins.modules', |
|
0 commit comments