Skip to content

Commit 3e439de

Browse files
authored
fix: Next 13 CSS File Location (#243)
1 parent 2cf60fc commit 3e439de

File tree

11 files changed

+484
-109
lines changed

11 files changed

+484
-109
lines changed

package-lock.json

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

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twify",
3-
"version": "0.4.2",
3+
"version": "0.4.3",
44
"description": "A Tool to Setup TailwindCSS in your Project with a Single Command",
55
"bin": {
66
"twify": "dist/main.js"
@@ -37,15 +37,17 @@
3737
"license": "MIT",
3838
"dependencies": {
3939
"chalk": "^5.0.1",
40-
"commander": "^9.3.0",
40+
"commander": "^10.0.0",
4141
"enquirer": "^2.3.6",
42-
"fs-extra": "^10.1.0",
42+
"fs-extra": "^11.1.0",
43+
"glob": "^8.1.0",
4344
"gradient-string": "^2.0.1",
4445
"jscodeshift": "^0.14.0",
4546
"ora": "^6.1.0"
4647
},
4748
"devDependencies": {
4849
"@types/fs-extra": "^9.0.13",
50+
"@types/glob": "^8.0.1",
4951
"@types/gradient-string": "^1.1.2",
5052
"@types/jscodeshift": "^0.11.5",
5153
"@types/node": "^18.0.0",
@@ -59,7 +61,7 @@
5961
"microbundle": "^0.15.0",
6062
"prettier": "^2.6.2",
6163
"typescript": "^4.6.4",
62-
"vitest": "^0.25.1"
64+
"vitest": "^0.27.2"
6365
},
6466
"prettier": {
6567
"singleQuote": true,

src/content.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ export function addContentToCode(code: string, content: string[]): string {
2323
}
2424

2525
export async function setupContent({ content }: Framework) {
26-
console.log(
27-
`\n${chalk.green('✔')} Configuring ${chalk.blue.bold(content)} content...`
28-
);
26+
console.log(`\n${chalk.green('✔')} Configuring content paths...`);
2927
console.log(chalk.blue(`- ${content.join('\n- ')}`));
3028

3129
const [contentPath] = tailwindConfigFiles

src/frameworks/nextjs.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Framework } from '../types';
2+
import { resolveCssLocation } from './steps/next';
23

34
const NextJS: Framework = {
45
requiredDependencies: ['tailwindcss', 'postcss', 'autoprefixer'],
56
initCommands: ['npx tailwindcss init -p'],
6-
cssLocation: './styles/globals.css',
7+
cssLocation: resolveCssLocation,
78
content: [
89
'./pages/**/*.{js,ts,jsx,tsx}',
910
'./components/**/*.{js,ts,jsx,tsx}',

src/frameworks/steps/next.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import glob from 'glob';
2+
import util from 'util';
3+
4+
export async function resolveCssLocation() {
5+
const [match] = await util.promisify(glob)('./**/globals.css');
6+
7+
if (!match) {
8+
return './styles/globals.css';
9+
}
10+
11+
return match;
12+
}

src/processor.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ export async function handle(framework: Framework, options: InitOptions) {
4545
}
4646

4747
// Write content to cssLocation
48+
const cssTarget =
49+
typeof cssLocation === 'string' ? cssLocation : await cssLocation();
4850
console.log(
49-
`\n${chalk.green('✔')} Setting up ${chalk.blue.bold(cssLocation)}...`
51+
`\n${chalk.green('✔')} Setting up ${chalk.blue.bold(cssTarget)}...`
5052
);
51-
fs.ensureFileSync(cssLocation);
52-
const exitingCss = fs.readFileSync(cssLocation, 'utf8');
53+
fs.ensureFileSync(cssTarget);
54+
const exitingCss = fs.readFileSync(cssTarget, 'utf8');
5355
const updatedCss = !options.keep ? CSS_STUB : `${exitingCss}\n\n${CSS_STUB}`;
54-
fs.writeFileSync(cssLocation, updatedCss);
56+
fs.writeFileSync(cssTarget, updatedCss);
5557

5658
await setupContent(framework);
5759

src/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ export interface Step {
1212
(): Promise<void>;
1313
}
1414

15+
type CSSLocation = () => Promise<string>;
16+
1517
export interface Framework {
1618
requiredDependencies: string[];
1719
initCommands: string[];
18-
cssLocation: string;
20+
cssLocation: string | CSSLocation;
1921
content: string[];
2022
steps: Step[];
2123
}

tests/__snapshots__/drivers.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports[`Drivers > has a list of drivers 1`] = `
88
"./src/**/*.{js,ts,jsx,tsx}",
99
"./app/**/*.{js,ts,jsx,tsx}",
1010
],
11-
"cssLocation": "./styles/globals.css",
11+
"cssLocation": [Function],
1212
"initCommands": [
1313
"npx tailwindcss init -p",
1414
],

tests/helpers.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
runCommand,
88
runCommandSpawn,
99
} from '../src/helpers';
10-
import { EventEmitter } from 'stream';
10+
import stream from 'stream';
1111

1212
vi.mock('child_process');
1313

@@ -106,7 +106,7 @@ describe('Helpers', () => {
106106
});
107107

108108
it('can run command with spawn', async () => {
109-
const emitter = new EventEmitter();
109+
const emitter = new stream.EventEmitter();
110110
// eslint-disable-next-line @typescript-eslint/no-explicit-any
111111
vi.mocked(spawn).mockReturnValue(emitter as any);
112112
const resolve = runCommandSpawn('cmd');

tests/processor.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe('Processor', () => {
9292
content: ['content'],
9393
requiredDependencies: [],
9494
initCommands: [],
95-
cssLocation: 'css',
95+
cssLocation: () => Promise.resolve('css'),
9696
steps: [step],
9797
};
9898

tests/steps/next.spec.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import glob from 'glob';
2+
import { resolveCssLocation } from '../../src/frameworks/steps/next';
3+
4+
vi.mock('glob');
5+
6+
describe('Next JS CSS', () => {
7+
afterEach(() => {
8+
vi.clearAllMocks();
9+
});
10+
11+
afterAll(() => {
12+
vi.resetAllMocks();
13+
});
14+
15+
it('can resolve css location', async () => {
16+
vi.mocked(glob)
17+
.mockImplementationOnce((_, cb) => {
18+
console.log(typeof cb, cb);
19+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20+
return (cb as any)(null, ['foo']) as any;
21+
})
22+
.mockImplementationOnce((_, cb) => {
23+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24+
return (cb as any)(null, []) as any;
25+
});
26+
const first = await resolveCssLocation();
27+
const second = await resolveCssLocation();
28+
29+
expect(first).toStrictEqual('foo');
30+
expect(second).toStrictEqual('./styles/globals.css');
31+
});
32+
});

0 commit comments

Comments
 (0)