Giter VIP home page Giter VIP logo

ejs-cli's Introduction

ejs-cli

Buy me a coffee CI NPM version

Enhanced version of EJS cli. A command-line tool based on the ejs wrapper, but with many useful features added.

Features

  • Support config files to configure ejs options.
  • Multiple .ejs files matching to generate html.
  • Multiple ways of data injection into templates.
  • Global data injection template
  • Monitoring .ejs to output .html files in real time.

Install

$ npm install @wcj/ejs-cli
# Or
$ npm install --global @wcj/ejs-cli

Quick start

$ ejsc "template/**/*"
$ ejsc "template/*.ejs" "template/about/*.ejs" --watch

Command Help

Below is a help of commands you might find useful. You can use the ejsc and ejs-cli commands:

Usage: ejs-cli <source...> [options]

Options:

  -v, --version             Show version number
  -h, --help                Show help
  -w, --watch               Listen to ejs changes and output HTML (default: "false")
  -o, --out                 Specify the output directory (default: "dist")
  -m, --delimiter           Use CHARACTER with angle brackets for open/close (defaults to %)
  -p, --open-delimiter      Use CHARACTER instead of left angle bracket to open.
  -c, --close-delimiter     Use CHARACTER instead of right angle bracket to close.
  -f, --data-file FILE      Must be JSON-formatted. Use parsed input from FILE as data for rendering
  --rm-whitespace           Remove all safe-to-remove whitespace, including leading and trailing

Examples:

  $ ejsc "template/*.ejs" "template/about/*.ejs"
  $ ejsc "template/*.ejs" "template/about/*.ejs" --watch
  # The above command: matches all `.ejs` files in the template folder
  $ ejsc "template/**/*" --watch
  $ ejs-cli "template/*.ejs" --watch --out build

Copyright 2023

Match files

Folders and .ejs files starting with an underscore (_) will be ignored.

$ ejs-cli "template/**/*"
$ ejsc "template/**/*"
$ ejsc "template/*.ejs" "template/about/*.ejs"
$ ejsc "template/home.ejs" "template/about.ejs"

The above command: matches all .ejs files in the template folder, excluding files starting with _ and .ejs files in folders starting with _.

Inject data

Inject data by default

PUBLIC_PATH

PUBLIC_PATH Relative path string concatenation. E.g: ../, ../../.

<link rel="stylesheet" href="<%= PUBLIC_PATH %>static/css/main.css">

<img src="<%= PUBLIC_PATH %>static/img/logo.png" />

<a href="<%= PUBLIC_PATH %>about/index.html"><a>

GLOBEL

You need to specify the data file --data-file ./data.json on the command line, or configure the globelData field in the configuration

<h2><%= GLOBEL.helloworld %></h2>

If the specified ./temp.json injection data content is an array, the value will be assigned to the template variable of TEMP. The variable naming rule is uppercase for file names:

  • ./a/data-name.json => DATA_NAME
  • ./temp/data.json => DATA
  • ./temp/temp.json => TEMP
//=> ./a/data-name.json
[
  { name: "ejs", version: "v1.2" },
  { name: "auto-config-loader", version: "^1.7.4" },
];

The value will be assigned to the template variable of DATA_NAME

<% DATA_NAME.forEach((item) => { %>
  <div><%= item.name %>@<%= item.version %></div>
<% }); %>

The rules are the same in configuration.

Specific Template

Inject data into a specific template, which needs to be configured in .ejscrc.mjs:

{
  "globelData": {
    "helloworld": "Hello Wrold!"
  },
  "data": {
    "template/about/index.ejs": "./data.json",
    "template/home.ejs": {
      "name": "Hello World",
      "age": 36
    }
  }
}

Used in template/home.ejs template

<h2><%= name %></h2>

Config

The default configuration is the parameter of EJS, you can add data to inject data into the EJS template, and add watchOption parameter to configure Chokidar settings.

Store .ejscrc.json in the root directory of the project:

{
  "watchOption": {},
  "data": {
    "template/home.ejs": {
      "name": "Hello World",
      "age": 36
    }
  }
}

Support JSON, JSONC, JSON5, YAML, TOML, INI, CJS, Typescript, and ESM config load.

.ejscrc.mjs config example:

/**
 * @type {import('@wcj/ejs-cli/lib/watch.mjs').Options}
 */
export default {
  /** Chokidar's watch parameter settings */
  watchOption: {},
  /** Injecting data into EJS templates */
  data: {
    "template/home.ejs": {
      name: "Hello World",
      age: 36,
    },
  },
};

You can configure in package.json:

{
  "name": "@wcj/examples",
  "version": "0.0.1",
  "ejsc": {
    "data": {
      "template/home.ejs": {
        "name": "Hello World",
        "age": 36
      }
    }
  }
}

For more configuration methods, please see default searchPlaces.

Development

$ npm i
$ npm run build

Contributors

As always, thanks to our amazing contributors!

Made with contributors.

License

MIT © Kenny Wong

ejs-cli's People

Contributors

jaywcjlove avatar renovate[bot] avatar

Stargazers

mindsets avatar Alexander Hartmann avatar  avatar Piotr Skiba avatar

Watchers

 avatar  avatar

ejs-cli's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Other Branches

These updates are pending. To force PRs open, click the checkbox below.

  • chore(deps): update actions/checkout action to v4
  • chore(deps): update actions/setup-node action to v4
  • chore(deps): update peaceiris/actions-gh-pages action to v4

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v3
  • actions/setup-node v3
  • actions/checkout v3
  • actions/setup-node v3
.github/workflows/main.yml
  • actions/checkout v3
  • actions/setup-node v3
  • peaceiris/actions-gh-pages v3
  • ncipollo/release-action v1
npm
core/package.json
  • auto-config-loader ^1.7.4
  • chokidar ^3.5.3
  • ejs ^3.1.9
  • fs-extra ^11.1.1
  • glob ^10.3.3
  • meow ^12.1.1
  • @types/ejs ^3.1.2
  • @types/fs-extra ^11.0.1
  • @types/node ^20.5.7
  • typescript ^5.2.2
  • node >=16
package.json
  • husky ^8.0.3
  • lerna ^7.0.0
  • lint-staged ^14.0.1
  • prettier ^3.0.3
  • typescript ^5.2.2
  • jest ^29.5.0
  • jest-watch-typeahead ^2.2.2
  • jest-environment-jsdom ^29.6.4
  • jest-environment-node ^29.5.0
  • node >=16.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.