Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This script allows you to spin up a new TailwindCSS project, it doesn't use any fancy hack of Node, it just simplifies the process of creating a new Node project with TailwindCSS default configuration.
- * Usage: node init-tailwind.js
- */
- const fs = require('fs');
- const path = require('path');
- const readline = require('readline');
- const { exec } = require('child_process');
- // Constants
- const DEFAULT_PROJECT_NAME = 'hello-tailwind';
- const DEFAULT_VERSION = '1.0.0';
- const DEFAULT_MAIN_FILE = 'index.js';
- const DEFAULT_LICENSE = 'MIT';
- function printEndScript() {
- console.log('\nNotes:');
- console.log('- Now, you can run: "npm install -D tailwindcss" if you want.');
- console.log('- if you want to use "start" command you should: "npm install -g http-server".\n');
- }
- // Create readline interface
- const rl = readline.createInterface({
- input: process.stdin,
- output: process.stdout
- });
- console.log('This script will walk you through creating a new TailwindCSS project structure:\nPress ^C at any time to quit.\n');
- // Ask for user input
- rl.question('Would you like to install TailwindCSS [Y/N]? (Y): ', (doInstall) => {
- rl.question(`package name (${DEFAULT_PROJECT_NAME}): `, (name) => {
- rl.question(`version (${DEFAULT_VERSION}): `, (version) => {
- rl.question('description: ', (description) => {
- rl.question(`entry point (${DEFAULT_MAIN_FILE}): `, (main) => {
- rl.question('author: ', (author) => {
- rl.question(`license (${DEFAULT_LICENSE}): `, (license) => {
- // Use default values if user didn't provide any input
- doInstall = doInstall.toLowerCase()[0] || 'y';
- name = name || DEFAULT_PROJECT_NAME;
- version = version || DEFAULT_VERSION;
- description = description || '';
- main = main || DEFAULT_MAIN_FILE;
- author = author || '';
- license = license || DEFAULT_LICENSE;
- // Create an empty Node.js project
- const packageJson = {
- name,
- version,
- description,
- main,
- scripts: {
- build: 'npx tailwindcss -i ./src/input.css -o ./build/css/style.css'
- },
- author,
- license
- };
- //fs.writeFileSync('package.json', JSON.stringify(packageJson, null, 2));
- // Add a "build" script to "package.json"
- packageJson.scripts = {
- ...packageJson.scripts,
- "dev": 'npx tailwindcss -i ./src/input.css -o ./build/css/style.css --watch',
- "build": 'npx tailwindcss -i ./src/input.css -o ./build/css/style.css',
- "start": 'http-server ./build',
- };
- fs.writeFileSync('package.json', JSON.stringify(packageJson, null, 2));
- // Create "tailwind.config.js" file
- fs.writeFileSync(path.join('tailwind.config.js'),
- `/** @type {import('tailwindcss').Config} */
- module.exports = {
- content: ["./build/**/*.{html,js,ts}"],
- theme: {
- extend: {}
- },
- plugins: [],
- }
- `);
- // Create "build" directory with a boilerplate "index.html" file
- fs.mkdirSync('build', { recursive: true });
- fs.writeFileSync(path.join('build', 'index.html'),
- `<!DOCTYPE html>
- <html>
- <head>
- <link rel="stylesheet" href="css/style.css">
- </head>
- <body>
- <div class="bg-red-500"><h1>It works!</h1></div>
- </body>
- </html>
- `);
- // Create "src" directory with a "input.css" file
- fs.mkdirSync('src', { recursive: true });
- fs.writeFileSync(path.join('src', 'input.css'),
- `@tailwind base;
- @tailwind components;
- @tailwind utilities;
- `);
- console.log(`\nGenerated files:
- build/index.html
- src/input.css
- package.json
- tailwind.config.js
- \n`);
- // Install Tailwind CSS
- if (doInstall == 'y') {
- console.log('Installing TailwindCSS... please wait.');
- exec('npm install tailwindcss -D', (error) => {
- if (error) {
- console.error(`exec error: ${error}`);
- return;
- } else {
- console.log('TailwindCSS seems to be installed successfully.');
- printEndScript();
- }
- });
- } else {
- printEndScript();
- }
- // Close readline interface
- rl.close();
- });
- });
- });
- });
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement