@typemvc/create 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 coretravis
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 ADDED
@@ -0,0 +1,39 @@
1
+ # @typemvc/create
2
+
3
+ Project initializer for [TypeMVC](https://github.com/coretravis/typemvc). Scaffolds a minimal, ready-to-run app.
4
+
5
+ ## Usage
6
+
7
+ ```bash
8
+ npm create @typemvc@latest my-app
9
+ # or: pnpm create @typemvc my-app
10
+ # or: yarn create @typemvc my-app
11
+ ```
12
+
13
+ Then:
14
+
15
+ ```bash
16
+ cd my-app
17
+ npm install
18
+ npm run dev
19
+ ```
20
+
21
+ ## Options
22
+
23
+ | Flag | Description |
24
+ | --- | --- |
25
+ | `--name <name>` | Project name (default: target directory name) |
26
+ | `--force`, `-f` | Scaffold into a non-empty directory |
27
+ | `--no-git` | Do not run `git init` |
28
+ | `--help`, `-h` | Show help |
29
+ | `--version`, `-v` | Show the version |
30
+
31
+ ## What you get
32
+
33
+ A Vite SPA wired for TypeMVC: a typed `tsconfig.json`, the `@typemvc/core/vite` plugin, a bootstrap entry, a Home controller with a reactive counter, a 404 catch-all controller, and matching `.tmvc` views. The generated app installs, typechecks, and builds against the published `@typemvc/core`.
34
+
35
+ This package has zero runtime dependencies and is non-interactive (flags only), so it works in CI and scripts.
36
+
37
+ ## License
38
+
39
+ MIT (c) coretravis
package/dist/index.js ADDED
@@ -0,0 +1,125 @@
1
+ #!/usr/bin/env node
2
+ import { spawnSync } from 'node:child_process';
3
+ import { readFileSync } from 'node:fs';
4
+ import { basename, dirname, join, resolve } from 'node:path';
5
+ import { fileURLToPath } from 'node:url';
6
+ import { scaffold } from './scaffold.js';
7
+ const here = dirname(fileURLToPath(import.meta.url));
8
+ function parseArgs(argv) {
9
+ let dir;
10
+ let name;
11
+ let force = false;
12
+ let git = true;
13
+ let help = false;
14
+ let version = false;
15
+ for (let i = 0; i < argv.length; i++) {
16
+ const arg = argv[i];
17
+ if (arg === undefined)
18
+ continue;
19
+ if (arg === '--help' || arg === '-h') {
20
+ help = true;
21
+ }
22
+ else if (arg === '--version' || arg === '-v') {
23
+ version = true;
24
+ }
25
+ else if (arg === '--force' || arg === '-f') {
26
+ force = true;
27
+ }
28
+ else if (arg === '--no-git') {
29
+ git = false;
30
+ }
31
+ else if (arg === '--name') {
32
+ name = argv[i + 1];
33
+ i++;
34
+ }
35
+ else if (arg.startsWith('--name=')) {
36
+ name = arg.slice('--name='.length);
37
+ }
38
+ else if (!arg.startsWith('-') && dir === undefined) {
39
+ dir = arg;
40
+ }
41
+ else {
42
+ throw new Error(`[TypeMVC] Unknown argument "${arg}". Run with --help for usage.`);
43
+ }
44
+ }
45
+ return { dir, name, force, git, help, version };
46
+ }
47
+ function readVersion() {
48
+ const raw = readFileSync(join(here, '..', 'package.json'), 'utf8');
49
+ const parsed = JSON.parse(raw);
50
+ return parsed.version ?? '0.0.0';
51
+ }
52
+ function detectPackageManager() {
53
+ const ua = process.env.npm_config_user_agent;
54
+ if (ua === undefined || ua === '')
55
+ return 'npm';
56
+ const name = ua.split(' ')[0]?.split('/')[0];
57
+ if (name === 'pnpm' || name === 'yarn' || name === 'bun')
58
+ return name;
59
+ return 'npm';
60
+ }
61
+ function printHelp() {
62
+ console.log([
63
+ 'Create a new TypeMVC app.',
64
+ '',
65
+ 'Usage:',
66
+ ' npm create @typemvc@latest <dir> [options]',
67
+ '',
68
+ 'Options:',
69
+ ' --name <name> Project name (default: target directory name)',
70
+ ' --force, -f Scaffold into a non-empty directory',
71
+ ' --no-git Do not run git init',
72
+ ' --help, -h Show this help',
73
+ ' --version, -v Show the version',
74
+ ].join('\n'));
75
+ }
76
+ function tryGitInit(targetDir) {
77
+ const result = spawnSync('git', ['init'], { cwd: targetDir, stdio: 'ignore' });
78
+ if (result.status !== 0) {
79
+ console.warn('[TypeMVC] Skipped git init (git not available or failed).');
80
+ }
81
+ }
82
+ function main() {
83
+ const options = parseArgs(process.argv.slice(2));
84
+ if (options.help) {
85
+ printHelp();
86
+ return;
87
+ }
88
+ if (options.version) {
89
+ console.log(readVersion());
90
+ return;
91
+ }
92
+ if (options.dir === undefined) {
93
+ throw new Error('[TypeMVC] Please specify the target directory. ' +
94
+ 'Example: npm create @typemvc@latest my-app');
95
+ }
96
+ const targetDir = resolve(process.cwd(), options.dir);
97
+ const projectName = options.name ?? basename(targetDir);
98
+ if (projectName === '' || projectName.includes('/') || projectName.includes('\\')) {
99
+ throw new Error(`[TypeMVC] Invalid project name "${projectName}". ` +
100
+ `Provide a valid name with --name, or use a directory name without path separators.`);
101
+ }
102
+ const result = scaffold({
103
+ targetDir,
104
+ projectName,
105
+ templateDir: join(here, '..', 'template'),
106
+ force: options.force,
107
+ });
108
+ if (options.git) {
109
+ tryGitInit(targetDir);
110
+ }
111
+ const pm = detectPackageManager();
112
+ console.log(`\nScaffolded ${String(result.files.length)} files into ${targetDir}\n`);
113
+ console.log('Next steps:');
114
+ console.log(` cd ${options.dir}`);
115
+ console.log(` ${pm} install`);
116
+ console.log(` ${pm} run dev\n`);
117
+ }
118
+ try {
119
+ main();
120
+ }
121
+ catch (err) {
122
+ const error = err instanceof Error ? err : new Error(String(err));
123
+ console.error(error.message);
124
+ process.exitCode = 1;
125
+ }
@@ -0,0 +1,35 @@
1
+ import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
2
+ import { join, relative, sep } from 'node:path';
3
+ const PROJECT_NAME_TOKEN = '{{projectName}}';
4
+ /**
5
+ * Copies the template tree into targetDir, replacing the project-name token and
6
+ * renaming `_gitignore` to `.gitignore`. Throws if the target is non-empty and
7
+ * force is not set.
8
+ */
9
+ export function scaffold(options) {
10
+ const { targetDir, projectName, templateDir, force = false } = options;
11
+ if (existsSync(targetDir) && readdirSync(targetDir).length > 0 && !force) {
12
+ throw new Error(`[TypeMVC] Target directory "${targetDir}" is not empty. ` +
13
+ `Pass --force to scaffold into it anyway.`);
14
+ }
15
+ mkdirSync(targetDir, { recursive: true });
16
+ const written = [];
17
+ copyDir(templateDir, targetDir, targetDir, projectName, written);
18
+ return { files: written };
19
+ }
20
+ function copyDir(srcDir, destDir, rootDest, projectName, written) {
21
+ for (const entry of readdirSync(srcDir, { withFileTypes: true })) {
22
+ const srcPath = join(srcDir, entry.name);
23
+ if (entry.isDirectory()) {
24
+ const subDest = join(destDir, entry.name);
25
+ mkdirSync(subDest, { recursive: true });
26
+ copyDir(srcPath, subDest, rootDest, projectName, written);
27
+ continue;
28
+ }
29
+ const destName = entry.name === '_gitignore' ? '.gitignore' : entry.name;
30
+ const destPath = join(destDir, destName);
31
+ const content = readFileSync(srcPath, 'utf8').replaceAll(PROJECT_NAME_TOKEN, projectName);
32
+ writeFileSync(destPath, content);
33
+ written.push(relative(rootDest, destPath).split(sep).join('/'));
34
+ }
35
+ }
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@typemvc/create",
3
+ "version": "0.1.0",
4
+ "description": "Project initializer for TypeMVC. Run with: npm create @typemvc",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "coretravis",
8
+ "homepage": "https://github.com/coretravis/typemvc#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/coretravis/typemvc.git",
12
+ "directory": "packages/create"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/coretravis/typemvc/issues"
16
+ },
17
+ "keywords": [
18
+ "typemvc",
19
+ "create",
20
+ "scaffold",
21
+ "starter",
22
+ "init"
23
+ ],
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "bin": {
28
+ "create-typemvc": "./dist/index.js"
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "template",
33
+ "README.md"
34
+ ],
35
+ "engines": {
36
+ "node": ">=20.0.0"
37
+ },
38
+ "devDependencies": {
39
+ "@eslint/js": "latest",
40
+ "@types/node": "^25.9.3",
41
+ "eslint": "latest",
42
+ "typescript": "latest",
43
+ "typescript-eslint": "latest",
44
+ "vitest": "latest"
45
+ },
46
+ "scripts": {
47
+ "build": "tsc -p tsconfig.build.json",
48
+ "typecheck": "tsc -p tsconfig.json --noEmit",
49
+ "lint": "eslint .",
50
+ "test": "vitest run",
51
+ "test:watch": "vitest",
52
+ "ci": "pnpm lint && pnpm typecheck && pnpm test && pnpm build"
53
+ }
54
+ }
@@ -0,0 +1,4 @@
1
+ node_modules/
2
+ dist/
3
+ *.local
4
+ .DS_Store
@@ -0,0 +1,18 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>{{projectName}}</title>
7
+ <style>
8
+ body { font-family: system-ui, sans-serif; max-width: 640px; margin: 48px auto; padding: 0 16px; }
9
+ nav a { margin-right: 12px; }
10
+ button { font-size: 1rem; padding: 4px 12px; cursor: pointer; }
11
+ .count { font-size: 2.5rem; font-weight: 700; margin: 8px 0; }
12
+ </style>
13
+ </head>
14
+ <body>
15
+ <div id="app"></div>
16
+ <script type="module" src="/src/main.ts"></script>
17
+ </body>
18
+ </html>
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "{{projectName}}",
3
+ "private": true,
4
+ "version": "0.0.0",
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "vite",
8
+ "build": "vite build",
9
+ "preview": "vite preview"
10
+ },
11
+ "dependencies": {
12
+ "@typemvc/core": "^0.1.0"
13
+ },
14
+ "devDependencies": {
15
+ "typescript": "latest",
16
+ "vite": "^6.0.0"
17
+ }
18
+ }
@@ -0,0 +1,41 @@
1
+ import { Controller, controller, get, action, retain, signal, computed, View } from '@typemvc/core';
2
+ import type { IView, ReadonlySignal } from '@typemvc/core';
3
+
4
+ type HomeData = {
5
+ title: string;
6
+ count: ReadonlySignal<number>;
7
+ doubled: ReadonlySignal<number>;
8
+ };
9
+
10
+ // '/' is the home route. @retain() keeps this instance, and its signal state,
11
+ // alive across navigations, so the count survives leaving and returning.
12
+ @controller('/')
13
+ @retain()
14
+ class HomeController extends Controller {
15
+ readonly #count = signal(0);
16
+ readonly #doubled = computed(() => this.#count.get() * 2);
17
+
18
+ // GET '/' renders views/home/index.tmvc (convention: <controller>/<action>).
19
+ @get()
20
+ index(): IView<HomeData> {
21
+ return View({
22
+ title: 'Hello, TypeMVC',
23
+ count: this.#count,
24
+ doubled: this.#doubled,
25
+ });
26
+ }
27
+
28
+ // @action marks a non-route method exposed to the view as context.increment.
29
+ // Mutating the signal reactively updates only the DOM nodes that read it.
30
+ @action
31
+ increment(): void {
32
+ this.#count.update((n) => n + 1);
33
+ }
34
+
35
+ @action
36
+ reset(): void {
37
+ this.#count.set(0);
38
+ }
39
+ }
40
+
41
+ export { HomeController };
@@ -0,0 +1,13 @@
1
+ import { Controller, controller, get, View } from '@typemvc/core';
2
+ import type { IView } from '@typemvc/core';
3
+
4
+ // '*' is the catch-all: any URL no other controller matches lands here.
5
+ @controller('*')
6
+ class NotFoundController extends Controller {
7
+ @get()
8
+ notFound(): IView {
9
+ return View('notfound/notfound');
10
+ }
11
+ }
12
+
13
+ export { NotFoundController };
@@ -0,0 +1,20 @@
1
+ import { bootstrap } from '@typemvc/core';
2
+ import { HomeController } from './controllers/HomeController.js';
3
+ import { NotFoundController } from './controllers/NotFoundController.js';
4
+
5
+ const outlet = document.getElementById('app');
6
+ if (outlet === null) throw new Error('[App] No #app element found in index.html');
7
+
8
+ bootstrap({
9
+ outlet,
10
+ viewsRoot: '/src/views/',
11
+ // Lazy view glob: each .tmvc view is code-split and loaded on navigation.
12
+ views: import.meta.glob('/src/views/**/*.tmvc'),
13
+ logging: { level: 'debug' },
14
+ configure(app) {
15
+ // Register routed controllers. Add services here too, e.g.
16
+ // app.singleton(MY_SERVICE, () => new MyService());
17
+ app.route(HomeController);
18
+ app.route(NotFoundController); // @controller('*') catch-all, keep this last.
19
+ },
20
+ });
@@ -0,0 +1,24 @@
1
+ @model from HomeController.index
2
+
3
+ <main>
4
+ <nav>
5
+ <a href="/">Home</a>
6
+ <a href="/does-not-exist">Broken link (404)</a>
7
+ </nav>
8
+
9
+ <h1>${context.model.title}</h1>
10
+
11
+ <p>
12
+ This number is a <code>signal()</code>. The buttons call
13
+ <code>@action</code> methods on the controller, and only the text node below
14
+ re-renders. No page reload, no virtual DOM.
15
+ </p>
16
+
17
+ <p class="count">${context.model.count}</p>
18
+ <p>x 2 = ${context.model.doubled}</p>
19
+
20
+ <div>
21
+ <button onclick="${context.increment}">+1</button>
22
+ <button onclick="${context.reset}">Reset</button>
23
+ </div>
24
+ </main>
@@ -0,0 +1,5 @@
1
+ <main>
2
+ <h1>404, not found</h1>
3
+ <p>No route matched <code>${context.router.current}</code>.</p>
4
+ <p><a href="/">Back home</a></p>
5
+ </main>
@@ -0,0 +1,8 @@
1
+ /// <reference types="vite/client" />
2
+
3
+ // Teaches TypeScript that importing a .tmvc file yields a view render function.
4
+ declare module '*.tmvc' {
5
+ import type { TmvcViewFunction } from '@typemvc/core';
6
+ const template: TmvcViewFunction;
7
+ export default template;
8
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "lib": ["ES2022", "dom", "dom.iterable"],
5
+ "module": "ESNext",
6
+ "moduleResolution": "bundler",
7
+ "strict": true,
8
+ "experimentalDecorators": true,
9
+ "ignoreDeprecations": "6.0",
10
+ "verbatimModuleSyntax": true,
11
+ "isolatedModules": true,
12
+ "skipLibCheck": true,
13
+ "noEmit": true
14
+ },
15
+ "include": ["src", "vite.config.ts"]
16
+ }
@@ -0,0 +1,9 @@
1
+ import { defineConfig, type PluginOption } from 'vite';
2
+ import { typemvcPlugin } from '@typemvc/core/vite';
3
+
4
+ // typemvcPlugin() compiles .tmvc view documents. The framework does not depend
5
+ // on vite's types, so cast its structural plugin to vite's PluginOption.
6
+ export default defineConfig({
7
+ plugins: [typemvcPlugin() as PluginOption],
8
+ appType: 'spa',
9
+ });