Skip to content

Commit e141e00

Browse files
authored
feat: Add Laravel Vite Driver (#33)
1 parent 118ef55 commit e141e00

File tree

14 files changed

+131
-8
lines changed

14 files changed

+131
-8
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ Below are the list of Supported Projects,
3232
| Nuxt 2 | :white_check_mark: | :white_check_mark: |
3333
| Nuxt 3 | :white_check_mark: | :white_check_mark: |
3434
| Svelte Kit | :white_check_mark: | :white_check_mark: |
35-
| Laravel Mix | :o: WIP | :o: WIP |
36-
| Laravel Vite | :o: WIP | :o: WIP |
37-
| Gatsby | :o: WIP | :o: WIP |
35+
| Laravel Vite | :white_check_mark: | :white_check_mark: |
3836
| Vite | :white_check_mark: | :white_check_mark: |
3937
| Angular | :white_check_mark: | :white_check_mark: |
4038
| Create React App | :white_check_mark: | :white_check_mark: |
39+
| Gatsby | :o: WIP | :o: WIP |
4140

4241

4342
## :microscope: Testing

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twify",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "A Tool to Setup TailwindCSS in your Project with a Single Command",
55
"bin": {
66
"twify": "dist/main.js"

src/drivers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export const drivers = {
99
Vite: (): DriverImport => import('./frameworks/vite'),
1010
Angular: (): DriverImport => import('./frameworks/angular'),
1111
CreateReactApp: (): DriverImport => import('./frameworks/cra'),
12+
LaravelVite: (): DriverImport => import('./frameworks/laravel'),
1213
};

src/frameworks/laravel.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Framework } from '../types';
2+
import { setupWelcomePage } from './steps/laravel';
3+
4+
const Laravel: Framework = {
5+
requiredDependencies: ['tailwindcss', 'postcss', 'autoprefixer'],
6+
initCommands: ['npx tailwindcss init -p'],
7+
cssLocation: './resources/css/app.css',
8+
content: {
9+
name: 'tailwind.config.js',
10+
files: [
11+
'./resources/**/*.blade.php',
12+
'./resources/**/*.{js,jsx,ts,tsx,vue,svelte}',
13+
],
14+
},
15+
steps: [setupWelcomePage],
16+
};
17+
18+
export default Laravel;

src/frameworks/steps/laravel.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import chalk from 'chalk';
2+
import fs from 'fs-extra';
3+
import path from 'path';
4+
5+
export async function setupWelcomePage() {
6+
const filename = 'welcome.blade.php';
7+
const welcomeFile = path.join(process.cwd(), `resources/views`, filename);
8+
const welcomePage = await fs.readFile(welcomeFile, 'utf8');
9+
10+
console.log(`\n- Setting up ${chalk.blue.bold(filename)}...`);
11+
12+
await fs.writeFile(
13+
welcomeFile,
14+
welcomePage.replace(
15+
/<\/head>/,
16+
"\t@vite(['resources/css/app.css'])\n\t</head>"
17+
)
18+
);
19+
}

src/frameworks/vite.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const Vite: Framework = {
77
cssLocation: './src/style.css',
88
content: {
99
name: 'tailwind.config.js',
10-
files: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}'],
10+
files: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx,svelte}'],
1111
},
1212
steps: [setupMainFile],
1313
};

