astro-decap-cms
Version:
Add Decap CMS's admin dashboard to your Astro project
62 lines (61 loc) • 2.64 kB
JavaScript
import { spawn } from 'node:child_process';
import AdminDashboard from './vite-plugin-admin-dashboard.js';
const widgetPath = 'astro-decap-cms/identity-widget';
/**
* Creates a DecapCMS integration with the given options.
*
* @param {DecapCMSOptions} options - the options for configuring the DecapCMS integration
* @return {AstroIntegration} the DecapCMS integration
*/
export default function DecapCMS(options) {
let { disableIdentityWidgetInjection = false, adminPath = '/admin', config: cmsConfig, previewStyles = [] } = options;
if (!adminPath.startsWith('/')) {
throw new Error(`'adminPath' option must be a root-relative pathname, starting with "/", got "${adminPath}"`);
}
if (adminPath.endsWith('/')) {
adminPath = adminPath.slice(0, -1);
}
let proxy;
const DecapCMSIntegration = {
name: 'decap-cms',
hooks: {
'astro:config:setup': ({ config, injectRoute, injectScript, updateConfig }) => {
// const widgetPath = 'path/to/identity/widget'; // Replace with the actual path to the identity widget script
var _a;
const identityWidgetScript = `import { initIdentity } from '${widgetPath}'; initIdentity('${adminPath}');`;
const newConfig = {
site: config.site || process.env.URL,
vite: {
plugins: [
...(((_a = config.vite) === null || _a === void 0 ? void 0 : _a.plugins) || []),
AdminDashboard({
config: cmsConfig,
previewStyles,
identityWidget: disableIdentityWidgetInjection ? identityWidgetScript : '',
}),
],
},
};
updateConfig(newConfig);
injectRoute({
pattern: adminPath,
entrypoint: 'astro-decap-cms/admin-dashboard.astro',
});
if (!disableIdentityWidgetInjection) {
injectScript('page', identityWidgetScript);
}
},
'astro:server:start': () => {
proxy = spawn('decap-server', {
stdio: 'inherit',
shell: process.platform === 'win32',
});
process.on('exit', () => proxy.kill());
},
'astro:server:done': () => {
proxy.kill();
},
},
};
return DecapCMSIntegration;
}