opticore-api-gateway 1.0.1 → 1.0.3
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/package.json +1 -1
- package/scripts/postinstall.cjs +30 -9
package/package.json
CHANGED
package/scripts/postinstall.cjs
CHANGED
|
@@ -15,13 +15,16 @@
|
|
|
15
15
|
const fs = require('fs');
|
|
16
16
|
const path = require('path');
|
|
17
17
|
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
|
|
18
|
+
// npm runs a dependency's lifecycle scripts with `cwd` set to that package's
|
|
19
|
+
// OWN directory (node_modules/opticore-api-gateway), never the consuming
|
|
20
|
+
// project's root — so process.cwd() can't be used to find the consumer's
|
|
21
|
+
// src/ tree. npm additionally exports INIT_CWD, the directory `npm install`
|
|
22
|
+
// was actually invoked from, which is the value we need here.
|
|
23
|
+
const projectRoot = process.env.INIT_CWD || process.cwd();
|
|
21
24
|
const packageDir = path.resolve(__dirname, '..');
|
|
22
25
|
|
|
23
|
-
if (projectRoot === packageDir) {
|
|
24
|
-
// Running inside the gateway package
|
|
26
|
+
if (path.resolve(projectRoot) === packageDir) {
|
|
27
|
+
// Running inside the gateway package's own dev install — nothing to do
|
|
25
28
|
process.exit(0);
|
|
26
29
|
}
|
|
27
30
|
|
|
@@ -110,13 +113,31 @@ if (lastImportIndex !== -1) {
|
|
|
110
113
|
serverContent.slice(insertAfter);
|
|
111
114
|
}
|
|
112
115
|
|
|
113
|
-
// Replace registerRouter() with gateway.getOpticoreRoutes()
|
|
116
|
+
// Replace registerRouter(...) with gateway.getOpticoreRoutes(), preserving any
|
|
117
|
+
// extra routes passed in (e.g. registerRouter([createProfilerRouter(profiler)])
|
|
118
|
+
// — real generated projects call it with the profiler router, never bare
|
|
119
|
+
// registerRouter()) by spreading them alongside the gateway's own routes.
|
|
120
|
+
let callSiteReplaced = false;
|
|
114
121
|
serverContent = serverContent.replace(
|
|
115
|
-
/registerRouter\(\)
|
|
116
|
-
|
|
122
|
+
/registerRouter\(\s*(\[[^\]]*\])?\s*\)/,
|
|
123
|
+
(_match, extrasArrayLiteral) => {
|
|
124
|
+
callSiteReplaced = true;
|
|
125
|
+
if (extrasArrayLiteral) {
|
|
126
|
+
const extras = extrasArrayLiteral.slice(1, -1).trim();
|
|
127
|
+
return `[...gateway.getOpticoreRoutes(), ${extras}]`;
|
|
128
|
+
}
|
|
129
|
+
return 'gateway.getOpticoreRoutes()';
|
|
130
|
+
}
|
|
117
131
|
);
|
|
118
132
|
|
|
119
|
-
|
|
133
|
+
if (!callSiteReplaced) {
|
|
134
|
+
console.warn('[opticore-api-gateway] Could not find a registerRouter(...) call in src/bootstrap/server/webApp.server.ts — leaving the file untouched. Wire the gateway in manually, see the README\'s "Integration with opticore-webapp" section.');
|
|
135
|
+
process.exit(0);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Only safe to drop the registerRouter import now that its only call site
|
|
139
|
+
// was actually replaced above — removing it unconditionally previously left
|
|
140
|
+
// a dangling reference to registerRouter() when the call had arguments.
|
|
120
141
|
serverContent = serverContent.replace(
|
|
121
142
|
/^import\s*\{[^}]*registerRouter[^}]*\}\s*from\s*["'][^"']+["'];\s*\n?/m,
|
|
122
143
|
''
|