@citolab/tspci 2.8.4 → 2.9.7

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/src/cli.js CHANGED
@@ -9,7 +9,8 @@ import fs from "fs";
9
9
  import os from "os";
10
10
  import clear from "clear";
11
11
  import figlet from "figlet";
12
- import inquirer from "inquirer";
12
+ import chokidar from "chokidar";
13
+ import { select } from "@inquirer/prompts";
13
14
 
14
15
  const args = process.argv.slice(2);
15
16
 
@@ -22,6 +23,52 @@ const PACKAGE_ROOT_PATH = process.cwd();
22
23
  const PACKAGE_DIST_PATH = path.join(PACKAGE_ROOT_PATH, "dist");
23
24
  const NODE_MODULES = path.join(process.cwd(), "node_modules");
24
25
 
26
+ const getDevIndexHtmlSourceCandidates = (target = "IMS") => {
27
+ const projectSource = path.join(PACKAGE_ROOT_PATH, "src/index.html");
28
+ const templateSource =
29
+ target === "IMS"
30
+ ? path.join(__dirname, "../public/index.html")
31
+ : path.join(NODE_MODULES, `@citolab/tspci-${target}/public/index.html`);
32
+ return { projectSource, templateSource };
33
+ };
34
+
35
+ const resolveActiveDevIndexHtmlSource = (target = "IMS") => {
36
+ const { projectSource, templateSource } = getDevIndexHtmlSourceCandidates(target);
37
+ return fs.existsSync(projectSource) ? projectSource : templateSource;
38
+ };
39
+
40
+ const copyDevIndexHtmlToDist = (target = "IMS", reason = "initial") => {
41
+ const source = resolveActiveDevIndexHtmlSource(target);
42
+ if (!fs.existsSync(source)) {
43
+ throw new Error(`No index.html template found for dev mode. Expected: ${source}`);
44
+ }
45
+ fs.copyFileSync(source, path.join(PACKAGE_DIST_PATH, "index.html"));
46
+ console.log(`index.html copied to dist (${reason}) from ${source}`);
47
+ };
48
+
49
+ const watchDevIndexHtmlSources = (target = "IMS") => {
50
+ const { projectSource, templateSource } = getDevIndexHtmlSourceCandidates(target);
51
+ const watcher = chokidar.watch([projectSource, templateSource], {
52
+ ignoreInitial: true,
53
+ awaitWriteFinish: {
54
+ stabilityThreshold: 120,
55
+ pollInterval: 20,
56
+ },
57
+ });
58
+
59
+ watcher.on("all", (event, changedPath) => {
60
+ try {
61
+ copyDevIndexHtmlToDist(target, `${event}: ${changedPath}`);
62
+ } catch (err) {
63
+ console.error(`Failed to refresh index.html after ${event} on ${changedPath}`, err);
64
+ }
65
+ });
66
+
67
+ const closeWatcher = () => watcher.close();
68
+ process.on("SIGINT", closeWatcher);
69
+ process.on("SIGTERM", closeWatcher);
70
+ };
71
+
25
72
  // https://rollupjs.org/guide/en/#programmatically-loading-a-config-file
26
73
  const watchBuild = (devServer = false, target = "IMS") => {
27
74
  const rollupFile = devServer ? "rollup.config.dev.js" : "rollup.config.js";
@@ -36,27 +83,8 @@ const watchBuild = (devServer = false, target = "IMS") => {
36
83
  }
37
84
  if (devServer) {
38
85
  try {
39
- if (fs.existsSync(path.join(PACKAGE_ROOT_PATH, "src/index.html"))) {
40
- fs.copyFile(
41
- path.join(PACKAGE_ROOT_PATH, "src/index.html"),
42
- path.join(PACKAGE_DIST_PATH, "index.html"),
43
- (err) => {
44
- if (err) throw err;
45
- console.log("using index.html found in src/index.html, copied to dist");
46
- }
47
- );
48
- } else {
49
- let fileToCopy;
50
- if (target == "IMS") {
51
- fileToCopy = path.join(__dirname, `../public/index.html`);
52
- } else {
53
- fileToCopy = path.join(NODE_MODULES, `@citolab/tspci-${target}/public/index.html`);
54
- }
55
- fs.copyFile(fileToCopy, path.join(PACKAGE_DIST_PATH, "index.html"), (err) => {
56
- if (err) throw err;
57
- console.log("using index.html was copied to /dist folder pci");
58
- });
59
- }
86
+ copyDevIndexHtmlToDist(target);
87
+ watchDevIndexHtmlSources(target);
60
88
  } catch (err) {
61
89
  console.error(err);
62
90
  }
@@ -110,16 +138,13 @@ const prodBuild = async () => {
110
138
  });
111
139
  };
112
140
 
113
- function selectOptions(prompt, options) {
114
- return inquirer.prompt([
115
- {
116
- type: "list",
117
- message: prompt,
118
- name: "value",
119
- choices: options,
120
- default: options[0],
121
- },
122
- ]);
141
+ async function selectOptions(prompt, options) {
142
+ const value = await select({
143
+ message: prompt,
144
+ choices: options.map((o) => ({ value: o })),
145
+ default: options[0],
146
+ });
147
+ return { value };
123
148
  }
124
149
 
125
150
  function askOption(prompt) {