flowbite-react
Version:
Official React components built for Flowbite and Tailwind CSS
70 lines (62 loc) • 2.19 kB
JavaScript
import fs__default from 'fs/promises';
import { parse } from 'recast';
import { initFilePath, initJsxFilePath } from '../consts.js';
import { compareNodes } from '../utils/compare-nodes.js';
async function setupInit(config) {
const content = `
/* eslint-disable */
// @ts-nocheck
// biome-ignore-all lint: auto-generated file
// This file is auto-generated by the flowbite-react CLI.
// Do not edit this file directly.
// Instead, edit the .flowbite-react/config.json file.
import { StoreInit } from "flowbite-react/store/init";
import React from "react";
export const CONFIG = {
dark: ${config.dark},
prefix: "${config.prefix}",
version: ${config.version},
};
export function ThemeInit() {
return <StoreInit {...CONFIG} />;
}
ThemeInit.displayName = "ThemeInit";
`.trim();
const targetPath = config.tsx ? initFilePath : initJsxFilePath;
const oldPath = config.tsx ? initJsxFilePath : initFilePath;
try {
let currentContent = "";
try {
currentContent = await fs__default.readFile(targetPath, "utf-8");
} catch {
console.log(`Creating ${targetPath} file...`);
setTimeout(() => fs__default.writeFile(targetPath, content), 10);
}
if (currentContent) {
const currentAst = removeReactImport(parse(currentContent));
const newAst = removeReactImport(parse(content));
if (!compareNodes(currentAst.program, newAst.program)) {
console.log(`Updating ${targetPath} file...`);
setTimeout(() => fs__default.writeFile(targetPath, content), 10);
}
}
try {
await fs__default.access(oldPath);
console.log(`Removing ${oldPath} file...`);
await fs__default.unlink(oldPath);
} catch {
}
} catch (error) {
console.error(`Failed to update ${targetPath}:`, error);
}
}
function removeReactImport(ast) {
if (ast?.program?.body) {
ast.program.body = ast.program.body.filter(
(node) => !(node.type === "ImportDeclaration" && "value" in node.source && typeof node.source.value === "string" && node.source.value === "react" && node.specifiers?.[0]?.local?.name === "React")
);
}
return ast;
}
export { setupInit };
//# sourceMappingURL=setup-init.js.map