@pyrpc/types 0.3.3 → 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.
Files changed (3) hide show
  1. package/README.md +10 -17
  2. package/package.json +2 -2
  3. 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.dev). Generated by `pyrpc codegen` reflects your actual Python backend procedures at compile time.
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
- ## How it works
5
+ ## Distribution modes
6
6
 
7
- `@pyrpc/types` is a dependency of `@pyrpc/client`. When you install the client, the postinstall script prompts for your server URL and writes real types:
7
+ When you `npm install @pyrpc/types`, the `postinstall` script prompts you to choose a distribution mode:
8
8
 
9
- ```bash
10
- npm install @pyrpc/client
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
- For CI or automated setups:
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
- ```bash
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 `Record<string, never>`. After codegen, it resolves to your actual procedure signatures full autocompletion, no boilerplate.
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
- pip install pyrpc-codegen
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.3.3",
4
- "description": "pyRPC TypeScript type definitions generated by pyrpc codegen",
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
- // Skip if real types already exist
9
- if (fs.existsSync(TYPES_FILE)) {
10
- var content = fs.readFileSync(TYPES_FILE, 'utf-8');
11
- if (!content.includes(PLACEHOLDER_MARKER)) {
12
- process.exit(0);
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 envUrl = process.env.PYRPC_URL;
108
- var url = null;
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
- if (isInteractive) {
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
- rl.question(' pyRPC backend URL (default: http://localhost:8000): ', function(answer) {
114
- rl.close();
115
- url = answer.trim() || 'http://localhost:8000';
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
- console.log(' Import: import type { Types } from "@pyrpc/types"');
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 manually: pyrpc codegen ' + url);
128
- });
129
- });
130
- }
158
+ console.log(' Run later: npx pyrpc sync');
131
159
 
132
- if (envUrl) {
133
- return fetchSchema(envUrl).then(function(schemas) {
134
- var code = generate(schemas);
135
- fs.mkdirSync(path.dirname(TYPES_FILE), { recursive: true });
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
- console.log('');
145
- console.log(' \u26A1 @pyrpc/types: no types generated yet.');
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
- main().catch(function(e) {});
172
+ module.exports = { main, toTs, generate, fetchSchema, findProjectRoot, getConfigPath, TYPES_FILE, PLACEHOLDER_MARKER };