src/helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function detectFramework(): Driver | undefined {
2222
if (dependencies['nuxt']) return 'Nuxt2';
2323
if (dependencies['@angular/core']) return 'Angular';
2424
if (dependencies['react-scripts']) return 'CreateReactApp';
25+
if (devDependencies['laravel-vite-plugin']) return 'LaravelVite';
2526
if (devDependencies['@sveltejs/kit']) return 'SvelteKit';
2627
if (devDependencies['nuxt']) return 'Nuxt3';
2728
if (devDependencies['vite']) return 'Vite';

tests/__snapshots__/drivers.spec.ts.snap

+25-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ exports[`Drivers > has a list of drivers 6`] = `
132132
"content": {
133133
"files": [
134134
"./index.html",
135-
"./src/**/*.{vue,js,ts,jsx,tsx}",
135+
"./src/**/*.{vue,js,ts,jsx,tsx,svelte}",
136136
],
137137
"name": "tailwind.config.js",
138138
},
@@ -192,3 +192,27 @@ exports[`Drivers > has a list of drivers 8`] = `
192192
"steps": [],
193193
}
194194
`;
195+
196+
exports[`Drivers > has a list of drivers 9`] = `
197+
{
198+
"content": {
199+
"files": [
200+
"./resources/**/*.blade.php",
201+
"./resources/**/*.{js,jsx,ts,tsx,vue,svelte}",
202+
],
203+
"name": "tailwind.config.js",
204+
},
205+
"cssLocation": "./resources/css/app.css",
206+
"initCommands": [
207+
"npx tailwindcss init -p",
208+
],
209+
"requiredDependencies": [
210+
"tailwindcss",
211+
"postcss",
212+
"autoprefixer",
213+
],
214+
"steps": [
215+
[Function],
216+
],
217+
}
218+
`;

tests/drivers.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('Drivers', () => {
1212
'Vite',
1313
'Angular',
1414
'CreateReactApp',
15+
'LaravelVite',
1516
] as (keyof typeof drivers)[];
1617
expect(driversList).toEqual(expectedDrivers);
1718

tests/fixtures/laravel/welcome.stub

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<head>
3+
<title>Laravel</title>
4+
</head>
5+
<body>
6+
</body>
7+
</html>

tests/helpers.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ describe('Helpers', () => {
2727
pkg.mockReturnValue({ dependencies: { 'react-scripts': '1.0.0' } });
2828
expect(detectFramework()).toBe('CreateReactApp');
2929

30+
pkg.mockReturnValue({
31+
devDependencies: { 'laravel-vite-plugin': '1.0.0' },
32+
});
33+
expect(detectFramework()).toBe('LaravelVite');
34+
3035
pkg.mockReturnValue({ devDependencies: { '@sveltejs/kit': '1.0.0' } });
3136
expect(detectFramework()).toBe('SvelteKit');
3237

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Vitest Snapshot v1
2+
3+
exports[`Vite Steps > can setup laravel project 1`] = `
4+
"<html>
5+
<head>
6+
<title>Laravel</title>
7+
@vite(['resources/css/app.css'])
8+
</head>
9+
<body>
10+
</body>
11+
</html>"
12+
`;

tests/steps/laravel.spec.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import fs from 'fs-extra';
2+
import { setupWelcomePage } from '../../src/frameworks/steps/laravel';
3+
4+
async function runStep(file: string) {
5+
const code = fs.readFileSync(`./tests/fixtures/laravel/${file}`, 'utf8');
6+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7+
vi.spyOn(fs, 'readFile').mockResolvedValue(code as any);
8+
const writeSpy = vi.spyOn(fs, 'writeFile').mockResolvedValue();
9+
10+
await setupWelcomePage();
11+
12+
const [fileName, content] = writeSpy.mock.lastCall || [];
13+
14+
return { fileName, content };
15+
}
16+
17+
describe('Vite Steps', () => {
18+
beforeAll(() => {
19+
vi.stubGlobal('console', { ...console, log: vi.fn() });
20+
});
21+
22+
afterEach(() => {
23+
vi.clearAllMocks();
24+
});
25+
26+
afterAll(() => {
27+
vi.resetAllMocks();
28+
});
29+
30+
it('can setup laravel project', async () => {
31+
const { fileName, content } = await runStep('welcome.stub');
32+
33+
expect(fileName).toMatch('welcome.blade.php');
34+
expect(content).toMatchSnapshot();
35+
});
36+
});

0 commit comments

Comments
 (0)