create-hammerspoon2-spoon 0.1.0 → 1.0.0-beta.3

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.
@@ -28,7 +28,11 @@ Options:
28
28
  -t, --template <javascript|typescript> Template to use (js / ts accepted). Prompts if omitted.
29
29
  -a, --author <"Name <email>"> Author for spoon.json (default: from git config)
30
30
  -d, --description <text> Description for spoon.json. Prompts if omitted.
31
+ -r, --remote <url|none> Git remote to add as "origin" and push to. Prompts if omitted;
32
+ "none" (or empty) keeps the repo local. The remote must be an
33
+ EMPTY repository (no README, license or commits), or the push fails.
31
34
  --no-install TypeScript only: don't run npm install
35
+ --no-git Don't initialize a git repository or commit
32
36
  -h, --help Show this help
33
37
 
34
38
  <SpoonName> (e.g. MySpoon) must start with a letter, contain only letters, digits, "_" and "-",
@@ -111,6 +115,76 @@ async function chooseDescription(given, name) {
111
115
  }
112
116
  }
113
117
 
118
+ // Returns the remote URL, or "" for none. Uses the -r value when given; otherwise asks when interactive.
119
+ async function chooseRemote(given) {
120
+ const normalize = (v) => (/^(none|no|-)?$/i.test(v.trim()) ? "" : v.trim());
121
+ if (given !== undefined) return normalize(given);
122
+ if (!process.stdin.isTTY) return "";
123
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
124
+ try {
125
+ return normalize(await rl.question("Git remote URL - must be an empty repository (Enter for none/local only): "));
126
+ } finally {
127
+ rl.close();
128
+ }
129
+ }
130
+
131
+ // "owner/repo" for a GitHub remote (https or ssh form), else "". hs.spoons.use() can only install from GitHub or a zip URL.
132
+ function githubSlug(remote) {
133
+ const m = /^(?:https:\/\/(?:www\.)?github\.com\/|git@github\.com:|ssh:\/\/git@github\.com\/)([\w.-]+\/[\w.-]+?)(?:\.git)?\/?$/.exec(remote);
134
+ return m ? m[1] : "";
135
+ }
136
+
137
+ // Markdown for the templates' READMEs ({{ repoInstall }}); empty unless there's a GitHub remote.
138
+ function repoInstallSection(slug, name, templateKey) {
139
+ if (!slug) return "";
140
+ const note =
141
+ templateKey === "typescript"
142
+ ? `Hammerspoon 2 installs the built \`spoons/${name}.spoon2\` folder from the repository, so run \`npm run package\` and commit the result whenever you change the spoon.`
143
+ : `The repository root is the spoon, so there is nothing to build.`;
144
+ return [
145
+ "## Install from GitHub",
146
+ "",
147
+ "Instead of installing by hand, let Hammerspoon 2 download, load and start the spoon from this repository. Put this in your Hammerspoon 2 config:",
148
+ "",
149
+ "```js",
150
+ `hs.spoons.use("${name}", { source: "${slug}", start: true });`,
151
+ "```",
152
+ "",
153
+ `It installs the spoon the first time it runs and just loads it afterwards. Add \`ref: "v1.0.0"\` (a tag or commit) to pin a version. ${note}`,
154
+ "",
155
+ "",
156
+ ].join("\n");
157
+ }
158
+
159
+ function git(args, cwd) {
160
+ execFileSync("git", args, { cwd, stdio: ["ignore", "pipe", "pipe"], encoding: "utf8" });
161
+ }
162
+
163
+ // Initializes a repo, commits the scaffold and pushes when a remote is given. Never throws:
164
+ // the project already exists, so a git problem is reported as a warning with the manual fix.
165
+ function initRepo(target, remote) {
166
+ const result = { committed: false, pushed: false };
167
+ const step = (label, args) => {
168
+ try {
169
+ git(args, target);
170
+ return true;
171
+ } catch (e) {
172
+ const detail = String(e.stderr || e.message).trim().split("\n")[0];
173
+ console.error(`warning: ${label} failed (${detail}).`);
174
+ return false;
175
+ }
176
+ };
177
+ if (!step("git init", ["init"])) return result;
178
+ if (!step("git add", ["add", "-A"])) return result;
179
+ if (!step("git commit", ["commit", "-m", "chore: scaffold spoon from create-hammerspoon2-spoon"])) return result;
180
+ result.committed = true;
181
+ if (remote) {
182
+ if (!step("git remote add", ["remote", "add", "origin", remote])) return result;
183
+ result.pushed = step("git push", ["push", "-u", "origin", "HEAD"]);
184
+ }
185
+ return result;
186
+ }
187
+
114
188
  async function main() {
115
189
  let parsed;
116
190
  try {
@@ -120,7 +194,9 @@ async function main() {
120
194
  template: { type: "string", short: "t" },
121
195
  author: { type: "string", short: "a" },
122
196
  description: { type: "string", short: "d" },
197
+ remote: { type: "string", short: "r" },
123
198
  "no-install": { type: "boolean" },
199
+ "no-git": { type: "boolean" },
124
200
  help: { type: "boolean", short: "h" },
125
201
  },
126
202
  });
