think-cli
is a command-line interface for ThinkJS.
$ npm install -g think-cli
- new - generate a new project from a template
- list - list available official templates
- module - add module from a template
- controller - add controller from a template
- service - add service from a template
- model - add model from a template
- middleware - add middleware from a template
- adapter - add adapter from a template
- migrate - migrate the project to think-cli 2.0
- sync - Synchronize the latest version of the project template to the local cache directory
- clean - Clear the project template cache
Usage:
$ thinkjs new <project-name> <template-name>
Example:
$ thinkjs new my-project standard
The above command pulls the template from think-template/standard, prompts for some information, and generates the project at ./my-project/.
- is optional, defaults to the current working directory
- is optional, default is the standard template, support offline use
Example:
$ thinkjs new
The above command, prompts for some information, and generates the project at ./ (current working directory), support offline use
If you want to create a multi-module project, you need to add the -m
parameter:
$ thinkjs new -m
thinkjs provide some recommended templates.
All official project templates are repos in the think-template organization. When a new template is added to the organization, you will be able to run thinkjs new <project-name> <template-name>
to use that template. You can also run thinkjs list
to see all available official templates.
Current available templates include:
- standard - A full-featured standard template
- api - API application template
- vue - ThinkJS Vue Template
- react - ThinkJS React Template
- typescript - ThinkJS typescript cli template
It's unlikely to make everyone happy with the official templates. You can simply fork an official template and then use it via think-cli
with:
$ thinkjs new my-project username/repo
Where username/repo
is the GitHub repo shorthand for your fork.
The shorthand repo notation is passed to download-git-repo so you can also use things like bitbucket:username/repo
for a Bitbucket repo and username/repo#branch
for tags or branches.
If you would like to download from a private repository use the —clone
flag and the cli will use git clone
so your SSH keys are used.
Instead of a GitHub repo, you can also use a template on your local file system:
$ thinkjs new my-project ~/fs/path/to-custom-template
Usage:
$ thinkjs controller <controller-name> [module-name]
Example:
$ thinkjs controller user home
The above command generates the controller at src/home/controller/user.js
- module-name is optional, defaults to the
thinkjs.defaultModule
in in the package.json file of project root directory, you can modify it, module-name only be used in multi-module projects
You can also add the -r
parameter to create a rest type controller
Example:
$ thinkjs controller user -r
Usage:
$ thinkjs service <service-name> [module-name]
Example:
$ thinkjs service user home
The above command generates the service at src/home/service/user.js
As with controller, module-name
is optional and can only be used in multi-module projects
Usage:
$ thinkjs model <model-name> [module-name]
Example:
$ thinkjs model user home
The above command generates the model at src/home/model/user.js
As with controller, module-name
is optional and can only be used in multi-module projects
Usage:
$ thinkjs middleware <middleware-name> [module-name]
Example:
$ thinkjs middleware user home
The above command generates the middleware at src/home/middleware/user.js
As with controller, module-name
is optional and can only be used in multi-module projects
Usage:
$ thinkjs adapter <adapter> [module-name]
Example: create a adapter with the name base type user
$ thinkjs adapter user/base home
The above command generates the adapter at src/home/adapter/user/base.js
adapter name is optional, defaults to the base
, Example:
$ thinkjs adapter user
The above command generates the adapter at src/adapter/user/base.js
As with controller, module-name
is optional and can only be used in multi-module projects
The command can only be used in multi-module projects
Usage:
$ thinkjs module [module-name]
Example:
$ thinkjs module user
The above command generates a module with the name user
As with controller, module-name
is optional,defaults to the thinkjs.defaultModule
in in the package.json file of project root directory.
Usage:
$ thinkjs module [module-name]
If your project was created with think-cli 1.0 and you want to use the capabilities of think-cli 2.0, you need to use migrate
command to migrate your project to think-cli 2.0.
Usage:
$ thinkjs sync
The command will synchronize the latest version of the project template to the local cache directory.
Usage:
$ thinkjs clean
The command will delete the project template cache.
After the template cache is deleted, your next create file command will pull the latest template
- A template repo must have a
template
directory that holds the template files. - A template repo must have a metadata file for the template which can be either a
metadata.js
ormetadata.json
file. It can contain the following fields:prompts
- used to collect user options data;skipCompile
- used to skip template compile, usually used for pictures and other resource files;completeMessage
- the message to be displayed to the user when the template has been generated. You can include custom instruction here.new
- new command mapping configurationcontroller
- controller command mapping configurationmodel
- model command mapping configurationservice
- service command mapping configurationmiddleware
- middleware command mapping configurationadapter
- adapter command mapping configurationmodule
- module command mapping configuration
- Template can use any parameter carried in the command line
The prompts field in the metadata file should be an object hash containing prompts for the user. For each entry, the key is the variable name and the value is an Inquirer.js question object. Example:
{
"prompts": {
"name": {
"type": "string",
"required": true,
"message": "Project name"
}
}
}
After all prompts are finished, all files inside template will be rendered using Ejs, with the prompt results as the data.
The project template is not always some code file, there are also some resource files, such as pictures, fonts, etc.
Only the code file needs to be compiled, because the code file may need to use syntax such as variables or conditional judgment, and pictures and other resource files are not needed, so we are use skipCompile field Skip these files.
The skipCompile
field in the metadata file should be a minimatch glob pattern. The files matched should skip rendering. Example:
{
"skipCompile": "src/**/*.png"
}
or
{
"skipCompile": [
"src/**/*.css",
"src/**/*.png"
]
}
The skipCompile
field in the metadata file, it can access the variables in the template, as well as all the syntax provided by EJS. Example:
{
"completeMessage": "To get started:\n\n<% if (!inPlace) { %># enter path\n$ cd <%= destDirName %>\n\n<% } %># install dependencies:\n$ npm install\n\n# run the app\n$ npm start"
}
The skipCompile
field in the metadata file should be an object hash containing map configuration required to generate a project from a template. It contain the following fields:
default
- Generate single module project mapping configurationmultiModule
- Generate multi-module project mapping configuration
mapping configuration can access these variables: action
,moduleName
, type
.
Example:
{
"new": {
"default": [
["src/bootstrap", "src/bootstrap"],
["src/config", "src/config"],
["src/controller/base.js", "src/controller/base.js"],
["src/controller/index.js", "src/controller/index.js"],
["src/logic", "src/logic"],
["src/model", "src/model"],
["test/index.js", "test/index.js"],
["view/index_index.html", "view/index_index.html"],
["development.js", "development.js"],
["eslintrc", ".eslintrc"],
["gitignore", ".gitignore"],
["nginx.conf", "nginx.conf"],
["package.json", "package.json"],
["pm2.json", "pm2.json"],
["production.js", "production.js"],
["README.md", "README.md"]
],
"multiModule": [
["src/bootstrap", "src/common/bootstrap"],
["src/config", "src/common/config"],
["src/config/config.js", "src/[moduleName]/config/config.js"],
["src/controller/base.js", "src/[moduleName]/controller/base.js"],
["src/controller/index.js", "src/[moduleName]/controller/index.js"],
["src/logic", "src/[moduleName]/logic"],
["src/model", "src/[moduleName]/model"],
["test/index.js", "test/index.js"],
["view/index_index.html", "view/[moduleName]/index_index.html"],
["development.js", "development.js"],
["eslintrc", ".eslintrc"],
["gitignore", ".gitignore"],
["nginx.conf", "nginx.conf"],
["package.json", "package.json"],
["pm2.json", "pm2.json"],
["production.js", "production.js"],
["README.md", "README.md"]
]
}
}
The skipCompile
field in the metadata file should be an object hash containing map configuration required to generate a controller from a template. It contain the following fields:
default
- Generate controller mapping configurationrest
- Generate rest controller mapping configuration
mapping configuration can access these variables: action
,moduleName
, type
.
Example:
{
"controller": {
"default": [
["src/controller/index.js", "src/[moduleName]/controller/[action].js"],
["src/logic/index.js", "src/[moduleName]/logic/[action].js"]
],
"rest": [
["src/controller/rest.js", "src/[moduleName]/controller/rest.js"],
["src/controller/restIndex.js", "src/[moduleName]/controller/[action].js"],
["src/logic/index.js", "src/[moduleName]/logic/[action].js"]
]
}
}
The skipCompile
field in the metadata file should be an array containing map configuration required to generate a model from a template.
mapping configuration can access these variables: action
,moduleName
, type
.
Example:
{
"model": [
["src/model/index.js", "src/[moduleName]/model/[action].js"]
]
}
The skipCompile
field in the metadata file should be an array containing map configuration required to generate a service from a template.
mapping configuration can access these variables: action
,moduleName
, type
.
Example:
{
"service": [
["src/service/index.js", "src/[moduleName]/service/[action].js"]
]
}
The skipCompile
field in the metadata file should be an array containing map configuration required to generate a middleware from a template.
mapping configuration can access these variables: action
,moduleName
, type
.
Example:
{
"middleware": [
["src/middleware/base.js", "src/[moduleName]/middleware/[action].js"]
]
}
The skipCompile
field in the metadata file should be an array containing map configuration required to generate a adapter from a template.
mapping configuration can access these variables: action
,moduleName
, type
.
Example:
{
"adapter": [
["src/adapter/base.js", "src/[moduleName]/adapter/[type]/[action].js"]
]
}
The skipCompile
field in the metadata file should be an array containing map configuration required to generate a module from a template.
mapping configuration can access these variables: action
,moduleName
, type
.
Example:
{
"module": [
["src/config/config.js", "src/[moduleName]/config/config.js"],
["src/controller/base.js", "src/[moduleName]/controller/base.js"],
["src/controller/index.js", "src/[moduleName]/controller/index.js"],
["src/logic/index.js", "src/[moduleName]/logic/index.js"],
["src/model/index.js", "src/[moduleName]/model/index.js"],
["view/index_index.html", "view/[moduleName]/index_index.html"]
]
}