nowebcli 0.2.15 → 0.2.17

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/scripts/noweb.mjs +30 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nowebcli",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
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)) {
@@ -107,9 +130,9 @@ async function checkRepositoryTemplates(value, quiet = false, assetRoot = "/asse
107
130
  return results;
108
131
  }
109
132
 
110
- async function checkProject(quiet = false) {
133
+ async function checkProject(quiet = false, requireCover = true) {
111
134
  if (!quiet) progress.start("Validating and compiling template…");
112
- const result = await buildTemplate(root);
135
+ const result = await buildTemplate(root, undefined, "/assets", requireCover);
113
136
  if (!quiet) success(`✓ ${result.slug} (${result.fields} fields, ${formatBytes(result.js.length + result.css.length)})`);
114
137
  return result;
115
138
  }
@@ -228,7 +251,7 @@ async function dev() {
228
251
  try {
229
252
  const url = new URL(request.url || "/", `http://${request.headers.host || "127.0.0.1"}`);
230
253
  if (url.pathname === "/") {
231
- const template = await checkProject(true);
254
+ const template = await checkProject(true, false);
232
255
  response.writeHead(200, { "Cache-Control": "no-store", "Content-Type": "text/html; charset=utf-8" });
233
256
  response.end(previewHtml(template));
234
257
  success(`✓ rebuilt ${template.slug}`);
@@ -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();