botpress
Version:
The world's first CMS for bots. Easily create, manage and extend chatbots.
92 lines (68 loc) • 3.25 kB
JavaScript
;
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _mkdirp = require('mkdirp');
var _mkdirp2 = _interopRequireDefault(_mkdirp);
var _bluebird = require('bluebird');
var _bluebird2 = _interopRequireDefault(_bluebird);
var _glob = require('glob');
var _glob2 = _interopRequireDefault(_glob);
var _util = require('./util');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
Transparent Ghost Content Manager hs the same API but
proxies all calls directly to the FS.
It's used while in development.
*/
_bluebird2.default.promisifyAll(_fs2.default);
const mkdirpAsync = _bluebird2.default.promisify(_mkdirp2.default);
module.exports = ({ logger, projectLocation }) => {
const normalizeFolder = (0, _util.normalizeFolder)(projectLocation);
const folderOptions = {};
logger.info('[Ghost Content Manager] (transparent) Initialized');
return {
addRootFolder: (rootFolder, options = {}) => {
const { normalizedFolderName } = normalizeFolder(rootFolder);
logger.debug(`[Ghost Content Manager] (transparent) Added root folder ${normalizedFolderName}, doing nothing.`);
folderOptions[normalizedFolderName] = options;
},
upsertFile: (folder, file, content) => {
const { folderPath } = normalizeFolder(folder);
const filePath = _path2.default.join(folderPath, file);
const fullFileFolder = _path2.default.dirname(filePath);
return mkdirpAsync(fullFileFolder).then(() => _fs2.default.writeFileAsync(filePath, content)).catch(e => {
logger.error('[Ghost Content Manager] (transparent) upsertFile error', e);
throw e;
});
},
readFile: (folder, file) => {
const { folderPath, normalizedFolderName } = normalizeFolder(folder);
const filePath = _path2.default.join(folderPath, file);
const isBinary = (folderOptions[normalizedFolderName] || {}).isBinary || false;
return _fs2.default.readFileAsync(filePath, isBinary ? null : 'utf8').catch({ code: 'ENOENT' }, () => null).catch(e => {
logger.error('[Ghost Content Manager] (transparent) readFile error', e);
throw e;
});
},
deleteFile: (folder, file) => {
const { folderPath } = normalizeFolder(folder);
const filePath = _path2.default.join(folderPath, file);
return _fs2.default.unlinkAsync(filePath).catch(e => {
logger.error('[Ghost Content Manager] (transparent) deleteFile error', e);
throw e;
});
},
directoryListing: (rootFolder, fileEndingPattern = '', pathsToOmit = []) => {
const { folderPath } = normalizeFolder(rootFolder);
return _fs2.default.accessAsync(folderPath).then(() => _bluebird2.default.fromCallback(cb => (0, _glob2.default)(`**/*${fileEndingPattern}`, { cwd: folderPath }, cb)).then(paths => paths.filter(path => !pathsToOmit.includes(path))), () => []).catch(e => {
logger.error('[Ghost Content Manager] (transparent) directoryListing error', e);
throw e;
});
},
getPending: () => ({}),
getPendingWithContent: () => ({})
};
};
//# sourceMappingURL=transparent.js.map