@sealcode/sealgen 0.11.6 → 0.11.8

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.
@@ -0,0 +1 @@
1
+ export declare function make_notifier(port: number): (message: string) => void;
package/lib/build.js CHANGED
@@ -37,6 +37,10 @@ const sleep_1 = require("./utils/sleep");
37
37
  const generate_collections_1 = require("./generate-collections");
38
38
  const generate_routes_1 = require("./generate-routes");
39
39
  const generate_components_1 = require("./generate-components");
40
+ const notifier_1 = require("./notifier");
41
+ const get_port_1 = __importDefault(require("get-port"));
42
+ const locreq_1 = __importDefault(require("locreq"));
43
+ const target_locreq = (0, locreq_1.default)(process.cwd());
40
44
  const generate_names = (function* generate_names() {
41
45
  const base_name = "____unique_import_";
42
46
  let count = 0;
@@ -45,6 +49,16 @@ const generate_names = (function* generate_names() {
45
49
  count++;
46
50
  }
47
51
  })();
52
+ async function get_notifier_port() {
53
+ return (0, get_port_1.default)({ port: 4000 });
54
+ }
55
+ async function write_notifier_config(watch, port) {
56
+ const config = { watch };
57
+ if (port) {
58
+ config.port = port;
59
+ }
60
+ await fs_1.promises.writeFile(target_locreq.resolve("public/dist/notifier.json"), JSON.stringify(config));
61
+ }
48
62
  async function build_ts() {
49
63
  await Promise.all([
50
64
  (0, generate_collections_1.generateCollections)(),
@@ -121,8 +135,12 @@ async function complex_build(watch) {
121
135
  });
122
136
  let ongoing_ts = false;
123
137
  let ongoing_css = false;
138
+ let ongoing_ts_promise = null;
124
139
  if (watch) {
125
140
  const watcher = chokidar.watch("src", { ignoreInitial: true });
141
+ const port = await get_notifier_port();
142
+ const notifier = (0, notifier_1.make_notifier)(port);
143
+ await write_notifier_config(watch, port);
126
144
  watcher.on("all", async (_, path) => {
127
145
  if (!css_build)
128
146
  return;
@@ -148,6 +166,10 @@ async function complex_build(watch) {
148
166
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
149
167
  .catch((e) => console.error(e?.message));
150
168
  }
169
+ if (ongoing_ts_promise) {
170
+ await ongoing_ts_promise;
171
+ }
172
+ notifier("css");
151
173
  ongoing_css = false;
152
174
  }
153
175
  if ((path.endsWith(".ts") || path.endsWith(".tsx")) &&
@@ -158,12 +180,18 @@ async function complex_build(watch) {
158
180
  return;
159
181
  }
160
182
  ongoing_ts = true;
161
- await build_ts();
183
+ ongoing_ts_promise = build_ts();
184
+ await ongoing_ts_promise;
185
+ notifier("ts");
186
+ ongoing_ts_promise = null;
162
187
  console.log(`Finished TS build [on ${path}]`);
163
188
  ongoing_ts = false;
164
189
  }
165
190
  });
166
191
  }
192
+ else {
193
+ await write_notifier_config(watch);
194
+ }
167
195
  await (0, find_css_includes_1.generate_css_includes)();
168
196
  await Promise.all([css_build.rebuild(), build_ts()]);
169
197
  watch && css_build?.rebuild?.();
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.make_notifier = void 0;
7
+ const ws_1 = __importDefault(require("ws"));
8
+ function make_notifier(port) {
9
+ const server = new ws_1.default.Server({
10
+ port,
11
+ });
12
+ let sockets = [];
13
+ server.on("connection", function (socket) {
14
+ sockets.push(socket);
15
+ // When a socket closes, or disconnects, remove it from the array.
16
+ socket.on("close", function () {
17
+ sockets = sockets.filter((s) => s !== socket);
18
+ });
19
+ });
20
+ console.log("build notifier listening on websocket at port " + port);
21
+ return function notify(message) {
22
+ sockets.forEach((s) => s.send(message));
23
+ };
24
+ }
25
+ exports.make_notifier = make_notifier;
26
+ //# sourceMappingURL=notifier.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.11.6",
3
+ "version": "0.11.8",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -27,13 +27,14 @@
27
27
  "@types/prettier": "^2.7.0",
28
28
  "@types/prompts": "^2.0.14",
29
29
  "@types/recursive-readdir": "^2.2.4",
30
+ "@types/ws": "^8.5.10",
30
31
  "@typescript-eslint/eslint-plugin": "^4.29.3",
31
32
  "@typescript-eslint/parser": "^4.29.3",
32
33
  "eslint": "^7.32.0",
33
34
  "eslint-config-prettier": "^8.3.0",
34
35
  "eslint-plugin-prettier": "^3.4.1",
35
36
  "eslint-plugin-with-tsc-error": "^0.0.7",
36
- "get-port": "^5.0.0",
37
+ "get-port": "^5.1.1",
37
38
  "koa-body": "^5.0.0",
38
39
  "mocha": "^9.2.2",
39
40
  "mri": "^1.2.0",
@@ -60,6 +61,7 @@
60
61
  "recursive-readdir": "^2.2.3",
61
62
  "tempstream": "^0.3.0",
62
63
  "tiny-glob": "^0.2.9",
64
+ "ws": "^8.16.0",
63
65
  "yargs": "^17.6.2"
64
66
  },
65
67
  "peerDependencies": {
package/src/build.ts CHANGED
@@ -8,6 +8,11 @@ import { sleep } from "./utils/sleep";
8
8
  import { generateCollections } from "./generate-collections";
9
9
  import { generateRoutes } from "./generate-routes";
10
10
  import { generateComponents } from "./generate-components";
11
+ import { make_notifier } from "./notifier";
12
+ import getPort from "get-port";
13
+
14
+ import _locreq from "locreq";
15
+ const target_locreq = _locreq(process.cwd());
11
16
 
12
17
  const generate_names = (function* generate_names() {
13
18
  const base_name = "____unique_import_";
@@ -18,6 +23,21 @@ const generate_names = (function* generate_names() {
18
23
  }
19
24
  })();
20
25
 
26
+ async function get_notifier_port() {
27
+ return getPort({ port: 4000 });
28
+ }
29
+
30
+ async function write_notifier_config(watch: boolean, port?: number) {
31
+ const config = { watch } as Record<string, boolean | number>;
32
+ if (port) {
33
+ config.port = port;
34
+ }
35
+ await fs.writeFile(
36
+ target_locreq.resolve("public/dist/notifier.json"),
37
+ JSON.stringify(config)
38
+ );
39
+ }
40
+
21
41
  async function build_ts() {
22
42
  await Promise.all([
23
43
  generateCollections(),
@@ -135,8 +155,12 @@ async function complex_build(watch: boolean): Promise<void> {
135
155
  });
136
156
  let ongoing_ts = false;
137
157
  let ongoing_css = false;
158
+ let ongoing_ts_promise: Promise<any> | null = null;
138
159
  if (watch) {
139
160
  const watcher = chokidar.watch("src", { ignoreInitial: true });
161
+ const port = await get_notifier_port();
162
+ const notifier = make_notifier(port);
163
+ await write_notifier_config(watch, port);
140
164
  watcher.on("all", async (_, path) => {
141
165
  if (!css_build) return;
142
166
  if (path.includes(".#")) return;
@@ -159,6 +183,10 @@ async function complex_build(watch: boolean): Promise<void> {
159
183
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
160
184
  .catch((e) => console.error(e?.message));
161
185
  }
186
+ if (ongoing_ts_promise) {
187
+ await ongoing_ts_promise;
188
+ }
189
+ notifier("css");
162
190
  ongoing_css = false;
163
191
  }
164
192
  if (
@@ -171,11 +199,16 @@ async function complex_build(watch: boolean): Promise<void> {
171
199
  return;
172
200
  }
173
201
  ongoing_ts = true;
174
- await build_ts();
202
+ ongoing_ts_promise = build_ts();
203
+ await ongoing_ts_promise;
204
+ notifier("ts");
205
+ ongoing_ts_promise = null;
175
206
  console.log(`Finished TS build [on ${path}]`);
176
207
  ongoing_ts = false;
177
208
  }
178
209
  });
210
+ } else {
211
+ await write_notifier_config(watch);
179
212
  }
180
213
  await generate_css_includes();
181
214
 
@@ -0,0 +1,22 @@
1
+ import WebSocket from "ws";
2
+
3
+ export function make_notifier(port: number) {
4
+ const server = new WebSocket.Server({
5
+ port,
6
+ });
7
+
8
+ let sockets: WebSocket[] = [];
9
+ server.on("connection", function (socket) {
10
+ sockets.push(socket);
11
+
12
+ // When a socket closes, or disconnects, remove it from the array.
13
+ socket.on("close", function () {
14
+ sockets = sockets.filter((s) => s !== socket);
15
+ });
16
+ });
17
+
18
+ console.log("build notifier listening on websocket at port " + port);
19
+ return function notify(message: string) {
20
+ sockets.forEach((s) => s.send(message));
21
+ };
22
+ }