serverless/test/utils/plugins.js
2020-09-09 11:17:21 +02:00

31 lines
816 B
JavaScript

'use strict';
const path = require('path');
const fse = require('fs-extra');
// mock to test functionality bound to a serverless plugin
class ServerlessPlugin {
constructor(serverless, options, testSubject) {
this.options = options;
this.serverless = serverless;
Object.assign(this, testSubject);
}
}
function installPlugin(installDir, PluginClass) {
const pluginPkg = { name: path.basename(installDir), version: '0.0.0' };
const className = new PluginClass().constructor.name;
fse.outputFileSync(path.join(installDir, 'package.json'), JSON.stringify(pluginPkg), 'utf8');
fse.outputFileSync(
path.join(installDir, 'index.js'),
`"use strict";\n${PluginClass.toString()}\nmodule.exports = ${className}`,
'utf8'
);
}
module.exports = {
ServerlessPlugin,
installPlugin,
};