Skip to content
Advertisement

Create a css file using node js [closed]

I am building a node module which will create a boilerplate for me…

in that i just want it to create a css file with the specified content.. How can i do it ?

similarly i also want to create a javascript file and a html file… i tried it with fs module in node but the content is not accepted by it…

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300&display=swap');


::selection {
        color: cyan;
        background: black;
}

/* Under customization */
:root{
    --main-background: #0b0c0f;
    --main-fonts-color: #fff;
    --main-decor-color:#00a9e2;
    --main-header-background:#21252e;
    --main-font-family: 'Poppins', sans-serif;
}
/* end */

*{
    margin: 0;
    padding: 0;
    scroll-behavior: smooth;
}

body {
    background-color: var(--main-background);
    color: var(--main-fonts-color);
    font-family: 'Courier New', Courier, monospace;
}

/* Nav bar css start */

.icon {
    position: absolute;
    top: 70%;
    left: 70%;
    transform: translateX(-50%) translateY(-50%);
  }
  .ham {
    content: ' ';
    position: relative;
    width: 20px;
    border: 2px solid #00a9e2;
    border-radius: 5px;
    transition: all 0.333333333333333s ease;
  }
  .ham:after,
  .ham:before {
    content: ' ';
    position: absolute;
    left: 50%;
    width: 100%;
    border: 2px solid #00a9e2;
    border-radius: 5px;
    transform: translateX(-50%);
    transition-property: top, bottom, transform;
    transition-duration: 1s, 1s, 0.25s;
  }
  .ham:after {
    top: -8px;
  }
  .hamr:before {
    bottom: -8px;
  }
  .menu {
    pointer-events: none;
  }
  .menu--circle {
    position: absolute;
    z-index: 13;
    width: 243.33333333333334px;
    height: 243.33333333333334px;
    overflow: hidden;
  }
  .menu__active {
    position: relative;
  }
  .menu__toggle {
    z-index: 11;
    position: fixed;
    top: -100px;
    left: -100px;
    width: 200px;
    height: 200px;
    background-color: #ffff;
    border-radius: 50%;
    transition: transform 1.3s;
    cursor: pointer;
    pointer-events: auto;
  }
  .menu__listings {
    z-index: 10;
    position: fixed;
    top: -200px;
    left: -200px;
    width: 400px;
    height: 400px;
    border-radius: 50%;
    transform: scale(0.1) rotate(150deg);
    transition: transform 1s;
  }
  .menu__arrow {
    visibility: hidden;
    position: fixed;
  }
  .menu__arrow input[type="radio"] {
    position: fixed;
    top: -99999px;
    left: -99999px;
  }
  .menu__arrow ul {
    padding: 0;
    margin: 0;
    list-style: none;
  }
  .menu__arrow--top {
    top: 0.5em;
    left: 220px;
  }
  .menu__arrow--top .arrow {
    transform: rotate(-45deg);
  }
  .menu__arrow--left {
    top: 220px;
    left: 0.5em;
  }
  .menu__arrow--left .arrow {
    transform: rotate(-135deg);
  }
  .arrow {
    width: 20px;
    height: 20px;
    border-right: 6.666666666666667px solid yellow;
    border-top: 6.666666666666667px solid #fff;
    border-radius: 3px;
    transition: border-color 0.3s;
    cursor: pointer;
    pointer-events: auto;
  }
  .arrow:hover {
    border-color: #ff947f;
    transition: border-color 0.3s;
  }
  .circle {
    position: relative;
    padding: 0;
    margin: 0;
    height: 100%;
    width: 100%;
    background-color: #0000;
    border-radius: 50%;
    transform: rotate(108deg);
    overflow: hidden;
    list-style: none;
  }
  .circle li {
    position: absolute;
    top: 0;
    right: 0;
    width: 50%;
    height: 50%;
    transform-origin: 0 100%;
  }
  .circle li .placeholder {
    position: absolute;
    left: -100%;
    padding-top: 1.5em;
    width: 200%;
    height: 200%;
    text-align: center;
    transform: skewY(54deg) rotate(18deg);
  }
  .circle li .placeholder .upside {
    transform: rotate(180deg);
  }
  .circle li .placeholder a {
    text-decoration: none;
    pointer-events: auto;
  }
  .circle li .placeholder button {
    pointer-events: auto;
  }
  .circle li .placeholder .button {
    font-size: 2.3em;
    background-color: transparent;
    border: none;
    color: #00a9e2;
    cursor: pointer;
  }
  .circle li:nth-child(1) {
    transform: rotate(0deg) skewY(-54deg);
    background-color: #0000;
  }
  .circle li:nth-child(2) {
    transform: rotate(36deg) skewY(-54deg);
    background-color: #0000;
  }
  .circle li:nth-child(3) {
    transform: rotate(72deg) skewY(-54deg);
    background-color: #0000;
  }
  .circle li:nth-child(4) {
    transform: rotate(108deg) skewY(-54deg);
    background-color: #0000;
  }
  .circle li:nth-child(5) {
    transform: rotate(144deg) skewY(-54deg);
    background-color: #0000;
  }
  .circle li:nth-child(6) {
    transform: rotate(180deg) skewY(-54deg);
    background-color: #0000;
  }
  .circle li:nth-child(7) {
    transform: rotate(216deg) skewY(-54deg);
    background-color: #0000;
  }
  .circle li:nth-child(8) {
    transform: rotate(252deg) skewY(-54deg);
    background-color: #0000;
  }
  .circle li:nth-child(9) {
    transform: rotate(288deg) skewY(-54deg);
    background-color: #0000;
  }
  .circle li:nth-child(10) {
    transform: rotate(324deg) skewY(-54deg);
    background-color: #0000;
  }
  #menu__active {
    position: fixed;
    top: -99999px;
    left: -99999px;
  }
  #menu__active:checked ~ label .menu__listings {
    transform: rotate(10deg) scale(1);
    transition: transform 1s;
  }
  #menu__active:checked ~ label .menu__toggle {
    background-color: #00a9e2;
    transition: all 1s;
  }
  #menu__active:checked ~ label .ham {
    border-color: transparent;
    transition: border-color 0.333333333333333s;
  }
  #menu__active:checked ~ label .ham:after {
    top: -2px;
    border-color: #fff;
    transform: translateX(-50%) rotate(45deg);
    transition-property: top, transform;
    transition-duration: 0.25s, 1s;
  }
  #menu__active:checked ~ label .ham:before {
    bottom: -2px;
    border-color: #fff;
    transform: translateX(-50%) rotate(-45deg);
    transition-property: bottom, transform;
    transition-duration: 0.25s, 1s;
  }
  #menu__active:checked ~ label .button:hover {
    color: #c1264e;
  }
  #menu__active:checked ~ label .menu__arrow {
    visibility: visible;
    transition: all 1s 1.111111111111111s;
  }
  #menu__active:checked ~ label .menu__arrow--top label {
    display: none;
  }
  #menu__active:checked ~ label .menu__arrow--top label[for="degree--up-0"] {
    display: block;
  }
  #menu__active:checked ~ label #degree--up-0:checked ~ .menu__listings {
    transform: rotate(116deg);
  }
  #menu__active:checked ~ label #degree--up-0:checked ~ .menu__arrow--top label {
    display: none;
  }
  #menu__active:checked ~ label #degree--up-0:checked ~ .menu__arrow--top label[for="degree--up-1"] {
    display: block;
  }
  #menu__active:checked ~ label #degree--up-1:checked ~ .menu__listings {
    transform: rotate(224deg);
  }
  #menu__active:checked ~ label #degree--up-1:checked ~ .menu__listings ~ .menu__arrow--top label {
    display: none;
  }
  #menu__active:checked ~ label #degree--up-1:checked ~ .menu__listings ~ .menu__arrow--top label[for="degree--up-2"] {
    display: block;
  }

