nowebcli 0.2.14 → 0.2.16

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nowebcli",
3
- "version": "0.2.14",
3
+ "version": "0.2.16",
4
4
  "description": "Create, preview and publish community templates for Noweb",
5
5
  "license": "MIT",
6
6
  "repository": {
package/scripts/noweb.mjs CHANGED
@@ -4,7 +4,7 @@ import { spawn, spawnSync } from "node:child_process";
4
4
  import { createHash, randomBytes } from "node:crypto";
5
5
  import { existsSync } from "node:fs";
6
6
  import { createServer } from "node:http";
7
- import { access, chmod, mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
7
+ import { access, chmod, mkdir, readFile, readdir, rm, stat, writeFile } from "node:fs/promises";
8
8
  import { homedir } from "node:os";
9
9
  import { basename, dirname, extname, join, resolve, sep } from "node:path";
10
10
  import { createInterface } from "node:readline/promises";
@@ -46,6 +46,7 @@ function usage() {
46
46
  console.log(`noweb
47
47
 
48
48
  create [slug] scaffold the current folder
49
+ reset [slug] reset template files and public/; keep dependencies (--yes)
49
50
  setup install local React and TypeScript dependencies
50
51
  dev preview at http://127.0.0.1:4173
51
52
  check validate and compile this template
@@ -58,7 +59,7 @@ function usage() {
58
59
  publish make the uploaded template public`);
59
60
  }
60
61
 
61
- async function createTemplate(value) {
62
+ async function createTemplate(value, install = shouldInstall) {
62
63
  const validSlug = requireSlug(value || `template-${randomBytes(6).toString("hex")}`);
63
64
  const directory = root;
64
65
 
@@ -73,10 +74,32 @@ async function createTemplate(value) {
73
74
  writeFile(join(directory, "customizations.json"), starterCustomizations),
74
75
  writeFile(join(directory, "public/.gitkeep"), ""),
75
76
  ]);
76
- await setupWorkspace(shouldInstall);
77
+ await setupWorkspace(install);
77
78
  success(`Created ${validSlug} in ${directory}. Add a 16:9 public/cover.jpg (JPEG, up to 5 MB).`);
78
79
  }
79
80
 
81
+ async function resetTemplate(value) {
82
+ const files = ["Template.tsx", "customizations.json", "template.json"];
83
+ if (root === engineRoot || !files.every((file) => existsSync(join(root, file)))) {
84
+ throw new Error("Run reset inside a template folder containing Template.tsx, customizations.json and template.json.");
85
+ }
86
+ const validSlug = requireSlug(value || `template-${randomBytes(6).toString("hex")}`);
87
+ if (!cliArgs.includes("--yes")) {
88
+ if (!process.stdin.isTTY) throw new Error("Pass --yes to confirm resetting the template files and public/.");
89
+ const prompt = createInterface({ input: process.stdin, output: process.stdout });
90
+ let answer;
91
+ try {
92
+ answer = await prompt.question(`Reset ${root}? Template files and public/ will be deleted; node_modules, package files and Git stay. [y/N] `);
93
+ } finally {
94
+ prompt.close();
95
+ }
96
+ if (!/^y(?:es)?$/i.test(answer.trim())) return;
97
+ }
98
+ progress.start("Resetting template files…");
99
+ for (const file of [...files, "public"]) await rm(join(root, file), { recursive: true, force: true });
100
+ await createTemplate(validSlug, false);
101
+ }
102
+
80
103
  async function setupWorkspace(install = true) {
81
104
  const packagePath = join(root, "package.json");
82
105
  if (!existsSync(packagePath)) {
@@ -278,7 +301,7 @@ async function upload() {
278
301
  if (!response.ok) throw new Error(result.error || `Upload failed (${response.status}).`);
279
302
  if (!Number.isInteger(result.id) || result.id < 1) throw new Error("Upload returned an invalid template ID.");
280
303
  await writeFile(join(root, "template.json"), `${JSON.stringify({ id: result.id, slug: template.slug, type: template.type }, null, 2)}\n`);
281
- success(`Uploaded ${template.slug} (unpublished): ${new URL(result.url, apiBase)}`);
304
+ success(`Uploaded ${template.slug} (${result.is_published ? "public" : "unpublished"}): ${new URL(result.url, apiBase)}`);
282
305
  }
283
306
 
284
307
  async function publishUploadedTemplate() {
@@ -758,6 +781,7 @@ try {
758
781
  await access(join(engineRoot, "templates/templateCompiler.ts"));
759
782
 
760
783
  if (command === "create") await createTemplate(slug);
784
+ else if (command === "reset") await resetTemplate(slug);
761
785
  else if (command === "setup") await setupWorkspace(shouldInstall);
762
786
  else if (command === "check") await (slug && engineRoot === root ? checkRepositoryTemplates(slug) : checkProject());
763
787
  else if (command === "dev") await dev();
@@ -1,5 +1,4 @@
1
1
  import { execFile } from "node:child_process";
2
- import { existsSync } from "node:fs";
3
2
  import { createRequire } from "node:module";
4
3
  import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
5
4
  import { tmpdir } from "node:os";
@@ -13,12 +12,9 @@ import { validateTemplateCode } from "./validateTemplateCode.ts";
13
12
 
14
13
  const run = promisify(execFile);
15
14
  const templatesPath = path.dirname(fileURLToPath(import.meta.url));
16
- const packageModulesPath = path.join(path.dirname(templatesPath), "node_modules");
17
- const modulesPath = existsSync(path.join(packageModulesPath, ".bin", "esbuild"))
18
- ? packageModulesPath
19
- : path.join(process.cwd(), "node_modules");
20
-
21
15
  const require = createRequire(import.meta.url);
16
+ const esbuildPath = require.resolve("esbuild/bin/esbuild");
17
+ const modulesPath = path.dirname(path.dirname(require.resolve("react/package.json")));
22
18
 
23
19
  const TEMPLATE_COMPILE_TIMEOUT_MS = 10_000;
24
20
  const TEMPLATE_COMPILE_MAX_BUFFER = 8 * 1024 * 1024;
@@ -46,7 +42,7 @@ export async function compileTemplate(
46
42
  await writeFile(input, withRuntimeImports(code));
47
43
  const nodePath = modulesPath;
48
44
 
49
- await runTemplateCommand(path.join(nodePath, ".bin", "esbuild"), [
45
+ await runTemplateCommand(esbuildPath, [
50
46
  input,
51
47
  "--bundle",
52
48
  "--format=esm",
@@ -121,7 +117,7 @@ window.__nowebRenderTemplate = (element, props) => {
121
117
 
122
118
  const nodePath = modulesPath;
123
119
 
124
- await runTemplateCommand(path.join(nodePath, ".bin", "esbuild"), [
120
+ await runTemplateCommand(esbuildPath, [
125
121
  entry,
126
122
  "--bundle",
127
123
  "--format=iife",
@@ -203,5 +199,9 @@ async function runTemplateCommand(
203
199
  },
204
200
  maxBuffer: TEMPLATE_COMPILE_MAX_BUFFER,
205
201
  timeout: TEMPLATE_COMPILE_TIMEOUT_MS,
202
+ }).catch((error) => {
203
+ throw new Error(error.killed
204
+ ? "Template compilation timed out after 10 seconds."
205
+ : error.stderr?.trim() || "Template compilation failed.");
206
206
  });
207
207
  }