From 1604e9fa461f8773ef8d7a307b70710f395b07ce Mon Sep 17 00:00:00 2001 From: CorwinDev <88144943+CorwinDev@users.noreply.github.com> Date: Mon, 16 Jan 2023 16:43:23 +0100 Subject: [PATCH 01/12] Added CLI for flowbite --- cli.js | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 6 +- 2 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 cli.js diff --git a/cli.js b/cli.js new file mode 100644 index 000000000..7ee562c33 --- /dev/null +++ b/cli.js @@ -0,0 +1,159 @@ +#! /usr/bin/env node +// Check if --debug is passed +const debug = process.argv.includes("--debug"); +const fs = require("fs"); + +if (process.argv.includes("-h") || process.argv.includes("--help") || process.argv.length == 2) { + if (process.argv.includes("add")) { + console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n flowbite add [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages"); + process.exit(1); + } else if (process.argv.includes("init")) { + console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\nflowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -d, --default\t\tCreates a default tailwind.config.js without Flowbite installed\n -p --postcss\t\tAdd postcss to your project\n --debug\t\tShow debug messages"); + process.exit(1); + } else { + console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n flowbite add [options]\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages"); + process.exit(1); + } +} + +if (process.argv.includes("-v") || process.argv.includes("--version")) { + console.log("Create-Flowbite " + require("../package.json").version); + process.exit(1); +} + +if (process.argv.includes("init")) { + init(); +} else { + console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n flowbite add [options]\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages"); + process.exit(1); +} + +function createTailwindConfig() { + if (fs.existsSync("tailwind.config.js")) { + log("tailwind.config.js already exists"); + return; + } + const data = ` +module.exports = { + content: [], + theme: { + extend: {}, + }, + plugins: [], +}`; + // Write the file + fs.writeFileSync("tailwind.config.js", data, "utf8"); + log("tailwind.config.js created"); +} + +async function updateTailwindConfig() { + // Read the file and replace plugins:[ with plugins: [require("flowbite") + const data = fs.readFileSync("tailwind.config.js", "utf8"); + // Check if the plugin is already added + if (data.includes("require('flowbite')")) { + log("Flowbite plugin already added to tailwind.config.js", true); + process.exit(1); + } + + // Check if flowbite is installed + if (!fs.existsSync("node_modules/flowbite")) { + log("Installing Flowbite..."); + // Check if package.json exists + if (!fs.existsSync("package.json")) { + console.log("No npm project found!\nCreate one with \x1b[1m\x1b[4mnpm init\x1b[0m"); + process.exit(1); + } + await run("npm install -D tailwindcss autoprefixer flowbite"); + log("Dependencies installed"); + updateTailwindConfig(); + + } + + var result = data.replace("plugins: [", "plugins: [require('flowbite'),").replace("content: [", "content: ['./node_modules/flowbite/**/*.js',"); + fs.writeFileSync("tailwind.config.js", result, "utf8"); + log("Flowbite plugin added to tailwind.config.js"); +} + +async function postCss() { + // Check if postcss.config.js exists + if (!fs.existsSync("postcss.config.js")) { + // Install tailwindcss and autoprefixer + log("Installing tailwindcss and autoprefixer..."); + await run("npm install -D tailwindcss postcss autoprefixer"); + // Create postcss.config.js + const data = ` +module.exports = { + plugins: { + tailwindcss: {}, + autoprefixer: {}, + }, +};`; + fs.writeFileSync("postcss.config.js", data, "utf8"); + log("postcss.config.js created"); + } else { + return; + } +} + + +// Functions +const { exec } = require('child_process'); +/** + * Function to run command in terminal + * @param {string} cmd + * @returns + */ +const run = async (cmd) => { + const child = exec(cmd, (err) => { + if (err) console.error(err); + }); + if (debug == true) { + child.stderr.pipe(process.stderr); + child.stdout.pipe(process.stdout); + } + + await new Promise((resolve) => child.on('close', resolve)); +}; + +/** + * + * @param {string} msg + * @param {boolean} important + * @returns + */ +function log(msg, important = false) { + if (important == true) { + console.log(msg + "\x1b[0m"); + } + if (debug == true) { + console.log(msg + "\x1b[0m"); + } +} + +/** + * Init the tailwind config + * @returns + */ +async function init() { + if (process.argv.includes("-d") || process.argv.includes("--default")) { + await createTailwindConfig(); + log("tailwind.config.js created", true); + if (process.argv.includes("-p") || process.argv.includes("--postcss")) { + await postCss(); + } + process.exit(1); + } + if (fs.existsSync("tailwind.config.js")) { + log("tailwind.config.js already exists"); + updateTailwindConfig(); + } + else { + await createTailwindConfig(); + await updateTailwindConfig(); + } + if (process.argv.includes("-p") || process.argv.includes("--postcss")) { + await postCss(); + log("postcss.config.js created", true); + } + log("tailwind.config.js created with Flowbite plugin", true); +} \ No newline at end of file diff --git a/package.json b/package.json index f5ef48f11..c25f0edf1 100644 --- a/package.json +++ b/package.json @@ -114,6 +114,8 @@ "files": [ "lib", "dist", - "plugin.js" - ] + "plugin.js", + "cli.js" + ], + "bin": "cli.js" } From 800c1acbec8e88ba887b4ff9f2eeb66d2328d141 Mon Sep 17 00:00:00 2001 From: CorwinDev <88144943+CorwinDev@users.noreply.github.com> Date: Mon, 16 Jan 2023 22:17:58 +0100 Subject: [PATCH 02/12] Fixed some bugs --- cli.js | 5 +---- package-lock.json | 14 ++++++++++++++ package.json | 3 ++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cli.js b/cli.js index 7ee562c33..15c9f8d9b 100644 --- a/cli.js +++ b/cli.js @@ -4,10 +4,7 @@ const debug = process.argv.includes("--debug"); const fs = require("fs"); if (process.argv.includes("-h") || process.argv.includes("--help") || process.argv.length == 2) { - if (process.argv.includes("add")) { - console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n flowbite add [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages"); - process.exit(1); - } else if (process.argv.includes("init")) { + if (process.argv.includes("init")) { console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\nflowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -d, --default\t\tCreates a default tailwind.config.js without Flowbite installed\n -p --postcss\t\tAdd postcss to your project\n --debug\t\tShow debug messages"); process.exit(1); } else { diff --git a/package-lock.json b/package-lock.json index 237bd0097..6f3058a8d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,12 @@ "license": "MIT", "dependencies": { "@popperjs/core": "^2.9.3", + "child_process": "^1.0.2", "mini-svg-data-uri": "^1.4.3" }, + "bin": { + "flowbite": "cli.js" + }, "devDependencies": { "@babel/core": "^7.14.8", "@babel/preset-env": "^7.14.8", @@ -3323,6 +3327,11 @@ "node": ">=4" } }, + "node_modules/child_process": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/child_process/-/child_process-1.0.2.tgz", + "integrity": "sha512-Wmza/JzL0SiWz7kl6MhIKT5ceIlnFPJX+lwUGj7Clhy5MMldsSoJR0+uvRzOS5Kv45Mq7t1PoE8TsOA9bzvb6g==" + }, "node_modules/chokidar": { "version": "3.5.3", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz", @@ -16470,6 +16479,11 @@ "supports-color": "^5.3.0" } }, + "child_process": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/child_process/-/child_process-1.0.2.tgz", + "integrity": "sha512-Wmza/JzL0SiWz7kl6MhIKT5ceIlnFPJX+lwUGj7Clhy5MMldsSoJR0+uvRzOS5Kv45Mq7t1PoE8TsOA9bzvb6g==" + }, "chokidar": { "version": "3.5.3", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz", diff --git a/package.json b/package.json index c25f0edf1..a49bcbe33 100644 --- a/package.json +++ b/package.json @@ -109,7 +109,8 @@ }, "dependencies": { "@popperjs/core": "^2.9.3", - "mini-svg-data-uri": "^1.4.3" + "mini-svg-data-uri": "^1.4.3", + "child_process": "^1.0.2" }, "files": [ "lib", From e5f1ffe3590b641156e2936d910cc3e48f311af1 Mon Sep 17 00:00:00 2001 From: CorwinDev <88144943+CorwinDev@users.noreply.github.com> Date: Tue, 17 Jan 2023 18:30:09 +0100 Subject: [PATCH 03/12] Remove add command --- cli.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index 15c9f8d9b..543a8f83c 100644 --- a/cli.js +++ b/cli.js @@ -8,7 +8,7 @@ if (process.argv.includes("-h") || process.argv.includes("--help") || process.ar console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\nflowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -d, --default\t\tCreates a default tailwind.config.js without Flowbite installed\n -p --postcss\t\tAdd postcss to your project\n --debug\t\tShow debug messages"); process.exit(1); } else { - console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n flowbite add [options]\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages"); + console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages"); process.exit(1); } } @@ -21,7 +21,7 @@ if (process.argv.includes("-v") || process.argv.includes("--version")) { if (process.argv.includes("init")) { init(); } else { - console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n flowbite add [options]\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages"); + console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages"); process.exit(1); } From bfc7de11b29b70086b6009e00be5840d53fe4308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Sz=C5=91gy=C3=A9nyi?= Date: Thu, 19 Jan 2023 14:16:51 +0200 Subject: [PATCH 04/12] ESLint formatting. --- cli.js | 108 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 48 deletions(-) diff --git a/cli.js b/cli.js index 543a8f83c..65c16dddb 100644 --- a/cli.js +++ b/cli.js @@ -1,33 +1,44 @@ #! /usr/bin/env node +/* eslint-disable @typescript-eslint/no-var-requires */ // Check if --debug is passed -const debug = process.argv.includes("--debug"); -const fs = require("fs"); +const debug = process.argv.includes('--debug'); +const fs = require('fs'); -if (process.argv.includes("-h") || process.argv.includes("--help") || process.argv.length == 2) { - if (process.argv.includes("init")) { - console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\nflowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -d, --default\t\tCreates a default tailwind.config.js without Flowbite installed\n -p --postcss\t\tAdd postcss to your project\n --debug\t\tShow debug messages"); +if ( + process.argv.includes('-h') || + process.argv.includes('--help') || + process.argv.length == 2 +) { + if (process.argv.includes('init')) { + console.log( + '\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\nflowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -d, --default\t\tCreates a default tailwind.config.js without Flowbite installed\n -p --postcss\t\tAdd postcss to your project\n --debug\t\tShow debug messages' + ); process.exit(1); } else { - console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages"); + console.log( + '\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages' + ); process.exit(1); } } -if (process.argv.includes("-v") || process.argv.includes("--version")) { - console.log("Create-Flowbite " + require("../package.json").version); +if (process.argv.includes('-v') || process.argv.includes('--version')) { + console.log('Create-Flowbite ' + require('../package.json').version); process.exit(1); } -if (process.argv.includes("init")) { +if (process.argv.includes('init')) { init(); } else { - console.log("\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages"); + console.log( + '\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages' + ); process.exit(1); } function createTailwindConfig() { - if (fs.existsSync("tailwind.config.js")) { - log("tailwind.config.js already exists"); + if (fs.existsSync('tailwind.config.js')) { + log('tailwind.config.js already exists'); return; } const data = ` @@ -39,44 +50,47 @@ module.exports = { plugins: [], }`; // Write the file - fs.writeFileSync("tailwind.config.js", data, "utf8"); - log("tailwind.config.js created"); + fs.writeFileSync('tailwind.config.js', data, 'utf8'); + log('tailwind.config.js created'); } async function updateTailwindConfig() { // Read the file and replace plugins:[ with plugins: [require("flowbite") - const data = fs.readFileSync("tailwind.config.js", "utf8"); + const data = fs.readFileSync('tailwind.config.js', 'utf8'); // Check if the plugin is already added if (data.includes("require('flowbite')")) { - log("Flowbite plugin already added to tailwind.config.js", true); + log('Flowbite plugin already added to tailwind.config.js', true); process.exit(1); } // Check if flowbite is installed - if (!fs.existsSync("node_modules/flowbite")) { - log("Installing Flowbite..."); + if (!fs.existsSync('node_modules/flowbite')) { + log('Installing Flowbite...'); // Check if package.json exists - if (!fs.existsSync("package.json")) { - console.log("No npm project found!\nCreate one with \x1b[1m\x1b[4mnpm init\x1b[0m"); + if (!fs.existsSync('package.json')) { + console.log( + 'No npm project found!\nCreate one with \x1b[1m\x1b[4mnpm init\x1b[0m' + ); process.exit(1); } - await run("npm install -D tailwindcss autoprefixer flowbite"); - log("Dependencies installed"); + await run('npm install -D tailwindcss autoprefixer flowbite'); + log('Dependencies installed'); updateTailwindConfig(); - } - var result = data.replace("plugins: [", "plugins: [require('flowbite'),").replace("content: [", "content: ['./node_modules/flowbite/**/*.js',"); - fs.writeFileSync("tailwind.config.js", result, "utf8"); - log("Flowbite plugin added to tailwind.config.js"); + var result = data + .replace('plugins: [', "plugins: [require('flowbite'),") + .replace('content: [', "content: ['./node_modules/flowbite/**/*.js',"); + fs.writeFileSync('tailwind.config.js', result, 'utf8'); + log('Flowbite plugin added to tailwind.config.js'); } async function postCss() { // Check if postcss.config.js exists - if (!fs.existsSync("postcss.config.js")) { + if (!fs.existsSync('postcss.config.js')) { // Install tailwindcss and autoprefixer - log("Installing tailwindcss and autoprefixer..."); - await run("npm install -D tailwindcss postcss autoprefixer"); + log('Installing tailwindcss and autoprefixer...'); + await run('npm install -D tailwindcss postcss autoprefixer'); // Create postcss.config.js const data = ` module.exports = { @@ -85,14 +99,13 @@ module.exports = { autoprefixer: {}, }, };`; - fs.writeFileSync("postcss.config.js", data, "utf8"); - log("postcss.config.js created"); + fs.writeFileSync('postcss.config.js', data, 'utf8'); + log('postcss.config.js created'); } else { return; } } - // Functions const { exec } = require('child_process'); /** @@ -113,17 +126,17 @@ const run = async (cmd) => { }; /** - * - * @param {string} msg - * @param {boolean} important + * + * @param {string} msg + * @param {boolean} important * @returns */ function log(msg, important = false) { if (important == true) { - console.log(msg + "\x1b[0m"); + console.log(msg + '\x1b[0m'); } if (debug == true) { - console.log(msg + "\x1b[0m"); + console.log(msg + '\x1b[0m'); } } @@ -132,25 +145,24 @@ function log(msg, important = false) { * @returns */ async function init() { - if (process.argv.includes("-d") || process.argv.includes("--default")) { + if (process.argv.includes('-d') || process.argv.includes('--default')) { await createTailwindConfig(); - log("tailwind.config.js created", true); - if (process.argv.includes("-p") || process.argv.includes("--postcss")) { + log('tailwind.config.js created', true); + if (process.argv.includes('-p') || process.argv.includes('--postcss')) { await postCss(); } process.exit(1); } - if (fs.existsSync("tailwind.config.js")) { - log("tailwind.config.js already exists"); + if (fs.existsSync('tailwind.config.js')) { + log('tailwind.config.js already exists'); updateTailwindConfig(); - } - else { + } else { await createTailwindConfig(); await updateTailwindConfig(); } - if (process.argv.includes("-p") || process.argv.includes("--postcss")) { + if (process.argv.includes('-p') || process.argv.includes('--postcss')) { await postCss(); - log("postcss.config.js created", true); + log('postcss.config.js created', true); } - log("tailwind.config.js created with Flowbite plugin", true); -} \ No newline at end of file + log('tailwind.config.js created with Flowbite plugin', true); +} From 656e440dea568ee010625fafc76df232c77952f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Sz=C5=91gy=C3=A9nyi?= Date: Thu, 19 Jan 2023 14:24:18 +0200 Subject: [PATCH 05/12] Update versioning text. --- cli.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index 65c16dddb..d611c6d44 100644 --- a/cli.js +++ b/cli.js @@ -1,5 +1,5 @@ #! /usr/bin/env node -/* eslint-disable @typescript-eslint/no-var-requires */ + // Check if --debug is passed const debug = process.argv.includes('--debug'); const fs = require('fs'); @@ -23,7 +23,7 @@ if ( } if (process.argv.includes('-v') || process.argv.includes('--version')) { - console.log('Create-Flowbite ' + require('../package.json').version); + console.log('Flowbite v' + require('../package.json').version); process.exit(1); } From 86e3d0e0d8b2913880389c58a6aceed2102dafb1 Mon Sep 17 00:00:00 2001 From: CorwinDev <88144943+CorwinDev@users.noreply.github.com> Date: Thu, 19 Jan 2023 14:24:37 +0100 Subject: [PATCH 06/12] Fixed comments --- cli.js | 42 +++++++++++++++++++++++++++++------------- package-lock.json | 8 +++++--- package.json | 4 ++-- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/cli.js b/cli.js index d611c6d44..5f3be6165 100644 --- a/cli.js +++ b/cli.js @@ -11,7 +11,7 @@ if ( ) { if (process.argv.includes('init')) { console.log( - '\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\nflowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -d, --default\t\tCreates a default tailwind.config.js without Flowbite installed\n -p --postcss\t\tAdd postcss to your project\n --debug\t\tShow debug messages' + '\x1b[0;34mFlowbite CLI \x1b[0m\n\nUsage:\nflowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -d, --default\t\tCreates a default tailwind.config.js without Flowbite installed\n -p --postcss\t\tAdd postcss to your project\n --debug\t\tShow debug messages' ); process.exit(1); } else { @@ -23,7 +23,9 @@ if ( } if (process.argv.includes('-v') || process.argv.includes('--version')) { - console.log('Flowbite v' + require('../package.json').version); + console.log( + '\x1b[0;34mFlowbite v' + require('./package.json').version + '\x1b[0m' + ); process.exit(1); } @@ -58,8 +60,11 @@ async function updateTailwindConfig() { // Read the file and replace plugins:[ with plugins: [require("flowbite") const data = fs.readFileSync('tailwind.config.js', 'utf8'); // Check if the plugin is already added - if (data.includes("require('flowbite')")) { - log('Flowbite plugin already added to tailwind.config.js', true); + if ( + data.includes("require('flowbite')") && + data.includes('./node_modules/flowbite/**/*.js') + ) { + log('Flowbite has already been installed in tailwind.config.js.', true); process.exit(1); } @@ -77,12 +82,19 @@ async function updateTailwindConfig() { log('Dependencies installed'); updateTailwindConfig(); } - - var result = data - .replace('plugins: [', "plugins: [require('flowbite'),") - .replace('content: [', "content: ['./node_modules/flowbite/**/*.js',"); + var result = data; + if (!data.includes("require('flowbite')")) { + result = data.replace('plugins: [', "plugins: [require('flowbite'),"); + log('Flowbite plugin added to tailwind.config.js'); + } + if (!data.includes('./node_modules/flowbite/**/*.js')) { + result = result.replace( + 'content: [', + "content: ['./node_modules/flowbite/**/*.js'," + ); + log('Flowbite node_modules added to tailwind.config.js'); + } fs.writeFileSync('tailwind.config.js', result, 'utf8'); - log('Flowbite plugin added to tailwind.config.js'); } async function postCss() { @@ -154,15 +166,19 @@ async function init() { process.exit(1); } if (fs.existsSync('tailwind.config.js')) { - log('tailwind.config.js already exists'); - updateTailwindConfig(); + await updateTailwindConfig(); + log('Flowbite has been installed in tailwind.config.js', true); } else { await createTailwindConfig(); await updateTailwindConfig(); + log( + 'Created new tailwind.config.js file with Flowbite installed.', + true + ); } if (process.argv.includes('-p') || process.argv.includes('--postcss')) { await postCss(); - log('postcss.config.js created', true); + log('Created postcss.config.js file', true); } - log('tailwind.config.js created with Flowbite plugin', true); + process.exit(1); } diff --git a/package-lock.json b/package-lock.json index 6f3058a8d..006b9ff71 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,6 @@ "license": "MIT", "dependencies": { "@popperjs/core": "^2.9.3", - "child_process": "^1.0.2", "mini-svg-data-uri": "^1.4.3" }, "bin": { @@ -24,6 +23,7 @@ "@typescript-eslint/parser": "^5.46.1", "autoprefixer": "^10.3.3", "babel-loader": "^8.2.2", + "child_process": "^1.0.2", "copyfiles": "^2.4.1", "core-js": "^3.8.1", "css-loader": "^5.2.7", @@ -3330,7 +3330,8 @@ "node_modules/child_process": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/child_process/-/child_process-1.0.2.tgz", - "integrity": "sha512-Wmza/JzL0SiWz7kl6MhIKT5ceIlnFPJX+lwUGj7Clhy5MMldsSoJR0+uvRzOS5Kv45Mq7t1PoE8TsOA9bzvb6g==" + "integrity": "sha512-Wmza/JzL0SiWz7kl6MhIKT5ceIlnFPJX+lwUGj7Clhy5MMldsSoJR0+uvRzOS5Kv45Mq7t1PoE8TsOA9bzvb6g==", + "dev": true }, "node_modules/chokidar": { "version": "3.5.3", @@ -16482,7 +16483,8 @@ "child_process": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/child_process/-/child_process-1.0.2.tgz", - "integrity": "sha512-Wmza/JzL0SiWz7kl6MhIKT5ceIlnFPJX+lwUGj7Clhy5MMldsSoJR0+uvRzOS5Kv45Mq7t1PoE8TsOA9bzvb6g==" + "integrity": "sha512-Wmza/JzL0SiWz7kl6MhIKT5ceIlnFPJX+lwUGj7Clhy5MMldsSoJR0+uvRzOS5Kv45Mq7t1PoE8TsOA9bzvb6g==", + "dev": true }, "chokidar": { "version": "3.5.3", diff --git a/package.json b/package.json index a49bcbe33..ecc0f6b78 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "@typescript-eslint/parser": "^5.46.1", "autoprefixer": "^10.3.3", "babel-loader": "^8.2.2", + "child_process": "^1.0.2", "copyfiles": "^2.4.1", "core-js": "^3.8.1", "css-loader": "^5.2.7", @@ -109,8 +110,7 @@ }, "dependencies": { "@popperjs/core": "^2.9.3", - "mini-svg-data-uri": "^1.4.3", - "child_process": "^1.0.2" + "mini-svg-data-uri": "^1.4.3" }, "files": [ "lib", From 1164b2c79d5a3e7ae28728ad7961676bd6e086b8 Mon Sep 17 00:00:00 2001 From: CorwinDev <88144943+CorwinDev@users.noreply.github.com> Date: Thu, 19 Jan 2023 17:55:22 +0100 Subject: [PATCH 07/12] Added -p/--postcss to help menu --- cli.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index 5f3be6165..10277530c 100644 --- a/cli.js +++ b/cli.js @@ -11,12 +11,12 @@ if ( ) { if (process.argv.includes('init')) { console.log( - '\x1b[0;34mFlowbite CLI \x1b[0m\n\nUsage:\nflowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -d, --default\t\tCreates a default tailwind.config.js without Flowbite installed\n -p --postcss\t\tAdd postcss to your project\n --debug\t\tShow debug messages' + '\x1b[0;34mFlowbite CLI \x1b[0m\n\nUsage:\nflowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -d, --default\t\tCreates a default tailwind.config.js without Flowbite installed\n -p --postcss\t\tInitialize a postcss.config.js file\n --debug\t\tShow debug messages' ); process.exit(1); } else { console.log( - '\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages' + '\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages\n -p --postcss\t\tInitialize a postcss.config.js file' ); process.exit(1); } From a4f52c8f21409e5543515e3ba5de6a9176382dee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Sz=C5=91gy=C3=A9nyi?= Date: Fri, 20 Jan 2023 16:42:28 +0200 Subject: [PATCH 08/12] Add period mark to all log texts. --- cli.js | 20 +++++++-------- package-lock.json | 63 +++++++++++++++++------------------------------ 2 files changed, 33 insertions(+), 50 deletions(-) diff --git a/cli.js b/cli.js index 10277530c..6daaaa4b1 100644 --- a/cli.js +++ b/cli.js @@ -1,5 +1,5 @@ #! /usr/bin/env node - +/* eslint-disable @typescript-eslint/no-var-requires */ // Check if --debug is passed const debug = process.argv.includes('--debug'); const fs = require('fs'); @@ -40,7 +40,7 @@ if (process.argv.includes('init')) { function createTailwindConfig() { if (fs.existsSync('tailwind.config.js')) { - log('tailwind.config.js already exists'); + log('tailwind.config.js already exists.'); return; } const data = ` @@ -53,7 +53,7 @@ module.exports = { }`; // Write the file fs.writeFileSync('tailwind.config.js', data, 'utf8'); - log('tailwind.config.js created'); + log('tailwind.config.js created.'); } async function updateTailwindConfig() { @@ -79,20 +79,20 @@ async function updateTailwindConfig() { process.exit(1); } await run('npm install -D tailwindcss autoprefixer flowbite'); - log('Dependencies installed'); + log('Dependencies installed.'); updateTailwindConfig(); } var result = data; if (!data.includes("require('flowbite')")) { result = data.replace('plugins: [', "plugins: [require('flowbite'),"); - log('Flowbite plugin added to tailwind.config.js'); + log('Flowbite plugin added to tailwind.config.js.'); } if (!data.includes('./node_modules/flowbite/**/*.js')) { result = result.replace( 'content: [', "content: ['./node_modules/flowbite/**/*.js'," ); - log('Flowbite node_modules added to tailwind.config.js'); + log('Flowbite node_modules added to tailwind.config.js.'); } fs.writeFileSync('tailwind.config.js', result, 'utf8'); } @@ -112,7 +112,7 @@ module.exports = { }, };`; fs.writeFileSync('postcss.config.js', data, 'utf8'); - log('postcss.config.js created'); + log('postcss.config.js created.'); } else { return; } @@ -159,7 +159,7 @@ function log(msg, important = false) { async function init() { if (process.argv.includes('-d') || process.argv.includes('--default')) { await createTailwindConfig(); - log('tailwind.config.js created', true); + log('tailwind.config.js created.', true); if (process.argv.includes('-p') || process.argv.includes('--postcss')) { await postCss(); } @@ -167,7 +167,7 @@ async function init() { } if (fs.existsSync('tailwind.config.js')) { await updateTailwindConfig(); - log('Flowbite has been installed in tailwind.config.js', true); + log('Flowbite has been installed in tailwind.config.js.', true); } else { await createTailwindConfig(); await updateTailwindConfig(); @@ -178,7 +178,7 @@ async function init() { } if (process.argv.includes('-p') || process.argv.includes('--postcss')) { await postCss(); - log('Created postcss.config.js file', true); + log('Created postcss.config.js file.', true); } process.exit(1); } diff --git a/package-lock.json b/package-lock.json index 006b9ff71..8f9418f3b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7201,9 +7201,9 @@ "dev": true }, "node_modules/json5": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz", - "integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true, "bin": { "json5": "lib/cli.js" @@ -15870,8 +15870,7 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/@webpack-cli/configtest/-/configtest-1.2.0.tgz", "integrity": "sha512-4FB8Tj6xyVkyqjj1OaTqCjXYULB9FMkqQ8yGrZjRDrYh0nOE+7Lhs45WioWQQMV+ceFlE368Ukhe6xdvJM9Egg==", - "dev": true, - "requires": {} + "dev": true }, "@webpack-cli/info": { "version": "1.5.0", @@ -15886,8 +15885,7 @@ "version": "1.7.0", "resolved": "https://registry.npmjs.org/@webpack-cli/serve/-/serve-1.7.0.tgz", "integrity": "sha512-oxnCNGj88fL+xzV+dacXs44HcDwf1ovs3AuEzvP7mqXw7fQntqIhQ1BRmynh4qEKQSSSRSWVyXRjmTbZIX9V2Q==", - "dev": true, - "requires": {} + "dev": true }, "@xtuc/ieee754": { "version": "1.2.0", @@ -15927,15 +15925,13 @@ "version": "1.8.0", "resolved": "https://registry.npmjs.org/acorn-import-assertions/-/acorn-import-assertions-1.8.0.tgz", "integrity": "sha512-m7VZ3jwz4eK6A4Vtt8Ew1/mNbP24u0FhdyfA7fSvnJR6LMdfOYnmuIrrJAgrYfYJ10F/otaHTtrtrtmHdMNzEw==", - "dev": true, - "requires": {} + "dev": true }, "acorn-jsx": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true, - "requires": {} + "dev": true }, "acorn-node": { "version": "1.8.2", @@ -15978,8 +15974,7 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/ajv-errors/-/ajv-errors-1.0.1.tgz", "integrity": "sha512-DCRfO/4nQ+89p/RK43i8Ezd41EqdGIU4ld7nGF8OQ14oc/we5rEntLCUa7+jrn3nn83BosfwZA0wb4pon2o8iQ==", - "dev": true, - "requires": {} + "dev": true }, "ajv-formats": { "version": "2.1.1", @@ -16014,8 +16009,7 @@ "version": "3.5.2", "resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-3.5.2.tgz", "integrity": "sha512-5p6WTN0DdTGVQk6VjcEju19IgaHudalcfabD7yhDGeA6bcQnmL+CpveLJq/3hvfwd1aof6L386Ougkx6RfyMIQ==", - "dev": true, - "requires": {} + "dev": true }, "algoliasearch": { "version": "4.14.2", @@ -16881,8 +16875,7 @@ "version": "6.3.1", "resolved": "https://registry.npmjs.org/css-declaration-sorter/-/css-declaration-sorter-6.3.1.tgz", "integrity": "sha512-fBffmak0bPAnyqc/HO8C3n2sHrp9wcqQz6ES9koRF2/mLOVAx9zIQ3Y7R29sYCteTPqMCwns4WYQoCX91Xl3+w==", - "dev": true, - "requires": {} + "dev": true }, "css-has-pseudo": { "version": "0.10.0", @@ -17144,8 +17137,7 @@ "version": "3.1.0", "resolved": "https://registry.npmjs.org/cssnano-utils/-/cssnano-utils-3.1.0.tgz", "integrity": "sha512-JQNR19/YZhz4psLX/rQ9M83e3z2Wf/HdJbryzte4a3NSuafyp9w/I4U+hx5C2S9g41qlstH7DEWnZaaj83OuEA==", - "dev": true, - "requires": {} + "dev": true }, "csso": { "version": "4.2.0", @@ -17801,8 +17793,7 @@ "version": "8.0.0", "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-8.0.0.tgz", "integrity": "sha512-5EaAVPsIHu+grmm5WKjxUia4yHgRrbkd8I0ffqUSwixCPMVBrbS97UnzlEY/Q7OWo584vgixefM0kJnUfo/VjA==", - "dev": true, - "requires": {} + "dev": true }, "eslint-plugin-prettier": { "version": "4.2.1", @@ -18943,8 +18934,7 @@ "version": "5.1.0", "resolved": "https://registry.npmjs.org/icss-utils/-/icss-utils-5.1.0.tgz", "integrity": "sha512-soFhflCVWLfRNOPU3iv5Z9VUdT44xFRbzjLsEzSr5AQmgqPMTHdU3PMT1Cf1ssx8fLNJDA1juftYl+PUcv3MqA==", - "dev": true, - "requires": {} + "dev": true }, "ignore": { "version": "5.2.1", @@ -19400,9 +19390,9 @@ "dev": true }, "json5": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz", - "integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true }, "jsonfile": { @@ -20781,29 +20771,25 @@ "version": "5.1.2", "resolved": "https://registry.npmjs.org/postcss-discard-comments/-/postcss-discard-comments-5.1.2.tgz", "integrity": "sha512-+L8208OVbHVF2UQf1iDmRcbdjJkuBF6IS29yBDSiWUIzpYaAhtNl6JYnYm12FnkeCwQqF5LeklOu6rAqgfBZqQ==", - "dev": true, - "requires": {} + "dev": true }, "postcss-discard-duplicates": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/postcss-discard-duplicates/-/postcss-discard-duplicates-5.1.0.tgz", "integrity": "sha512-zmX3IoSI2aoenxHV6C7plngHWWhUOV3sP1T8y2ifzxzbtnuhk1EdPwm0S1bIUNaJ2eNbWeGLEwzw8huPD67aQw==", - "dev": true, - "requires": {} + "dev": true }, "postcss-discard-empty": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/postcss-discard-empty/-/postcss-discard-empty-5.1.1.tgz", "integrity": "sha512-zPz4WljiSuLWsI0ir4Mcnr4qQQ5e1Ukc3i7UfE2XcrwKK2LIPIqE5jxMRxO6GbI3cv//ztXDsXwEWT3BHOGh3A==", - "dev": true, - "requires": {} + "dev": true }, "postcss-discard-overridden": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/postcss-discard-overridden/-/postcss-discard-overridden-5.1.0.tgz", "integrity": "sha512-21nOL7RqWR1kasIVdKs8HNqQJhFxLsyRfAnUDm4Fe4t4mCWL9OJiHvlHPjcd8zc5Myu89b/7wZDnOSjFgeWRtw==", - "dev": true, - "requires": {} + "dev": true }, "postcss-double-position-gradients": { "version": "1.0.0", @@ -21238,8 +21224,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/postcss-modules-extract-imports/-/postcss-modules-extract-imports-3.0.0.tgz", "integrity": "sha512-bdHleFnP3kZ4NYDhuGlVK+CMrQ/pqUm8bx/oGL93K6gVwiclvX5x0n76fYMKuIGKzlABOy13zsvqjb0f92TEXw==", - "dev": true, - "requires": {} + "dev": true }, "postcss-modules-local-by-default": { "version": "4.0.0", @@ -21310,8 +21295,7 @@ "version": "5.1.0", "resolved": "https://registry.npmjs.org/postcss-normalize-charset/-/postcss-normalize-charset-5.1.0.tgz", "integrity": "sha512-mSgUJ+pd/ldRGVx26p2wz9dNZ7ji6Pn8VWBajMXFf8jk7vUoSrZ2lt/wZR7DtlZYKesmZI680qjr2CeFF2fbUg==", - "dev": true, - "requires": {} + "dev": true }, "postcss-normalize-display-values": { "version": "5.1.0", @@ -21791,8 +21775,7 @@ "version": "0.2.1", "resolved": "https://registry.npmjs.org/prettier-plugin-tailwindcss/-/prettier-plugin-tailwindcss-0.2.1.tgz", "integrity": "sha512-aIO8IguumORyRsmT+E7JfJ3A9FEoyhqZR7Au7TBOege3VZkgMvHJMkufeYp4zjnDK2iq4ktkvGMNOQR9T8lisQ==", - "dev": true, - "requires": {} + "dev": true }, "pretty-hrtime": { "version": "1.0.3", From 6fc20f17e656f51676245d6b26df3862c0a28789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Sz=C5=91gy=C3=A9nyi?= Date: Fri, 20 Jan 2023 16:48:52 +0200 Subject: [PATCH 09/12] Format tailwind.config.js file tabs, new line and semicolons. --- cli.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/cli.js b/cli.js index 6daaaa4b1..35c930326 100644 --- a/cli.js +++ b/cli.js @@ -43,14 +43,14 @@ function createTailwindConfig() { log('tailwind.config.js already exists.'); return; } - const data = ` -module.exports = { - content: [], - theme: { - extend: {}, - }, - plugins: [], -}`; + const data = `module.exports = { + content: [], + theme: { + extend: {}, + }, + plugins: [], +}; +`; // Write the file fs.writeFileSync('tailwind.config.js', data, 'utf8'); log('tailwind.config.js created.'); @@ -61,7 +61,7 @@ async function updateTailwindConfig() { const data = fs.readFileSync('tailwind.config.js', 'utf8'); // Check if the plugin is already added if ( - data.includes("require('flowbite')") && + data.includes("require('flowbite/plugin')") && data.includes('./node_modules/flowbite/**/*.js') ) { log('Flowbite has already been installed in tailwind.config.js.', true); @@ -83,8 +83,11 @@ async function updateTailwindConfig() { updateTailwindConfig(); } var result = data; - if (!data.includes("require('flowbite')")) { - result = data.replace('plugins: [', "plugins: [require('flowbite'),"); + if (!data.includes("require('flowbite/plugin')")) { + result = data.replace( + 'plugins: [', + "plugins: [require('flowbite/plugin')," + ); log('Flowbite plugin added to tailwind.config.js.'); } if (!data.includes('./node_modules/flowbite/**/*.js')) { @@ -104,13 +107,13 @@ async function postCss() { log('Installing tailwindcss and autoprefixer...'); await run('npm install -D tailwindcss postcss autoprefixer'); // Create postcss.config.js - const data = ` -module.exports = { + const data = `module.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, }, -};`; +}; +`; fs.writeFileSync('postcss.config.js', data, 'utf8'); log('postcss.config.js created.'); } else { From 5e5d39609c17d8effe1880d648b63b0a20ad8eff Mon Sep 17 00:00:00 2001 From: CorwinDev <88144943+CorwinDev@users.noreply.github.com> Date: Sat, 21 Jan 2023 09:57:15 +0100 Subject: [PATCH 10/12] Optimalization CLI, also added formatting --- cli.js | 124 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 50 deletions(-) diff --git a/cli.js b/cli.js index 35c930326..085ce31ed 100644 --- a/cli.js +++ b/cli.js @@ -16,7 +16,7 @@ if ( process.exit(1); } else { console.log( - '\x1b[0;34m Flowbite CLI \x1b[0m\n\nUsage:\n\n flowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -v, --version\t\tShow the version\n --debug\t\tShow debug messages\n -p --postcss\t\tInitialize a postcss.config.js file' + '\x1b[0;34mFlowbite CLI \x1b[0m\n\nUsage:\nflowbite init [options]\n\nOptions:\n -h, --help\t\tShow this help\n -d, --default\t\tCreates a default tailwind.config.js without Flowbite installed\n -p --postcss\t\tInitialize a postcss.config.js file\n --debug\t\tShow debug messages' ); process.exit(1); } @@ -38,7 +38,7 @@ if (process.argv.includes('init')) { process.exit(1); } -function createTailwindConfig() { +async function createTailwindConfig() { if (fs.existsSync('tailwind.config.js')) { log('tailwind.config.js already exists.'); return; @@ -51,6 +51,7 @@ function createTailwindConfig() { plugins: [], }; `; + // Write the file fs.writeFileSync('tailwind.config.js', data, 'utf8'); log('tailwind.config.js created.'); @@ -58,7 +59,15 @@ function createTailwindConfig() { async function updateTailwindConfig() { // Read the file and replace plugins:[ with plugins: [require("flowbite") - const data = fs.readFileSync('tailwind.config.js', 'utf8'); + var data = fs.readFileSync('tailwind.config.js', 'utf8'); + var config; + try { + // Try to read the config file + config = await require(process.cwd() + '\\tailwind.config.js'); + } catch (e) { + log('Error: Invalid tailwind.config.js file.', true); + process.exit(1); + } // Check if the plugin is already added if ( data.includes("require('flowbite/plugin')") && @@ -70,42 +79,73 @@ async function updateTailwindConfig() { // Check if flowbite is installed if (!fs.existsSync('node_modules/flowbite')) { - log('Installing Flowbite...'); - // Check if package.json exists - if (!fs.existsSync('package.json')) { - console.log( - 'No npm project found!\nCreate one with \x1b[1m\x1b[4mnpm init\x1b[0m' - ); - process.exit(1); - } - await run('npm install -D tailwindcss autoprefixer flowbite'); - log('Dependencies installed.'); - updateTailwindConfig(); - } - var result = data; - if (!data.includes("require('flowbite/plugin')")) { - result = data.replace( - 'plugins: [', - "plugins: [require('flowbite/plugin')," + log( + 'Flowbite is not installed.\nInstall it with \x1b[1m\x1b[4mnpm install flowbite\x1b[0m.', + true ); + } + // Use regular expressions to find the line where the plugins are defined + const pluginLine = data.match(/plugins: \[[\s\S]*\]/)[0]; + + // Use regular expressions to find the current plugins + var currentPlugins = pluginLine?.match(/require\('.*'\)/g); + + // Checks if the plugin is already added + if (pluginLine) { + if (!currentPlugins) currentPlugins = []; + if (currentPlugins.includes("require('flowbite/plugin')")) + return log( + 'Flowbite has already been installed in tailwind.config.js.' + ); + // Add the new plugin to the current plugins + currentPlugins.push("require('flowbite/plugin')"); + log('Flowbite plugin added to tailwind.config.js.'); } - if (!data.includes('./node_modules/flowbite/**/*.js')) { - result = result.replace( - 'content: [', - "content: ['./node_modules/flowbite/**/*.js'," - ); + + // Check if the content is already added + if (!config.content.includes('./node_modules/flowbite/**/*.js')) { + config.content.push('./node_modules/flowbite/**/*.js'); log('Flowbite node_modules added to tailwind.config.js.'); } + + var end = JSON.stringify(config, null, 4).replace(/"([^"]+)":/g, '$1:'); + // Add the new plugin to the current plugins + end = end.replace( + /"require\('flowbite\/plugin'\)"/g, + "require('flowbite/plugin')" + ); + + // Updates the file + var result = data.replace( + /module.exports\s*=\s*\{(.*)$/gs, + 'module.exports = ' + + end + .replace( + /plugins: \[[\s\S]*\]/, + `plugins: [${currentPlugins.join(', ')}]` + ) + .replace(/"/g, "'") + ); + + // Write the file fs.writeFileSync('tailwind.config.js', result, 'utf8'); + log('tailwind.config.js updated.'); } async function postCss() { // Check if postcss.config.js exists if (!fs.existsSync('postcss.config.js')) { - // Install tailwindcss and autoprefixer - log('Installing tailwindcss and autoprefixer...'); - await run('npm install -D tailwindcss postcss autoprefixer'); + if ( + !fs.existsSync('node_modules/postcss') && + !fs.existsSync('node_modules/tailwindcss') && + !fs.existsSync('node_modules/autoprefixer') + ) { + log( + "PostCSS and AutoPrefixer aren't installed.\nInstall them with \x1b[1m\x1b[4mnpm install -D tailwindcss postcss autoprefixer\x1b[0m.", + true + ); + } // Create postcss.config.js const data = `module.exports = { plugins: { @@ -121,25 +161,6 @@ async function postCss() { } } -// Functions -const { exec } = require('child_process'); -/** - * Function to run command in terminal - * @param {string} cmd - * @returns - */ -const run = async (cmd) => { - const child = exec(cmd, (err) => { - if (err) console.error(err); - }); - if (debug == true) { - child.stderr.pipe(process.stderr); - child.stdout.pipe(process.stdout); - } - - await new Promise((resolve) => child.on('close', resolve)); -}; - /** * * @param {string} msg @@ -149,8 +170,7 @@ const run = async (cmd) => { function log(msg, important = false) { if (important == true) { console.log(msg + '\x1b[0m'); - } - if (debug == true) { + } else if (debug == true) { console.log(msg + '\x1b[0m'); } } @@ -161,8 +181,12 @@ function log(msg, important = false) { */ async function init() { if (process.argv.includes('-d') || process.argv.includes('--default')) { + if (fs.existsSync('tailwind.config.js')) { + log('tailwind.config.js already exists.', true); + process.exit(1); + } await createTailwindConfig(); - log('tailwind.config.js created.', true); + log('Tailwind config created without Flowbite', true); if (process.argv.includes('-p') || process.argv.includes('--postcss')) { await postCss(); } From b45dcde833236e7806099f9a56e5dcb509d263f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Sz=C5=91gy=C3=A9nyi?= Date: Mon, 23 Jan 2023 16:47:00 +0200 Subject: [PATCH 11/12] Update documentation with the next npx flowbite init instructions. --- README.md | 30 ++++++++++++----------- content/getting-started/introduction.md | 32 +++++++++++++------------ content/getting-started/quickstart.md | 32 +++++++++++++------------ 3 files changed, 50 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index d377be665..c5af8f161 100644 --- a/README.md +++ b/README.md @@ -51,31 +51,33 @@ Make sure that you have Node.js npm install flowbite ``` -2. Require Flowbite as a plugin inside the `tailwind.config.js` file: +2. Use the Flowbite CLI and run the following command to set up Flowbite in the `tailwind.config.js` file: -```javascript -module.exports = { - - plugins: [ - require('flowbite/plugin') - ] - -} +```bash +npx flowbite init ``` -3. Make sure that you add the template path to the `tailwind.config.js` file: +This will either generate a new default Tailwnd configuration file with Flowbite installed or set up Flowbite inside an existing Tailwind CSS project: ```javascript module.exports = { - content: [ - "./node_modules/flowbite/**/*.js" - ] + // other options ... + + content: [ + // other templates paths... + "./node_modules/flowbite/**/*.js" + ] + + plugins: [ + // other plugins... + require('flowbite/plugin') + ], } ``` -4. Include the main JavaScript file to make interactive elements work: +3. Include the main JavaScript file to make interactive elements work: ```html diff --git a/content/getting-started/introduction.md b/content/getting-started/introduction.md index bac9cc27c..c911d1bce 100644 --- a/content/getting-started/introduction.md +++ b/content/getting-started/introduction.md @@ -61,34 +61,36 @@ Flowbite is technically a plugin that can be included into any existing Tailwind npm install flowbite ``` -2. Include Flowbite as a plugin inside the `tailwind.config.js` file: +2. Use the Flowbite CLI and run the following command to set up Flowbite in the `tailwind.config.js` file: -```javascript -module.exports = { - - plugins: [ - require('flowbite/plugin') - ] - -} +```bash +npx flowbite init ``` -3. Additionally to your own `content` data you should add `flowbite` to apply the classes from the interactive elements in the `tailwind.config.js` file: +This will either generate a new default Tailwnd configuration file with Flowbite installed or set up Flowbite inside an existing Tailwind CSS project: ```javascript module.exports = { - content: [ - "./node_modules/flowbite/**/*.js" - ] + // other options ... + + content: [ + // other templates paths... + "./node_modules/flowbite/**/*.js" + ] + + plugins: [ + // other plugins... + require('flowbite/plugin') + ], } ``` -4. Require the JavaScript code that powers the interactive elements before the end of your `` tag: +3. Include the main JavaScript file to make interactive elements work: ```html - + ``` ### Include via CDN diff --git a/content/getting-started/quickstart.md b/content/getting-started/quickstart.md index 36a57a9a2..d14a2aab9 100644 --- a/content/getting-started/quickstart.md +++ b/content/getting-started/quickstart.md @@ -27,34 +27,36 @@ Make sure that you have Node.js< npm install flowbite ``` -2. Require Flowbite as a plugin inside the `tailwind.config.js` file: +2. Use the Flowbite CLI and run the following command to set up Flowbite in the `tailwind.config.js` file: -```javascript -module.exports = { - - plugins: [ - require('flowbite/plugin') - ] - -} +```bash +npx flowbite init ``` -3. Additionally to your own `content` data you should add `flowbite` to apply the classes from the interactive elements in the `tailwind.config.js` file: +This will either generate a new default Tailwnd configuration file with Flowbite installed or set up Flowbite inside an existing Tailwind CSS project: ```javascript module.exports = { - content: [ - "./node_modules/flowbite/**/*.js" - ] + // other options ... + + content: [ + // other templates paths... + "./node_modules/flowbite/**/*.js" + ] + + plugins: [ + // other plugins... + require('flowbite/plugin') + ], } ``` -4. Require the JavaScript code that powers the interactive elements before the end of your `` tag: +3. Include the main JavaScript file to make interactive elements work: ```html - + ``` ### Include via CDN From 453476720e8b3e3f83fb26123785d5186c06386a Mon Sep 17 00:00:00 2001 From: CorwinDev <88144943+CorwinDev@users.noreply.github.com> Date: Mon, 23 Jan 2023 16:02:44 +0100 Subject: [PATCH 12/12] Replace only neccesary things --- cli.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/cli.js b/cli.js index 085ce31ed..676d9b815 100644 --- a/cli.js +++ b/cli.js @@ -63,7 +63,7 @@ async function updateTailwindConfig() { var config; try { // Try to read the config file - config = await require(process.cwd() + '\\tailwind.config.js'); + config = await require('./tailwind.config.js'); } catch (e) { log('Error: Invalid tailwind.config.js file.', true); process.exit(1); @@ -109,23 +109,22 @@ async function updateTailwindConfig() { log('Flowbite node_modules added to tailwind.config.js.'); } - var end = JSON.stringify(config, null, 4).replace(/"([^"]+)":/g, '$1:'); - // Add the new plugin to the current plugins - end = end.replace( - /"require\('flowbite\/plugin'\)"/g, - "require('flowbite/plugin')" - ); - // Updates the file var result = data.replace( - /module.exports\s*=\s*\{(.*)$/gs, - 'module.exports = ' + - end - .replace( - /plugins: \[[\s\S]*\]/, - `plugins: [${currentPlugins.join(', ')}]` - ) - .replace(/"/g, "'") + /plugins\s*:\s*\[(.*)]/gs, + 'plugins: [' + currentPlugins.join(', ') + ']' + ); + + // Add " to all the content paths + var content = config.content.map((path, index) => { + // Check if index is the last one + if (index === config.content.length - 1) + return "\n '" + path + "'\n "; + return "\n '" + path + "'"; + }); + result = result.replace( + /content\s*:\s*\[(.*?)]/gs, + 'content: [' + content.join(',') + ']' ); // Write the file