create-hammerspoon2-spoon 0.1.0 → 0.1.1

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/README.md CHANGED
@@ -15,7 +15,9 @@ npx create-hammerspoon2-spoon MySpoon -t js -a "Jane <jane@example.com>" -d "Doe
15
15
  | `-t, --template <javascript\|typescript>` | Template to use (`js` and `ts` also work). Prompts if omitted. |
16
16
  | `-a, --author <"Name <email>">` | Author written to `spoon.json`. Defaults to your `git config` user. |
17
17
  | `-d, --description <text>` | Description written to `spoon.json`. Prompts if omitted. |
18
+ | `-r, --remote <url\|none>` | Git remote to add as `origin` and push the initial commit to. Prompts if omitted; `none` (or empty) keeps the repo local. The remote must be an **empty** repository (no README, license or commits), or the push fails. |
18
19
  | `--no-install` | TypeScript only: skip `npm install`. |
20
+ | `--no-git` | Don't initialize a git repository or make the initial commit. Can't be combined with `--remote`. |
19
21
  | `-h, --help` | Show help. |
20
22
 
21
23
  ## Templates
@@ -23,6 +25,8 @@ npx create-hammerspoon2-spoon MySpoon -t js -a "Jane <jane@example.com>" -d "Doe
23
25
  - **javascript** creates `./MySpoon.spoon2`. There is no build step: edit `init.js`, then double-click `MySpoon.spoon2` in Finder to install it into Hammerspoon 2.
24
26
  - **typescript** creates `./MySpoon`, creates its `build/` and `spoons/` folders, and runs `npm install` (skip with `--no-install`). Edit `src/init.ts`, run `npm run typecheck` after each change, then `npm run package` to build `spoons/MySpoon.spoon2` and double-click it to install.
25
27
 
28
+ Unless you pass `--no-git`, the new project is also initialized as a git repository with a single initial commit. If you give a remote, it's added as `origin` and the commit is pushed (`git push -u origin HEAD`). The remote repository must be empty: create it on GitHub without a README, license or `.gitignore`, otherwise the push is rejected because its history differs from the new project's. If the remote is a GitHub repository (HTTPS or SSH URL), the generated READMEs also get an "Install from GitHub" section showing `hs.spoons.use("MySpoon", { source: "owner/repo", start: true })`; other remotes get no such section, since `hs.spoons.use` only installs from GitHub or a zip URL. If any git step fails, the project is still created and a warning is printed.
29
+
26
30
  Installing does not load a spoon. Load it from your Hammerspoon 2 config with `hs.spoons.load("MySpoon")`.
27
31
 
28
32
  ## Spoon names
@@ -6,7 +6,7 @@
6
6
 
7
7
  Double-click the `{{ name }}.spoon2` bundle to install it into Hammerspoon 2. Installing does not load it.
8
8
 
9
- ## Usage
9
+ {{ repoInstall }}## Usage
10
10
 
11
11
  Load the spoon from your Hammerspoon 2 config:
12
12
 
@@ -14,10 +14,10 @@ The runtime is JavaScriptCore plus Hammerspoon, **not Node**. Do not use Node AP
14
14
 
15
15
  - `npm run typecheck`: type-check without emitting (`tsc --noEmit`). Run this after every code change.
16
16
  - `npm run build`: clean `build/`, compile `src/` with `tsc`, then copy non-TypeScript assets (e.g. `spoon.json`) into `build/`.
17
- - `npm run package`: build, then create `package/<name>.spoon2` from `build/`. `<name>` comes from `spoon.json`.
18
- - `npm run clean`: remove `build/` and `package/`.
17
+ - `npm run package`: build, then create `spoons/<name>.spoon2` from `build/`. `<name>` comes from `spoon.json`.
18
+ - `npm run clean`: remove `build/` and `spoons/`.
19
19
 
