Skip to content

Commit

Permalink
feat: Search for Proper Tailwind Config File (#87)
Browse files Browse the repository at this point in the history
* feat: Search for Proper Tailwind Config File

* Update to Patch Version

* Minor tweaks

* Proper message when tailwind file not found
  • Loading branch information
tzsk authored Aug 19, 2022
1 parent e61f85a commit dbd86ee
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 138 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twify",
"version": "0.3.2",
"version": "0.3.3",
"description": "A Tool to Setup TailwindCSS in your Project with a Single Command",
"bin": {
"twify": "dist/main.js"
Expand Down
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ export const CSS_STUB = `@tailwind base;
`;

export const prettierDependencies = ['prettier', 'prettier-plugin-tailwindcss'];

export const tailwindConfigFiles = [
'tailwind.config.js',
'tailwind.config.cjs',
];
18 changes: 12 additions & 6 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs-extra';
import path from 'path';
import j from 'jscodeshift';
import { Framework } from './types';
import { tailwindConfigFiles } from './constants';

export function addContentToCode(code: string, content: string[]): string {
const parse = j.withParser('flow');
Expand All @@ -23,16 +24,21 @@ export function addContentToCode(code: string, content: string[]): string {

export async function setupContent({ content }: Framework) {
console.log(
`\n${chalk.green('✔')} Configuring ${chalk.blue.bold(
content.name
)} content...`
`\n${chalk.green('✔')} Configuring ${chalk.blue.bold(content)} content...`
);
console.log(chalk.blue(`- ${content.files.join('\n- ')}`));
console.log(chalk.blue(`- ${content.join('\n- ')}`));

const contentPath = path.join(process.cwd(), content.name);
const [contentPath] = tailwindConfigFiles
.map((file) => path.join(process.cwd(), file))
.filter((file) => fs.existsSync(file));

if (!contentPath) {
console.log(chalk.red('✘ Tailwind config not found.'));
return;
}

const fileContent = await fs.readFile(contentPath, 'utf8');
const newContent = addContentToCode(fileContent, content.files);
const newContent = addContentToCode(fileContent, content);

await fs.writeFile(contentPath, newContent);
}
5 changes: 1 addition & 4 deletions src/frameworks/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ const Angular: Framework = {
requiredDependencies: ['tailwindcss', 'postcss', 'autoprefixer'],
initCommands: ['npx tailwindcss init'],
cssLocation: './src/styles.css',
content: {
name: 'tailwind.config.js',
files: ['./src/**/*.{html,ts}'],
},
content: ['./src/**/*.{html,ts}'],
steps: [],
};

Expand Down
5 changes: 1 addition & 4 deletions src/frameworks/cra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ const CreateReactApp: Framework = {
requiredDependencies: ['tailwindcss', 'postcss', 'autoprefixer'],
initCommands: ['npx tailwindcss init -p'],
cssLocation: './src/index.css',
content: {
name: 'tailwind.config.js',
files: ['./src/**/*.{js,jsx,ts,tsx}'],
},
content: ['./src/**/*.{js,jsx,ts,tsx}'],
steps: [],
};

Expand Down
11 changes: 4 additions & 7 deletions src/frameworks/laravel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ const Laravel: Framework = {
requiredDependencies: ['tailwindcss', 'postcss', 'autoprefixer'],
initCommands: ['npx tailwindcss init -p'],
cssLocation: './resources/css/app.css',
content: {
name: 'tailwind.config.js',
files: [
'./resources/**/*.blade.php',
'./resources/**/*.{js,jsx,ts,tsx,vue,svelte}',
],
},
content: [
'./resources/**/*.blade.php',
'./resources/**/*.{js,jsx,ts,tsx,vue,svelte}',
],
steps: [setupWelcomePage],
};

Expand Down
11 changes: 4 additions & 7 deletions src/frameworks/nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ const NextJS: Framework = {
requiredDependencies: ['tailwindcss', 'postcss', 'autoprefixer'],
initCommands: ['npx tailwindcss init -p'],
cssLocation: './styles/globals.css',
content: {
name: 'tailwind.config.js',
files: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
],
},
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
],
steps: [],
};

Expand Down
17 changes: 7 additions & 10 deletions src/frameworks/nuxt2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ const NuxtJS: Framework = {
],
initCommands: ['npx tailwindcss init'],
cssLocation: './assets/css/main.css',
content: {
name: 'tailwind.config.js',
files: [
'./components/**/*.{js,vue,ts}',
'./layouts/**/*.vue',
'./pages/**/*.vue',
'./plugins/**/*.{js,ts}',
'./nuxt.config.{js,ts}',
],
},
content: [
'./components/**/*.{js,vue,ts}',
'./layouts/**/*.vue',
'./pages/**/*.vue',
'./plugins/**/*.{js,ts}',
'./nuxt.config.{js,ts}',
],
steps: [setupConfigFile],
};

Expand Down
17 changes: 7 additions & 10 deletions src/frameworks/nuxt3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ const NuxtJS: Framework = {
],
initCommands: ['npx tailwindcss init'],
cssLocation: './assets/css/main.css',
content: {
name: 'tailwind.config.js',
files: [
'./components/**/*.{js,vue,ts}',
'./layouts/**/*.vue',
'./pages/**/*.vue',
'./app.vue',
'./plugins/**/*.{js,ts}',
],
},
content: [
'./components/**/*.{js,vue,ts}',
'./layouts/**/*.vue',
'./pages/**/*.vue',
'./app.vue',
'./plugins/**/*.{js,ts}',
],
steps: [setupConfigFile],
};

