| 1 | // @ts-check
|
|---|
| 2 |
|
|---|
| 3 | import fs from 'fs'
|
|---|
| 4 | import path from 'path'
|
|---|
| 5 |
|
|---|
| 6 | function isESM() {
|
|---|
| 7 | const pkgPath = path.resolve('./package.json')
|
|---|
| 8 |
|
|---|
| 9 | try {
|
|---|
| 10 | let pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
|
|---|
| 11 | return pkg.type && pkg.type === 'module'
|
|---|
| 12 | } catch (err) {
|
|---|
| 13 | return false
|
|---|
| 14 | }
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | export function init(args) {
|
|---|
| 18 | let messages = []
|
|---|
| 19 |
|
|---|
| 20 | let isProjectESM = args['--ts'] || args['--esm'] || isESM()
|
|---|
| 21 | let syntax = args['--ts'] ? 'ts' : isProjectESM ? 'js' : 'cjs'
|
|---|
| 22 | let extension = args['--ts'] ? 'ts' : 'js'
|
|---|
| 23 |
|
|---|
| 24 | let tailwindConfigLocation = path.resolve(args['_'][1] ?? `./tailwind.config.${extension}`)
|
|---|
| 25 |
|
|---|
| 26 | if (fs.existsSync(tailwindConfigLocation)) {
|
|---|
| 27 | messages.push(`${path.basename(tailwindConfigLocation)} already exists.`)
|
|---|
| 28 | } else {
|
|---|
| 29 | let stubContentsFile = fs.readFileSync(
|
|---|
| 30 | args['--full']
|
|---|
| 31 | ? path.resolve(__dirname, '../../../stubs/config.full.js')
|
|---|
| 32 | : path.resolve(__dirname, '../../../stubs/config.simple.js'),
|
|---|
| 33 | 'utf8'
|
|---|
| 34 | )
|
|---|
| 35 |
|
|---|
| 36 | let stubFile = fs.readFileSync(
|
|---|
| 37 | path.resolve(__dirname, `../../../stubs/tailwind.config.${syntax}`),
|
|---|
| 38 | 'utf8'
|
|---|
| 39 | )
|
|---|
| 40 |
|
|---|
| 41 | // Change colors import
|
|---|
| 42 | stubContentsFile = stubContentsFile.replace('../colors', 'tailwindcss/colors')
|
|---|
| 43 |
|
|---|
| 44 | // Replace contents of {ts,js,cjs} file with the stub {simple,full}.
|
|---|
| 45 | stubFile =
|
|---|
| 46 | stubFile
|
|---|
| 47 | .replace('__CONFIG__', stubContentsFile.replace('module.exports =', '').trim())
|
|---|
| 48 | .trim() + '\n\n'
|
|---|
| 49 |
|
|---|
| 50 | fs.writeFileSync(tailwindConfigLocation, stubFile, 'utf8')
|
|---|
| 51 |
|
|---|
| 52 | messages.push(`Created Tailwind CSS config file: ${path.basename(tailwindConfigLocation)}`)
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | if (args['--postcss']) {
|
|---|
| 56 | let postcssConfigLocation = path.resolve('./postcss.config.js')
|
|---|
| 57 | if (fs.existsSync(postcssConfigLocation)) {
|
|---|
| 58 | messages.push(`${path.basename(postcssConfigLocation)} already exists.`)
|
|---|
| 59 | } else {
|
|---|
| 60 | let stubFile = fs.readFileSync(
|
|---|
| 61 | isProjectESM
|
|---|
| 62 | ? path.resolve(__dirname, '../../../stubs/postcss.config.js')
|
|---|
| 63 | : path.resolve(__dirname, '../../../stubs/postcss.config.cjs'),
|
|---|
| 64 | 'utf8'
|
|---|
| 65 | )
|
|---|
| 66 |
|
|---|
| 67 | fs.writeFileSync(postcssConfigLocation, stubFile, 'utf8')
|
|---|
| 68 |
|
|---|
| 69 | messages.push(`Created PostCSS config file: ${path.basename(postcssConfigLocation)}`)
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | if (messages.length > 0) {
|
|---|
| 74 | console.log()
|
|---|
| 75 | for (let message of messages) {
|
|---|
| 76 | console.log(message)
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|