vovk 0.2.3-beta.113 → 0.2.3-beta.114

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.
@@ -1,15 +1,15 @@
1
1
  // @ts-check
2
- const fs = require('fs/promises');
3
- const path = require('path');
4
- const getReturnPath = require('./lib/getReturnPath');
2
+ import fs from 'fs/promises';
3
+ import path from 'path';
4
+ import getReturnPath from './lib/getReturnPath.mjs';
5
5
 
6
- /** @type {(moduleName: string) => boolean} */
7
- function canRequire(moduleName) {
6
+ /** @type {(moduleName: string) => Promise<boolean>} */
7
+ async function canImport(moduleName) {
8
8
  try {
9
- require.resolve(moduleName);
10
- return true; // The module exists and can be required
9
+ await import(moduleName);
10
+ return true; // The module exists and can be imported
11
11
  } catch (e) {
12
- return false; // The module does not exist
12
+ return false; // The module does not exist or cannot be imported
13
13
  }
14
14
  }
15
15
 
@@ -18,7 +18,7 @@ function canRequire(moduleName) {
18
18
  * TODO: Check fetcher for existence
19
19
  * @type {(rcPath: import('../src').VovkEnv) => Promise<{ written: boolean; path: string }>}
20
20
  */
21
- async function generateClient({ ...env }) {
21
+ export default async function generateClient({ ...env }) {
22
22
  const outDir = env.VOVK_CLIENT_OUT;
23
23
  const returnDir = getReturnPath(outDir, process.cwd());
24
24
  const jsonPath = path.join(returnDir, '.vovk.json');
@@ -26,20 +26,22 @@ async function generateClient({ ...env }) {
26
26
  const fetcherPath = env.VOVK_FETCHER.startsWith('.') ? path.join(returnDir, env.VOVK_FETCHER) : env.VOVK_FETCHER;
27
27
 
28
28
  if (!env.VOVK_VALIDATE_ON_CLIENT) {
29
- env.VOVK_VALIDATE_ON_CLIENT = canRequire('vovk-zod/zodValidateOnClient') ? 'vovk-zod/zodValidateOnClient' : '';
29
+ env.VOVK_VALIDATE_ON_CLIENT = (await canImport('vovk-zod/zodValidateOnClient'))
30
+ ? 'vovk-zod/zodValidateOnClient'
31
+ : '';
30
32
  }
31
33
  const validatePath = env.VOVK_VALIDATE_ON_CLIENT.startsWith('.')
32
34
  ? path.join(returnDir, env.VOVK_VALIDATE_ON_CLIENT)
33
35
  : env.VOVK_VALIDATE_ON_CLIENT;
34
36
  const localValidatePath = env.VOVK_VALIDATE_ON_CLIENT.startsWith('.') ? path.join('..', validatePath) : validatePath;
35
37
 
36
- if (env.VOVK_VALIDATE_ON_CLIENT && !canRequire(localValidatePath)) {
38
+ if (env.VOVK_VALIDATE_ON_CLIENT && !(await canImport(localValidatePath))) {
37
39
  throw new Error(
38
40
  `Unble to generate Vovk Client: cannot find "validateOnClient" module '${env.VOVK_VALIDATE_ON_CLIENT}'. Check your vovk.config.js file`
39
41
  );
40
42
  }
41
43
 
42
- if (!canRequire(localJsonPath)) {
44
+ if (!(await canImport(localJsonPath))) {
43
45
  throw new Error(`Unble to generate Vovk Client: cannot find ".vovk.json" file '${jsonPath}'.`);
44
46
  }
45
47
 
@@ -116,5 +118,3 @@ type Options = typeof fetcher extends VovkClientFetcher<infer U> ? U : never;
116
118
 
117
119
  return { written: true, path: outDir };
118
120
  }
119
-
120
- module.exports = generateClient;
@@ -1,11 +1,11 @@
1
1
  // @ts-check
2
2
 
3
- const path = require('path');
3
+ import path from 'path';
4
4
 
5
5
  /** @type {import('../src').VovkEnv} */
6
6
  let vars;
7
- /** @type {(rcPath: string, options?: { VOVK_CLIENT_OUT?: string; PORT?: string; }) => import('../src').VovkEnv} */
8
- function getVars(configPath, options = {}) {
7
+ /** @type {(rcPath: string, options?: { VOVK_CLIENT_OUT?: string; PORT?: string; }) => Promise<import('../src').VovkEnv>} */
8
+ export default async function getVars(configPath, options = {}) {
9
9
  if (vars) return vars;
10
10
  /** @type {Required<import('../src').VovkConfig>} */
11
11
  const vovkConfig = {
@@ -19,7 +19,7 @@ function getVars(configPath, options = {}) {
19
19
  try {
20
20
  // make PORT available to the config file
21
21
  process.env.PORT = options.PORT || process.env.PORT || '3000';
22
- Object.assign(vovkConfig, require(configPath));
22
+ Object.assign(vovkConfig, await import(configPath));
23
23
  } catch {
24
24
  // noop
25
25
  }
@@ -43,5 +43,3 @@ function getVars(configPath, options = {}) {
43
43
 
44
44
  return vars;
45
45
  }
46
-
47
- module.exports = getVars;
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  // @ts-check
3
- const generateClient = require('./generateClient');
4
- const path = require('path');
5
- const parallel = require('./lib/parallel');
6
- const getAvailablePort = require('./lib/getAvailablePort');
7
- const getVars = require('./getVars');
8
- const parseCommandLineArgs = require('./lib/parseCommandLineArgs');
3
+ import path from 'path';
4
+ import generateClient from './generateClient.mjs';
5
+ import parallel from './lib/parallel.mjs';
6
+ import getAvailablePort from './lib/getAvailablePort.mjs';
7
+ import getVars from './getVars.mjs';
8
+ import parseCommandLineArgs from './lib/parseCommandLineArgs.mjs';
9
9
 
10
10
  const { command, flags, restArgs } = parseCommandLineArgs();
11
11
  const {
@@ -23,7 +23,7 @@ if (command === 'dev') {
23
23
  throw new Error(' 🐺 Failed to find available Next port');
24
24
  }));
25
25
 
26
- const env = getVars(config, { VOVK_CLIENT_OUT: clientOut, PORT });
26
+ const env = await getVars(config, { VOVK_CLIENT_OUT: clientOut, PORT });
27
27
 
28
28
  let VOVK_PORT = parseInt(env.VOVK_PORT);
29
29
 
@@ -44,11 +44,13 @@ if (command === 'dev') {
44
44
  console.info(' 🐺 All processes have ended');
45
45
  })();
46
46
  } else if (command === 'generate') {
47
- const env = getVars(config, { VOVK_CLIENT_OUT: clientOut });
47
+ void (async () => {
48
+ const env = await getVars(config, { VOVK_CLIENT_OUT: clientOut });
48
49
 
49
- void generateClient(env).then(({ path }) => {
50
- console.info(` 🐺 Client generated in ${path}`);
51
- });
50
+ void generateClient(env).then(({ path }) => {
51
+ console.info(` 🐺 Client generated in ${path}`);
52
+ });
53
+ })();
52
54
  } else if (command === 'help') {
53
55
  console.info(` 🐺 Vovk CLI
54
56
  dev - Start development server
@@ -1,4 +1,4 @@
1
- const net = require('net');
1
+ import net from 'net';
2
2
 
3
3
  function checkPort(port, callback) {
4
4
  const server = net.createServer();
@@ -15,7 +15,7 @@ function checkPort(port, callback) {
15
15
  }
16
16
 
17
17
  /** @type {(startPort: number, maxAttempts: number, attempt?: number) => Promise<string>} */
18
- function getAvailablePort(startPort, maxAttempts, attempt = 1) {
18
+ export default function getAvailablePort(startPort, maxAttempts, attempt = 1) {
19
19
  return new Promise((resolve, reject) => {
20
20
  checkPort(startPort, (isAvailable) => {
21
21
  if (isAvailable) {
@@ -28,5 +28,3 @@ function getAvailablePort(startPort, maxAttempts, attempt = 1) {
28
28
  });
29
29
  });
30
30
  }
31
-
32
- module.exports = getAvailablePort;
@@ -1,6 +1,6 @@
1
1
  // @ts-check
2
2
  /** @type {(fromPath: string, toPath: string) => string} */
3
- function getReturnPath(fromPath, toPath) {
3
+ export default function getReturnPath(fromPath, toPath) {
4
4
  // Split the paths into components
5
5
  const fromParts = fromPath.replace(/^\.?\/|\/$/g, '').split('/');
6
6
  const toParts = toPath.replace(/^\.?\/|\/$/g, '').split('/');
@@ -24,5 +24,3 @@ function getReturnPath(fromPath, toPath) {
24
24
 
25
25
  return result;
26
26
  }
27
-
28
- module.exports = getReturnPath;
@@ -1,6 +1,6 @@
1
1
  // @ts-check
2
2
  /** @type {(obj1: any, obj2: any) => boolean} */
3
- const isEqual = (obj1, obj2) => {
3
+ export default function isEqual(obj1, obj2) {
4
4
  if (obj1 === obj2) {
5
5
  return true;
6
6
  }
@@ -23,6 +23,4 @@ const isEqual = (obj1, obj2) => {
23
23
  }
24
24
 
25
25
  return true;
26
- };
27
-
28
- module.exports = isEqual;
26
+ }
@@ -1,8 +1,8 @@
1
1
  // @ts-check
2
- const { spawn } = require('child_process');
2
+ import { spawn } from 'child_process';
3
3
 
4
4
  /** @type {(commands: { command: string; name: string; }[], env: import('../../src').VovkEnv) => Promise<void>} */
5
- function parallel(commands, env) {
5
+ export default function parallel(commands, env) {
6
6
  return new Promise((resolve, reject) => {
7
7
  /** @type {{ name: string; process: import('child_process').ChildProcess; }[]} */
8
8
  let processes = [];
@@ -46,5 +46,3 @@ function parallel(commands, env) {
46
46
  }
47
47
  });
48
48
  }
49
-
50
- module.exports = parallel;
@@ -7,7 +7,7 @@ function toCamelCase(str) {
7
7
 
8
8
  /** @typedef {{ config?: string; project?: string; clientOut?: string }} Flags */
9
9
  /** @typedef {'dev' | 'build' | 'generate' | 'help'} Command */
10
- function parseCommandLineArgs() {
10
+ export default function parseCommandLineArgs() {
11
11
  const args = process.argv.slice(2); // Slice off node and script path
12
12
  let command = /** @type {Command} */ null;
13
13
  /** @type {Flags} */
@@ -37,5 +37,3 @@ function parseCommandLineArgs() {
37
37
 
38
38
  return { command, flags, restArgs };
39
39
  }
40
-
41
- module.exports = parseCommandLineArgs;
@@ -1,6 +1,6 @@
1
1
  // @ts-check
2
- const fs = require('fs/promises');
3
- const path = require('path');
2
+ import fs from 'fs/promises';
3
+ import path from 'path';
4
4
 
5
5
  /** @type {(path: string) => Promise<boolean>} */
6
6
  const fileExists = async (path) => !!(await fs.stat(path).catch(() => false));
@@ -1,11 +1,11 @@
1
1
  // @ts-check
2
- const http = require('http');
3
- const fs = require('fs/promises');
4
- const path = require('path');
5
- const parseCommandLineArgs = require('./lib/parseCommandLineArgs');
6
- const generateClient = require('./generateClient');
7
- const getVars = require('./getVars');
8
- const isEqual = require('./lib/isEqual');
2
+ import http from 'http';
3
+ import fs from 'fs/promises';
4
+ import path from 'path';
5
+ import parseCommandLineArgs from './lib/parseCommandLineArgs.mjs';
6
+ import generateClient from './generateClient.mjs';
7
+ import getVars from './getVars.mjs';
8
+ import isEqual from './lib/isEqual.mjs';
9
9
 
10
10
  const { flags } = parseCommandLineArgs();
11
11
 
@@ -36,9 +36,9 @@ let pingInterval;
36
36
  /** @type {import('../src').VovkEnv} */
37
37
  let vars;
38
38
 
39
- /** @type {() => void} */
40
- const ping = () => {
41
- vars = vars ?? getVars(config);
39
+ /** @type {() => Promise<void>} */
40
+ const ping = async () => {
41
+ vars = vars ?? (await getVars(config));
42
42
  let prefix = vars.VOVK_PREFIX;
43
43
  prefix = prefix.startsWith('http://')
44
44
  ? prefix
@@ -56,12 +56,12 @@ const ping = () => {
56
56
  };
57
57
 
58
58
  // make initial ping
59
- setTimeout(ping, 1000 * 3);
59
+ setTimeout(() => void ping(), 1000 * 3);
60
60
 
61
61
  /** @type {() => void} */
62
62
  const constantlyPing = () => {
63
63
  clearInterval(pingInterval);
64
- pingInterval = setInterval(ping, 1000 * 3);
64
+ pingInterval = setInterval(() => void ping(), 1000 * 3);
65
65
  };
66
66
 
67
67
  const server = http.createServer((req, res) => {
@@ -78,7 +78,7 @@ const server = http.createServer((req, res) => {
78
78
  /** @type {{ metadata?: import('../src') }} */
79
79
  const { metadata } = JSON.parse(body); // Parse the JSON data
80
80
  const metadataWritten = metadata ? await writeMetadata(metadata) : { written: false, path: metadataPath };
81
- vars = vars ?? getVars(config);
81
+ vars = vars ?? (await getVars(config));
82
82
  const codeWritten = await generateClient(vars);
83
83
  res.writeHead(200, { 'Content-Type': 'text/plain' });
84
84
  res.end('JSON data received and file created');
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "vovk",
3
- "version": "0.2.3-beta.113",
3
+ "version": "0.2.3-beta.114",
4
4
  "description": "Structural add-on for Next.js",
5
- "bin": "./cli/index.js",
5
+ "bin": "./cli/index.mjs",
6
6
  "scripts": {
7
7
  "postinstall": "node ./cli/postinstall.js",
8
8
  "upgrade": "npx npm-check-updates -u && npm i",