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
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// create-hammerspoon2-spoon: scaffold a Hammerspoon 2 spoon from a template.
|
|
3
|
+
import { cp, mkdir, readFile, readdir, rename, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import { execFileSync } from "node:child_process";
|
|
6
|
+
import { dirname, join, resolve } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { createInterface } from "node:readline/promises";
|
|
9
|
+
import { parseArgs } from "node:util";
|
|
10
|
+
|
|
11
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
const pkgRoot = join(here, "..");
|
|
13
|
+
const templatesRoot = join(pkgRoot, "Templates");
|
|
14
|
+
|
|
15
|
+
const TEMPLATES = {
|
|
16
|
+
javascript: { dir: "Spoon2TemplateJavascript.spoon2", outputSuffix: ".spoon2", label: "JavaScript (no build step)" },
|
|
17
|
+
typescript: { dir: "Spoon2TemplateTypescript", outputSuffix: "", label: "TypeScript (tsc build + packaging)" },
|
|
18
|
+
};
|
|
19
|
+
const ALIASES = { js: "javascript", ts: "typescript" };
|
|
20
|
+
const NAME_PATTERN = /^[A-Za-z][A-Za-z0-9_-]*$/;
|
|
21
|
+
// Names of hs.spoons' own members: Hammerspoon 2 refuses to load a spoon with one of these names.
|
|
22
|
+
const RESERVED_NAMES = new Set(["load", "install", "available", "update", "uninstall", "installed", "use"]);
|
|
23
|
+
const TEXT_FILE = /\.(js|mjs|ts|json|md|txt)$|(^|\/)(_gitignore|\.gitignore)$/;
|
|
24
|
+
|
|
25
|
+
const USAGE = `Usage: create-hammerspoon2-spoon <SpoonName> [options]
|
|
26
|
+
|
|
27
|
+
Options:
|
|
28
|
+
-t, --template <javascript|typescript> Template to use (js / ts accepted). Prompts if omitted.
|
|
29
|
+
-a, --author <"Name <email>"> Author for spoon.json (default: from git config)
|
|
30
|
+
-d, --description <text> Description for spoon.json. Prompts if omitted.
|
|
31
|
+
--no-install TypeScript only: don't run npm install
|
|
32
|
+
-h, --help Show this help
|
|
33
|
+
|
|
34
|
+
<SpoonName> (e.g. MySpoon) must start with a letter, contain only letters, digits, "_" and "-",
|
|
35
|
+
and not be the name of an hs.spoons member (load, install, available, update, uninstall, installed, use).`;
|
|
36
|
+
|
|
37
|
+
function fail(message) {
|
|
38
|
+
console.error(`error: ${message}`);
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function toIdentifier(name) {
|
|
43
|
+
return name.split(/[-_]+/).filter(Boolean).map((s) => s[0].toUpperCase() + s.slice(1)).join("");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function gitAuthor() {
|
|
47
|
+
const get = (key) => {
|
|
48
|
+
try {
|
|
49
|
+
return execFileSync("git", ["config", "--get", key], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
50
|
+
} catch {
|
|
51
|
+
return "";
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const name = get("user.name");
|
|
55
|
+
const email = get("user.email");
|
|
56
|
+
if (!name) return "";
|
|
57
|
+
return email ? `${name} <${email}>` : name;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function walk(dir) {
|
|
61
|
+
const files = [];
|
|
62
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
63
|
+
const p = join(dir, entry.name);
|
|
64
|
+
if (entry.isDirectory()) files.push(...(await walk(p)));
|
|
65
|
+
else files.push(p);
|
|
66
|
+
}
|
|
67
|
+
return files;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Replaces {{ token }} placeholders. Unknown tokens throw so a typo in a template can't ship.
|
|
71
|
+
// Inside .json files values are JSON-escaped (without the surrounding quotes) so the file stays valid.
|
|
72
|
+
function render(content, file, tokens) {
|
|
73
|
+
const isJson = file.endsWith(".json");
|
|
74
|
+
return content.replace(/\{\{\s*(\w+)\s*\}\}/g, (_match, key) => {
|
|
75
|
+
if (!Object.hasOwn(tokens, key)) throw new Error(`unknown placeholder {{ ${key} }} in ${file}`);
|
|
76
|
+
return isJson ? JSON.stringify(tokens[key]).slice(1, -1) : tokens[key];
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function chooseTemplate(given) {
|
|
81
|
+
if (given) {
|
|
82
|
+
const key = ALIASES[given.toLowerCase()] ?? given.toLowerCase();
|
|
83
|
+
if (!TEMPLATES[key]) fail(`unknown template "${given}" (use javascript or typescript)`);
|
|
84
|
+
return key;
|
|
85
|
+
}
|
|
86
|
+
if (!process.stdin.isTTY) return "javascript";
|
|
87
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
88
|
+
try {
|
|
89
|
+
const keys = Object.keys(TEMPLATES);
|
|
90
|
+
keys.forEach((k, i) => console.log(` ${i + 1}) ${TEMPLATES[k].label}`));
|
|
91
|
+
const answer = (await rl.question("Template [1]: ")).trim();
|
|
92
|
+
const index = answer === "" ? 0 : Number(answer) - 1;
|
|
93
|
+
if (!Number.isInteger(index) || !keys[index]) fail(`invalid choice "${answer}"`);
|
|
94
|
+
return keys[index];
|
|
95
|
+
} finally {
|
|
96
|
+
rl.close();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Uses the -d value when given; otherwise asks (Enter accepts the default) when interactive.
|
|
101
|
+
async function chooseDescription(given, name) {
|
|
102
|
+
const fallback = `The ${name} spoon`;
|
|
103
|
+
if (given) return given;
|
|
104
|
+
if (!process.stdin.isTTY) return fallback;
|
|
105
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
106
|
+
try {
|
|
107
|
+
const answer = (await rl.question(`Description [${fallback}]: `)).trim();
|
|
108
|
+
return answer === "" ? fallback : answer;
|
|
109
|
+
} finally {
|
|
110
|
+
rl.close();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function main() {
|
|
115
|
+
let parsed;
|
|
116
|
+
try {
|
|
117
|
+
parsed = parseArgs({
|
|
118
|
+
allowPositionals: true,
|
|
119
|
+
options: {
|
|
120
|
+
template: { type: "string", short: "t" },
|
|
121
|
+
author: { type: "string", short: "a" },
|
|
122
|
+
description: { type: "string", short: "d" },
|
|
123
|
+
"no-install": { type: "boolean" },
|
|
124
|
+
help: { type: "boolean", short: "h" },
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
} catch (e) {
|
|
128
|
+
fail(`${e.message}\n\n${USAGE}`);
|
|
129
|
+
}
|
|
130
|
+
const { values, positionals } = parsed;
|
|
131
|
+
if (values.help) return console.log(USAGE);
|
|
132
|
+
if (positionals.length !== 1) fail(`expected exactly one <SpoonName>\n\n${USAGE}`);
|
|
133
|
+
|
|
134
|
+
const name = positionals[0];
|
|
135
|
+
if (!NAME_PATTERN.test(name)) fail(`invalid spoon name "${name}": use letters, digits, "_" and "-", starting with a letter (e.g. MySpoon)`);
|
|
136
|
+
if (RESERVED_NAMES.has(name)) fail(`"${name}" is reserved: it is the name of an hs.spoons member (reserved: ${[...RESERVED_NAMES].join(", ")})`);
|
|
137
|
+
|
|
138
|
+
const templateKey = await chooseTemplate(values.template);
|
|
139
|
+
const template = TEMPLATES[templateKey];
|
|
140
|
+
const description = (await chooseDescription(values.description, name)).replace(/\s+/g, " ").trim();
|
|
141
|
+
const tokens = {
|
|
142
|
+
name,
|
|
143
|
+
identifier: toIdentifier(name),
|
|
144
|
+
// The TypeScript template's package.json is the build project, not the spoon. npm names must be lowercase.
|
|
145
|
+
packageName: `${name.toLowerCase()}-builder`,
|
|
146
|
+
author: values.author ?? (gitAuthor() || "Your Name <you@example.com>"),
|
|
147
|
+
description,
|
|
148
|
+
};
|
|
149
|
+
const source = join(templatesRoot, template.dir);
|
|
150
|
+
if (!existsSync(source)) fail(`template files not found at ${source}`);
|
|
151
|
+
|
|
152
|
+
const target = resolve(process.cwd(), `${name}${template.outputSuffix}`);
|
|
153
|
+
if (existsSync(target) && (await readdir(target)).length > 0) fail(`${target} already exists and is not empty`);
|
|
154
|
+
|
|
155
|
+
await mkdir(target, { recursive: true });
|
|
156
|
+
await cp(source, target, {
|
|
157
|
+
recursive: true,
|
|
158
|
+
filter: (src) => !src.endsWith(".DS_Store") && !src.includes("node_modules"),
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
for (const file of await walk(target)) {
|
|
162
|
+
let path = file;
|
|
163
|
+
// Templates store .gitignore as _gitignore because npm drops files with that name when publishing.
|
|
164
|
+
if (file.endsWith("/_gitignore")) {
|
|
165
|
+
path = join(dirname(file), ".gitignore");
|
|
166
|
+
await rename(file, path);
|
|
167
|
+
}
|
|
168
|
+
const rel = path.slice(target.length + 1);
|
|
169
|
+
if (!TEXT_FILE.test(rel) || rel.endsWith("hammerspoon.d.ts")) continue;
|
|
170
|
+
const original = await readFile(path, "utf8");
|
|
171
|
+
const updated = render(original, rel, tokens);
|
|
172
|
+
if (updated !== original) await writeFile(path, updated);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const rel = `${name}${template.outputSuffix}`;
|
|
176
|
+
let installed = false;
|
|
177
|
+
if (templateKey === "typescript") {
|
|
178
|
+
// npm doesn't ship empty directories, so create the ones the build scripts expect.
|
|
179
|
+
await mkdir(join(target, "build"), { recursive: true });
|
|
180
|
+
await mkdir(join(target, "spoons"), { recursive: true });
|
|
181
|
+
if (!values["no-install"]) {
|
|
182
|
+
console.log("\nInstalling dependencies (npm install)...\n");
|
|
183
|
+
try {
|
|
184
|
+
execFileSync("npm", ["install"], { cwd: target, stdio: "inherit" });
|
|
185
|
+
installed = true;
|
|
186
|
+
} catch {
|
|
187
|
+
console.error("\nwarning: npm install failed; the project was still created. Run it yourself in the project folder.");
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
console.log(`\nCreated ${templateKey} spoon "${name}" in ./${rel}\n`);
|
|
193
|
+
if (templateKey === "typescript") {
|
|
194
|
+
console.log(
|
|
195
|
+
[
|
|
196
|
+
"Next steps:",
|
|
197
|
+
` cd ${rel}`,
|
|
198
|
+
...(installed ? [] : [" npm install"]),
|
|
199
|
+
" Edit src/init.ts",
|
|
200
|
+
" npm run typecheck # after every change",
|
|
201
|
+
` npm run package # builds spoons/${name}.spoon2`,
|
|
202
|
+
` Double-click spoons/${name}.spoon2 to install it into Hammerspoon 2`,
|
|
203
|
+
` In your Hammerspoon 2 config: hs.spoons.load("${name}")`,
|
|
204
|
+
].join("\n"),
|
|
205
|
+
);
|
|
206
|
+
} else {
|
|
207
|
+
console.log(
|
|
208
|
+
[
|
|
209
|
+
"Next steps:",
|
|
210
|
+
` cd ${rel}`,
|
|
211
|
+
" Edit init.js",
|
|
212
|
+
` Double-click ${rel} in Finder to install it into Hammerspoon 2`,
|
|
213
|
+
` In your Hammerspoon 2 config: hs.spoons.load("${name}")`,
|
|
214
|
+
].join("\n"),
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
main().catch((e) => fail(e.message));
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-hammerspoon2-spoon",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Scaffold a new Hammerspoon 2 spoon (JavaScript or TypeScript)",
|
|
5
|
+
"bin": {
|
|
6
|
+
"create-hammerspoon2-spoon": "bin/create-hammerspoon2-spoon.mjs"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"Templates"
|
|
12
|
+
],
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=20"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"hammerspoon",
|
|
18
|
+
"hammerspoon2",
|
|
19
|
+
"spoon",
|
|
20
|
+
"create",
|
|
21
|
+
"scaffold"
|
|
22
|
+
],
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/asp55/create-hammerspoon2-spoon.git"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/asp55/create-hammerspoon2-spoon#readme",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/asp55/create-hammerspoon2-spoon/issues"
|
|
31
|
+
}
|
|
32
|
+
}
|