Coder Social home page Coder Social logo

ace's Introduction

Ace

Node.js framework for creating command line applications. Used by AdonisJs

circleci-image npm-image

Table of contents

Usage

Install the package from npm registry as follows:

npm i @adonisjs/ace

# yarn
yarn add @adonisjs/ace

And then use it as follows:

import {
  Kernel,
  BaseCommand,
  args,
  flags
} from '@adonisjs/ace'

import { Ioc } from '@adonisjs/fold'
import { Application } from '@adonisjs/application/build/standalone'

class Make extends BaseCommand {
  @args.string()
  public resource: string

  @args.string()
  public name: string

  @flags.boolean()
  public overwrite: boolean
  
  public static commandName = 'make'
  public static description = 'Make a new resource'
  
  // called when the command is executed
  async handle () {
    console.log(this.name)
    console.log(this.resource)
    console.log(this.overwrite)
  }  
}

const application = new Application(__dirname, new Ioc(), {}, {})
const kernel = new Kernel(app)
kernel.register([Make]) 

kernel.handle(process.argv.splice(2))

Displaying help

Ace doesn't hijack any flags or commands to display the help. You are free to decide when and how to show the help screen.

import { Kernel, BaseCommand } from '@adonisjs/ace'

import { Ioc } from '@adonisjs/fold'
import { Application } from '@adonisjs/application/build/standalone'

const application = new Application(__dirname, new Ioc(), {}, {})
const kernel = new Kernel(app)

kernel.flag('help', (value, options, command) => {
  if (!value) {
    return
  }

  /**
   * When a command is not defined, then it will show
   * help for all the commands
   */
  Kernel.printHelp(command)
  process.exit(0)
})

kernel.handle(process.argv.splice(2))

Decorators

The module comes with ES6 decorators to define arguments and flags for a given command.

args.string

Define an argument. To make the argument optional, you can set the required property to false

args.string({ required: false })

You can also define the argument description as follows:

args.string({ description: 'The resource type to create' })

args.spread

Argument that receives all of the remaining values passed as arguments to a given command. Think of it as a spread operator in Javascript.

class Make extends BaseCommand {
  @args.spread({ description: 'One or more files' })
  public files: string[]
}

flags.boolean

Define a flag that accepts a boolean value.

flags.string

Define a flag that accepts a string value.

flags.array

Define a flag that accepts an array of values.

You can also define description for a flag, similar to the arg. Also, a flag can define aliases and the default values.

class Make extends BaseCommand {

  flags.string({
    alias: 'r',
    description: 'The resource name',
    default: 'controller',
  })
  resource: string
}

MIT License, see the included MIT file.

ace's People

Contributors

thetutlage avatar dependabot-preview[bot] avatar greenkeeper[bot] avatar romainlanz avatar greenkeeperio-bot avatar lmj0011 avatar marcuspoehls avatar

Watchers

James Cloos avatar

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.