-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(create-vuestic): tailwind option (#3718)
- Loading branch information
Showing
16 changed files
with
322 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { resolvePath } from "../utils/resolve-path" | ||
import { dirname, resolve } from "path" | ||
import { useUserAnswers } from "./useUserAnswers" | ||
import { mkdir, readFile, writeFile } from "fs/promises" | ||
import { existsSync } from "fs" | ||
|
||
export const useFiles = async () => { | ||
const { projectName } = await useUserAnswers() | ||
|
||
const resolveCorrectExt = (path: string, ext: string[]) => { | ||
for (const e of ext) { | ||
const resolvedPath = resolve(process.cwd(), projectName, `${path}.${e}`) | ||
|
||
if (resolvedPath) { | ||
return resolvedPath | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
const addFile = async (path: string, content: string) => { | ||
const resolvedPath = resolve(process.cwd(), projectName, path) | ||
|
||
const dir = dirname(resolvedPath) | ||
|
||
if (!existsSync(dir)) { | ||
await mkdir(dir, { recursive: true }) | ||
} | ||
|
||
return writeFile(resolvedPath, content, { encoding: 'utf-8', flag: 'wx' }) | ||
} | ||
|
||
const replaceFileContent = async (path: string, content: (existingContent: string) => string) => { | ||
const resolvedPath = resolvePath(process.cwd(), projectName, path) | ||
|
||
if (!resolvedPath) { | ||
throw new Error(`Unexpected error: Could not find ${path}`) | ||
} | ||
|
||
const existingContent = (await readFile(resolvedPath)).toString() | ||
|
||
return writeFile(resolvedPath, content(existingContent)) | ||
} | ||
|
||
const addToTopOfFile = async (path: string, content: string) => { | ||
return replaceFileContent(path, (existingContent) => { | ||
return `${content}\n${existingContent}` | ||
}) | ||
} | ||
|
||
return { | ||
addToTopOfFile, | ||
addFile, | ||
replaceFileContent, | ||
resolveCorrectExt, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { versions } from './../versions'; | ||
import { UserAnswers } from './../prompts'; | ||
import { usePackageJson } from "../composables/usePackageJson" | ||
import { useFiles } from '../composables/useFiles'; | ||
|
||
const installInVite = async () => { | ||
const { addFile, resolveCorrectExt, replaceFileContent } = await useFiles() | ||
|
||
const css = resolveCorrectExt('src/assets/main', ['css', 'scss', 'sass']) | ||
|
||
return Promise.all([ | ||
addFile('tailwind.config.js', ` | ||
/** @type {import('tailwindcss').Config} */ | ||
module.exports = { | ||
content: [ | ||
"./src/**/*.{vue,js,ts,jsx,tsx}", | ||
], | ||
theme: { | ||
extend: {}, | ||
screens: { | ||
xs: '0px', | ||
sm: '576px', | ||
md: '768px', | ||
lg: '992px', | ||
xl: '1200px', | ||
}, | ||
}, | ||
plugins: [], | ||
} | ||
`.trim()), | ||
addFile('postcss.config.js', ` | ||
module.exports = { | ||
plugins: { | ||
tailwindcss: {}, | ||
autoprefixer: {}, | ||
}, | ||
} | ||
`.trim()), | ||
replaceFileContent(css!, (content) => | ||
content.replace("@import './base.css';", ` | ||
@import './base.css'; | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
`.trim()) | ||
) | ||
]) | ||
} | ||
|
||
const installInNuxt = async () => { | ||
const { addFile, resolveCorrectExt, replaceFileContent } = await useFiles() | ||
|
||
const nuxtConfig = resolveCorrectExt('nuxt.config', ['ts', 'js']) | ||
|
||
return Promise.all([ | ||
addFile('tailwind.config.js', ` | ||
/** @type {import('tailwindcss').Config} */ | ||
module.exports = { | ||
content: [ | ||
"./components/**/*.{js,vue,ts}", | ||
"./layouts/**/*.vue", | ||
"./pages/**/*.vue", | ||
"./plugins/**/*.{js,ts}", | ||
"./nuxt.config.{js,ts}", | ||
"./app.vue", | ||
], | ||
theme: { | ||
extend: {}, | ||
screens: { | ||
xs: '0px', | ||
sm: '576px', | ||
md: '768px', | ||
lg: '992px', | ||
xl: '1200px', | ||
}, | ||
}, | ||
plugins: [], | ||
} | ||
`.trim()), | ||
addFile('./assets/css/main.css', ` | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
`.trim() | ||
), | ||
replaceFileContent(nuxtConfig!, (content) => | ||
content.replace('export default defineNuxtConfig({', ` | ||
export default defineNuxtConfig({ | ||
css: ['~/assets/css/main.css'], | ||
postcss: { | ||
plugins: { | ||
tailwindcss: {}, | ||
autoprefixer: {}, | ||
}, | ||
}, | ||
`.trim()) | ||
), | ||
]) | ||
} | ||
|
||
export const addTailwind = async (options: UserAnswers) => { | ||
const { vuesticFeatures, projectType } = options | ||
if (vuesticFeatures && !vuesticFeatures.includes('tailwind')) { | ||
return | ||
} | ||
|
||
const { addDependencies } = await usePackageJson() | ||
|
||
if (projectType === 'nuxt') { | ||
await installInNuxt() | ||
} else if (projectType === 'create-vue') { | ||
await installInVite() | ||
} | ||
|
||
await Promise.all([ | ||
addDependencies({ | ||
dependencies: { | ||
'@vuestic/tailwind': versions['@vuestic/tailwind'], | ||
}, | ||
devDependencies: { | ||
tailwindcss: versions['tailwindcss'], | ||
autoprefixer: versions['autoprefixer'], | ||
postcss: versions['postcss'], | ||
} | ||
}), | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.