@opentf/create-afw 0.1.0 → 0.3.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 +21 -0
- package/README.md +37 -13
- package/index.js +274 -80
- package/package.json +11 -4
- package/templates.js +483 -118
- package/versions.js +51 -13
- package/versions.json +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Open Tech Foundation and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -3,30 +3,54 @@
|
|
|
3
3
|
Create an [AFW](https://afw.opentechf.org) app on **Vite and Node**:
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
|
-
npm create @opentf/afw
|
|
6
|
+
npm create @opentf/afw@latest
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
The prompts ask for a project name and a template, then only what that template needs
|
|
10
|
+
(arrow keys and Enter). The dependencies are installed and the command to start the
|
|
11
|
+
dev server is shown. Nothing is started for you.
|
|
12
|
+
|
|
13
|
+
| Prompt | Asked for | Choices |
|
|
14
|
+
| --- | --- | --- |
|
|
15
|
+
| Template | every project | `app`, `docs`, `library` |
|
|
16
|
+
| Rendering | `app` | `spa` (in the browser), `ssr` (per request, hydrated), `ssg` (prerendered) |
|
|
17
|
+
| Server | `app`, unless `ssr` | API routes, middleware, loaders and server functions on Node: yes or no |
|
|
18
|
+
| Styling | `app`, `library` | CSS, or Tailwind CSS |
|
|
19
|
+
| Language | every project | TypeScript (default), JavaScript |
|
|
20
|
+
|
|
21
|
+
Or pass the choices:
|
|
10
22
|
|
|
11
23
|
```sh
|
|
12
|
-
npm create @opentf/afw my-app -- --template
|
|
24
|
+
npm create @opentf/afw@latest my-app -- --template app --render ssg --server --tailwind
|
|
25
|
+
npm create @opentf/afw@latest my-docs -- --template docs-js
|
|
13
26
|
```
|
|
14
27
|
|
|
15
28
|
| Option | Values |
|
|
16
29
|
| --- | --- |
|
|
17
|
-
| `--template` | `
|
|
18
|
-
| `--
|
|
19
|
-
| `--
|
|
30
|
+
| `-t`, `--template` | `app`, `docs`, `library`; add `-ts` or `-js` for the language (`app-js`) |
|
|
31
|
+
| `-r`, `--render` | `spa` (default), `ssr`, `ssg` — `app` only |
|
|
32
|
+
| `--server`, `--no-server` | Add a Node server, or not (default) — `app` only; `ssr` always has one |
|
|
33
|
+
| `-s`, `--styling` | `css` (default) or `tailwind`; `--tailwind` for short — `app` and `library` |
|
|
34
|
+
| `-l`, `--language` | `ts` (default) or `js` |
|
|
35
|
+
| `--overwrite` | Remove the files in a non-empty target directory first |
|
|
36
|
+
| `--no-install` | Skip installing dependencies |
|
|
37
|
+
| `--no-interactive` | Never ask; use defaults for anything not given (also the default without a terminal) |
|
|
20
38
|
|
|
21
39
|
| Template | What you get |
|
|
22
40
|
| --- | --- |
|
|
23
|
-
| `
|
|
24
|
-
| `
|
|
25
|
-
| `
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
41
|
+
| `app` | File-based pages and a component test; with a server, an API route, middleware, a loader and server functions |
|
|
42
|
+
| `docs` | An [`@opentf/afw-docs`](https://afw.opentechf.org/packages/afw-docs) site: MDX docs and a blog, prerendered, with search, RSS/Atom feeds, `sitemap.xml` and `llms.txt` |
|
|
43
|
+
| `library` | Components published as JSX source (`index.js` and `src/`), `@opentf/afw` as a peer dependency, a preview app and a test |
|
|
44
|
+
|
|
45
|
+
The names of earlier releases still work: `spa`, `fullstack` (`app --server`), `ssr`
|
|
46
|
+
and `ssg`.
|
|
47
|
+
|
|
48
|
+
- A non-empty target directory: choose to cancel, remove its files, or ignore them.
|
|
49
|
+
- A folder name that is not a valid package name: you are asked for one (`My App` → `my-app`).
|
|
50
|
+
- Apps and docs sites have `dev`, `build` and `test` (Vitest) scripts, and either `start`
|
|
51
|
+
(the built Node server) or `preview`. A library has `dev` (its preview) and `test`.
|
|
52
|
+
TypeScript projects add `typecheck`.
|
|
53
|
+
- New projects depend on the current AFW release from the npm registry.
|
|
30
54
|
|
|
31
55
|
For the esdev + ES-Runtime workflow, use `esdev create` instead.
|
|
32
56
|
|
package/index.js
CHANGED
|
@@ -1,133 +1,327 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// `npm create @opentf/afw` — scaffold an AFW app on Vite and Node.
|
|
3
3
|
//
|
|
4
|
-
// npm create @opentf/afw
|
|
5
|
-
// npm create @opentf/afw my-app -- --template
|
|
4
|
+
// npm create @opentf/afw@latest
|
|
5
|
+
// npm create @opentf/afw@latest my-app -- --template app --render ssr --styling tailwind
|
|
6
6
|
//
|
|
7
7
|
// For the esdev + ES-Runtime workflow, use `esdev create` instead.
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import { spawn } from "node:child_process";
|
|
10
|
+
import { mkdir, readdir, realpath, rm, writeFile } from "node:fs/promises";
|
|
10
11
|
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
12
|
+
import { argv, cwd, env, exit, stdin, stdout } from "node:process";
|
|
13
|
+
import { fileURLToPath } from "node:url";
|
|
14
|
+
import { styleText } from "node:util";
|
|
13
15
|
|
|
14
|
-
import
|
|
16
|
+
import * as prompts from "@clack/prompts";
|
|
17
|
+
import { slugify } from "@opentf/std";
|
|
18
|
+
|
|
19
|
+
import { LEGACY_TEMPLATES, RENDERINGS, STYLINGS, TEMPLATES, projectFiles } from "./templates.js";
|
|
15
20
|
import { afwVersions } from "./versions.js";
|
|
16
21
|
|
|
17
|
-
const
|
|
22
|
+
const DEFAULT_DIRECTORY = "afw-app";
|
|
23
|
+
const LANGUAGES = { ts: "TypeScript", js: "JavaScript" };
|
|
24
|
+
const TEMPLATE_NAMES = Object.keys(TEMPLATES);
|
|
25
|
+
const SERVER_HINT = "API routes, middleware, loaders and server functions on Node";
|
|
26
|
+
const VALID_NAME = /^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/;
|
|
27
|
+
|
|
28
|
+
const HELP = `Usage: npm create @opentf/afw [directory] -- [options]
|
|
18
29
|
|
|
19
|
-
|
|
30
|
+
Create an AFW project on Vite and Node. With no options, you are asked for each choice.
|
|
20
31
|
|
|
21
32
|
Options:
|
|
22
|
-
--template
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
--
|
|
33
|
+
-t, --template <name> ${TEMPLATE_NAMES.join(", ")} (add -ts or -js to pick the language)
|
|
34
|
+
-r, --render <mode> app: ${Object.keys(RENDERINGS).join(", ")}
|
|
35
|
+
--server app: add ${SERVER_HINT}
|
|
36
|
+
--no-server app: no server (not with --render ssr, which always has one)
|
|
37
|
+
-s, --styling <name> app and library: ${Object.keys(STYLINGS).join(", ")} (--tailwind for short)
|
|
38
|
+
-l, --language <lang> ts or js
|
|
39
|
+
--overwrite Remove the files in a non-empty target directory first
|
|
40
|
+
--no-install Skip installing dependencies
|
|
41
|
+
--no-interactive Never ask; use the defaults for anything not given
|
|
42
|
+
-h, --help Show this help
|
|
43
|
+
|
|
44
|
+
Templates:
|
|
45
|
+
${TEMPLATE_NAMES.map((t) => ` ${t.padEnd(10)} ${TEMPLATES[t]}`).join("\n")}
|
|
46
|
+
|
|
47
|
+
Renderings (app):
|
|
48
|
+
${Object.entries(RENDERINGS).map(([r, label]) => ` ${r.padEnd(10)} ${label}`).join("\n")}
|
|
49
|
+
|
|
50
|
+
Shorthands from earlier releases: ${Object.entries(LEGACY_TEMPLATES).map(([name, t]) => `${name} = app --render ${t.render}${t.server ? " --server" : ""}`).join("; ")}.
|
|
27
51
|
|
|
28
52
|
For the esdev + ES-Runtime workflow, use \`esdev create\`.`;
|
|
29
53
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
54
|
+
/** Parse the command line. Throws on an unknown option or value, or conflicting ones. */
|
|
55
|
+
export function parseArgs(args) {
|
|
56
|
+
const options = {
|
|
57
|
+
directory: undefined, template: undefined, render: undefined, server: undefined, styling: undefined,
|
|
58
|
+
language: undefined, overwrite: false, install: true, interactive: undefined, help: false,
|
|
59
|
+
};
|
|
60
|
+
const value = (i, flag) => {
|
|
61
|
+
const next = args[i + 1];
|
|
62
|
+
if (next === undefined || next.startsWith("-")) throw new Error(`${flag} needs a value`);
|
|
63
|
+
return next;
|
|
64
|
+
};
|
|
65
|
+
for (let i = 0; i < args.length; i++) {
|
|
66
|
+
const arg = args[i];
|
|
67
|
+
const [flag, inline] = arg.startsWith("--") && arg.includes("=") ? [arg.slice(0, arg.indexOf("=")), arg.slice(arg.indexOf("=") + 1)] : [arg, undefined];
|
|
68
|
+
if (flag === "-h" || flag === "--help") options.help = true;
|
|
69
|
+
else if (flag === "-t" || flag === "--template") options.template = inline ?? value(i++, flag);
|
|
70
|
+
else if (flag === "-r" || flag === "--render") options.render = inline ?? value(i++, flag);
|
|
71
|
+
else if (flag === "--server") options.server = true;
|
|
72
|
+
else if (flag === "--no-server") options.server = false;
|
|
73
|
+
else if (flag === "-s" || flag === "--styling") options.styling = inline ?? value(i++, flag);
|
|
74
|
+
else if (flag === "--tailwind") options.styling = "tailwind";
|
|
75
|
+
else if (flag === "-l" || flag === "--language") options.language = inline ?? value(i++, flag);
|
|
76
|
+
else if (flag === "--ts" || flag === "--typescript") options.language = "ts";
|
|
77
|
+
else if (flag === "--js") options.language = "js";
|
|
78
|
+
else if (flag === "--overwrite") options.overwrite = true;
|
|
79
|
+
else if (flag === "--no-install") options.install = false;
|
|
80
|
+
else if (flag === "--no-interactive" || flag === "-y" || flag === "--yes") options.interactive = false;
|
|
81
|
+
else if (flag === "--interactive") options.interactive = true;
|
|
82
|
+
else if (flag.startsWith("-")) throw new Error(`Unknown option: ${arg}`);
|
|
42
83
|
else if (options.directory === undefined) options.directory = arg;
|
|
43
84
|
else throw new Error(`Unexpected argument: ${arg}`);
|
|
44
85
|
}
|
|
45
|
-
if (options.template !== undefined
|
|
46
|
-
|
|
86
|
+
if (options.template !== undefined) {
|
|
87
|
+
const [, base, lang] = options.template.match(/^(.*?)(?:-(ts|js))?$/);
|
|
88
|
+
if (!TEMPLATES[base] && !LEGACY_TEMPLATES[base]) {
|
|
89
|
+
throw new Error(`Unknown template "${options.template}". Choose one of: ${TEMPLATE_NAMES.join(", ")} (optionally with -ts or -js).`);
|
|
90
|
+
}
|
|
91
|
+
if (lang) {
|
|
92
|
+
if (options.language && options.language !== lang) throw new Error(`--template ${options.template} conflicts with --language ${options.language}`);
|
|
93
|
+
options.language = lang;
|
|
94
|
+
}
|
|
95
|
+
const legacy = LEGACY_TEMPLATES[base];
|
|
96
|
+
if (legacy) {
|
|
97
|
+
// An earlier release's name: a shorthand for app with its rendering and server.
|
|
98
|
+
if (options.render !== undefined && options.render !== legacy.render) throw new Error(`--template ${base} conflicts with --render ${options.render}`);
|
|
99
|
+
if (legacy.server && options.server === false) throw new Error(`--template ${base} conflicts with --no-server`);
|
|
100
|
+
options.template = legacy.template;
|
|
101
|
+
options.render = legacy.render;
|
|
102
|
+
options.server ??= legacy.server;
|
|
103
|
+
} else {
|
|
104
|
+
options.template = base;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (options.render !== undefined && !RENDERINGS[options.render]) {
|
|
108
|
+
throw new Error(`Unknown rendering "${options.render}". Choose one of: ${Object.keys(RENDERINGS).join(", ")}.`);
|
|
109
|
+
}
|
|
110
|
+
if (options.styling !== undefined && !STYLINGS[options.styling]) {
|
|
111
|
+
throw new Error(`Unknown styling "${options.styling}". Choose one of: ${Object.keys(STYLINGS).join(", ")}.`);
|
|
112
|
+
}
|
|
113
|
+
if (options.language !== undefined && !LANGUAGES[options.language]) {
|
|
114
|
+
throw new Error(`Unknown language "${options.language}". Choose ts or js.`);
|
|
115
|
+
}
|
|
116
|
+
if (options.render === "ssr" && options.server === false) throw new Error("--render ssr always has a server; drop --no-server");
|
|
117
|
+
if (options.template && options.template !== "app") {
|
|
118
|
+
if (options.render !== undefined) throw new Error(`--render applies to the app template, not ${options.template}`);
|
|
119
|
+
if (options.server !== undefined) throw new Error(`--server applies to the app template, not ${options.template}`);
|
|
47
120
|
}
|
|
48
|
-
if (options.
|
|
49
|
-
throw new Error(
|
|
121
|
+
if (options.template === "docs" && options.styling !== undefined) {
|
|
122
|
+
throw new Error("--styling applies to the app and library templates; the docs template uses the afw-docs theme");
|
|
50
123
|
}
|
|
51
124
|
return options;
|
|
52
125
|
}
|
|
53
126
|
|
|
54
127
|
/** The package manager that ran us (`npm create`, `pnpm create`, …). */
|
|
55
|
-
export function packageManager(userAgent =
|
|
56
|
-
const name = userAgent.split("/")[0];
|
|
57
|
-
return ["pnpm", "yarn", "bun"].includes(name) ? name : "npm";
|
|
128
|
+
export function packageManager(userAgent = env.npm_config_user_agent ?? "") {
|
|
129
|
+
const name = userAgent.split(" ")[0]?.split("/")[0];
|
|
130
|
+
return ["pnpm", "yarn", "bun", "deno"].includes(name) ? name : "npm";
|
|
58
131
|
}
|
|
59
132
|
|
|
60
|
-
|
|
133
|
+
/** Whether `name` can be an npm package name. */
|
|
134
|
+
export const isValidPackageName = (name) => VALID_NAME.test(name);
|
|
135
|
+
|
|
136
|
+
/** A valid package name derived from a folder name (`My App` → `my-app`). */
|
|
137
|
+
export const toValidPackageName = (name) => slugify(name) || "afw-app";
|
|
138
|
+
|
|
139
|
+
async function isEmpty(dir) {
|
|
61
140
|
try {
|
|
62
|
-
return (await readdir(dir)).
|
|
141
|
+
return (await readdir(dir)).every((entry) => entry === ".git");
|
|
63
142
|
} catch {
|
|
64
143
|
return true;
|
|
65
144
|
}
|
|
66
145
|
}
|
|
67
146
|
|
|
68
|
-
async function
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async function choose(rl, question, choices, fallback) {
|
|
74
|
-
const keys = Object.keys(choices);
|
|
75
|
-
stdout.write(`${question}\n${keys.map((k, i) => ` ${i + 1}. ${k.padEnd(10)} ${choices[k]}`).join("\n")}\n`);
|
|
76
|
-
for (;;) {
|
|
77
|
-
const answer = (await rl.question(`Choose 1-${keys.length} (${fallback}): `)).trim();
|
|
78
|
-
if (!answer) return fallback;
|
|
79
|
-
const picked = keys[Number(answer) - 1] ?? (choices[answer] ? answer : undefined);
|
|
80
|
-
if (picked) return picked;
|
|
147
|
+
async function emptyDir(dir) {
|
|
148
|
+
for (const entry of await readdir(dir)) {
|
|
149
|
+
if (entry !== ".git") await rm(join(dir, entry), { recursive: true, force: true });
|
|
81
150
|
}
|
|
82
151
|
}
|
|
83
152
|
|
|
84
|
-
/**
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
153
|
+
/**
|
|
154
|
+
* Scaffold a project. `existing`: what to do with a non-empty target — `"error"`
|
|
155
|
+
* (default), `"remove"` its files first, or `"ignore"` and write beside them.
|
|
156
|
+
*/
|
|
157
|
+
export async function create({
|
|
158
|
+
directory = DEFAULT_DIRECTORY, template = "app", render = "spa", server = false, styling = "css", language = "ts",
|
|
159
|
+
packageName, existing = "error", cwd: base = cwd(),
|
|
160
|
+
} = {}) {
|
|
161
|
+
const target = resolve(base, directory);
|
|
162
|
+
const name = packageName ?? basename(target);
|
|
163
|
+
if (!isValidPackageName(name)) throw new Error(`"${name}" is not a valid package name: use lowercase letters, digits, "-", "_" or ".".`);
|
|
164
|
+
if (!(await isEmpty(target))) {
|
|
165
|
+
if (existing === "remove") await emptyDir(target);
|
|
166
|
+
else if (existing !== "ignore") throw new Error(`${target} is not empty. Use --overwrite to remove its files.`);
|
|
167
|
+
}
|
|
168
|
+
const files = projectFiles({ name, template, render, server, styling, typescript: language === "ts", versions: await afwVersions() });
|
|
91
169
|
for (const [path, contents] of Object.entries(files)) {
|
|
92
170
|
const file = join(target, path);
|
|
93
171
|
await mkdir(dirname(file), { recursive: true });
|
|
94
172
|
await writeFile(file, contents);
|
|
95
173
|
}
|
|
96
|
-
return { target, files: Object.keys(files) };
|
|
174
|
+
return { target, name, files: Object.keys(files) };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** The commands to run next, for the package manager in use. */
|
|
178
|
+
export function nextSteps({ directory, pm, installed = false }) {
|
|
179
|
+
const run = (script) => (pm === "npm" || pm === "bun" ? `${pm} run ${script}` : `${pm} ${script}`);
|
|
180
|
+
return [
|
|
181
|
+
...(directory === "." ? [] : [`cd ${/\s/.test(directory) ? JSON.stringify(directory) : directory}`]),
|
|
182
|
+
...(installed ? [] : [`${pm} install`]),
|
|
183
|
+
run("dev"),
|
|
184
|
+
];
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Run `pm install` in `dir`, its output shown. Resolves whether it succeeded. */
|
|
188
|
+
export function installDependencies(pm, dir) {
|
|
189
|
+
return new Promise((done) => {
|
|
190
|
+
const child = spawn(pm, ["install"], { cwd: dir, stdio: "inherit", shell: process.platform === "win32" });
|
|
191
|
+
child.once("error", () => done(false));
|
|
192
|
+
child.once("close", (code) => done(code === 0));
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Exit on Ctrl+C or Escape, as create-vite does.
|
|
197
|
+
function cancelled() {
|
|
198
|
+
prompts.cancel("Operation cancelled.");
|
|
199
|
+
exit(0);
|
|
97
200
|
}
|
|
98
201
|
|
|
99
|
-
async
|
|
100
|
-
const
|
|
202
|
+
const ask = async (prompt) => {
|
|
203
|
+
const answer = await prompt;
|
|
204
|
+
if (prompts.isCancel(answer)) cancelled();
|
|
205
|
+
return answer;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
export async function main(args = argv.slice(2)) {
|
|
209
|
+
let options;
|
|
210
|
+
try {
|
|
211
|
+
options = parseArgs(args);
|
|
212
|
+
} catch (error) {
|
|
213
|
+
console.error(`${styleText("red", "✖")} ${error.message}\n\nRun with --help for usage.`);
|
|
214
|
+
exit(1);
|
|
215
|
+
}
|
|
101
216
|
if (options.help) {
|
|
102
217
|
console.log(HELP);
|
|
103
218
|
return;
|
|
104
219
|
}
|
|
105
|
-
const interactive = stdin.isTTY && stdout.isTTY && !
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
220
|
+
const interactive = options.interactive ?? (stdin.isTTY && stdout.isTTY && !prompts.isCI());
|
|
221
|
+
const pm = packageManager();
|
|
222
|
+
|
|
223
|
+
prompts.intro(styleText(["bgCyan", "black"], " create-afw "));
|
|
224
|
+
|
|
225
|
+
// Project name → target directory.
|
|
226
|
+
let directory = options.directory;
|
|
227
|
+
if (!directory && interactive) {
|
|
228
|
+
directory = await ask(prompts.text({
|
|
229
|
+
message: "Project name:",
|
|
230
|
+
placeholder: DEFAULT_DIRECTORY,
|
|
231
|
+
defaultValue: DEFAULT_DIRECTORY,
|
|
232
|
+
}));
|
|
233
|
+
}
|
|
234
|
+
directory = (directory || DEFAULT_DIRECTORY).trim().replace(/\/+$/, "") || DEFAULT_DIRECTORY;
|
|
235
|
+
const target = resolve(cwd(), directory);
|
|
236
|
+
|
|
237
|
+
// A target that already has files.
|
|
238
|
+
let existing = options.overwrite ? "remove" : "error";
|
|
239
|
+
if (!options.overwrite && !(await isEmpty(target))) {
|
|
240
|
+
if (!interactive) {
|
|
241
|
+
prompts.cancel(`Target directory "${directory}" is not empty. Use --overwrite to remove its files.`);
|
|
242
|
+
exit(1);
|
|
114
243
|
}
|
|
244
|
+
existing = await ask(prompts.select({
|
|
245
|
+
message: `${directory === "." ? "Current directory" : `Target directory "${directory}"`} is not empty. How do you want to continue?`,
|
|
246
|
+
options: [
|
|
247
|
+
{ value: "cancel", label: "Cancel" },
|
|
248
|
+
{ value: "remove", label: "Remove existing files and continue" },
|
|
249
|
+
{ value: "ignore", label: "Ignore files and continue" },
|
|
250
|
+
],
|
|
251
|
+
}));
|
|
252
|
+
if (existing === "cancel") cancelled();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// The package name, when the folder name cannot be one.
|
|
256
|
+
let packageName = basename(target);
|
|
257
|
+
if (!isValidPackageName(packageName)) {
|
|
258
|
+
packageName = interactive
|
|
259
|
+
? await ask(prompts.text({
|
|
260
|
+
message: "Package name:",
|
|
261
|
+
defaultValue: toValidPackageName(packageName),
|
|
262
|
+
placeholder: toValidPackageName(packageName),
|
|
263
|
+
validate: (value) => (value === "" || isValidPackageName(value) ? undefined : "Invalid package.json name"),
|
|
264
|
+
}))
|
|
265
|
+
: toValidPackageName(packageName);
|
|
266
|
+
packageName ||= toValidPackageName(basename(target));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const choose = (message, entries) => ask(prompts.select({
|
|
270
|
+
message, options: Object.entries(entries).map(([value, hint]) => ({ value, label: value, hint })),
|
|
271
|
+
}));
|
|
272
|
+
|
|
273
|
+
const template = options.template ?? (interactive ? await choose("Select a template:", TEMPLATES) : "app");
|
|
274
|
+
const isApp = template === "app";
|
|
275
|
+
const render = isApp ? (options.render ?? (interactive ? await choose("How should pages render?", RENDERINGS) : "spa")) : undefined;
|
|
276
|
+
// Server-rendered pages need a server; the other renderings may add one.
|
|
277
|
+
const server = isApp && (render === "ssr" || (options.server ?? (interactive
|
|
278
|
+
? await ask(prompts.confirm({ message: `Add a server? (${SERVER_HINT})`, initialValue: false }))
|
|
279
|
+
: false)));
|
|
280
|
+
const styling = template === "docs" ? undefined : (options.styling ?? (interactive
|
|
281
|
+
? await ask(prompts.select({ message: "Select a styling:", options: Object.entries(STYLINGS).map(([value, label]) => ({ value, label })) }))
|
|
282
|
+
: "css"));
|
|
283
|
+
const language = options.language ?? (interactive
|
|
284
|
+
? await ask(prompts.select({
|
|
285
|
+
message: "Select a language:",
|
|
286
|
+
options: Object.entries(LANGUAGES).map(([value, label]) => ({ value, label })),
|
|
287
|
+
}))
|
|
288
|
+
: "ts");
|
|
289
|
+
|
|
290
|
+
const where = relative(cwd(), target) || ".";
|
|
291
|
+
const summary = [isApp && render, server && "server", styling && STYLINGS[styling], LANGUAGES[language]].filter(Boolean).join(", ");
|
|
292
|
+
prompts.log.step(`Scaffolding ${template} (${summary}) in ${styleText("cyan", target)}`);
|
|
293
|
+
try {
|
|
294
|
+
await create({ directory: target, template, render, server, styling, language, packageName, existing });
|
|
295
|
+
} catch (error) {
|
|
296
|
+
prompts.cancel(error.message);
|
|
297
|
+
exit(1);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Install the dependencies; never start anything — the user runs the dev server.
|
|
301
|
+
let installed = false;
|
|
302
|
+
if (options.install) {
|
|
303
|
+
prompts.log.step(`Installing dependencies with ${pm}…`);
|
|
304
|
+
installed = await installDependencies(pm, target);
|
|
305
|
+
if (!installed) prompts.log.warn(`${pm} install failed; run it yourself in ${where}.`);
|
|
306
|
+
}
|
|
307
|
+
const steps = nextSteps({ directory: where, pm, installed }).map((s) => ` ${styleText("cyan", s)}`).join("\n");
|
|
308
|
+
prompts.outro(`Done. Now run:\n\n${steps}\n`);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Run as a CLI, not when imported (tests). The bin is a symlink under npm, so both
|
|
312
|
+
// paths are resolved before comparing.
|
|
313
|
+
async function invokedDirectly() {
|
|
314
|
+
if (!argv[1]) return false;
|
|
315
|
+
try {
|
|
316
|
+
return (await realpath(argv[1])) === (await realpath(fileURLToPath(import.meta.url)));
|
|
317
|
+
} catch {
|
|
318
|
+
return false;
|
|
115
319
|
}
|
|
116
|
-
options.directory ??= "afw-app";
|
|
117
|
-
const { target } = await create(options);
|
|
118
|
-
const pm = packageManager();
|
|
119
|
-
const run = (script) => (pm === "npm" ? `npm run ${script}` : `${pm} ${script}`);
|
|
120
|
-
const dir = relative(process.cwd(), target) || ".";
|
|
121
|
-
console.log(`\nCreated ${options.template ?? "spa"} app in ${dir}\n\nNext:\n${dir === "." ? "" : ` cd ${dir}\n`} ${pm} install\n ${run("dev")}\n`);
|
|
122
320
|
}
|
|
123
321
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (invoked) {
|
|
129
|
-
main(process.argv.slice(2)).catch((error) => {
|
|
130
|
-
console.error(`✗ ${error.message}`);
|
|
131
|
-
process.exit(1);
|
|
322
|
+
if (await invokedDirectly()) {
|
|
323
|
+
main().catch((error) => {
|
|
324
|
+
console.error(`${styleText("red", "✖")} ${error?.message ?? error}`);
|
|
325
|
+
exit(1);
|
|
132
326
|
});
|
|
133
327
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opentf/create-afw",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Create an AFW app on Vite and Node: npm create @opentf/afw.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -22,10 +22,13 @@
|
|
|
22
22
|
"node": ">=22"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@opentf/afw": "0.
|
|
26
|
-
"@opentf/afw-
|
|
27
|
-
"@opentf/
|
|
25
|
+
"@opentf/afw": "0.35.0",
|
|
26
|
+
"@opentf/afw-docs": "0.33.0",
|
|
27
|
+
"@opentf/afw-test": "1.30.0",
|
|
28
|
+
"@opentf/vite-plugin-afw": "0.3.0",
|
|
29
|
+
"@tailwindcss/vite": "4.3.3",
|
|
28
30
|
"happy-dom": "21.0.4",
|
|
31
|
+
"tailwindcss": "4.3.3",
|
|
29
32
|
"vite": "8.3.5",
|
|
30
33
|
"vitest": "5.0.5"
|
|
31
34
|
},
|
|
@@ -47,5 +50,9 @@
|
|
|
47
50
|
"license": "MIT",
|
|
48
51
|
"publishConfig": {
|
|
49
52
|
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@clack/prompts": "1.8.1",
|
|
56
|
+
"@opentf/std": "0.20.0"
|
|
50
57
|
}
|
|
51
58
|
}
|