Expand Down
5 changes: 1 addition & 4 deletions src/frameworks/remix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { setupIndexFile, setupPackageJson } from './steps/remix';
const Remix: Framework = {
requiredDependencies: ['tailwindcss', 'postcss', 'autoprefixer'],
initCommands: ['npx tailwindcss init -p'],
content: {
name: 'tailwind.config.js',
files: ['./app/**/*.{js,ts,jsx,tsx}'],
},
content: ['./app/**/*.{js,ts,jsx,tsx}'],
cssLocation: './styles/app.css',
steps: [setupPackageJson, setupIndexFile],
};
Expand Down
5 changes: 1 addition & 4 deletions src/frameworks/sveltekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ const SvelteKit: Framework = {
],
initCommands: ['npx tailwindcss init tailwind.config.cjs -p'],
cssLocation: './src/app.css',
content: {
name: 'tailwind.config.cjs',
files: ['./src/**/*.{html,js,svelte,ts}'],
},
content: ['./src/**/*.{html,js,svelte,ts}'],
steps: [movePostCSS, setupConfigFile, setupLayoutFile],
};

Expand Down
5 changes: 1 addition & 4 deletions src/frameworks/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ const Vite: Framework = {
requiredDependencies: ['tailwindcss', 'postcss', 'autoprefixer'],
initCommands: ['npx tailwindcss init -p'],
cssLocation: './src/style.css',
content: {
name: 'tailwind.config.js',
files: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx,svelte}'],
},
content: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx,svelte}'],
steps: [setupMainFile],
};

Expand Down
5 changes: 1 addition & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ export interface Framework {
requiredDependencies: string[];
initCommands: string[];
cssLocation: string;
content: {
name: string;
files: string[];
};
content: string[];
steps: Step[];
}

Expand Down
103 changes: 38 additions & 65 deletions tests/__snapshots__/drivers.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

exports[`Drivers > has a list of drivers 1`] = `
{
"content": {
"files": [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],
"name": "tailwind.config.js",
},
"content": [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],
"cssLocation": "./styles/globals.css",
"initCommands": [
"npx tailwindcss init -p",
Expand All @@ -24,16 +21,13 @@ exports[`Drivers > has a list of drivers 1`] = `

exports[`Drivers > has a list of drivers 2`] = `
{
"content": {
"files": [
"./components/**/*.{js,vue,ts}",
"./layouts/**/*.vue",
"./pages/**/*.vue",
"./plugins/**/*.{js,ts}",
"./nuxt.config.{js,ts}",
],
"name": "tailwind.config.js",
},
"content": [
"./components/**/*.{js,vue,ts}",
"./layouts/**/*.vue",
"./pages/**/*.vue",
"./plugins/**/*.{js,ts}",
"./nuxt.config.{js,ts}",
],
"cssLocation": "./assets/css/main.css",
"initCommands": [
"npx tailwindcss init",
Expand All @@ -52,16 +46,13 @@ exports[`Drivers > has a list of drivers 2`] = `

