create-hammerspoon2-spoon 0.0.0-stage → 0.1.0
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/LICENSE +15 -0
- package/README.md +37 -2
- package/Templates/Spoon2TemplateJavascript.spoon2/AI_POLICY.md +5 -0
- package/Templates/Spoon2TemplateJavascript.spoon2/CLAUDE.md +40 -0
- package/Templates/Spoon2TemplateJavascript.spoon2/CODE_OF_CONDUCT.md +5 -0
- package/Templates/Spoon2TemplateJavascript.spoon2/README.md +27 -0
- package/Templates/Spoon2TemplateJavascript.spoon2/_gitignore +2 -0
- package/Templates/Spoon2TemplateJavascript.spoon2/init.js +41 -0
- package/Templates/Spoon2TemplateJavascript.spoon2/modules/Logger.js +34 -0
- package/Templates/Spoon2TemplateJavascript.spoon2/spoon.json +6 -0
- package/Templates/Spoon2TemplateTypescript/AI_POLICY.md +5 -0
- package/Templates/Spoon2TemplateTypescript/CLAUDE.md +45 -0
- package/Templates/Spoon2TemplateTypescript/CODE_OF_CONDUCT.md +5 -0
- package/Templates/Spoon2TemplateTypescript/_gitignore +4 -0
- package/Templates/Spoon2TemplateTypescript/npm_scripts/clean-build.mjs +11 -0
- package/Templates/Spoon2TemplateTypescript/npm_scripts/clean-package.mjs +11 -0
- package/Templates/Spoon2TemplateTypescript/npm_scripts/copy-assets.mjs +23 -0
- package/Templates/Spoon2TemplateTypescript/npm_scripts/package.mjs +41 -0
- package/Templates/Spoon2TemplateTypescript/package.json +25 -0
- package/Templates/Spoon2TemplateTypescript/src/README.md +27 -0
- package/Templates/Spoon2TemplateTypescript/src/init.ts +51 -0
- package/Templates/Spoon2TemplateTypescript/src/modules/Logger.ts +43 -0
- package/Templates/Spoon2TemplateTypescript/src/spoon.json +6 -0
- package/Templates/Spoon2TemplateTypescript/tsconfig.json +27 -0
- package/Templates/Spoon2TemplateTypescript/types/commonjs.d.ts +4 -0
- package/Templates/Spoon2TemplateTypescript/types/hammerspoon.d.ts +12366 -0
- package/bin/create-hammerspoon2-spoon.mjs +219 -0
- package/package.json +30 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andrew S. Parnell
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
|
-
#
|
|
1
|
+
# create-hammerspoon2-spoon
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Scaffold a new [Hammerspoon 2](https://github.com/cmsj/Hammerspoon2) spoon.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npx create-hammerspoon2-spoon MySpoon # prompts for a template and description
|
|
7
|
+
npx create-hammerspoon2-spoon MySpoon -t ts # TypeScript
|
|
8
|
+
npx create-hammerspoon2-spoon MySpoon -t js -a "Jane <jane@example.com>" -d "Does things"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Options
|
|
12
|
+
|
|
13
|
+
| Option | Description |
|
|
14
|
+
| --- | --- |
|
|
15
|
+
| `-t, --template <javascript\|typescript>` | Template to use (`js` and `ts` also work). Prompts if omitted. |
|
|
16
|
+
| `-a, --author <"Name <email>">` | Author written to `spoon.json`. Defaults to your `git config` user. |
|
|
17
|
+
| `-d, --description <text>` | Description written to `spoon.json`. Prompts if omitted. |
|
|
18
|
+
| `--no-install` | TypeScript only: skip `npm install`. |
|
|
19
|
+
| `-h, --help` | Show help. |
|
|
20
|
+
|
|
21
|
+
## Templates
|
|
22
|
+
|
|
23
|
+
- **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
|
+
- **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
|
+
|
|
26
|
+
Installing does not load a spoon. Load it from your Hammerspoon 2 config with `hs.spoons.load("MySpoon")`.
|
|
27
|
+
|
|
28
|
+
## Spoon names
|
|
29
|
+
|
|
30
|
+
The name (for example `MySpoon`) must start with a letter and contain only letters, digits, `_` and `-`, and it can't be the name of an `hs.spoons` member: `load`, `install`, `available`, `update`, `uninstall`, `installed` or `use`.
|
|
31
|
+
|
|
32
|
+
## Development
|
|
33
|
+
|
|
34
|
+
`Templates/` is the published template source. Run the CLI from a checkout with `node bin/create-hammerspoon2-spoon.mjs <SpoonName>`.
|
|
35
|
+
|
|
36
|
+
Template files use `{{ token }}` placeholders that the CLI fills in: `name`, `identifier` (PascalCase, for code), `packageName`, `author` and `description`. An unknown placeholder is an error. Inside `.json` files values are JSON-escaped.
|
|
37
|
+
|
|
38
|
+
Each template stores its ignore file as `_gitignore` (npm drops `.gitignore` files when publishing); the CLI renames it to `.gitignore` in the generated project.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
Guidance for Claude Code when working in this repository.
|
|
4
|
+
|
|
5
|
+
## Background
|
|
6
|
+
|
|
7
|
+
{{ name }} is a [Hammerspoon 2](https://github.com/cmsj/Hammerspoon2) spoon written in plain JavaScript. Hammerspoon 2 is a macOS app that embeds JavaScriptCore and exposes macOS APIs to user JavaScript.
|
|
8
|
+
|
|
9
|
+
**Purpose:** {{ description }}
|
|
10
|
+
|
|
11
|
+
There is no build step, no `package.json`, no TypeScript and no `node_modules`. The files in this repository are the spoon: what you edit is what Hammerspoon 2 loads.
|
|
12
|
+
|
|
13
|
+
The runtime is JavaScriptCore plus Hammerspoon, **not Node**. Do not use Node APIs (`fs`, `path`, `process`, `Buffer`, etc.). Use the `hs.*` APIs that Hammerspoon 2 provides, and standard JavaScript supported by JavaScriptCore.
|
|
14
|
+
|
|
15
|
+
## Commands
|
|
16
|
+
|
|
17
|
+
There are no build, lint or test commands. To check a change, load the spoon in Hammerspoon 2 and watch its console output (set `logLevel` to `"debug"` for more detail).
|
|
18
|
+
|
|
19
|
+
## Layout
|
|
20
|
+
|
|
21
|
+
- `init.js`: the spoon entry point. It builds the spoon object with `init`, `start`, `stop` and a `logLevel` property, and exports it with `module.exports`.
|
|
22
|
+
- `modules/`: supporting modules (currently `Logger.js`).
|
|
23
|
+
- `spoon.json`: spoon metadata (`name`, `author`, `version`, `description`). `hs.spoons.load()` copies `author`, `description` and `version` onto the exported object.
|
|
24
|
+
- `AI_POLICY.md`, `CODE_OF_CONDUCT.md`, `README.md`: project documents.
|
|
25
|
+
|
|
26
|
+
## Conventions
|
|
27
|
+
|
|
28
|
+
- **Module format:** CommonJS. Hammerspoon 2 loads spoons with `require()`, so use `require()` and `module.exports`, not ES `import`/`export`. Include the `.js` extension in relative requires, as `init.js` does.
|
|
29
|
+
- **Logging:** use `createLogger()` from `modules/Logger.js`, not bare `console.*`, so the spoon's `logLevel` applies. Levels, least to most verbose: `error`, `warn`, `info`, `log`, `debug`.
|
|
30
|
+
- **Lifecycle:** `init()` is called by `hs.spoons.load()` and may run more than once, so keep it free of side effects that must only happen once. Put start-up behavior in `start()` and teardown in `stop()`. `start()` and `stop()` return the spoon so calls can be chained.
|
|
31
|
+
- **Spoon name:** keep the `name` in `spoon.json` limited to letters, digits, `_` and `-`, because it is used as a directory name.
|
|
32
|
+
- **Dependencies:** do not add runtime or dev dependencies, and do not introduce a build toolchain or `package.json`. The spoon ships as plain files.
|
|
33
|
+
- **Style:** match the existing code (4-space indentation, double-quoted strings, semicolons). Prefer editing existing files over adding new ones, and do not add features or refactors that were not requested.
|
|
34
|
+
|
|
35
|
+
## Project policies
|
|
36
|
+
|
|
37
|
+
- AI usage: see [AI_POLICY.md](AI_POLICY.md).
|
|
38
|
+
- Conduct: see [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md).
|
|
39
|
+
- Commits: conventional commits, English, imperative, one change each. Do not commit or push unless asked.
|
|
40
|
+
- Co-authorship: if you (Claude) contributed to the files being committed, add a `Co-Authored-By:` line to the commit message, e.g. `Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>`.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# {{ name }}
|
|
2
|
+
|
|
3
|
+
{{ description }}
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Double-click the `{{ name }}.spoon2` bundle to install it into Hammerspoon 2. Installing does not load it.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
Load the spoon from your Hammerspoon 2 config:
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
hs.spoons.load("{{ name }}");
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Nothing starts automatically, so call `start()` when you want it running (and `stop()` to stop it):
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
hs.spoons["{{ name }}"].start();
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
To see more output in the Hammerspoon 2 console, set the log level (`"error"`, `"warn"`, `"info"`, `"log"` or `"debug"`):
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
hs.spoons["{{ name }}"].logLevel = "debug";
|
|
27
|
+
```
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
|
|
2
|
+
const { createLogger } = require("./modules/Logger.js");
|
|
3
|
+
|
|
4
|
+
const Logger = createLogger("{{ name }}");
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
//-----------------------------------------
|
|
8
|
+
// Public API
|
|
9
|
+
//-----------------------------------------
|
|
10
|
+
const {{ identifier }} = {};
|
|
11
|
+
|
|
12
|
+
{{ identifier }}.init = function () {
|
|
13
|
+
// Called automatically by hs.spoons.load(). author/description/version are already set on
|
|
14
|
+
// `this` by this point - safe to log or read, but this could run more than once, so avoid
|
|
15
|
+
// anything that shouldn't happen twice.
|
|
16
|
+
Logger.log(`v${this.version} initialized`)
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
{{ identifier }}.start = function() {
|
|
22
|
+
Logger.debug("start()");
|
|
23
|
+
return {{ identifier }};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
{{ identifier }}.stop = function() {
|
|
27
|
+
Logger.debug("stop()");
|
|
28
|
+
return {{ identifier }};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Properties
|
|
32
|
+
Object.defineProperties({{ identifier }}, {
|
|
33
|
+
// Sets the level on this spoon's loggers: "error" | "warn" | "info" | "log" | "debug"
|
|
34
|
+
logLevel: {
|
|
35
|
+
get: () => Logger.level,
|
|
36
|
+
set: (v) => { Logger.level = v; },
|
|
37
|
+
enumerable: true,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
module.exports = {{ identifier }};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Levelled logger using the JavaScript console's method names and levels.
|
|
2
|
+
// Ordered from least to most verbose: error < warn < info < log < debug
|
|
3
|
+
const LEVELS = { error: 0, warn: 1, info: 2, log: 3, debug: 4 };
|
|
4
|
+
|
|
5
|
+
const DEFAULT_LEVEL = "log";
|
|
6
|
+
|
|
7
|
+
function assertLevel(value) {
|
|
8
|
+
if (!(value in LEVELS)) {
|
|
9
|
+
throw new Error(`invalid log level: ${value} (expected one of ${Object.keys(LEVELS).join(", ")})`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function createLogger(name, initialLevel = DEFAULT_LEVEL) {
|
|
14
|
+
assertLevel(initialLevel);
|
|
15
|
+
let level = initialLevel;
|
|
16
|
+
const emit = (lvl, params) => {
|
|
17
|
+
if (LEVELS[lvl] <= LEVELS[level]) console[lvl](`[${name}]`, ...params);
|
|
18
|
+
};
|
|
19
|
+
const logger = {
|
|
20
|
+
error: (...p) => emit("error", p),
|
|
21
|
+
warn: (...p) => emit("warn", p),
|
|
22
|
+
info: (...p) => emit("info", p),
|
|
23
|
+
log: (...p) => emit("log", p),
|
|
24
|
+
debug: (...p) => emit("debug", p),
|
|
25
|
+
get level() { return level; },
|
|
26
|
+
set level(value) {
|
|
27
|
+
assertLevel(value);
|
|
28
|
+
level = value;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
return logger;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = { createLogger, LEVELS };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
Guidance for Claude Code when working in this repository.
|
|
4
|
+
|
|
5
|
+
## Background
|
|
6
|
+
|
|
7
|
+
{{ name }} is a [Hammerspoon 2](https://github.com/cmsj/Hammerspoon2) spoon written in TypeScript. Hammerspoon 2 is a macOS app that embeds JavaScriptCore and exposes macOS APIs to user JavaScript. The code here is compiled with `tsc` to CommonJS and packaged as a `.spoon2` directory.
|
|
8
|
+
|
|
9
|
+
**Purpose:** {{ description }}
|
|
10
|
+
|
|
11
|
+
The runtime is JavaScriptCore plus Hammerspoon, **not Node**. Do not use Node APIs or add `@types/node` in `src/`. Only the `npm_scripts/*.mjs` build scripts run under Node.
|
|
12
|
+
|
|
13
|
+
## Commands
|
|
14
|
+
|
|
15
|
+
- `npm run typecheck`: type-check without emitting (`tsc --noEmit`). Run this after every code change.
|
|
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/`.
|
|
19
|
+
|
|
20
|
+
`build/` and `package/` are generated and git-ignored. Never edit them by hand.
|
|
21
|
+
|
|
22
|
+
## Layout
|
|
23
|
+
|
|
24
|
+
- `src/init.ts`: the spoon entry point. It exports the spoon object with `init`, `start`, `stop` and a `logLevel` property, using `export =` (CommonJS).
|
|
25
|
+
- `src/modules/`: supporting modules (currently `Logger.ts`).
|
|
26
|
+
- `src/spoon.json`: spoon metadata (`name`, `author`, `version`, `description`). `hs.spoons.load()` copies `author`, `description` and `version` onto the exported object.
|
|
27
|
+
- `types/hammerspoon.d.ts`: Hammerspoon 2 API typings. It is auto-generated ("DO NOT EDIT"); regenerate it from the Hammerspoon 2 repo, don't hand-edit.
|
|
28
|
+
- `types/commonjs.d.ts`: CommonJS globals for the JavaScriptCore runtime.
|
|
29
|
+
- `npm_scripts/`: build and packaging scripts.
|
|
30
|
+
|
|
31
|
+
## Conventions
|
|
32
|
+
|
|
33
|
+
- **Module format:** CommonJS, because Hammerspoon 2 loads spoons with `require()`. Keep `"module": "commonjs"` in `tsconfig.json`.
|
|
34
|
+
- **Strict TypeScript:** `strict`, `noUnusedLocals`, `noUnusedParameters` and `noImplicitReturns` are on. Fix errors rather than suppressing them.
|
|
35
|
+
- **Logging:** use `createLogger()` from `src/modules/Logger.ts`, not bare `console.*`, so the spoon's `logLevel` applies. Levels, least to most verbose: `error`, `warn`, `info`, `log`, `debug`.
|
|
36
|
+
- **Lifecycle:** `init()` is called by `hs.spoons.load()` and may run more than once, so keep it free of side effects that must only happen once. Put start-up behavior in `start()` and teardown in `stop()`. `start()` and `stop()` return the spoon so calls can be chained.
|
|
37
|
+
- **Spoon name:** the `name` in `spoon.json` becomes a directory name in `package.mjs`, which only accepts `[A-Za-z0-9_-]+`. Keep it that way.
|
|
38
|
+
- **Imports:** do not add runtime dependencies. The spoon is shipped as plain compiled files with no `node_modules`. `typescript` is the only dev dependency.
|
|
39
|
+
|
|
40
|
+
## Project policies
|
|
41
|
+
|
|
42
|
+
- AI usage: see [AI_POLICY.md](AI_POLICY.md).
|
|
43
|
+
- Conduct: see [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md).
|
|
44
|
+
- Commits: conventional commits, English, imperative, one change each. Do not commit or push unless asked.
|
|
45
|
+
- Co-authorship: if you (Claude) contributed to the files being committed, add a `Co-Authored-By:` line to the commit message, e.g. `Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>`.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Empties build/ without removing the directory itself.
|
|
2
|
+
import { mkdir, readdir, rm } from "node:fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
const buildDir = join(dirname(fileURLToPath(import.meta.url)), "..", "build");
|
|
7
|
+
|
|
8
|
+
await mkdir(buildDir, { recursive: true });
|
|
9
|
+
for (const entry of await readdir(buildDir)) {
|
|
10
|
+
await rm(join(buildDir, entry), { recursive: true, force: true });
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Empties build/ without removing the directory itself.
|
|
2
|
+
import { mkdir, readdir, rm } from "node:fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
const packageDir = join(dirname(fileURLToPath(import.meta.url)), "..", "spoons");
|
|
7
|
+
|
|
8
|
+
await mkdir(packageDir, { recursive: true });
|
|
9
|
+
for (const entry of await readdir(packageDir)) {
|
|
10
|
+
await rm(join(packageDir, entry), { recursive: true, force: true });
|
|
11
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copies every file in src/ into build/ (directory structure preserved) except the
|
|
2
|
+
// ones tsc handles: TypeScript sources (.ts/.tsx/.mts/.cts, including .d.ts) and
|
|
3
|
+
// macOS .DS_Store files.
|
|
4
|
+
import { cp, mkdir, readdir } from "node:fs/promises";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
|
|
8
|
+
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
9
|
+
const srcDir = join(root, "src");
|
|
10
|
+
const outDir = join(root, "build");
|
|
11
|
+
const TYPESCRIPT_FILE = /\.[cm]?tsx?$/i; // also matches .d.ts, .d.mts, .d.cts
|
|
12
|
+
const IGNORED_FILES = new Set([".DS_Store"]);
|
|
13
|
+
|
|
14
|
+
let copied = 0;
|
|
15
|
+
for (const entry of await readdir(srcDir, { recursive: true, withFileTypes: true })) {
|
|
16
|
+
if (!entry.isFile() || IGNORED_FILES.has(entry.name) || TYPESCRIPT_FILE.test(entry.name)) continue;
|
|
17
|
+
const from = join(entry.parentPath, entry.name);
|
|
18
|
+
const to = join(outDir, from.slice(srcDir.length));
|
|
19
|
+
await mkdir(dirname(to), { recursive: true });
|
|
20
|
+
await cp(from, to);
|
|
21
|
+
copied++;
|
|
22
|
+
}
|
|
23
|
+
console.log(`copied ${copied} asset file(s) to build/`);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Packages the built spoon:
|
|
2
|
+
// 1. empty package/ (the directory itself is kept)
|
|
3
|
+
// 2. create package/<name>.spoon2, where <name> comes from the built spoon.json
|
|
4
|
+
// 3. copy everything from build/ into it
|
|
5
|
+
// Run `npm run build` first (the `package` npm script does this for you).
|
|
6
|
+
import { cp, mkdir, readFile, readdir, rm } from "node:fs/promises";
|
|
7
|
+
import { dirname, join } from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
|
|
10
|
+
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
11
|
+
const buildDir = join(root, "build");
|
|
12
|
+
const packageDir = join(root, "spoons");
|
|
13
|
+
|
|
14
|
+
let name;
|
|
15
|
+
try {
|
|
16
|
+
({ name } = JSON.parse(await readFile(join(buildDir, "spoon.json"), "utf8")));
|
|
17
|
+
} catch {
|
|
18
|
+
throw new Error("build/spoon.json not found - run `npm run build` first");
|
|
19
|
+
}
|
|
20
|
+
// Allowlist: the name becomes a directory name, so reject anything path-like.
|
|
21
|
+
if (typeof name !== "string" || !/^[A-Za-z0-9_-]+$/.test(name)) {
|
|
22
|
+
throw new Error(`spoon.json: "name" must match [A-Za-z0-9_-]+ (got ${JSON.stringify(name)})`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 1. Empty package/ without removing the directory itself.
|
|
26
|
+
await mkdir(packageDir, { recursive: true });
|
|
27
|
+
for (const entry of await readdir(packageDir)) {
|
|
28
|
+
await rm(join(packageDir, entry), { recursive: true, force: true });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 2. Create the .spoon2 directory.
|
|
32
|
+
const spoonDir = join(packageDir, `${name}.spoon2`);
|
|
33
|
+
await mkdir(spoonDir);
|
|
34
|
+
|
|
35
|
+
// 3. Copy build/ into it.
|
|
36
|
+
await cp(buildDir, spoonDir, {
|
|
37
|
+
recursive: true,
|
|
38
|
+
filter: (src) => !src.endsWith(".DS_Store"),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
console.log(`packaged build/ into package/${name}.spoon2`);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{ packageName }}",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"description": "Build tooling for the {{ name }} spoon",
|
|
6
|
+
"author": "{{ author }}",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"package": "npm run build && node npm_scripts/package.mjs",
|
|
9
|
+
"build": "npm run clean-build && npm run compile && npm run copy-assets",
|
|
10
|
+
"clean": "npm run clean-build && npm run clean-package",
|
|
11
|
+
"clean-package": "node npm_scripts/clean-package.mjs",
|
|
12
|
+
"clean-build": "node npm_scripts/clean-build.mjs",
|
|
13
|
+
"compile": "tsc -p tsconfig.json",
|
|
14
|
+
"copy-assets": "node npm_scripts/copy-assets.mjs",
|
|
15
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"hammerspoon",
|
|
19
|
+
"spoon"
|
|
20
|
+
],
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "^7.0.2"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# {{ name }}
|
|
2
|
+
|
|
3
|
+
{{ description }}
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Double-click the `{{ name }}.spoon2` bundle to install it into Hammerspoon 2. Installing does not load it.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
Load the spoon from your Hammerspoon 2 config:
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
hs.spoons.load("{{ name }}");
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Nothing starts automatically, so call `start()` when you want it running (and `stop()` to stop it):
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
hs.spoons["{{ name }}"].start();
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
To see more output in the Hammerspoon 2 console, set the log level (`"error"`, `"warn"`, `"info"`, `"log"` or `"debug"`):
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
hs.spoons["{{ name }}"].logLevel = "debug";
|
|
27
|
+
```
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { createLogger, type LogLevel } from "./modules/Logger";
|
|
2
|
+
|
|
3
|
+
const Logger = createLogger("{{ name }}");
|
|
4
|
+
|
|
5
|
+
// Properties hs.spoons.load() copies onto the exported object from spoon.json.
|
|
6
|
+
interface SpoonMetadata {
|
|
7
|
+
author?: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
version?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface {{ identifier }} extends SpoonMetadata {
|
|
13
|
+
init(): void;
|
|
14
|
+
start(): {{ identifier }};
|
|
15
|
+
stop(): {{ identifier }};
|
|
16
|
+
logLevel: LogLevel;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//-----------------------------------------
|
|
20
|
+
// Public API
|
|
21
|
+
//-----------------------------------------
|
|
22
|
+
const {{ identifier }} = {} as {{ identifier }};
|
|
23
|
+
|
|
24
|
+
{{ identifier }}.init = function (this: {{ identifier }}) {
|
|
25
|
+
// Called automatically by hs.spoons.load(). author/description/version are already set on
|
|
26
|
+
// `this` by this point - safe to log or read, but this could run more than once, so avoid
|
|
27
|
+
// anything that shouldn't happen twice.
|
|
28
|
+
Logger.log(`v${this.version} initialized`);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
{{ identifier }}.start = function () {
|
|
32
|
+
Logger.debug("start()");
|
|
33
|
+
return {{ identifier }};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
{{ identifier }}.stop = function () {
|
|
37
|
+
Logger.debug("stop()");
|
|
38
|
+
return {{ identifier }};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Properties
|
|
42
|
+
Object.defineProperties({{ identifier }}, {
|
|
43
|
+
// Sets the level on this spoon's loggers: "error" | "warn" | "info" | "log" | "debug"
|
|
44
|
+
logLevel: {
|
|
45
|
+
get: () => Logger.level,
|
|
46
|
+
set: (v: LogLevel) => { Logger.level = v; },
|
|
47
|
+
enumerable: true,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
export = {{ identifier }};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Levelled logger using the JavaScript console's method names and levels.
|
|
2
|
+
// Ordered from least to most verbose: error < warn < info < log < debug
|
|
3
|
+
export const LEVELS = { error: 0, warn: 1, info: 2, log: 3, debug: 4 } as const;
|
|
4
|
+
|
|
5
|
+
export type LogLevel = keyof typeof LEVELS;
|
|
6
|
+
|
|
7
|
+
export interface Logger {
|
|
8
|
+
error(...params: unknown[]): void;
|
|
9
|
+
warn(...params: unknown[]): void;
|
|
10
|
+
info(...params: unknown[]): void;
|
|
11
|
+
log(...params: unknown[]): void;
|
|
12
|
+
debug(...params: unknown[]): void;
|
|
13
|
+
level: LogLevel;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const DEFAULT_LEVEL: LogLevel = "log";
|
|
17
|
+
|
|
18
|
+
function assertLevel(value: string): asserts value is LogLevel {
|
|
19
|
+
if (!(value in LEVELS)) {
|
|
20
|
+
throw new Error(`invalid log level: ${value} (expected one of ${Object.keys(LEVELS).join(", ")})`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createLogger(name: string, initialLevel: LogLevel = DEFAULT_LEVEL): Logger {
|
|
25
|
+
assertLevel(initialLevel);
|
|
26
|
+
let level: LogLevel = initialLevel;
|
|
27
|
+
const emit = (lvl: LogLevel, params: unknown[]): void => {
|
|
28
|
+
if (LEVELS[lvl] <= LEVELS[level]) console[lvl](`[${name}]`, ...params);
|
|
29
|
+
};
|
|
30
|
+
const logger: Logger = {
|
|
31
|
+
error: (...p) => emit("error", p),
|
|
32
|
+
warn: (...p) => emit("warn", p),
|
|
33
|
+
info: (...p) => emit("info", p),
|
|
34
|
+
log: (...p) => emit("log", p),
|
|
35
|
+
debug: (...p) => emit("debug", p),
|
|
36
|
+
get level() { return level; },
|
|
37
|
+
set level(value: LogLevel) {
|
|
38
|
+
assertLevel(value);
|
|
39
|
+
level = value;
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
return logger;
|
|
43
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Hammerspoon 2 loads spoons with require(), so emit CommonJS.
|
|
4
|
+
"target": "ES2022",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
|
|
8
|
+
"rootDir": "src",
|
|
9
|
+
"outDir": "build",
|
|
10
|
+
|
|
11
|
+
"strict": true,
|
|
12
|
+
"noUnusedLocals": true,
|
|
13
|
+
"noUnusedParameters": true,
|
|
14
|
+
"noImplicitReturns": true,
|
|
15
|
+
"noFallthroughCasesInSwitch": true,
|
|
16
|
+
|
|
17
|
+
"esModuleInterop": true,
|
|
18
|
+
"resolveJsonModule": true,
|
|
19
|
+
"forceConsistentCasingInFileNames": true,
|
|
20
|
+
"skipLibCheck": true,
|
|
21
|
+
|
|
22
|
+
// No @types/* packages (e.g. @types/node): the runtime is JavaScriptCore +
|
|
23
|
+
// Hammerspoon, not Node. Globals come from types/*.d.ts instead.
|
|
24
|
+
"types": []
|
|
25
|
+
},
|
|
26
|
+
"include": ["src/**/*.ts", "types/**/*.d.ts"]
|
|
27
|
+
}
|