From bfcfdd52d93c11e3ec7064b015db862d009f1ee9 Mon Sep 17 00:00:00 2001 From: "Khoi.LN" <147135167+khoilen@users.noreply.github.com> Date: Wed, 18 Dec 2024 15:19:56 +0700 Subject: [PATCH] feat: update init tailwind bin (#8) Co-authored-by: khoilen --- apps/nt-stylesheet/README.md | 9 ++-- apps/nt-stylesheet/bin/init-tailwind.js | 62 ++++++++++++++++++------- apps/nt-stylesheet/package.json | 7 +-- 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/apps/nt-stylesheet/README.md b/apps/nt-stylesheet/README.md index bbf0722..190e889 100644 --- a/apps/nt-stylesheet/README.md +++ b/apps/nt-stylesheet/README.md @@ -19,7 +19,7 @@ You can install **nt-stylesheet** via npm or pnpm. ### Using npm ```sh -npm install nt-stylesheet +npx nt-stylesheet ``` ### Usage @@ -35,15 +35,16 @@ You can customize nt-stylesheet by editing the `tailwind.config.js` file. Add yo ```js /** @type {import('tailwindcss').Config} */ -const ntTheme = require('nt-stylesheet/dist/theme.cjs') +const ntTheme = require('nt-stylesheet/dist/theme.cjs'); module.exports = { content: ['*.{html,js}'], theme: { - extend: ntTheme.theme.extend, + extend: ntTheme.extend, }, plugins: [], -} +}; + ``` ### Acknowledgements diff --git a/apps/nt-stylesheet/bin/init-tailwind.js b/apps/nt-stylesheet/bin/init-tailwind.js index fb51e9e..75487b4 100644 --- a/apps/nt-stylesheet/bin/init-tailwind.js +++ b/apps/nt-stylesheet/bin/init-tailwind.js @@ -1,15 +1,32 @@ #!/usr/bin/env node import fs from 'fs' +import { exec } from 'child_process' import readline from 'readline' +const installPackage = (packageName) => { + return new Promise((resolve, reject) => { + exec(`npm install ${packageName}`, (error) => { + if (error) { + console.error( + `Error installing ${packageName}: ${error.message}`, + ) + reject(error) + return + } + console.log(`${packageName} installed successfully!`) + resolve() + }) + }) +} + const createTailwindConfig = () => { const tailwindConfigContent = `/** @type {import('tailwindcss').Config} */ const ntTheme = require('nt-stylesheet/dist/theme.cjs'); module.exports = { content: ['*.{html,js}'], theme: { - extend: ntTheme.theme.extend, + extend: ntTheme.extend, }, plugins: [], }; @@ -59,20 +76,31 @@ const createPostCSSConfig = () => { ) } -const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout, -}) +const initialize = async () => { + try { + await installPackage('nt-stylesheet') + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + }) -rl.question( - 'Do you want to initialize Tailwind CSS? (y/n) ', - (answer) => { - if (answer.toLowerCase() === 'y') { - createTailwindConfig() - createPostCSSConfig() - } else { - console.log('Tailwind CSS initialization skipped.') - } - rl.close() - }, -) + rl.question( + 'Do you want to initialize Tailwind CSS? (y/n) ', + (answer) => { + if (answer.toLowerCase() === 'y') { + createTailwindConfig() + createPostCSSConfig() + } else { + console.log( + 'Tailwind CSS initialization skipped.', + ) + } + rl.close() + }, + ) + } catch (error) { + console.error('Initialization failed:', error) + } +} + +initialize() diff --git a/apps/nt-stylesheet/package.json b/apps/nt-stylesheet/package.json index c1d60b5..8742031 100644 --- a/apps/nt-stylesheet/package.json +++ b/apps/nt-stylesheet/package.json @@ -1,6 +1,6 @@ { "name": "nt-stylesheet", - "version": "1.1.7", + "version": "1.1.8", "description": "", "packageManager": "pnpm@9.0.5", "scripts": { @@ -19,10 +19,7 @@ "main": "./dist/theme.umd.cjs", "module": "./dist/theme.js", "exports": { - ".": { - "import": "./dist/theme.js", - "require": "./dist/theme.umd.cjs" - } + "./dist/theme.cjs": "./dist/theme.cjs" }, "devEngines": { "node": "20.x "