20
- `build/` and `package/` are generated and git-ignored. Never edit them by hand.
20
+ `build/` and `spoons/` are generated and git-ignored. Never edit them by hand.
21
21
 
22
22
  ## Layout
23
23
 
@@ -0,0 +1,56 @@
1
+ # {{ name }}
2
+
3
+ {{ description }}
4
+
5
+ A [Hammerspoon 2](https://github.com/cmsj/Hammerspoon2) spoon written in TypeScript. This file is for developing the spoon. The README that ships inside the installed spoon is [src/README.md](src/README.md).
6
+
7
+ ## Getting started
8
+
9
+ ```sh
10
+ npm install # skipped if you used --no-install
11
+ npm run typecheck # type-check after every change
12
+ npm run package # build spoons/{{ name }}.spoon2
13
+ ```
14
+
15
+ Double-click `spoons/{{ name }}.spoon2` to install it into Hammerspoon 2, then load it from your Hammerspoon 2 config:
16
+
17
+ ```js
18
+ hs.spoons.load("{{ name }}");
19
+ ```
20
+
21
+ Installing does not load a spoon, and loading does not start it. Call `hs.spoons["{{ name }}"].start()` to run it.
22
+
23
+ {{ repoInstall }}## Scripts
24
+
25
+ | Script | Description |
26
+ | --- | --- |
27
+ | `npm run typecheck` | Type-check without emitting (`tsc --noEmit`). |
28
+ | `npm run build` | Clean `build/`, compile `src/` with `tsc`, then copy non-TypeScript files (such as `spoon.json`) into `build/`. |
29
+ | `npm run package` | Build, then create `spoons/{{ name }}.spoon2` from `build/`. |
30
+ | `npm run clean` | Remove the contents of `build/` and `spoons/`. |
31
+
32
+ `build/` and `spoons/` are generated: don't edit them by hand.
33
+
34
+ ## Project layout
35
+
36
+ | Path | Purpose |
37
+ | --- | --- |
38
+ | `src/init.ts` | Spoon entry point. Exports the spoon object with `init`, `start`, `stop` and `logLevel`. |
39
+ | `src/modules/` | Supporting modules, such as `Logger.ts`. |
40
+ | `src/spoon.json` | Spoon metadata (`name`, `author`, `version`, `description`). |
41
+ | `src/README.md` | End-user README, copied into the packaged spoon. |
42
+ | `types/` | Typings for the Hammerspoon 2 API (`hammerspoon.d.ts`, auto-generated, don't hand-edit) and CommonJS globals. |
43
+ | `npm_scripts/` | Build and packaging scripts (run under Node). |
44
+
45
+ ## Things to know
46
+
47
+ - The spoon runs in JavaScriptCore inside Hammerspoon, **not Node**. Don't use Node APIs in `src/`. Only `npm_scripts/` runs under Node.
48
+ - The output is CommonJS, because Hammerspoon 2 loads spoons with `require()`.
49
+ - The spoon ships as plain compiled files with no `node_modules`, so don't add runtime dependencies.
50
+ - `init()` can run more than once. Put start-up behavior in `start()` and teardown in `stop()`.
51
+ - Use `createLogger()` from `src/modules/Logger.ts` instead of `console.*`, so the spoon's `logLevel` applies.
52
+ - The `name` in `src/spoon.json` becomes a directory name, so it may only contain letters, digits, `_` and `-`.
53
+
54
+ ## Contributing
55
+
56
+ See [AI_POLICY.md](AI_POLICY.md) and [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md). [CLAUDE.md](CLAUDE.md) has guidance for AI coding assistants.
@@ -6,7 +6,7 @@
6
6
 
7
7
  Double-click the `{{ name }}.spoon2` bundle to install it into Hammerspoon 2. Installing does not load it.
8
8
 
9
- ## Usage
9
+ {{ repoInstall }}## Usage
10
10
 
11
11
  Load the spoon from your Hammerspoon 2 config:
12
12
 
@@ -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": "0.1.1",
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"