TODO: Flags here
vovk new
The command vovk new
(vovk n
) allows to create new segments and modules in the project. It uses templates
option defined at config and can be extended with custom templates. It includes two commands in one: vovk new segment
and vovk new [module]
.
vovk new segment
Root segment
npx vovk new segment
If vovk new segment
is run without the segment argument, it creates a root segment represented as prettifiedΒ /src/app/api/[[β¦vovk]]/route.ts file. segmentName
option of initSegment
equals to an empty string and can be omitted.
import { initSegment } from 'vovk';
const controllers = {};
export type Controllers = typeof controllers;
export const { GET, POST, PATCH, PUT, HEAD, OPTIONS, DELETE } = initSegment({
emitSchema: true,
controllers,
});
The segment is going to produce a new schema when the vovk dev
command is run located at .vovk-schema/root.json.
The segment produces an API thatβs going to be available at /api/β¦ endpoint.
Root segment can be used together with other segments of any depth.
Nested segment
npx vovk new segment foo
npx vovk new segment foo
creates a new prettifiedΒ route.ts file in the /src/app/api/foo/[[β¦vovk]]/ directory. The file contains a new segment with the following content:
// ...
export const { GET, POST, PATCH, PUT, HEAD, OPTIONS, DELETE } = initSegment({
segmentName: 'foo',
emitSchema: true,
controllers,
});
The segment is going to produce a new schema when the vovk dev
command is run located at .vovk-schema/foo.json.
The segment produces an API thatβs going to be available at /api/foo/β¦ endpoint.
If vovk new segment foo/bar/baz
is run, it creates a nested segment represented as /src/app/api/foo/bar/baz/[[β¦vovk]]/route.ts file. segmentName
option equals to foo/bar/baz
.
The segment is going to produce a new schema when the vovk dev
command is run located at .vovk-schema/foo/bar/baz.json.
The segment produces an API thatβs going to be available at /api/foo/bar/baz/β¦ endpoint.
vovk new [module]
npx vovk new controller service foo/user
vovk new [anything but segment]
is used to create new prettifiedΒ modules at /src/modules folder.
The command has the following structure:
npx vovk new
- the command itself.- Components - types of modules to create (
controller
,service
, or a custom module). - Module name (singular) with an optional segment name separated by a slash.
foo/user
, wherefoo
is a segment name anduser
is a module name, creates a module in the /src/modules/foo/user/ folder and updatesfoo
segment. The segment name is optional and can be omitted if the module is related to the root segment:user
creates a module in the /src/modules/user/ folder and updates the root segment.
Paths to templates are defined at the config file with the templates
option.
/** @type {import('vovk-cli').VovkConfig} */
const config = {
validationLibrary: 'vovk-dto',
validateOnClient: 'vovk-dto/validateOnClient',
templates: {
controller: 'vovk-dto/templates/controller.ejs',
service: 'vovk-cli/templates/service.ejs',
state: './templates/state.ejs',
dto: './templates/dto.ejs',
},
logLevel: 'info',
};
export default config;
npx vovk new controller state dto user
creates UserController.ts, UserService.ts, and UserDto.ts files at /src/modules/user folder and will update the root segment.
Built-in templates
vovk-cli provides built-in templates for controllers and services located at templates folder at the NPM package. Every validation library has its own template for controllers thatβs also located at templates folder of corresponding NPM package.
vovk init initializes the Vovk.ts project with the corresponding templates defined at the config file.
When a new controller is created with vovk new
command, the script updates the list of controllers
at the segment file and updates route.ts
file with the new controller using ASTΒ .
Shortcuts
Controller and service can be created with shortcuts:
npx vovk n c s user
This command is an equivalent of:
npx vovk new controller service user
Custom templates
Module templates are implemented with EJSΒ and YAML that allows to define dir
and fileName
of the module.
dir
- the directory where the module is going to be created it can usegetModuleDirName
function that simplifies the process of defining the directory, or it can be defined differently for purposes outside of Vovk.ts framework.
<% var modulePascalName = _.upperFirst(_.camelCase(moduleName)); %>
<% var stateName = modulePascalName + 'State'; %>
<% var rpcName = modulePascalName + 'RPC'; %>
<% var modelTypeName = modulePascalName + 'ModelType'; %>
---
dir: <%= getModuleDirName(segmentName, moduleName) %>
fileName: <%= stateName + '.ts' %>
---
import { <%= rpcName %> } from 'vovk-client';
import { <%= modelTypeName %> } from '@/types';
export default class <%= stateName %> {
static async get<%= modulePascalName %>s() {
return <%= rpcName %>.get<%= modulePascalName %>s();
}
}