botpress
Version:
The world's first CMS for bots. Easily create, manage and extend chatbots.
117 lines (89 loc) • 4.08 kB
JavaScript
;
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _bluebird = require('bluebird');
var _bluebird2 = _interopRequireDefault(_bluebird);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _listeners = require('./listeners');
var _listeners2 = _interopRequireDefault(_listeners);
var _util = require('./util');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new _bluebird2.default(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return _bluebird2.default.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
module.exports = ({ projectLocation, bp }) => {
const licensesPath = _path2.default.join(bp.botpressPath, './licenses');
const getLicenses = () => {
const packageJsonPath = (0, _util.resolveProjectFile)('package.json', projectLocation, true);
const { license } = JSON.parse(_fs2.default.readFileSync(packageJsonPath));
const agplContent = _fs2.default.readFileSync(_path2.default.join(licensesPath, 'LICENSE_AGPL3')).toString();
const botpressContent = _fs2.default.readFileSync(_path2.default.join(licensesPath, 'LICENSE_BOTPRESS')).toString();
return {
agpl: {
name: 'AGPL-3.0-only',
licensedUnder: license === 'AGPL-3.0-only',
text: agplContent
},
botpress: {
name: 'Botpress',
licensedUnder: license.toLowerCase().indexOf('botpress') >= 0,
text: botpressContent
}
};
};
const changeLicense = _bluebird2.default.method(license => {
const packageJsonPath = (0, _util.resolveProjectFile)('package.json', projectLocation, true);
const licensePath = (0, _util.resolveProjectFile)('LICENSE', projectLocation, true);
const licenseFileName = license === 'AGPL-3.0-only' ? 'LICENSE_AGPL3' : 'LICENSE_BOTPRESS';
const licenseContent = _fs2.default.readFileSync(_path2.default.join(licensesPath, licenseFileName));
const pkg = JSON.parse(_fs2.default.readFileSync(packageJsonPath));
pkg.license = license;
_fs2.default.writeFileSync(licensePath, licenseContent);
_fs2.default.writeFileSync(packageJsonPath, JSON.stringify(pkg, null, 2));
});
const middleware = _listeners2.default.hear(/^BOT_LICENSE$/, (event, next) => {
const packageJsonPath = (0, _util.resolveProjectFile)('package.json', projectLocation, true);
const { license, name, author } = JSON.parse(_fs2.default.readFileSync(packageJsonPath));
const response = `Bot: ${name}
Created by: ${author}
License: ${license}
Botpress: ${bp.version}`;
const userId = event.user && event.user.id; // TODO Use botpress standard user getter
if (bp[event.platform] && bp[event.platform].sendText) {
bp[event.platform].sendText(userId, response);
} else {
bp.middlewares.sendOutgoing({
platform: event.platform,
type: 'text',
text: response,
raw: {
to: userId,
message: response,
responseTo: event
}
});
}
});
return {
getLicensing: (() => {
var _ref = _asyncToGenerator(function* () {
const licenses = getLicenses();
const currentLicense = _lodash2.default.find(licenses, { licensedUnder: true }) || licenses.botpress;
return {
licensed: true,
name: currentLicense.name,
status: 'Active',
text: currentLicense.text
};
});
return function getLicensing() {
return _ref.apply(this, arguments);
};
})(),
changeLicense,
middleware,
getFeatures: () => ({})
};
};
//# sourceMappingURL=licensing.js.map