Skip to Content
vovk new

vovk new

Quick CLI Ref
npx vovk-cli new --help Usage: vovk new|n [options] [components...] Create new components. "vovk new [...components] [segmentName/]moduleName" to create a new module or "vovk new segment [segmentName]" to create a new segment Options: -o, --overwrite overwrite existing files --static (new segment only) if the segment is static --template, --templates <templates...> (new module only) override config template; accepts an array of strings that correspond the order of the components --out, --out-dir <dirname> (new module only) override outDir in template file; relative to the root of the project --no-segment-update (new module only) do not update segment files when creating a new module --empty (new module only) create an empty module --dry-run do not write files to disk --log-level <level> set the log level -h, --help display help for command

The command vovk new allows to create new segments and modules (such as controllers, services, or custom modules) in the project. It uses moduleTemplates option defined at config and can be extended with custom templates. It includes two commands in one: vovk new segment and vovk new [module] [name].

vovk new segment

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, available as /api/foo/bar/baz/… endpoint. segmentName option equals to foo/bar/baz.

vovk new [module] [name]

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, where foo is a segment name and user is a module name, creates a module in the /src/modules/foo/user/ folder and updates foo segment. The segment name is optional and can be omitted if the module is related to the root segment:
npx vovk new controller service user

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 .

Paths to templates are defined at the config file with the moduleTemplates option.

vovk.config.mjs
/** @type {import('vovk-cli').VovkConfig} */ const config = { moduleTemplates: { controller: 'vovk-dto/module-templates/controller.ts.ejs', service: 'vovk-cli/module-templates/type/service.ts.ejs', state: './my-templates/state.ts.ejs', dto: './my-templates/dto.ts.ejs', }, }; 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 example templates for controllers and services located at module-templates folder at the NPM package. Every validation library has its own example template for controllers that’s also located at module-templates folder of corresponding NPM package.

vovk init initializes the Vovk.ts project with the corresponding templates defined at the config file.

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 🚧

Coming soon.

Last updated on