vovk-cli 0.0.1-draft.5 → 0.0.1-draft.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +29 -1
  2. package/client-templates/main/main.d.ts.ejs +15 -0
  3. package/client-templates/main/main.js.ejs +16 -0
  4. package/client-templates/module/module.d.ts.ejs +15 -0
  5. package/client-templates/module/module.js.ejs +20 -0
  6. package/client-templates/python/__init__.py +276 -0
  7. package/client-templates/ts/index.ts.ejs +25 -0
  8. package/dist/dev/diffSchema.d.mts +36 -0
  9. package/dist/{watcher → dev}/diffSchema.mjs +3 -11
  10. package/dist/dev/ensureClient.d.mts +5 -0
  11. package/dist/dev/ensureClient.mjs +30 -0
  12. package/dist/{watcher → dev}/ensureSchemaFiles.d.mts +3 -0
  13. package/dist/{watcher → dev}/ensureSchemaFiles.mjs +6 -4
  14. package/dist/dev/index.d.mts +6 -0
  15. package/dist/{watcher → dev}/index.mjs +128 -62
  16. package/dist/{watcher → dev}/isMetadataEmpty.mjs +1 -1
  17. package/dist/{watcher → dev}/logDiffResult.d.mts +2 -2
  18. package/dist/dev/logDiffResult.mjs +57 -0
  19. package/dist/{watcher → dev}/writeOneSchemaFile.d.mts +1 -1
  20. package/dist/{watcher → dev}/writeOneSchemaFile.mjs +3 -3
  21. package/dist/generate/getClientTemplates.d.mts +11 -0
  22. package/dist/generate/getClientTemplates.mjs +27 -0
  23. package/dist/generate/index.d.mts +12 -0
  24. package/dist/generate/index.mjs +79 -0
  25. package/dist/getProjectInfo/getConfig.mjs +5 -5
  26. package/dist/getProjectInfo/getConfigAbsolutePaths.mjs +2 -2
  27. package/dist/getProjectInfo/getRelativeSrcRoot.mjs +3 -3
  28. package/dist/getProjectInfo/getUserConfig.mjs +3 -1
  29. package/dist/getProjectInfo/importUncachedModule.mjs +0 -1
  30. package/dist/getProjectInfo/importUncachedModuleWorker.mjs +0 -1
  31. package/dist/getProjectInfo/index.d.mts +2 -1
  32. package/dist/getProjectInfo/index.mjs +14 -10
  33. package/dist/index.d.mts +1 -27
  34. package/dist/index.mjs +47 -60
  35. package/dist/init/checkTSConfigForExperimentalDecorators.mjs +2 -2
  36. package/dist/init/createConfig.d.mts +3 -4
  37. package/dist/init/createConfig.mjs +6 -8
  38. package/dist/init/getTemplateFilesFromPackage.d.mts +2 -1
  39. package/dist/init/getTemplateFilesFromPackage.mjs +4 -5
  40. package/dist/init/index.d.mts +1 -2
  41. package/dist/init/index.mjs +46 -93
  42. package/dist/init/installDependencies.d.mts +4 -1
  43. package/dist/init/installDependencies.mjs +2 -2
  44. package/dist/init/logUpdateDependenciesError.d.mts +11 -0
  45. package/dist/init/logUpdateDependenciesError.mjs +45 -0
  46. package/dist/init/updateDependenciesWithoutInstalling.d.mts +3 -2
  47. package/dist/init/updateDependenciesWithoutInstalling.mjs +13 -8
  48. package/dist/init/updateNPMScripts.d.mts +3 -1
  49. package/dist/init/updateNPMScripts.mjs +10 -6
  50. package/dist/init/updateTypeScriptConfig.mjs +2 -2
  51. package/dist/initProgram.d.mts +2 -0
  52. package/dist/initProgram.mjs +21 -0
  53. package/dist/locateSegments.d.mts +7 -1
  54. package/dist/locateSegments.mjs +9 -6
  55. package/dist/new/addClassToSegmentCode.d.mts +1 -2
  56. package/dist/new/addClassToSegmentCode.mjs +9 -5
  57. package/dist/new/addCommonTerms.mjs +1 -0
  58. package/dist/new/index.d.mts +2 -2
  59. package/dist/new/index.mjs +4 -4
  60. package/dist/new/newModule.d.mts +4 -4
  61. package/dist/new/newModule.mjs +45 -33
  62. package/dist/new/newSegment.mjs +6 -6
  63. package/dist/new/render.mjs +2 -5
  64. package/dist/postinstall.mjs +16 -17
  65. package/dist/types.d.mts +42 -9
  66. package/dist/utils/debounceWithArgs.d.mts +1 -1
  67. package/dist/utils/debounceWithArgs.mjs +24 -9
  68. package/dist/utils/formatLoggedSegmentName.mjs +1 -1
  69. package/dist/utils/getAvailablePort.mjs +3 -2
  70. package/dist/utils/getFileSystemEntryType.mjs +1 -1
  71. package/package.json +20 -17
  72. package/templates/controller.ejs +12 -11
  73. package/templates/service.ejs +6 -6
  74. package/dist/generateClient.d.mts +0 -7
  75. package/dist/generateClient.mjs +0 -97
  76. package/dist/watcher/diffSchema.d.mts +0 -43
  77. package/dist/watcher/index.d.mts +0 -6
  78. package/dist/watcher/logDiffResult.mjs +0 -90
  79. package/templates/worker.ejs +0 -1
  80. /package/dist/{watcher → dev}/isMetadataEmpty.d.mts +0 -0