@@ -135,9 +211,15 @@ async function main() {
135
211
  if (!NAME_PATTERN.test(name)) fail(`invalid spoon name "${name}": use letters, digits, "_" and "-", starting with a letter (e.g. MySpoon)`);
136
212
  if (RESERVED_NAMES.has(name)) fail(`"${name}" is reserved: it is the name of an hs.spoons member (reserved: ${[...RESERVED_NAMES].join(", ")})`);
137
213
 
214
+ if (values["no-git"] && values.remote && !/^(none|no|-)$/i.test(values.remote.trim())) {
215
+ fail("--remote can't be used with --no-git");
216
+ }
217
+
138
218
  const templateKey = await chooseTemplate(values.template);
139
219
  const template = TEMPLATES[templateKey];
140
220
  const description = (await chooseDescription(values.description, name)).replace(/\s+/g, " ").trim();
221
+ const remote = values["no-git"] ? "" : await chooseRemote(values.remote);
222
+ if (remote.startsWith("-")) fail(`invalid remote "${remote}"`);
141
223
  const tokens = {
142
224
  name,
143
225
  identifier: toIdentifier(name),
@@ -145,6 +227,7 @@ async function main() {
145
227
  packageName: `${name.toLowerCase()}-builder`,
146
228
  author: values.author ?? (gitAuthor() || "Your Name <you@example.com>"),
147
229
  description,
230
+ repoInstall: repoInstallSection(githubSlug(remote), name, templateKey),
148
231
  };
149
232
  const source = join(templatesRoot, template.dir);
150
233
  if (!existsSync(source)) fail(`template files not found at ${source}`);
@@ -174,6 +257,7 @@ async function main() {
174
257
 
175
258
  const rel = `${name}${template.outputSuffix}`;
176
259
  let installed = false;
260
+ let packaged = false;
177
261
  if (templateKey === "typescript") {
178
262
  // npm doesn't ship empty directories, so create the ones the build scripts expect.
179
263
  await mkdir(join(target, "build"), { recursive: true });
@@ -187,9 +271,26 @@ async function main() {
187
271
  console.error("\nwarning: npm install failed; the project was still created. Run it yourself in the project folder.");
188
272
  }
189
273
  }
274
+ // With a remote, others install from the repo, so the first commit must include the built spoon.
275
+ if (remote && installed) {
276
+ console.log("\nBuilding the spoon (npm run package)...\n");
277
+ try {
278
+ execFileSync("npm", ["run", "package"], { cwd: target, stdio: "inherit" });
279
+ packaged = true;
280
+ } catch {
281
+ console.error("\nwarning: npm run package failed; run it and commit spoons/ before others install from the repo.");
282
+ }
283
+ }
190
284
  }
191
285
 
192
- console.log(`\nCreated ${templateKey} spoon "${name}" in ./${rel}\n`);
286
+ let repo = null;
287
+ if (!values["no-git"]) repo = initRepo(target, remote);
288
+
289
+ console.log(`\nCreated ${templateKey} spoon "${name}" in ./${rel}`);
290
+ if (repo?.committed) console.log(`Initialized git repository with an initial commit${repo.pushed ? ` and pushed to ${remote}` : ""}.`);
291
+ if (packaged) console.log(`Built spoons/${name}.spoon2 and included it in the commit.`);
292
+ if (repo?.committed && remote && !repo.pushed) console.log(`Push manually when ready: git push -u origin HEAD`);
293
+ console.log();
193
294
  if (templateKey === "typescript") {
194
295
  console.log(
195
296
  [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-hammerspoon2-spoon",
3
- "version": "0.1.0",
3
+ "version": "1.0.0-beta.3",
4
4
  "description": "Scaffold a new Hammerspoon 2 spoon (JavaScript or TypeScript)",
5
5
  "bin": {
6
6
  "create-hammerspoon2-spoon": "bin/create-hammerspoon2-spoon.mjs"