UNPKG

contentful-export

Version:

this tool allows you to export a space to a JSON dump

93 lines (90 loc) 4.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = parseOptions; var _contentfulBatchLibs = require("contentful-batch-libs"); var _format = require("date-fns/format"); var _path = require("path"); var _querystring = _interopRequireDefault(require("querystring")); var _package = require("../package.json"); var _headers = require("./utils/headers"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function parseOptions(params) { const defaultOptions = { environmentId: 'master', exportDir: process.cwd(), includeDrafts: false, includeArchived: false, skipRoles: false, skipContentModel: false, skipEditorInterfaces: false, skipContent: false, skipWebhooks: false, skipTags: false, stripTags: false, maxAllowedLimit: 1000, saveFile: true, useVerboseRenderer: false, rawProxy: false }; const configFile = params.config // eslint-disable-next-line @typescript-eslint/no-require-imports ? require((0, _path.resolve)(process.cwd(), params.config)) : {}; const options = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, defaultOptions), configFile), params), {}, { headers: (0, _contentfulBatchLibs.addSequenceHeader)(params.headers || (0, _headers.getHeadersConfig)(params.header)) }); // Validation if (!options.spaceId) { throw new Error('The `spaceId` option is required.'); } if (!options.managementToken) { throw new Error('The `managementToken` option is required.'); } options.startTime = new Date(); options.contentFile = options.contentFile || `contentful-export-${options.spaceId}-${options.environmentId}-${(0, _format.format)(options.startTime, "yyyy-MM-dd'T'HH-mm-ss")}.json`; options.logFilePath = (0, _path.resolve)(options.exportDir, options.contentFile); if (!options.errorLogFile) { options.errorLogFile = (0, _path.resolve)(options.exportDir, `contentful-export-error-log-${options.spaceId}-${options.environmentId}-${(0, _format.format)(options.startTime, "yyyy-MM-dd'T'HH-mm-ss")}.json`); } else { options.errorLogFile = (0, _path.resolve)(process.cwd(), options.errorLogFile); } // Further processing options.accessToken = options.managementToken; if (options.proxy) { if (typeof options.proxy === 'string') { const proxySimpleExp = /.+:\d+/; const proxyAuthExp = /.+:.+@.+:\d+/; if (!(proxySimpleExp.test(options.proxy) || proxyAuthExp.test(options.proxy))) { throw new Error('Please provide the proxy config in the following format:\nhost:port or user:password@host:port'); } options.proxy = (0, _contentfulBatchLibs.proxyStringToObject)(options.proxy); } if (!options.rawProxy) { options.httpsAgent = (0, _contentfulBatchLibs.agentFromProxy)(options.proxy); delete options.proxy; } } if (options.queryEntries && options.queryEntries.length > 0) { const querystr = options.queryEntries.join('&'); options.queryEntries = _querystring.default.parse(querystr); } if (options.queryAssets && options.queryAssets.length > 0) { const querystr = options.queryAssets.join('&'); options.queryAssets = _querystring.default.parse(querystr); } if (options.contentOnly) { options.skipRoles = true; options.skipContentModel = true; options.skipWebhooks = true; } options.application = options.managementApplication || `contentful.export/${_package.version}`; options.feature = options.managementFeature || 'library-export'; return options; } module.exports = exports.default;