@@ -0,0 +1,45 @@
1
+ import chalkHighlightThing from '../utils/chalkHighlightThing.mjs';
2
+ import { getPackageManager } from './installDependencies.mjs';
3
+ export default function logUpdateDependenciesError({ useNpm, useYarn, usePnpm, useBun, log, dependencies, devDependencies, error, }) {
4
+ const packageManager = getPackageManager({ useNpm, useYarn, usePnpm, useBun });
5
+ const installCommands = [];
6
+ if (dependencies.length > 0) {
7
+ let depInstallCmd = '';
8
+ switch (packageManager) {
9
+ case 'npm':
10
+ depInstallCmd = `npm install ${dependencies.join(' ')}`;
11
+ break;
12
+ case 'yarn':
13
+ depInstallCmd = `yarn add ${dependencies.join(' ')}`;
14
+ break;
15
+ case 'pnpm':
16
+ depInstallCmd = `pnpm add ${dependencies.join(' ')}`;
17
+ break;
18
+ case 'bun':
19
+ depInstallCmd = `bun add ${dependencies.join(' ')}`;
20
+ break;
21
+ }
22
+ installCommands.push(depInstallCmd);
23
+ }
24
+ if (devDependencies.length > 0) {
25
+ let devDepInstallCmd = '';
26
+ switch (packageManager) {
27
+ case 'npm':
28
+ devDepInstallCmd = `npm install -D ${devDependencies.join(' ')}`;
29
+ break;
30
+ case 'yarn':
31
+ devDepInstallCmd = `yarn add --dev ${devDependencies.join(' ')}`;
32
+ break;
33
+ case 'pnpm':
34
+ devDepInstallCmd = `pnpm add -D ${devDependencies.join(' ')}`;
35
+ break;
36
+ case 'bun':
37
+ devDepInstallCmd = `bun add -d ${devDependencies.join(' ')}`;
38
+ break;
39
+ }
40
+ installCommands.push(devDepInstallCmd);
41
+ }
42
+ const installCmd = installCommands.join(' && ');
43
+ // Log the error with the appropriate manual installation instructions
44
+ log.warn(`Failed to update dependencies: ${error.message}. Please, install them manually with ${chalkHighlightThing(installCmd)}`);
45
+ }
@@ -1,8 +1,9 @@
1
- import getLogger from '../utils/getLogger.mjs';
1
+ import type getLogger from '../utils/getLogger.mjs';
2
+ import { InitOptions } from '../types.mjs';
2
3
  export default function updateDependenciesWithoutInstalling({ log, dir, dependencyNames, devDependencyNames, channel, }: {
3
4
  log: ReturnType<typeof getLogger>;
4
5
  dir: string;
5
6
  dependencyNames: string[];
6
7
  devDependencyNames: string[];
7
- channel: 'latest' | 'beta' | 'dev';
8
+ channel: InitOptions['channel'];
8
9
  }): Promise<void>;
@@ -1,13 +1,12 @@
1
- import fs from 'fs/promises';
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import chalk from 'chalk';
2
4
  import getNPMPackageMetadata from '../utils/getNPMPackageMetadata.mjs';
