@spfn/core 0.1.0-alpha.1 → 0.1.0-alpha.2

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.
@@ -6,10 +6,10 @@ import { fileURLToPath } from 'url';
6
6
  import { dirname, join, resolve } from 'path';
7
7
  import { mkdir, writeFile, readdir, stat } from 'fs/promises';
8
8
  import * as ts from 'typescript';
9
- import { readFileSync, existsSync, mkdirSync, createWriteStream } from 'fs';
9
+ import { existsSync, mkdirSync, createWriteStream, readFileSync } from 'fs';
10
+ import { watch } from 'chokidar';
10
11
  import pino from 'pino';
11
12
  import chalk from 'chalk';
12
- import { watch } from 'chokidar';
13
13
 
14
14
  // src/scripts/migrate.ts
15
15
  config({ path: ".env.local" });
@@ -967,40 +967,84 @@ var logger = createAdapter(getAdapterType());
967
967
 
968
968
  // src/codegen/watch-generate.ts
969
969
  var codegenLogger = logger.child("codegen");
970
- async function watchAndGenerate(options = {}) {
970
+ async function generateOnce(options) {
971
971
  const cwd = process.cwd();
972
972
  const routesDir = options.routesDir ?? join(cwd, "src", "server", "routes");
973
- const outputPath = options.outputPath ?? join(cwd, "src", "lib", "api", "client.ts");
974
- const debug = options.debug ?? false;
975
- if (debug) {
976
- codegenLogger.info("Contract Watcher Started", { routesDir, outputPath });
977
- }
973
+ const outputPath = options.outputPath ?? join(cwd, "src", "lib", "api.ts");
978
974
  try {
979
975
  const contracts = await scanContracts(routesDir);
980
976
  if (contracts.length === 0) {
981
- if (debug) {
977
+ if (options.debug) {
982
978
  codegenLogger.warn("No contracts found");
983
979
  }
984
- return;
980
+ return null;
985
981
  }
986
982
  const stats = await generateClient(contracts, {
987
983
  outputPath,
988
984
  includeTypes: true,
989
985
  includeJsDoc: true
990
986
  });
991
- if (debug) {
987
+ if (options.debug) {
992
988
  codegenLogger.info("Client generated", {
993
989
  endpoints: stats.methodsGenerated,
994
990
  resources: stats.resourcesGenerated,
995
991
  duration: stats.duration
996
992
  });
997
993
  }
994
+ return stats;
998
995
  } catch (error) {
999
996
  codegenLogger.error(
1000
997
  "Generation failed",
1001
998
  error instanceof Error ? error : new Error(String(error))
1002
999
  );
1003
- throw error;
1000
+ return null;
1001
+ }
1002
+ }
1003
+ async function watchAndGenerate(options = {}) {
1004
+ const cwd = process.cwd();
1005
+ const routesDir = options.routesDir ?? join(cwd, "src", "server", "routes");
1006
+ const outputPath = options.outputPath ?? join(cwd, "src", "lib", "api.ts");
1007
+ const watchMode = options.watch !== false;
1008
+ if (options.debug) {
1009
+ codegenLogger.info("Contract Watcher Started", { routesDir, outputPath, watch: watchMode });
1010
+ }
1011
+ await generateOnce(options);
1012
+ if (watchMode) {
1013
+ let isGenerating = false;
1014
+ let pendingRegeneration = false;
1015
+ const watcher = watch(routesDir, {
1016
+ ignored: /(^|[\/\\])\../,
1017
+ // ignore dotfiles
1018
+ persistent: true,
1019
+ ignoreInitial: true,
1020
+ awaitWriteFinish: {
1021
+ stabilityThreshold: 100,
1022
+ pollInterval: 50
1023
+ }
1024
+ });
1025
+ const regenerate = async () => {
1026
+ if (isGenerating) {
1027
+ pendingRegeneration = true;
1028
+ return;
1029
+ }
1030
+ isGenerating = true;
1031
+ pendingRegeneration = false;
1032
+ if (options.debug) {
1033
+ codegenLogger.info("Contracts changed, regenerating...");
1034
+ }
1035
+ await generateOnce(options);
1036
+ isGenerating = false;
1037
+ if (pendingRegeneration) {
1038
+ await regenerate();
1039
+ }
1040
+ };
1041
+ watcher.on("add", regenerate).on("change", regenerate).on("unlink", regenerate);
1042
+ process.on("SIGINT", () => {
1043
+ watcher.close();
1044
+ process.exit(0);
1045
+ });
1046
+ await new Promise(() => {
1047
+ });
1004
1048
  }
1005
1049
  }
1006
1050
  if (import.meta.url === `file://${process.argv[1]}`) {