vovk 0.2.3-beta.28 → 0.2.3-beta.29
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.
- package/cli/generateClient.js +36 -2
- package/cli/getVovkrc.js +1 -1
- package/cli/index.js +8 -14
- package/cli/server.js +1 -1
- package/package.json +1 -1
package/cli/generateClient.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
const fs = require('fs/promises');
|
|
2
2
|
const path = require('path');
|
|
3
3
|
const getVovkrc = require('./getVovkrc');
|
|
4
|
+
|
|
5
|
+
function canRequire(moduleName) {
|
|
6
|
+
try {
|
|
7
|
+
require.resolve(moduleName);
|
|
8
|
+
return true; // The module exists and can be required
|
|
9
|
+
} catch (e) {
|
|
10
|
+
return false; // The module does not exist
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
4
14
|
/**
|
|
5
15
|
* Generates client code with string concatenation so it should be much faster than using AST
|
|
6
16
|
* TODO: Check modules for existence before compiling, use vovk-zod by default
|
|
@@ -8,12 +18,36 @@ const getVovkrc = require('./getVovkrc');
|
|
|
8
18
|
*/
|
|
9
19
|
async function generateClient(rcPath) {
|
|
10
20
|
const vovkrc = getVovkrc(rcPath);
|
|
11
|
-
|
|
21
|
+
const jsonPath = '../../.vovk.json';
|
|
12
22
|
const fetcherPath = vovkrc.fetcher.startsWith('.') ? path.join(process.cwd(), vovkrc.fetcher) : vovkrc.fetcher;
|
|
13
23
|
const streamFetcherPath = vovkrc.streamFetcher.startsWith('.')
|
|
14
24
|
? path.join(process.cwd(), vovkrc.streamFetcher)
|
|
15
25
|
: vovkrc.streamFetcher;
|
|
16
26
|
|
|
27
|
+
if (typeof vovkrc.validateOnClient === 'undefined') {
|
|
28
|
+
vovkrc.validateOnClient = canRequire('vovk-zod/validateOnClient') ? 'vovk-zod/validateOnClient' : null;
|
|
29
|
+
} else if (!canRequire(vovkrc.validateOnClient)) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`Unble to generate Vovk Client: cannot find "validateOnClient" module '${vovkrc.validateOnClient}'. Check your .vovkrc.js file`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!canRequire(fetcherPath)) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`Unble to generate Vovk Client: cannot find "fetcher" module '${fetcherPath}'. Check your .vovkrc.js file`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!canRequire(streamFetcherPath)) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Unble to generate Vovk Client: cannot find "streamFetcher" module '${streamFetcherPath}'. Check your .vovkrc.js file`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!canRequire(jsonPath)) {
|
|
48
|
+
throw new Error(`Unble to generate Vovk Client: cannot find ".vovk.json" file '${jsonPath}'.`);
|
|
49
|
+
}
|
|
50
|
+
|
|
17
51
|
const controllersPath = path.join('../..', vovkrc.route).replace(/\.ts$/, '');
|
|
18
52
|
let ts = `import type { Controllers, Workers } from "${controllersPath}";
|
|
19
53
|
import type { clientizeController } from 'vovk/client';
|
|
@@ -25,7 +59,7 @@ type Options = typeof fetcher extends VovkClientFetcher<infer U> ? U : never;
|
|
|
25
59
|
`;
|
|
26
60
|
let js = `const { clientizeController } = require('vovk/client');
|
|
27
61
|
const { promisifyWorker } = require('vovk/worker');
|
|
28
|
-
const metadata = require('
|
|
62
|
+
const metadata = require('${jsonPath}');
|
|
29
63
|
const { default: fetcher } = require('${fetcherPath}');
|
|
30
64
|
const { default: streamFetcher } = require('${streamFetcherPath}');
|
|
31
65
|
const prefix = '${vovkrc.prefix ?? '/api'}';
|
package/cli/getVovkrc.js
CHANGED
|
@@ -17,7 +17,7 @@ function getVovkrc(rcPath) {
|
|
|
17
17
|
if (fs.existsSync(rcPath)) {
|
|
18
18
|
Object.assign(vovkRc, require(rcPath));
|
|
19
19
|
} else {
|
|
20
|
-
console.info(` 🐺 No .vovkrc.js file found in ${process.cwd()}
|
|
20
|
+
console.info(` 🐺 No .vovkrc.js file found in ${process.cwd()}`);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
vovkRcRef = vovkRc;
|
package/cli/index.js
CHANGED
|
@@ -46,12 +46,6 @@ const builder = {
|
|
|
46
46
|
default: process.cwd(),
|
|
47
47
|
describe: 'Path to Next.js project',
|
|
48
48
|
},
|
|
49
|
-
|
|
50
|
-
output: {
|
|
51
|
-
type: 'string',
|
|
52
|
-
default: path.join(__dirname, '../.vovk/index.d.ts'),
|
|
53
|
-
describe: 'Path to the wildcard route file',
|
|
54
|
-
},
|
|
55
49
|
};
|
|
56
50
|
|
|
57
51
|
const options = {
|
|
@@ -59,7 +53,7 @@ const options = {
|
|
|
59
53
|
raw: true,
|
|
60
54
|
killOthers: ['failure', 'success'],
|
|
61
55
|
};
|
|
62
|
-
/** @type {{ rc: string, project: string
|
|
56
|
+
/** @type {{ rc: string, project: string }} */
|
|
63
57
|
const argv = yargs(hideBin(process.argv))
|
|
64
58
|
.command('dev', 'Run development server', builder)
|
|
65
59
|
.command('build', 'Build the app', builder)
|
|
@@ -75,7 +69,7 @@ if (argv._.includes('dev')) {
|
|
|
75
69
|
const { result } = concurrently(
|
|
76
70
|
[
|
|
77
71
|
{
|
|
78
|
-
command: `VOVK_PORT=${PORT} node ${__dirname}/server.js --rc ${argv.rc}
|
|
72
|
+
command: `VOVK_PORT=${PORT} node ${__dirname}/server.js --rc ${argv.rc}`,
|
|
79
73
|
name: 'Vovk',
|
|
80
74
|
},
|
|
81
75
|
{ command: `cd ${argv.project} && VOVK_PORT=${PORT} npx next dev ${nextArgs}`, name: 'Next' },
|
|
@@ -84,11 +78,11 @@ if (argv._.includes('dev')) {
|
|
|
84
78
|
);
|
|
85
79
|
|
|
86
80
|
void result.then(() => {
|
|
87
|
-
console.info(' 🐺 All processes have completed
|
|
81
|
+
console.info(' 🐺 All processes have completed');
|
|
88
82
|
});
|
|
89
83
|
})
|
|
90
84
|
.catch(() => {
|
|
91
|
-
console.error(' 🐺 Failed to find available port
|
|
85
|
+
console.error(' 🐺 Failed to find available port');
|
|
92
86
|
});
|
|
93
87
|
}
|
|
94
88
|
|
|
@@ -98,7 +92,7 @@ if (argv._.includes('build')) {
|
|
|
98
92
|
const { result } = concurrently(
|
|
99
93
|
[
|
|
100
94
|
{
|
|
101
|
-
command: `VOVK_PORT=${PORT} node ${__dirname}/server.js --once --rc ${argv.rc}
|
|
95
|
+
command: `VOVK_PORT=${PORT} node ${__dirname}/server.js --once --rc ${argv.rc}`,
|
|
102
96
|
name: 'Vovk',
|
|
103
97
|
},
|
|
104
98
|
{ command: `cd ${argv.project} && VOVK_PORT=${PORT} npx next build ${nextArgs}`, name: 'Next' },
|
|
@@ -109,12 +103,12 @@ if (argv._.includes('build')) {
|
|
|
109
103
|
void result.catch((e) => console.error(e));
|
|
110
104
|
})
|
|
111
105
|
.catch(() => {
|
|
112
|
-
console.error(' 🐺 Failed to find available port
|
|
106
|
+
console.error(' 🐺 Failed to find available port');
|
|
113
107
|
});
|
|
114
108
|
}
|
|
115
109
|
|
|
116
110
|
if (argv._.includes('generate')) {
|
|
117
|
-
void generateClient(argv.rc
|
|
118
|
-
console.info(' 🐺 Client generated
|
|
111
|
+
void generateClient(argv.rc).then(() => {
|
|
112
|
+
console.info(' 🐺 Client generated');
|
|
119
113
|
});
|
|
120
114
|
}
|
package/cli/server.js
CHANGED
|
@@ -56,7 +56,7 @@ const server = http.createServer((req, res) => {
|
|
|
56
56
|
const metadata = JSON.parse(body); // Parse the JSON data
|
|
57
57
|
const filePath = path.join(__dirname, '../../../.vovk.json');
|
|
58
58
|
const metadataWritten = await writeMetadata(filePath, metadata);
|
|
59
|
-
const codeWritten = await generateClient(argv.rc
|
|
59
|
+
const codeWritten = await generateClient(argv.rc);
|
|
60
60
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
61
61
|
res.end('JSON data received and file created');
|
|
62
62
|
if (metadataWritten || codeWritten) {
|