@kithinji/pod 1.0.13 → 1.0.15

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/dist/main.js CHANGED
@@ -2148,36 +2148,59 @@ function j2d({ types: t }) {
2148
2148
  }
2149
2149
 
2150
2150
  // src/plugins/generators/generate_server_component.ts
2151
- import { parseSync as parseSync3 } from "@swc/core";
2151
+ import { parseSync as parseSync3, printSync as printSync2 } from "@swc/core";
2152
2152
  function generateServerComponent(filePath, code) {
2153
2153
  const ast = parseSync3(code, {
2154
2154
  syntax: "typescript",
2155
2155
  tsx: filePath.endsWith("x"),
2156
2156
  decorators: true
2157
2157
  });
2158
- const componentInfo = extractComponentInfo(ast);
2159
- return generateStubCode(componentInfo);
2160
- }
2161
- function extractComponentInfo(ast) {
2162
- let componentClass = null;
2158
+ const importMap = {};
2163
2159
  for (const item of ast.body) {
2164
- if (item.type === "ExportDeclaration" && item.declaration.type === "ClassDeclaration") {
2160
+ if (item.type === "ImportDeclaration") {
2161
+ const decl = item;
2162
+ for (const specifier of decl.specifiers ?? []) {
2163
+ let localName;
2164
+ if (specifier.type === "ImportSpecifier") {
2165
+ localName = specifier.local.value;
2166
+ } else if (specifier.type === "ImportDefaultSpecifier") {
2167
+ localName = specifier.local.value;
2168
+ } else {
2169
+ continue;
2170
+ }
2171
+ importMap[localName] = decl.source.value;
2172
+ }
2173
+ }
2174
+ }
2175
+ const preservedNodes = [];
2176
+ const stubbedClasses = [];
2177
+ for (const item of ast.body) {
2178
+ let shouldStub = false;
2179
+ if (item.type === "ExportDeclaration" && item.declaration?.type === "ClassDeclaration") {
2165
2180
  const classDecl = item.declaration;
2166
2181
  if (hasComponentDecorator2(classDecl.decorators)) {
2167
- componentClass = classDecl;
2168
- break;
2182
+ shouldStub = true;
2183
+ const stub = extractClassStub2(classDecl);
2184
+ if (stub) {
2185
+ stubbedClasses.push(stub);
2186
+ }
2169
2187
  }
2170
2188
  }
2189
+ if (!shouldStub) {
2190
+ preservedNodes.push(item);
2191
+ }
2171
2192
  }
2172
- if (!componentClass || !componentClass.identifier) {
2173
- throw new Error("Component class is undefined");
2174
- }
2175
- const className = componentClass.identifier.value;
2176
- const methods = extractMethods2(componentClass);
2177
- return {
2178
- className,
2179
- methods
2180
- };
2193
+ const preservedCode = preservedNodes.length > 0 ? printSync2({
2194
+ type: "Module",
2195
+ span: ast.span,
2196
+ body: preservedNodes,
2197
+ interpreter: ast.interpreter
2198
+ }).code : "";
2199
+ const stubCode = stubbedClasses.map((stub) => generateStubCode(stub)).join("\n\n");
2200
+ return `
2201
+ ${preservedCode}
2202
+ ${stubCode}
2203
+ `.trim();
2181
2204
  }
2182
2205
  function hasComponentDecorator2(decorators) {
2183
2206
  if (!decorators) return false;
@@ -2194,6 +2217,111 @@ function hasComponentDecorator2(decorators) {
2194
2217
  return false;
2195
2218
  });
2196
2219
  }