3
- import path from 'path';
4
5
  async function updateDeps({ packageJson, packageNames, channel, key, }) {
5
6
  return Promise.all(packageNames.map(async (packageName) => {
6
- if (packageJson[key]?.[packageName])
7
- return; // Skip if already present
8
7
  const metadata = await getNPMPackageMetadata(packageName);
9
- const isVovk = packageName.startsWith('vovk');
10
- const latestVersion = metadata['dist-tags'][isVovk ? channel : 'latest'];
8
+ const isVovk = packageName.startsWith('vovk') && packageName !== 'vovk-mapped-types';
9
+ const latestVersion = metadata['dist-tags'][isVovk ? (channel ?? 'latest') : 'latest'];
11
10
  if (!packageJson[key]) {
12
11
  packageJson[key] = {};
13
12
  }
@@ -18,8 +17,14 @@ export default async function updateDependenciesWithoutInstalling({ log, dir, de
18
17
  const packageJsonPath = path.join(dir, 'package.json');
19
18
  const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8'));
20
19
  await updateDeps({ packageJson, packageNames: dependencyNames, channel, key: 'dependencies' });
21
- log.debug('Updated dependencies in package.json');
22
20
  await updateDeps({ packageJson, packageNames: devDependencyNames, channel, key: 'devDependencies' });
23
- log.debug('Updated devDependencies in package.json');
24
21
  await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
22
+ log.info('Added dependencies to package.json:');
23
+ for (const dependency of dependencyNames) {
24
+ log.raw.info(` - ${chalk.cyan(dependency)}`);
25
+ }
26
+ log.info('Added devDependencies to package.json:');
27
+ for (const dependency of devDependencyNames) {
28
+ log.raw.info(` - ${chalk.cyan(dependency)}`);
29
+ }
25
30
  }
@@ -1 +1,3 @@
1
- export default function updateNPMScripts(root: string, updateScriptsMode: 'implicit' | 'explicit'): Promise<void>;
1
+ import NPMCliPackageJson from '@npmcli/package-json';
2
+ export declare function getDevScript(pkgJson: NPMCliPackageJson, updateScriptsMode: 'implicit' | 'explicit'): string;
3
+ export default function updateNPMScripts(pkgJson: NPMCliPackageJson, root: string, updateScriptsMode: 'implicit' | 'explicit'): Promise<void>;
@@ -1,12 +1,16 @@
1
- import NPMCliPackageJson from '@npmcli/package-json';
2
- export default async function updateNPMScripts(root, updateScriptsMode) {
3
- const pkgJson = await NPMCliPackageJson.load(root);
1
+ export function getDevScript(pkgJson, updateScriptsMode) {
2
+ const nextDev = pkgJson.content.scripts?.dev ?? 'next dev';
3
+ const nextDevFlags = nextDev.replace('next dev', '').trim();
4
+ return updateScriptsMode === 'explicit'
5
+ ? `PORT=3000 concurrently '${nextDev}' 'vovk dev' --kill-others`
6
+ : `vovk dev --next-dev${nextDevFlags ? ` -- ${nextDevFlags}` : ''}`;
7
+ }
8
+ export default async function updateNPMScripts(pkgJson, root, updateScriptsMode) {
4
9
  pkgJson.update({
5
10
  scripts: {
11
+ ...pkgJson.content.scripts,
6
12
  generate: 'vovk generate',
7
- dev: updateScriptsMode === 'explicit'
8
- ? "PORT=3000 concurrently 'vovk dev' 'next dev' --kill-others"
9
- : 'vovk dev --next-dev',
13
+ dev: getDevScript(pkgJson, updateScriptsMode),
10
14
  },
11
15
  });
12
16
  await pkgJson.save();
@@ -1,5 +1,5 @@
1
- import path from 'path';
2
- import fs from 'fs/promises';
1
+ import path from 'node:path';
2
+ import fs from 'node:fs/promises';
3
3
  import * as jsonc from 'jsonc-parser';
4
4
  import prettify from '../utils/prettify.mjs';
5
5
  export default async function updateTypeScriptConfig(root) {
@@ -0,0 +1,2 @@
1
+ import { Command } from 'commander';
2
+ export default function initProgram(program: Command): Command;
@@ -0,0 +1,21 @@
1
+ import { Init } from './init/index.mjs';
2
+ // reused at vovk-init
3
+ export default function initProgram(program) {
4
+ return program
5
+ .argument('[prefix]', 'Directory to initialize project in', '.')
6
+ .description('Initialize Vovk project')
7
+ .option('-y, --yes', 'Skip all prompts and use default values')
8
+ .option('--log-level <level>', 'Set log level', 'info')
9
+ .option('--use-npm', 'Use npm as package manager')
10
+ .option('--use-yarn', 'Use yarn as package manager')
11
+ .option('--use-pnpm', 'Use pnpm as package manager')
12
+ .option('--use-bun', 'Use bun as package manager')
13
+ .option('--skip-install', 'Skip installing dependencies')
14
+ .option('--update-ts-config', 'Update tsconfig.json')
15
+ .option('--update-scripts <mode>', 'Update package.json scripts ("implicit" or "explicit")')
16
+ .option('--validation-library <library>', 'Validation library to use ("vovk-zod", "vovk-yup", "vovk-dto" or another). Set to "none" to skip validation')
17
+ .option('--validate-on-client', 'Path to validateOnClient file')
18
+ .option('--channel <channel>', 'Channel to use for fetching packages', 'latest')
19
+ .option('--dry-run', 'Do not write files to disk')
20
+ .action((prefix = '.', options) => new Init().main(prefix, options));
21
+ }
@@ -1,5 +1,11 @@
1
+ import type { VovkConfig } from './types.mjs';
1
2
  export type Segment = {
2
3
  routeFilePath: string;
3
4
  segmentName: string;
5
+ segmentImportPath: string;
4
6
  };
5
- export default function locateSegments(dir: string, rootDir?: string): Promise<Segment[]>;
7
+ export default function locateSegments({ dir, rootDir, config, }: {
8
+ dir: string;
9
+ rootDir?: string;
10
+ config: Required<VovkConfig> | null;
11
+ }): Promise<Segment[]>;
@@ -1,8 +1,10 @@
1
- import { promises as fs } from 'fs';
2
- import * as path from 'path';
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
3
  import getFileSystemEntryType from './utils/getFileSystemEntryType.mjs';
4
- export default async function locateSegments(dir, rootDir = dir) {
4
+ // config: null is used for testing
5
+ export default async function locateSegments({ dir, rootDir, config, }) {
5
6
  let results = [];
7
+ rootDir = rootDir ?? dir;
6
8
  // Read the contents of the directory
7
9
  const list = await fs.readdir(dir);
8
10
  // Iterate through each item in the directory
@@ -16,12 +18,13 @@ export default async function locateSegments(dir, rootDir = dir) {
16
18
  const routeFilePath = path.join(filePath, 'route.ts');
17
19
  if (await getFileSystemEntryType(routeFilePath)) {
18
20
  // Calculate the basePath relative to the root directory
19
- const segmentName = path.relative(rootDir, dir);
20
- results.push({ routeFilePath, segmentName });
21
+ const segmentName = path.relative(rootDir, dir).replace(/\\/g, '/'); // windows fix
22
+ const segmentImportPath = path.relative(config?.clientOutDir ?? '.__', routeFilePath);
23
+ results.push({ routeFilePath, segmentName, segmentImportPath });
21
24
  }
22
25
  }
23
26
  // Recursively search inside subdirectories
24
- const subDirResults = await locateSegments(filePath, rootDir);
27
+ const subDirResults = await locateSegments({ dir: filePath, rootDir, config });
25
28
  results = results.concat(subDirResults);
26
29
  }
27
30
  }
@@ -1,6 +1,5 @@
1
- export default function addClassToSegmentCode(segmentSourceCode: string, { sourceName, compiledName, type, importPath, }: {
1
+ export default function addClassToSegmentCode(segmentSourceCode: string, { sourceName, compiledName, importPath, }: {
2
2
  sourceName: string;
3
3
  compiledName: string;
4
- type: 'worker' | 'controller';
5
4
  importPath: string;
6
5
  }): string;
@@ -1,6 +1,10 @@
1
- import { Project, SyntaxKind } from 'ts-morph';
2
- export default function addClassToSegmentCode(segmentSourceCode, { sourceName, compiledName, type, importPath, }) {
3
- const project = new Project();
1
+ import { Project, QuoteKind, SyntaxKind } from 'ts-morph';
2
+ export default function addClassToSegmentCode(segmentSourceCode, { sourceName, compiledName, importPath, }) {
3
+ const project = new Project({
4
+ manipulationSettings: {
5
+ quoteKind: QuoteKind.Single,
6
+ },
7
+ });
4
8
  const sourceFile = project.createSourceFile('route.ts', segmentSourceCode, { overwrite: true });
5
9
  // Add the import if it doesn't exist
6
10
  let importDeclaration = sourceFile.getImportDeclaration((imp) => {
@@ -12,8 +16,8 @@ export default function addClassToSegmentCode(segmentSourceCode, { sourceName, c
12
16
  moduleSpecifier: importPath,
13
17
  });
14
18
  }
15
- // Get the variable declaration for controllers or workers
16
- const variableDeclaration = sourceFile.getVariableDeclaration(`${type}s`);
19
+ // Get the variable declaration for controllers
20
+ const variableDeclaration = sourceFile.getVariableDeclaration('controllers');
17
21
  if (variableDeclaration) {
18
22
  const initializer = variableDeclaration.getInitializer();
19
23
  if (initializer && initializer.getKind() === SyntaxKind.ObjectLiteralExpression) {
@@ -2,6 +2,7 @@ import pluralize from 'pluralize';
2
2
  // feel free to open a direct PR if you have more common terms to add
3
3
  const terms = [
4
4
  ['entity', 'entities'],
5
+ ['entry', 'entries'],
5
6
  ['regex', 'regexes'],
6
7
  ['index', 'indices'],
7
8
  ['matrix', 'matrices'],
@@ -1,2 +1,2 @@
1
- import type { NewOptions } from '../index.mjs';
2
- export default function newComponents(components: string[], { dryRun, fileName, dirName, template, overwrite }: NewOptions): Promise<void>;
1
+ import type { NewOptions } from '../types.mjs';
2
+ export default function newComponents(components: string[], { dryRun, dir, templates, overwrite, noSegmentUpdate }: NewOptions): Promise<void>;
@@ -1,6 +1,6 @@
1
1
  import newModule from './newModule.mjs';
2
2
  import newSegment from './newSegment.mjs';
3
- export default async function newComponents(components, { dryRun, fileName, dirName, template, overwrite }) {
3
+ export default async function newComponents(components, { dryRun, dir, templates, overwrite, noSegmentUpdate }) {
4
4
  if (components[0] === 'segment' || components[0] === 'segments') {
5
5
  // vovk new segment [segmentName]
6
6
  let segmentNames = components
@@ -26,10 +26,10 @@ export default async function newComponents(components, { dryRun, fileName, dirN
26
26
  await newModule({
27
27
  what,
28
28
  moduleNameWithOptionalSegment,
29
- fileName,
30
- dirName,
31
- template,
29
+ dir,
30
+ templates,
32
31
  overwrite,
32
+ noSegmentUpdate,
33
33
  dryRun,
34
34
  });
35
35
  }
@@ -1,9 +1,9 @@
1
- export default function newModule({ what, moduleNameWithOptionalSegment, dryRun, fileName: fileNameFlag, dirName: dirNameFlag, template: templateFlag, overwrite, }: {
1
+ export default function newModule({ what, moduleNameWithOptionalSegment, dryRun, dir: dirFlag, templates: templatesFlag, noSegmentUpdate, overwrite, }: {
2
2
  what: string[];
3
3
  moduleNameWithOptionalSegment: string;
4
4
  dryRun?: boolean;
5
- fileName?: string;
6
- dirName?: string;
7
- template?: string;
5
+ dir?: string;
6
+ templates?: string[];
7
+ noSegmentUpdate?: boolean;
8
8
  overwrite?: boolean;
9
9
  }): Promise<void>;
@@ -1,11 +1,11 @@
1
- import path from 'path';
2
- import fs from 'fs/promises';
3
- import getProjectInfo from '../getProjectInfo/index.mjs';
1
+ import path from 'node:path';
2
+ import fs from 'node:fs/promises';
4
3
  import render from './render.mjs';
4
+ import addClassToSegmentCode from './addClassToSegmentCode.mjs';
5
+ import getProjectInfo from '../getProjectInfo/index.mjs';
6
+ import locateSegments from '../locateSegments.mjs';
5
7
  import chalkHighlightThing from '../utils/chalkHighlightThing.mjs';
6
8
  import formatLoggedSegmentName from '../utils/formatLoggedSegmentName.mjs';
7
- import locateSegments from '../locateSegments.mjs';
8
- import addClassToSegmentCode from './addClassToSegmentCode.mjs';
9
9
  import getFileSystemEntryType from '../utils/getFileSystemEntryType.mjs';
10
10
  import prettify from '../utils/prettify.mjs';
11
11
  function splitByLast(str, delimiter = '/') {
@@ -18,50 +18,61 @@ function splitByLast(str, delimiter = '/') {
18
18
  const after = str.substring(index + delimiter.length);
19
19
  return [before, after];
20
20
  }
21
- export default async function newModule({ what, moduleNameWithOptionalSegment, dryRun, fileName: fileNameFlag, dirName: dirNameFlag, template: templateFlag, overwrite, }) {
21
+ export default async function newModule({ what, moduleNameWithOptionalSegment, dryRun, dir: dirFlag, templates: templatesFlag, noSegmentUpdate, overwrite, }) {
22
22
  const { config, log, apiDir, cwd } = await getProjectInfo();
23
- const templates = config.templates;
23
+ let templates = config.templates;
24
24
  const [segmentName, moduleName] = splitByLast(moduleNameWithOptionalSegment);
25
- // replace c by controller, s by service, w by worker, everything else keeps the same
25
+ // replace c by controller, s by service, everything else keeps the same
26
26
  what = what.map((s) => {
27
27
  switch (s) {
28
28
  case 'c':
29
29
  return 'controller';
30
30
  case 's':
31
31
  return 'service';
32
- case 'w':
33
- return 'worker';
34
32
  default:
35
33
  return s;
36
34
  }
37
35
  });
38
- // check if template exists
36
+ if (templatesFlag) {
37
+ if (templatesFlag.length > what.length) {
38
+ throw new Error('Too many templates provided');
39
+ }
40
+ templates = templatesFlag.reduce((acc, templatePath, index) => ({
41
+ ...acc,
42
+ [what[index]]: templatePath,
43
+ }), templates);
44
+ }
39
45
  for (const type of what) {
40
46
  if (!templates[type]) {
41
- throw new Error(`Template for ${type} not found in config`);
47
+ throw new Error(`Template for "${type}" not found`);
42
48
  }
43
49
  }
44
- const segments = await locateSegments(apiDir);
50
+ const segments = await locateSegments({ dir: apiDir, config });
45
51
  const segment = segments.find((s) => s.segmentName === segmentName);
46
52
  if (!segment) {
47
- throw new Error(`Segment ${segmentName} not found`);
53
+ throw new Error(`Unable to create module. Segment "${segmentName}" not found. Run "vovk new segment ${segmentName}" to create it`);
48
54
  }
49
55
  for (const type of what) {
50
- const templatePath = templateFlag ?? templates[type];
56
+ const templatePath = templates[type];
51
57
  const templateAbsolutePath = templatePath.startsWith('/') || templatePath.startsWith('.')
52
58
  ? path.resolve(cwd, templatePath)
53
59
  : path.resolve(cwd, './node_modules', templatePath);
54
60
  const templateCode = await fs.readFile(templateAbsolutePath, 'utf-8');
55
- const { dirName: renderedDirName, fileName: renderedFileName, sourceName, compiledName, code, } = await render(templateCode, {
61
+ const { dir: renderedDir, fileName, sourceName, compiledName, code, } = await render(templateCode, {
56
62
  cwd,
57
63
  config,
58
64
  withService: what.includes('service'),
59
65
  segmentName,
60
66
  moduleName,
61
67
  });
62
- const fileName = fileNameFlag || renderedFileName;
63
- const dirName = dirNameFlag || renderedDirName;
64
- const absoluteModuleDir = path.join(cwd, config.modulesDir, dirName);
68
+ const dir = dirFlag || renderedDir;
69
+ if (!dir) {
70
+ throw new Error(`The template for "${type}" does not provide a dir`);
71
+ }
72
+ if (!fileName) {
73
+ throw new Error(`The template for "${type}" does not provide a fileName`);
74
+ }
75
+ const absoluteModuleDir = path.join(cwd, dir);
65
76
  const absoluteModulePath = path.join(absoluteModuleDir, fileName);
66
77
  const prettiedCode = await prettify(code, absoluteModulePath);
67
78
  if (!dryRun) {
@@ -71,29 +82,30 @@ export default async function newModule({ what, moduleNameWithOptionalSegment, d
71
82
  else {
72
83
  await fs.mkdir(absoluteModuleDir, { recursive: true });
73
84
  await fs.writeFile(absoluteModulePath, prettiedCode);
85
+ log.info(`Created ${chalkHighlightThing(fileName)} using ${chalkHighlightThing(`"${type}"`)} template for ${formatLoggedSegmentName(segmentName)}`);
74
86
  }
75
87
  }
76
- if (type === 'controller' || type === 'worker') {
88
+ if (type === 'controller') {
77
89
  if (!sourceName) {
78
- throw new Error('sourceName is required');
90
+ throw new Error(`The template for "${type}" does not provide a sourceName`);
79
91
  }
80
92
  if (!compiledName) {
81
- throw new Error('compiledName is required');
93
+ throw new Error(`The template for "${type}" does not provide a compiledName`);
82
94
  }
83
95
  const { routeFilePath } = segment;
84
96
  const segmentSourceCode = await fs.readFile(routeFilePath, 'utf-8');
85
- const importPath = path.relative(absoluteModuleDir, absoluteModulePath).replace(/\.(ts|tsx)$/, '');
86
- const newSegmentCode = addClassToSegmentCode(segmentSourceCode, {
87
- sourceName,
88
- compiledName,
89
- type,
90
- importPath,
91
- });
92
- if (!dryRun) {
93
- await fs.writeFile(routeFilePath, newSegmentCode);
97
+ const importPath = path.relative(path.dirname(routeFilePath), absoluteModulePath).replace(/\.(ts|tsx)$/, '');
98
+ if (!noSegmentUpdate) {
99
+ const newSegmentCode = await prettify(addClassToSegmentCode(segmentSourceCode, {
100
+ sourceName,
101
+ compiledName,
102
+ importPath,
103
+ }), routeFilePath);
104
+ if (!dryRun) {
105
+ await fs.writeFile(routeFilePath, newSegmentCode);
106
+ }
94
107
  }
95
- log.info(`Added ${chalkHighlightThing(sourceName)} ${type} to ${formatLoggedSegmentName(segmentName)} as ${chalkHighlightThing(compiledName)}`);
108
+ log.info(`Added ${chalkHighlightThing(sourceName)} ${type} as ${chalkHighlightThing(compiledName)} to ${formatLoggedSegmentName(segmentName)}`);
96
109
  }
97
- log.info(`Created ${chalkHighlightThing(fileName)} using "${chalkHighlightThing(type)}" template for ${formatLoggedSegmentName(segmentName)}`);
98
110
  }
99
111
  }
@@ -1,5 +1,5 @@
1
- import path from 'path';
2
- import fs from 'fs/promises';
1
+ import path from 'node:path';
2
+ import fs from 'node:fs/promises';
3
3
  import getProjectInfo from '../getProjectInfo/index.mjs';
4
4
  import getFileSystemEntryType from '../utils/getFileSystemEntryType.mjs';
5
5
  import chalkHighlightThing from '../utils/chalkHighlightThing.mjs';
@@ -13,15 +13,14 @@ export default async function newSegment({ segmentName, overwrite, dryRun, }) {
13
13
  }
14
14
  const code = await prettify(`import { initVovk } from 'vovk';
15
15
 
16
+ export const runtime = 'edge';
17
+
16
18
  const controllers = {};
17
- const workers = {};
18
19
 
19
20
  export type Controllers = typeof controllers;
20
- export type Workers = typeof workers;
21
21
 
22
22
  export const { GET, POST, PATCH, PUT, HEAD, OPTIONS, DELETE } = initVovk({
23
23
  ${segmentName ? ` segmentName: '${segmentName}',\n` : ''} emitSchema: true,
24
- workers,
25
24
  controllers,
26
25
  });
27
26
  `, absoluteSegmentRoutePath);
@@ -29,5 +28,6 @@ ${segmentName ? ` segmentName: '${segmentName}',\n` : ''} emitSchema: true,
29
28
  await fs.mkdir(path.dirname(absoluteSegmentRoutePath), { recursive: true });
30
29
  await fs.writeFile(absoluteSegmentRoutePath, code);
31
30
  }
32
- log.info(`${formatLoggedSegmentName(segmentName, { upperFirst: true })} created at ${absoluteSegmentRoutePath}. Run ${chalkHighlightThing(`vovk new controller ${segmentName}/someName`)} to create a controller or modify the segment file manually`);
31
+ log.info(`${formatLoggedSegmentName(segmentName, { upperFirst: true })} created at ${absoluteSegmentRoutePath}.`);
32
+ log.info(`Run ${chalkHighlightThing(`npx vovk new service controller ${[segmentName, 'thing'].filter(Boolean).join('/')}`)} to create a new controller with a service at /modules/thing/ folder.`);
33
33
  }
@@ -20,14 +20,11 @@ export default async function render(codeTemplate, { config, withService, segmen
20
20
  _, // lodash
21
21
  pluralize,
22
22
  };
23
- // first, render the front matter because it can use ejs variables
24
23
  const parsed = matter((await ejs.render(codeTemplate, templateVars, { async: true })).trim());
25
- const { dirName, fileName, sourceName, compiledName } = parsed.data;
24
+ const { dir, fileName, sourceName, compiledName } = parsed.data;
26
25
  const code = parsed.content;
27
- // const templateContent = parsed.content; TODO
28
- // const code = await ejs.render(templateContent, templateVars, { async: true });
29
26
  return {
30
- dirName,
27
+ dir,
31
28
  fileName,
32
29
  sourceName,
33
30
  compiledName,
@@ -1,22 +1,21 @@
1
- import { promises as fs } from 'fs';
2
- import path from 'path';
3
- /**
4
- * Checks if a file exists at the given path.
5
- * @param {string} filePath - The path to the file.
6
- * @returns {Promise<boolean>} - A promise that resolves to true if the file exists, false otherwise.
7
- */
8
- const getFileSystemEntryType = async (filePath) => !!(await fs.stat(filePath).catch(() => false));
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import getFileSystemEntryType from './utils/getFileSystemEntryType.mjs';
9
4
  async function postinstall() {
10
- const vovk = path.join(import.meta.dirname, '../../.vovk');
11
- const js = path.join(vovk, 'client.js');
12
- const ts = path.join(vovk, 'client.d.ts');
5
+ // TODO: The function doesn't consider client templates, how to do that?
6
+ const vovk = path.join(import.meta.dirname, '../../.vovk-client');
7
+ const js = path.join(vovk, 'compiled.js');
8
+ const ts = path.join(vovk, 'compiled.d.ts');
13
9
  const index = path.join(vovk, 'index.ts');
14
- if ((await getFileSystemEntryType(js)) || (await getFileSystemEntryType(ts)) || (await getFileSystemEntryType(index))) {
15
- return;
16
- }
17
10
  await fs.mkdir(vovk, { recursive: true });
18
- await fs.writeFile(js, '/* postinstall */');
19
- await fs.writeFile(ts, '/* postinstall */');
20
- await fs.writeFile(index, '/* postinstall */');
11
+ if (!(await getFileSystemEntryType(js))) {
12
+ await fs.writeFile(js, '/* postinstall */');
13
+ }
14
+ if (!(await getFileSystemEntryType(ts))) {
15
+ await fs.writeFile(ts, '/* postinstall */');
16
+ }
17
+ if (!(await getFileSystemEntryType(index))) {
18
+ await fs.writeFile(index, '/* postinstall */');
19
+ }
21
20
  }
22
21
  void postinstall();
package/dist/types.d.mts CHANGED
@@ -1,13 +1,13 @@
1
- import { LogLevelNames } from 'loglevel';
1
+ import type { LogLevelNames } from 'loglevel';
2
2
  export type KnownAny = any;
3
3
  export type VovkEnv = {
4
4
  PORT?: string;
5
5
  VOVK_CLIENT_OUT_DIR?: string;
6
6
  VOVK_SCHEMA_OUT_DIR?: string;
7
- VOVK_FETCHER?: string;
8
- VOVK_VALIDATE_ON_CLIENT?: string;
7
+ VOVK_FETCHER_PATH?: string;
8
+ VOVK_VALIDATE_ON_CLIENT_PATH?: string;
9
+ VOVK_CREATE_RPC_PATH?: string;
9
10
  VOVK_MODULES_DIR?: string;
10
- VOVK_VALIDATION_LIBRARY?: string;
11
11
  VOVK_ORIGIN?: string;
12
12
  VOVK_ROOT_ENTRY?: string;
13
13
  VOVK_API_ENTRY_POINT?: string;
@@ -16,31 +16,64 @@ export type VovkEnv = {
16
16
  VOVK_PRETTIFY_CLIENT?: string;
17
17
  VOVK_DEV_HTTPS?: string;
18
18
  __VOVK_START_WATCHER_IN_STANDALONE_MODE__?: 'true';
19
+ __VOVK_EXIT__?: 'true' | 'false';
19
20
  };
20
21
  export type VovkConfig = {
21
22
  clientOutDir?: string;
22
23
  schemaOutDir?: string;
23
- fetcher?: string;
24
- validateOnClient?: string | null;
24
+ fetcherPath?: string;
25
+ validateOnClientPath?: string | null;
26
+ createRPCPath?: string;
25
27
  modulesDir?: string;
26
- validationLibrary?: string | null;
27
28
  rootEntry?: string;
28
29
  origin?: string;
29
30
  rootSegmentModulesDirName?: string;
30
31
  logLevel?: LogLevelNames;
31
32
  prettifyClient?: boolean;
32
33
  devHttps?: boolean;
34
+ experimental_clientGenerateTemplateNames?: string[];
33
35
  templates?: {
34
36
  service?: string;
35
37
  controller?: string;
36
- worker?: string;
37
38
  [key: string]: string | undefined;
38
39
  };
39
40
  };
40
41
  export type VovkModuleRenderResult = {
41
42
  fileName: string;
42
- dirName: string;
43
+ dir: string;
43
44
  sourceName?: string;
44
45
  compiledName?: string;
45
46
  code: string;
46
47
  };
48
+ export interface DevOptions {
49
+ nextDev?: boolean;
50
+ exit?: boolean;
51
+ }
52
+ export interface GenerateOptions {
53
+ clientOutDir?: string;
54
+ templates?: string[];
55
+ prettify?: boolean;
56
+ fullSchema?: string | boolean;
57
+ }
58
+ export interface InitOptions {
59
+ yes?: boolean;
60
+ logLevel: LogLevelNames;
61
+ useNpm?: boolean;
62
+ useYarn?: boolean;
63
+ usePnpm?: boolean;
64
+ useBun?: boolean;
65
+ skipInstall?: boolean;
66
+ updateTsConfig?: boolean;
67
+ updateScripts?: 'implicit' | 'explicit';
68
+ validationLibrary?: string | null;
69
+ validateOnClient?: boolean;
70
+ dryRun?: boolean;
71
+ channel?: 'latest' | 'beta' | 'draft';
72
+ }
73
+ export interface NewOptions {
74
+ dryRun?: boolean;
75
+ templates?: string[];
76
+ dir?: string;
77
+ overwrite?: boolean;
78
+ noSegmentUpdate?: boolean;
79
+ }
@@ -1,2 +1,2 @@
1
1
  import { KnownAny } from '../types.mjs';
2
- export default function debounceWithArgs<T extends (...args: KnownAny[]) => KnownAny>(fn: T, wait: number): (...args: Parameters<T>) => void | Promise<void>;
2
+ export default function debounceWithArgs<Callback extends (...args: KnownAny[]) => KnownAny>(callback: Callback, wait: number): (...args: Parameters<Callback>) => Promise<Awaited<ReturnType<Callback>>>;