fix(init,readme): component imports follow the project aliases, cn patch takes double quotes
This commit is contained in:
1 parent
35cb8a2914
commit
1f1b83ed49
2 files changed
+24
-6
No files matched your search
+22
-5
@@ -41,12 +41,29 @@ function write(file, content) {
|
||||
fs.writeFileSync(file, content);
|
||||
}
|
||||
|
||||
function copyDir(src, dest) {
|
||||
function rewriteImports(source, aliases = {}) {
|
||||
const lib = aliases.lib ?? '$lib';
|
||||
const targets = [
|
||||
['$lib/components/ui', aliases.ui ?? `${lib}/components/ui`],
|
||||
['$lib/components', aliases.components ?? `${lib}/components`],
|
||||
['$lib/hooks', aliases.hooks ?? `${lib}/hooks`],
|
||||
['$lib/utils', aliases.utils ?? `${lib}/utils`],
|
||||
['$lib', lib]
|
||||
];
|
||||
return source.replace(/(['"])(\$lib(?:[/.][^'"]*)?)\1/g, (_, quote, spec) => {
|
||||
const [from, to] = targets.find(
|
||||
([from]) => spec.startsWith(from) && /^([/.]|$)/.test(spec.slice(from.length))
|
||||
);
|
||||
return `${quote}${to}${spec.slice(from.length)}${quote}`;
|
||||
});
|
||||
}
|
||||
|
||||
function copyDir(src, dest, aliases) {
|
||||
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
||||
const from = path.join(src, entry.name);
|
||||
const to = path.join(dest, entry.name);
|
||||
if (entry.isDirectory()) copyDir(from, to);
|
||||
else write(to, fs.readFileSync(from));
|
||||
if (entry.isDirectory()) copyDir(from, to, aliases);
|
||||
else write(to, rewriteImports(fs.readFileSync(from, 'utf8'), aliases));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +152,7 @@ function patchUtils(utilsFile) {
|
||||
const ours = fs.readFileSync(UTILS_TS, 'utf8');
|
||||
const block = ours.slice(0, ours.indexOf('export function cn'));
|
||||
const next = src.replace(
|
||||
/import \{ clsx, type ClassValue \} from 'clsx';\s*\nimport \{ twMerge \} from 'tailwind-merge';\s*\n/,
|
||||
/import \{[^}]*\} from ['"]clsx['"];?\s*\nimport \{ twMerge \} from ['"]tailwind-merge['"];?\s*\n/,
|
||||
block
|
||||
);
|
||||
if (next === src) {
|
||||
@@ -192,7 +209,7 @@ function init() {
|
||||
fs.mkdirSync(backup, { recursive: true });
|
||||
fs.cpSync(target, path.join(backup, name), { recursive: true });
|
||||
}
|
||||
copyDir(path.join(REGISTRY_UI, name), target);
|
||||
copyDir(path.join(REGISTRY_UI, name), target, config.aliases);
|
||||
}
|
||||
log(`${components.length} components written to ${path.relative(cwd, uiDir)}`);
|
||||
if (fs.existsSync(backup)) log(`previous versions saved in ${path.relative(cwd, backup)}`);
|
||||
|
||||
Reference in new issue
Block a user