exports[`Drivers > has a list of drivers 3`] = `
{
"content": {
"files": [
"./components/**/*.{js,vue,ts}",
"./layouts/**/*.vue",
"./pages/**/*.vue",
"./app.vue",
"./plugins/**/*.{js,ts}",
],
"name": "tailwind.config.js",
},
"content": [
"./components/**/*.{js,vue,ts}",
"./layouts/**/*.vue",
"./pages/**/*.vue",
"./app.vue",
"./plugins/**/*.{js,ts}",
],
"cssLocation": "./assets/css/main.css",
"initCommands": [
"npx tailwindcss init",
Expand All @@ -79,12 +70,9 @@ exports[`Drivers > has a list of drivers 3`] = `

exports[`Drivers > has a list of drivers 4`] = `
{
"content": {
"files": [
"./app/**/*.{js,ts,jsx,tsx}",
],
"name": "tailwind.config.js",
},
"content": [
"./app/**/*.{js,ts,jsx,tsx}",
],
"cssLocation": "./styles/app.css",
"initCommands": [
"npx tailwindcss init -p",
Expand All @@ -103,12 +91,9 @@ exports[`Drivers > has a list of drivers 4`] = `

exports[`Drivers > has a list of drivers 5`] = `
{
"content": {
"files": [
"./src/**/*.{html,js,svelte,ts}",
],
"name": "tailwind.config.cjs",
},
"content": [
"./src/**/*.{html,js,svelte,ts}",
],
"cssLocation": "./src/app.css",
"initCommands": [
"npx tailwindcss init tailwind.config.cjs -p",
Expand All @@ -129,13 +114,10 @@ exports[`Drivers > has a list of drivers 5`] = `

exports[`Drivers > has a list of drivers 6`] = `
{
"content": {
"files": [
"./index.html",
"./src/**/*.{vue,js,ts,jsx,tsx,svelte}",
],
"name": "tailwind.config.js",
},
"content": [
"./index.html",
"./src/**/*.{vue,js,ts,jsx,tsx,svelte}",
],
"cssLocation": "./src/style.css",
"initCommands": [
"npx tailwindcss init -p",
Expand All @@ -153,12 +135,9 @@ exports[`Drivers > has a list of drivers 6`] = `

exports[`Drivers > has a list of drivers 7`] = `
{
"content": {
"files": [
"./src/**/*.{html,ts}",
],
"name": "tailwind.config.js",
},
"content": [
"./src/**/*.{html,ts}",
],
"cssLocation": "./src/styles.css",
"initCommands": [
"npx tailwindcss init",
Expand All @@ -174,12 +153,9 @@ exports[`Drivers > has a list of drivers 7`] = `

exports[`Drivers > has a list of drivers 8`] = `
{
"content": {
"files": [
"./src/**/*.{js,jsx,ts,tsx}",
],
"name": "tailwind.config.js",
},
"content": [
"./src/**/*.{js,jsx,ts,tsx}",
],
"cssLocation": "./src/index.css",
"initCommands": [
"npx tailwindcss init -p",
Expand All @@ -195,13 +171,10 @@ exports[`Drivers > has a list of drivers 8`] = `

exports[`Drivers > has a list of drivers 9`] = `
{
"content": {
"files": [
"./resources/**/*.blade.php",
"./resources/**/*.{js,jsx,ts,tsx,vue,svelte}",
],
"name": "tailwind.config.js",
},
"content": [
"./resources/**/*.blade.php",
"./resources/**/*.{js,jsx,ts,tsx,vue,svelte}",
],
"cssLocation": "./resources/css/app.css",
"initCommands": [
"npx tailwindcss init -p",
Expand Down
Loading

0 comments on commit dbd86ee

Please sign in to comment.