@itxch/contentful-import
Version:
This tool allows you to import JSON dump exported by contentful-export
90 lines (89 loc) • 3.45 kB
JavaScript
const fs = require("fs");
const path = require("path");
const format = require("date-fns/format");
const _package = require("../package.json.cjs");
const headers = require("./utils/headers.cjs");
const proxy_js = require("contentful-batch-libs/dist/proxy.js");
const addSequenceHeader_js = require("contentful-batch-libs/dist/add-sequence-header.js");
const jsonExt = require("@discoveryjs/json-ext");
const SUPPORTED_ENTITY_TYPES = [
"contentTypes",
"tags",
"entries",
"assets",
"locales",
"webhooks",
"editorInterfaces"
];
async function parseOptions(params) {
const defaultOptions = {
skipContentModel: false,
skipLocales: false,
skipContentPublishing: false,
skipAssetUpdates: false,
skipContentUpdates: false,
useVerboseRenderer: false,
environmentId: "master",
rawProxy: false,
uploadAssets: false,
rateLimit: 7
};
const configFile = params.config ? require(path.resolve(process.cwd(), params.config)) : {};
const options = {
...defaultOptions,
...configFile,
...params,
headers: addSequenceHeader_js.addSequenceHeader(params.headers || headers.getHeadersConfig(params.header))
};
if (!options.spaceId) {
throw new Error("The `spaceId` option is required.");
}
if (!options.managementToken) {
throw new Error("The `managementToken` option is required.");
}
if (!options.contentFile && !options.content) {
throw new Error("Either the `contentFile` or `content` option are required.");
}
if (options.contentModelOnly && options.skipContentModel) {
throw new Error("`contentModelOnly` and `skipContentModel` cannot be used together");
}
if (options.skipLocales && !options.contentModelOnly) {
throw new Error("`skipLocales` can only be used together with `contentModelOnly`");
}
const proxySimpleExp = /.+:\d+/;
const proxyAuthExp = /.+:.+@.+:\d+/;
if (typeof options.proxy === "string" && options.proxy && !(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.startTime = /* @__PURE__ */ new Date();
if (!options.errorLogFile) {
options.errorLogFile = path.resolve(process.cwd(), `contentful-import-error-log-${options.spaceId}-${format(options.startTime, "yyyy-MM-dd'T'HH-mm-ss")}.json`);
} else {
options.errorLogFile = path.resolve(process.cwd(), options.errorLogFile);
}
options.accessToken = options.managementToken;
if (!options.content) {
const fileStream = fs.createReadStream(options.contentFile, { encoding: "utf8" });
options.content = await jsonExt.parseChunked(fileStream);
}
Object.keys(options.content).forEach((type) => {
if (SUPPORTED_ENTITY_TYPES.indexOf(type) === -1) {
delete options.content[type];
}
});
SUPPORTED_ENTITY_TYPES.forEach((type) => {
options.content[type] = options.content[type] || [];
});
if (typeof options.proxy === "string") {
options.proxy = proxy_js.proxyStringToObject(options.proxy);
}
if (!options.rawProxy && options.proxy) {
options.httpsAgent = proxy_js.agentFromProxy(options.proxy);
delete options.proxy;
}
options.application = options.managementApplication || `contentful.import/${_package.version}`;
options.feature = options.managementFeature || "library-import";
return options;
}
module.exports = parseOptions;
;