# Xjs-Cli

xjs-cli (opens new window) is a command-line tool made for xpresser. It provides helpful commands that can assist you in building your application faster.

# Requirements

  • Node version >=v10
  • Git command line tool.

# Installation

# Usage

To view list of available commands

xjs
# OR
xjs --help

Note: Available commands differs when the above command is called from a folder without xjs-cli init file.

# Init File

An init file is required to tell xjs-cli your Boot File where xpresser was called from.

Create init file assuming you have xpresser booted in app.js

xjs init app.js
# OR (Typescript)
xjs init app.ts

Your init file use-xjs-cli.json will be generated in current working directory.

The commands are seperated in to two parts. The first part has the dev commands the second the prod commands.

key Description
main Your boot file.
start_cron The bash command to start cron
start_server The bash command to start the server
start_console The bash command to execute your main file when running console commands.
stop_cron The bash command to stop cron
stop_server The bash command to stop already running server
jobs_path The path to your jobs folder. Relative to base folder.
async_cron_jobs If set to true, Cron jobs will run asynchronously. By default is false.
tsc An array of commands to call when we run xjs tsc build

# Non-Project Commands

These commands are only available when there is no use-xjs-cli.json file in the current working directory.

# new/create

Create new xjs project.

xjs new [name]

# init

Initialize xjs-cli in your project and creates a use-xjs-cli.json. You need to pass the name of your xpresser boot file.

xjs init [xpresser_file]

# If boot file is app.js
xjs init app.js

# nginx:config

This command helps you create a minimal nginx configuration for your project on the fly.

xjs nginx:config

# Result (Questionnaire)
? Name of config file: my_app
? Path to file: /path/to/desired/folder.
? Your app domain: myapp.com
? Your app url (including port): localhost:3000
=>  Conf: my_app has been created at /path/to/desired/folder

The above questionnaire will create the file below.

server {
	listen 80;
#	listen 443 ssl;

	server_name myapp.com www.myapp.com;

#	ssl_certificate "cert.pem";
#   ssl_certificate_key "cert.key";


	location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

# Project Commands

Project commands are only available when there is a valid use-xjs-cli.json in the current working directory.


Commands can have arguments that are required or optional

[optional] Square braces represents the optional arguments
<required> Less & Greater Than signs represents the required arguments


# help

Display help for commands

xjs help [command]

# Example
xjs help
# With Command
xjs help start

# up

Removes app from maintenance mode.

xjs up

# down

Put App in maintenance mood.

xjs down

# start

Start the main file defined in use-xjs-cli.json

xjs start

By default, the development init config will be used. To start script in production xjs-cli uses pm2 (opens new window) to start your main file.

Run xjs install-prod-tools to install pm2 on your machine.

xjs start --prod

# routes

Show all registered routes in your project.

xjs routes [search] [query]
┌─────────┬────────┬──────┬────────────┬──────┐
│ (index) │ method │ path │ controller │ name │
├─────────┼────────┼──────┼────────────┼──────┤
│    0    │ 'ALL'  │ '/*' │ [Function] │ null │
└─────────┴────────┴──────┴────────────┴──────┘

# stack

Return concatenated commands of a stack without running it. This way you can run it yourself. See Xjs-Cli Stacks

xjs stack <name>

# Running returned commands with bash
xjs stack myCommands | bash

# @stack

Run concatenated commands of a stack using node exec. See Xjs-Cli Stacks

xjs @stack <name>

# @/run

Run job

xjs @ <job>
# OR
xjs run <job>

# make:job

Generate a job file.

xjs make:job <name>

# Example
xjs make:job EmptyPostTrash

# make:event

Generate an event file.

xjs make:event <name> [namespace]

# Example
xjs make:event UserEvents
# Adding namespace
xjs make:event UserEvents user

# make:view

Generate a view file using template extension set in config {template.extension}

xjs make:view <name>

# make:model

Generate a model file. By default, this command is not useful without a plugin that plugs to it.

xjs make:model <name>

# make:controller

Generate a controller file.

xjs make:controller [name]

# make:controllerService

Generate a controllerService file.

xjs make:controllerService <name>

# Example
xjs make:controllerService UserService

# make:middleware

Generate a middleware file.

xjs make:middleware <name>

# Example
xjs make:middleware Auth

# cron

Run jobs that have the schedule property defined.

xjs cron

# stop

xjs stop all   ## Stop all services.
xjs stop cron   ## Stop cron service.
xjs stop server   ## Stop server

# restart

xjs stop all   ## Restart all services.
xjs stop cron   ## Restart cron service.
xjs stop server   ## Restart server

# import

This command imports required plugin files into your project.

xjs import <plugin> <folder> [overwrite]

# For the @xpresser/auth plugin
xjs import Auth configs
# Or with overwrite
xjs import Auth configs overwrite

# check-for-update

Checks if you are using the latest version of xpresser, if you are not, it will ask if you would love to update.

xjs check-for-update

# Result
=>  Checking npm registry for version update...
=>  xpresser latest version is 0.2.83 but yours is 0.2.81
? Would you like to update? (Y/n) 

# Stacks

The stack command is a utility command that helps you when running multiple bash commands.
For Example, most times we want to delete the build directory before we rebuild or run series of commands, we end up running something like this

rm -rf ./build && some-other-command && npm run build

With stack we can stack them up and call them with one command.

# Register a Stack

To register a stack of commands, all you need to do is add {stacks.<commandName>} in your use-xjs-cli.json

{
  "stacks": {
    "myCommands": [
       "rm -rf ./build",
       "some-other-command",
       "npm run build"
    ]
  }
}

# Using a stack

Once your stack has been registered, you can call it like so:

xjs stack myCommands

# Returns concatenated version of your commands
### rm -rf ./build && some-other-command && npm run build

or run it like so:

xjs @stack myCommands

# Runs all commands
### => Running stack {myCommands}
### => rm -rf ./build && some-other-command && npm run build
### => Stack {myCommands} executed successfully!

# Cron Jobs

xjs-cli makes running cron jobs easier using this great node package: cron (opens new window).

To add a job to cron all you need to do is: Register it in your paths/to/jobs/folder/cron.(js|json). if you don't have a cron file then create one in your jobs folder. Running xjs cron without a cron file will throw an error that should include the expected path to your cron file.

# why Js or Json?

The default is cron.json, but when not found xjs-cli will try looking for a cron.js file.
We considered adding the .js support to give more options when declaring your cron jobs.

Given a scenerio when you want to use some cron time parser like cron-time-generator (opens new window)

const cronTime = require('cron-time-generator');

module.exports = [
     {
         job: "SomeJob",
         schedule: cronTime.everyMinute()
     },

    {
         job: "SomeOtherJob",
         schedule: cronTime.everyDayAt(4, 30)
     }
]

with cron.js you have power todo more.