npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kooofly/easy-code

v2.0.4

Published

easy-code is a tool for create files use template.

Downloads

599

Readme

easy-code

easy-code is a tool for generate files use template.

For example:

  • you can use it to generate model file
  • you can use it to generate SPA route file, and watch folder change
  • you can use it to generate other custom file etc.

Installation

To install easy-code globally

npm install -g @kooofly/easy-code

To add easy-code as a dependency to you project

npm install --save-dev @kooofly/easy-code

Usage

Configuring easy-code with a configuration file

You can provide a javascript that exports a single configuration object. default file name is ec.config.mjs.

example

JavaScript configuration file example ec.config.mjs

export default {
  hello: {
    async beforeCreate (ctx, next) {
      const { name, commandOptions, helper, watchPaths } = ctx
      next({
        override: true,
        template: '@/template.html',
        output: '@/output.html',
        params: {
          message: {
            hello: name,
            name: '<span>easy-code</span>',
            now: Date.now()
          }
        }
      })
    }
  }
}

template.html

<!DOCTYPE html>
<html>
<head>
  <title>hello-example</title>
</head>
<body>
  <% if (message) { %>
    <h1><%= message.hello %><%- message.name %> <%= message.now %></h1>
  <% } %>
</body>
</html>

Then run the easy-code command:

ecode hello

output: output.html

<!DOCTYPE html>
<html>
<head>
  <title>hello-example</title>
</head>
<body>
  
    <h1>hello<span>easy-code</span> 1660657538278</h1>
  
</body>
</html>

More example

Command-line arguments to easy-code

Usage: ecode [options]

Options:
  -V, --version                 output the version number
  -k, --key <config key>        config key
  -p, --params <custom params>  custom params. "ecode hello abc" or "ecode hello foo=1^bar=2"
  -c, --config <config file>    config file, default is ec.config.mjs
  -d, --debug <debug>           output debug info
  -h, --help                    display help for command

If you installed easy-code globally, run the easy-code command:

ecode -k YourCustomKey -p YourParams

or

ecode YourCustomKey YourParams

Configuration

{
  [CustomKey: string]: {
    watchPaths: (string or array of strings) Paths to files, dirs to be watched recursively, or glob patterns.

    beforeCreate: (context: Context, next) => {
      next([{
        format: (default: false)
          boolean or Customize formatter function (v: string) => string or IPrettierOptions object.
          true: equivalent to executing the method prettier.format(fileData, { semi: false, parser: 'babel', singleQuote: true, trailingComma: 'none' })
          IPrettierOptions: more details: https://prettier.io/docs/en/options.html
        
        override: (default: false)
          If set to true will overwrite the file regardless of whether the file exists

        template: template path or template string. 
          path example: '@/template.js'.
          string example: 'hello <%- foo %>'; more detail: https://ejs.co/#docs .

        output: output path. example: '@/output.js' or 'output.js'.

        params: json object. example: { foo: 1, bar: 2 }.

      }])
    }

    afterCreate: (
      context: Context,
      results: file contents
    ) => void
    
    onWatch?: (
      {
        event: Available events: add, addDir, change, unlink, unlinkDir, ready, raw, error.
        path: path string.
        stats: fs.Stats: https://nodejs.org/api/fs.html#class-fsstats
      },
      next
    ) => {
      // If you execute the `next` method, the configuration will be executed immediately
      // IF not it will go default process
      next()
    }
  }
}

interface Config {
  [customKey: string]: Options
}

interface Options {
  watchPaths: string | string[]
  beforeCreate: (context: Context, next) => void,
  afterCreate: (context: Context, results: Array<string>) => void
  onWatch?: ({ event, path, stats }, next) => void
}

type BeforeCreateNextOptions = BeforeCreateNextOption[] | BeforeCreateNextOption
interface BeforeCreateNextOption {
  format: boolean | ((v: string) => string) | IPrettierOptions
  override: boolean
  template: string
  output: string
  params: Record<string, any>
}

interface Context {
  helper: {
    firstToUpperCase: (str: string) => string
    firstToLowerCase: (str: string) => string
    getFileName: (name: string, operator?: string) => string
    getExtension: (name: string, operator?: string) => string
    importModule: (filePath: string) => Promise<any> // example: await importModule('foo.mjs')
    importDeCache: (filePath: string) => any // example: importDeCache('foo.js')
    getFileContent: (filePath: string) => Promise<string>
    getFileList: (folderPath: string) =>  Promise<{ filePath: string, fileName: string, fileExtension: string }[]>
    getDirTree: (folderPath: string, onEachFile: (item: Item, path, stats) => void, onEachDirectory: (item: Item, path, stats) => void) =>  Promise<TreeObject>
    createRequire: Function /* module.createRequire */
    ejs: {/* https://ejs.co/#docs */}
    fastGlob: {/* https://www.npmjs.com/package/fast-glob */}
    logMethods: {
      info: Function
      warn: Function
      error: Function
    }
  }
  name: string
  watchPaths?: string | string[]
}

type Item = {
  id: string
  name: string
  path: string
  relativePath: string
  extension: string
  type: 'file' | 'directory'
}

interface IPrettierOptions {/* https://prettier.io/docs/en/options.html */}

type TreeObject = Object

Dependents

Changelog

  • v2.0.2: normalize onWatch path (Unix => foo/bar; Windows => foo/bar)
  • v2.0.1: Optimized README
  • v2.0.0:
    • Support recursive watching.
    • Support customize formatter.
    • Optimized configuration Items.
    • Optimized command options.