@pyrpc/types 0.3.2 → 0.6.0
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/README.md +10 -17
- package/package.json +2 -2
- package/postinstall.js +55 -37
package/README.md
CHANGED
|
@@ -1,34 +1,27 @@
|
|
|
1
1
|
# @pyrpc/types
|
|
2
2
|
|
|
3
|
-
TypeScript type definitions for [pyRPC](https://pyrpc.
|
|
3
|
+
TypeScript type definitions for [pyRPC](https://pyrpc.com). Generated by `pyrpc dev` or `pyrpc codegen` - reflects your actual Python backend procedures at compile time.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Distribution modes
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
When you `npm install @pyrpc/types`, the `postinstall` script prompts you to choose a distribution mode:
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
```
|
|
9
|
+
### Workspace mode
|
|
10
|
+
Types are written directly by the server-side `pyrpc dev` / `pyrpc codegen` commands into `node_modules/@pyrpc/types/src/index.ts`. The postinstall creates `pyrpc-client.json` with `distribution: "workspace"` and types are generated during development.
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
### Server mode
|
|
13
|
+
Types are fetched from a running pyRPC server. The postinstall prompts for a `server_url`, fetches the schema from `{server_url}/rpc`, generates types immediately, and creates `pyrpc-client.json`.
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
PYRPC_URL=https://api.example.com npm install @pyrpc/client
|
|
17
|
-
```
|
|
15
|
+
By default, types are written to `{client_root}/node_modules/@pyrpc/types/src/index.ts`, where `client_root` is configured in `pyrpc.json`.
|
|
18
16
|
|
|
19
|
-
Before codegen runs, `Types` resolves to
|
|
17
|
+
Before codegen runs, `Types` resolves to an empty interface. After codegen, it resolves to your actual procedure signatures - full autocompletion, no boilerplate.
|
|
20
18
|
|
|
21
19
|
## Manual generation
|
|
22
20
|
|
|
23
|
-
If the postinstall prompt is skipped:
|
|
24
|
-
|
|
25
21
|
```bash
|
|
26
|
-
|
|
27
|
-
pyrpc codegen https://your-server.com
|
|
22
|
+
pyrpc codegen http://localhost:8000
|
|
28
23
|
```
|
|
29
24
|
|
|
30
|
-
By default, types are written to `node_modules/@pyrpc/types/src/index.ts`. Use `--output` to override.
|
|
31
|
-
|
|
32
25
|
## License
|
|
33
26
|
|
|
34
27
|
MIT
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyrpc/types",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "pyRPC TypeScript type definitions
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "pyRPC TypeScript type definitions - generated by pyrpc codegen",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
7
7
|
"types": "./src/index.ts",
|
package/postinstall.js
CHANGED
|
@@ -5,14 +5,23 @@ var readline = require('readline');
|
|
|
5
5
|
var TYPES_FILE = path.join(__dirname, 'src', 'index.ts');
|
|
6
6
|
var PLACEHOLDER_MARKER = 'types not generated yet';
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
function findProjectRoot() {
|
|
9
|
+
var dir = path.dirname(__dirname);
|
|
10
|
+
while (true) {
|
|
11
|
+
var pkgPath = path.join(dir, 'package.json');
|
|
12
|
+
if (fs.existsSync(pkgPath) && !pkgPath.replace(/\\/g, '/').includes('/node_modules/')) {
|
|
13
|
+
return dir;
|
|
14
|
+
}
|
|
15
|
+
var parent = path.dirname(dir);
|
|
16
|
+
if (parent === dir) return process.cwd();
|
|
17
|
+
dir = parent;
|
|
13
18
|
}
|
|
14
19
|
}
|
|
15
20
|
|
|
21
|
+
function getConfigPath() {
|
|
22
|
+
return path.join(findProjectRoot(), 'pyrpc-client.json');
|
|
23
|
+
}
|
|
24
|
+
|
|
16
25
|
var TYPE_MAP = {
|
|
17
26
|
int: 'number',
|
|
18
27
|
float: 'number',
|
|
@@ -103,17 +112,36 @@ function fetchSchema(url) {
|
|
|
103
112
|
}
|
|
104
113
|
|
|
105
114
|
function main() {
|
|
115
|
+
if (fs.existsSync(getConfigPath())) {
|
|
116
|
+
return Promise.resolve();
|
|
117
|
+
}
|
|
118
|
+
|
|
106
119
|
var isInteractive = process.stdin.isTTY;
|
|
107
|
-
var
|
|
108
|
-
|
|
120
|
+
var isCI = process.env.CI;
|
|
121
|
+
|
|
122
|
+
if (!isInteractive || isCI) {
|
|
123
|
+
return Promise.resolve();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
var rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
127
|
+
return new Promise(function(resolve) {
|
|
128
|
+
rl.question('How are types distributed to the client?\n 1) workspace (default) - server writes types directly to your project\n 2) server - client fetches types via HTTP\nEnter choice [1]: ', function(answer) {
|
|
129
|
+
rl.close();
|
|
130
|
+
resolve(answer.trim() === '2' ? 'server' : 'workspace');
|
|
131
|
+
});
|
|
132
|
+
}).then(function(distribution) {
|
|
133
|
+
if (distribution === 'workspace') {
|
|
134
|
+
var config = { distribution: 'workspace' };
|
|
135
|
+
fs.writeFileSync(getConfigPath(), JSON.stringify(config, null, 2) + '\n');
|
|
136
|
+
console.log(' \u2713 pyrpc-client.json created (workspace mode)');
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
109
139
|
|
|
110
|
-
|
|
111
|
-
var rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
140
|
+
var rl2 = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
112
141
|
return new Promise(function(resolve) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
resolve(url);
|
|
142
|
+
rl2.question('pyRPC backend URL (default: http://localhost:8000): ', function(answer) {
|
|
143
|
+
rl2.close();
|
|
144
|
+
resolve(answer.trim() || 'http://localhost:8000');
|
|
117
145
|
});
|
|
118
146
|
}).then(function(url) {
|
|
119
147
|
return fetchSchema(url).then(function(schemas) {
|
|
@@ -121,34 +149,24 @@ function main() {
|
|
|
121
149
|
fs.mkdirSync(path.dirname(TYPES_FILE), { recursive: true });
|
|
122
150
|
fs.writeFileSync(TYPES_FILE, code, 'utf-8');
|
|
123
151
|
console.log(' \u2713 @pyrpc/types: generated for ' + Object.keys(schemas).length + ' procedures');
|
|
124
|
-
|
|
152
|
+
|
|
153
|
+
var config = { distribution: 'server', server_url: url };
|
|
154
|
+
fs.writeFileSync(getConfigPath(), JSON.stringify(config, null, 2) + '\n');
|
|
155
|
+
console.log(' \u2713 pyrpc-client.json created (server mode)');
|
|
125
156
|
}).catch(function(err) {
|
|
126
157
|
console.log(' \u2717 @pyrpc/types: could not connect (' + err.message + ')');
|
|
127
|
-
console.log(' Run
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
}
|
|
158
|
+
console.log(' Run later: npx pyrpc sync');
|
|
131
159
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
fs.writeFileSync(TYPES_FILE, code, 'utf-8');
|
|
137
|
-
console.log(' \u2713 @pyrpc/types: generated for ' + Object.keys(schemas).length + ' procedures');
|
|
138
|
-
}).catch(function(err) {
|
|
139
|
-
console.log(' \u2717 @pyrpc/types: could not connect (' + err.message + ')');
|
|
140
|
-
console.log(' Run manually: pyrpc codegen ' + envUrl);
|
|
160
|
+
var config = { distribution: 'server', server_url: url };
|
|
161
|
+
fs.writeFileSync(getConfigPath(), JSON.stringify(config, null, 2) + '\n');
|
|
162
|
+
console.log(' \u2713 pyrpc-client.json created (server mode)');
|
|
163
|
+
});
|
|
141
164
|
});
|
|
142
|
-
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
143
167
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
console.log(' To connect to your pyRPC backend:');
|
|
147
|
-
console.log(' pyrpc codegen http://localhost:8000');
|
|
148
|
-
console.log(' Or install interactively in a terminal:');
|
|
149
|
-
console.log(' npm install @pyrpc/client');
|
|
150
|
-
console.log('');
|
|
151
|
-
return Promise.resolve();
|
|
168
|
+
if (require.main === module) {
|
|
169
|
+
main().catch(function(e) {});
|
|
152
170
|
}
|
|
153
171
|
|
|
154
|
-
|
|
172
|
+
module.exports = { main, toTs, generate, fetchSchema, findProjectRoot, getConfigPath, TYPES_FILE, PLACEHOLDER_MARKER };
|