Skip to content

Commit

Permalink
🔨 feat: copy components script (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
omero authored Sep 18, 2024
1 parent 77aa302 commit b0cd0c8
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"private": true,
"scripts": {
"build": "yarn lerna run build",
"release": "yarn build && yarn changeset publish"
"release": "yarn build && yarn changeset publish",
"copy:components": "node scripts/copy-components.js"
},
"engines": {
"node": ">=20.15.1",
Expand Down
55 changes: 55 additions & 0 deletions scripts/copy-components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const fs = require('fs');
const path = require('path');

// Storybook components directory
const componentsDir = 'starters/storybook/app/components/ui';
// Remix components directory
const remixDir = 'starters/remix/app/components/ui';
// Next components directory
const nextDir = 'starters/next/components/ui';

function copyComponents(source, target) {
try {
fs.rmSync(target, { recursive: true, force: true });
fs.cpSync(source, target, { recursive: true });
} catch (error) {
console.error('An error occurred:', error);
}
}

function modifyComponents() {
try {
// Add use client to next components
const clientComponents = [
'/Header/Header.tsx',
];
for (const component of clientComponents) {
const fileName = path.join(nextDir, component);
const content = fs.readFileSync(fileName, 'utf8');
fs.writeFileSync(fileName, `'use client'\n\n${content}`);
}
// Replace path alias to next components
const files = fs.readdirSync(nextDir, { withFileTypes: true, recursive: true, encoding: 'utf8' });
for (const file of files) {
if (file.isDirectory()) {
continue;
}
if (!file.name.endsWith('.tsx')) {
continue;
}
const fileName = path.join(file.parentPath, file.name);
const content = fs.readFileSync(fileName, 'utf8');
fs.writeFileSync(fileName, content.replaceAll('~/', '@/'));
}
}
catch (error) {
console.error('An error occurred:', error);
}
}

console.log('Copying remix components...');
copyComponents(componentsDir, remixDir)
console.log('Copying next components...');
copyComponents(componentsDir, nextDir)
console.log('Modifying next components...');
modifyComponents();
12 changes: 10 additions & 2 deletions starters/next/components/ui/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ export const Header = ({
asChild
{...actionProps}
>
<a href={href} target={internal ? '_self' : '_blank'}>
<a
href={href}
target={internal ? '_self' : '_blank'}
rel="noreferrer"
>
{text}
</a>
</Button>
Expand Down Expand Up @@ -201,7 +205,11 @@ export const Header = ({
asChild
{...actionProps}
>
<a href={href} target={internal ? '_self' : '_blank'}>
<a
href={href}
target={internal ? '_self' : '_blank'}
rel="noreferrer"
>
{text}
</a>
</Button>
Expand Down

0 comments on commit b0cd0c8

Please sign in to comment.