sb-mig
Version:
CLI to rule the world. (and handle stuff related to Storyblok CMS)
139 lines (138 loc) • 4.44 kB
JavaScript
import Logger from "../../utils/logger.js";
import { getAllItemsWithPagination } from "../utils/request.js";
export const getAllPlugins = (config) => {
const { sbApi, spaceId } = config;
Logger.log("Trying to get all plugins.");
return getAllItemsWithPagination({
apiFn: ({ per_page, page }) => sbApi
.get(`field_types`, {
per_page,
page,
})
.then((res) => {
Logger.log(`Amount of field types: ${res.total}`);
return res;
})
.catch((err) => Logger.error(err)),
params: {},
itemsKey: "field_types",
});
};
export const getPlugin = (pluginName, config) => {
return getAllPlugins(config)
.then((res) => res.find((plugin) => plugin.name === pluginName))
.then((res) => {
if (!res) {
throw Error("Not Found - plugins does not exist");
}
if (Array.isArray(res) && res.length === 0) {
console.info(`There is no plugin named '${pluginName}'`);
return false;
}
return res;
})
.then((plugin) => {
return getPluginDetails(plugin, config)
.then((res) => res)
.catch((err) => console.error(err));
})
.catch((err) => {
Logger.warning(err.message);
return false;
});
};
export const getPluginDetails = (plugin, config) => {
const { sbApi } = config;
console.log(`Trying to get ${plugin.name} details `);
return sbApi
.get(`field_types/${plugin.id}`)
.then((res) => res.data)
.catch((err) => console.error(err));
};
export const updatePlugin = ({ plugin, body }, config) => {
const { sbApi, spaceId } = config;
return sbApi
.put(`field_types/${plugin.id}`, {
publish: true,
field_type: {
body,
compiled_body: "",
},
})
.then((res) => {
Logger.success(`'${plugin.name}' plugin updated!`);
return res.data;
})
.catch((err) => {
console.log(err);
console.error("Error happened :()");
});
};
export const createPlugin = (pluginName, config) => {
const { sbApi, spaceId } = config;
return sbApi
.post(`field_types`, {
publish: true,
field_type: {
name: pluginName,
},
})
.then((res) => {
Logger.success(`'${pluginName}' plugin created!`);
return res.data;
})
.catch((err) => {
console.log(err);
console.error("Error happened :()");
});
};
// File-based sync wrapper lives in `plugins.sync.ts` to keep this module CJS-safe.
export const syncPluginsData = async ({ plugins, dryRun, }, config) => {
const result = {
created: [],
updated: [],
skipped: [],
errors: [],
};
if (dryRun) {
Logger.warning("[dry-run] Plugin sync will only read remote data and report planned changes.");
}
const remotePlugins = dryRun ? await getAllPlugins(config) : [];
for (const p of plugins) {
const name = String(p?.name ?? "unknown");
if (!p?.name) {
result.skipped.push(name);
continue;
}
try {
if (dryRun) {
const plugin = Array.isArray(remotePlugins)
? remotePlugins.find((remotePlugin) => remotePlugin.name === name)
: undefined;
if (plugin) {
Logger.warning(`[dry-run] Would update plugin '${name}'.`);
result.updated.push(name);
}
else {
Logger.warning(`[dry-run] Would create plugin '${name}'.`);
result.created.push(name);
}
continue;
}
const plugin = await getPlugin(name, config);
if (plugin) {
await updatePlugin({ plugin: plugin.field_type, body: p.body }, config);
result.updated.push(name);
}
else {
const created = await createPlugin(name, config);
await updatePlugin({ plugin: created.field_type, body: p.body }, config);
result.created.push(name);
}
}
catch (e) {
result.errors.push({ name, message: String(e) });
}
}
return result;
};