/* Nav bar css end */


can anyone please help

thank you in advance… ^^

Advertisement

Answer

Basically, you have two separate tasks ahead. Creating the file content and writing it into a file. The first one is usually the most problematic, because there are infinite ways of doing it.

I recommend you to use a templating engine like Mustache to create the content by passing variables to a template.

Finally you’ll use Node’s FS to effectively create the file using that content.

The following is an oversimplified example:

./index.js

import {default as Mustache} from 'mustache';
import {readFileSync, writeFileSync} from 'fs';
import {resolve} from 'path';

const templatePath = resolve(__dirname, './template.mustache');
const outputPath = resolve(__dirname, './my-file.css');

const templateContent = readFileSync(templatePath, 'utf8');

const data = {
  justifyContent: 'center'
};

const fileContent = Mustache.render(templateContent, data);


writeFileSync(outputPath, fileContent, 'utf8');

./index.js (CJS Version)

const Mustache = require('mustache');
const fs = require('fs');
const path = require('path');

const templatePath = path.resolve(__dirname, './template.mustache');
const outputPath = path.resolve(__dirname, './my-file.css');

const templateContent = fs.readFileSync(templatePath, 'utf8');

const data = {
  justifyContent: 'center'
};

const fileContent = Mustache.render(templateContent, data);


fs.writeFileSync(outputPath, fileContent, 'utf8');

./template.mustache

.my-class {
  justify-content: {{justifyContent}};
}

./my-file.css (output)

.my-class {
  justify-content: center;
}

To remove files and directories you can use recently added fs.rm() (requires Node v14.14.0 or newer):

const fs = require('fs');
const path = require('path');

const deletePath = path.resolve(__dirname, './file/or/directory/to/delete');

fs.rm(deletePath, {recursive: true, force: true});
Advertisement