2.0.6 • Published 8 months ago

@kooofly/easy-code v2.0.6

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

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.