2220
+ function extractClassStub2(classDecl) {
2221
+ const className = classDecl.identifier?.value;
2222
+ if (!className) return null;
2223
+ let propsType = "{}";
2224
+ const decorators = [];
2225
+ const constructorParams = [];
2226
+ if (classDecl.decorators) {
2227
+ for (const dec of classDecl.decorators) {
2228
+ const str = stringifyDecorator2(dec);
2229
+ if (str) decorators.push(str);
2230
+ }
2231
+ }
2232
+ for (const member of classDecl.body) {
2233
+ if (member.type === "ClassProperty") {
2234
+ if (member.key.type === "Identifier" && member.key.value === "props") {
2235
+ propsType = extractPropsType2(member);
2236
+ }
2237
+ } else if (member.type === "Constructor") {
2238
+ for (const param of member.params) {
2239
+ const paramStr = stringifyParam2(param);
2240
+ if (paramStr) constructorParams.push(paramStr);
2241
+ }
2242
+ }
2243
+ }
2244
+ const methods = extractMethods2(classDecl);
2245
+ return {
2246
+ name: className,
2247
+ propsType,
2248
+ decorators,
2249
+ constructorParams,
2250
+ methods
2251
+ };
2252
+ }
2253
+ function stringifyDecorator2(decorator) {
2254
+ const exprCode = printSync2({
2255
+ type: "Module",
2256
+ span: { start: 0, end: 0, ctxt: 0 },
2257
+ body: [
2258
+ {
2259
+ type: "ExpressionStatement",
2260
+ expression: decorator.expression,
2261
+ span: { start: 0, end: 0, ctxt: 0 }
2262
+ }
2263
+ ],
2264
+ interpreter: ""
2265
+ }).code;
2266
+ const cleanCode = exprCode.replace(/^#!.*\n/, "").trim();
2267
+ return `@${cleanCode.replace(/;$/, "")}`;
2268
+ }
2269
+ function extractPropsType2(member) {
2270
+ const typeAnn = member.typeAnnotation?.typeAnnotation;
2271
+ if (!typeAnn) return "{}";
2272
+ if (typeAnn.type === "TsTypeLiteral") {
2273
+ const props = [];
2274
+ for (const m of typeAnn.members) {
2275
+ if (m.type === "TsPropertySignature") {
2276
+ const key = m.key.type === "Identifier" ? m.key.value : "?";
2277
+ const t = m.typeAnnotation ? stringifyType3(m.typeAnnotation.typeAnnotation) : "any";
2278
+ props.push(`${key}: ${t}`);
2279
+ }
2280
+ }
2281
+ return `{ ${props.join("; ")} }`;
2282
+ }
2283
+ return stringifyType3(typeAnn);
2284
+ }
2285
+ function stringifyParam2(param) {
2286
+ let decorators = [];
2287
+ if (param.decorators) {
2288
+ for (const d of param.decorators) {
2289
+ const str = stringifyDecorator2(d);
2290
+ if (str) decorators.push(str);
2291
+ }
2292
+ }
2293
+ const decoratorPrefix = decorators.length ? decorators.join(" ") + " " : "";
2294
+ let typeName = "any";
2295
+ let paramName = "";
2296
+ let accessibility = "";
2297
+ if (param.type === "TsParameterProperty") {
2298
+ accessibility = param.accessibility || "";
2299
+ const inner = param.param;
2300
+ if (inner.type !== "Identifier") return "";
2301
+ paramName = inner.value;
2302
+ if (inner.typeAnnotation?.typeAnnotation) {
2303
+ typeName = extractTypeName2(inner.typeAnnotation.typeAnnotation);
2304
+ }
2305
+ } else if (param.type === "Parameter") {
2306
+ const pat = param.pat;
2307
+ if (pat.type !== "Identifier") return "";
2308
+ paramName = pat.value;
2309
+ if (pat.typeAnnotation?.typeAnnotation) {
2310
+ typeName = extractTypeName2(pat.typeAnnotation.typeAnnotation);
2311
+ }
2312
+ } else {
2313
+ return "";
2314
+ }
2315
+ const accessPrefix = accessibility ? `${accessibility} ` : "";
2316
+ const result = `${decoratorPrefix}${accessPrefix}${paramName}: ${typeName}`;
2317
+ return result;
2318
+ }
2319
+ function extractTypeName2(typeNode) {
2320
+ if (typeNode.type === "TsTypeReference" && typeNode.typeName.type === "Identifier") {
2321
+ return typeNode.typeName.value;
2322
+ }
2323
+ return stringifyType3(typeNode);
2324
+ }
2197
2325
  function extractMethods2(classDecl) {
2198
2326
  const methods = [];
2199
2327
  for (const member of classDecl.body) {
@@ -2283,29 +2411,31 @@ function stringifyType3(typeNode) {
2283
2411
  return "any";
2284
2412
  }
2285
2413
  }
2286
- function generateStubCode(componentInfo) {
2287
- const className = componentInfo.className;
2288
- const build = componentInfo.methods.find((p) => p.name == "build");
2414
+ function generateStubCode(stub) {
2415
+ const className = stub.name;
2416
+ const build = stub.methods.find((p) => p.name == "build");
2289
2417
  if (build == void 0) {
2290
2418
  throw new Error("Component has no build function");
2291
2419
  }
2420
+ const decoratorsStr = stub.decorators.length > 0 ? stub.decorators.join("\n") + "\n" : "";
2292
2421
  return `import {
2293
- Component,
2294
- Inject,
2295
- getCurrentInjector,
2296
- OrcaComponent,
2297
- JSX,
2298
- OSC,
2299
- HttpClient,
2422
+ Inject as _Inject,
2423
+ getCurrentInjector as _getCurrentInjector,
2424
+ OrcaComponent as _OrcaComponent,
2425
+ JSX as _JSX,
2426
+ OSC as _OSC,
2427
+ HttpClient as _HttpClient,
2428
+ symbolValueReviver as _symbolValueReviver
2300
2429
  } from "@kithinji/orca";
2301
2430
 
2302
- @Component()
2303
- export class ${className} extends OrcaComponent {
2431
+
2432
+ ${decoratorsStr}export class ${className} extends _OrcaComponent {
2304
2433
  props!: any;
2305
2434
 
2306
2435
  constructor(
2307
- @Inject("OSC_URL", { maybe: true }) private oscUrl?: string,
2308
- private readonly http: HttpClient,
2436
+ @_Inject("OSC_URL", { maybe: true }) private oscUrl?: string,
2437
+ private readonly http: _HttpClient,
2438
+ ${stub.constructorParams.join(", ")}
2309
2439
  ) {
2310
2440
  super();
2311
2441
 
@@ -2318,19 +2448,20 @@ export class ${className} extends OrcaComponent {
2318
2448
  const root = document.createElement("div");
2319
2449
  root.textContent = "loading...";
2320
2450
 
2321
- const injector = getCurrentInjector();
2451
+ const injector = _getCurrentInjector();
2322
2452
 
2323
2453
  if(injector == null) {
2324
2454
  throw new Error("Injector is null");
2325
2455
  }
2326
2456
 
2327
- const osc = new OSC(root);
2457
+ const osc = new _OSC(root);
2328
2458
 
2329
- const subscription = this.http.post<JSX.Element>(
2459
+ const subscription = this.http.post<_JSX.Element>(
2330
2460
  \`\${this.oscUrl}?c=${className}\`, {
2331
- body: this.props
2461
+ body: this.props,
2462
+ reviver: _symbolValueReviver,
2332
2463
  }
2333
- ).subscribe((jsx: JSX.Element) => {
2464
+ ).subscribe((jsx: _JSX.Element) => {
2334
2465
  const action = jsx.action || "insert";
2335
2466
 
2336
2467
  if (action === "insert") {
@@ -4539,7 +4670,7 @@ function printNextSteps(projectName, env, services) {
4539
4670
 
4540
4671
  // src/main.ts
4541
4672
  var program = new Command();
4542
- program.name("pod").description("Pod cli tool").version("1.0.13");
4673
+ program.name("pod").description("Pod cli tool").version("1.0.15");
4543
4674
  program.command("new <name>").description("Start a new Pod Project").action(async (name) => {
4544
4675
  await addNew(name);
4545
4676
  const appDir = path13.resolve(process.cwd(), name);