create-clarity 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/README.md +56 -0
- package/bin/create-clarity.js +132 -0
- package/lib/prompts.js +88 -0
- package/lib/scaffold.js +40 -0
- package/lib/templates.js +386 -0
- package/package.json +25 -0
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# create-clarity
|
|
2
|
+
|
|
3
|
+
Scaffold a new [Clarity.js](https://gitlab.com/ozsarman/clarity.js) app in seconds.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm create clarity@latest my-app
|
|
7
|
+
# or
|
|
8
|
+
npx create-clarity my-app
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Run with no arguments for a fully interactive setup:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm create clarity@latest
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## What you get
|
|
18
|
+
|
|
19
|
+
An interactive prompt lets you pick:
|
|
20
|
+
|
|
21
|
+
- **Template** — Basic (client-rendered SPA) or SSR (server-side rendering + hydration)
|
|
22
|
+
- **Add-ons** — TypeScript, Router, i18n, Testing (any combination)
|
|
23
|
+
|
|
24
|
+
The generated project is a ready-to-run Vite app wired to the Clarity compiler,
|
|
25
|
+
with a working counter component that demonstrates `state`, `beforeMount` and
|
|
26
|
+
`onMount`.
|
|
27
|
+
|
|
28
|
+
## Non-interactive / CI
|
|
29
|
+
|
|
30
|
+
Pass flags to skip the prompts:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npx create-clarity my-app --ssr --ts --router --i18n --testing
|
|
34
|
+
npx create-clarity my-app -y # all defaults, no prompts
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
| Flag | Effect |
|
|
38
|
+
|------|--------|
|
|
39
|
+
| `--spa` / `--ssr` | choose template |
|
|
40
|
+
| `--ts`, `--typescript` | add TypeScript |
|
|
41
|
+
| `--router` | add the router |
|
|
42
|
+
| `--i18n` | add internationalization |
|
|
43
|
+
| `--testing` | add a test setup |
|
|
44
|
+
| `-y`, `--yes` | accept defaults, no prompts |
|
|
45
|
+
| `--local <path>` | use a local clarity-js checkout (`file:` dependency) — handy for development |
|
|
46
|
+
|
|
47
|
+
## After scaffolding
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
cd my-app
|
|
51
|
+
npm install
|
|
52
|
+
npm run dev
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Zero runtime dependencies — `create-clarity` uses only Node built-ins, so it
|
|
56
|
+
starts instantly via `npm create` / `npx`.
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* create-clarity — scaffold a new Clarity.js project.
|
|
4
|
+
*
|
|
5
|
+
* npm create clarity@latest my-app
|
|
6
|
+
* npm create clarity@latest # prompts for everything
|
|
7
|
+
* npx create-clarity my-app --ssr --ts --router
|
|
8
|
+
*
|
|
9
|
+
* Flags (skip the matching prompt):
|
|
10
|
+
* --ssr / --spa template
|
|
11
|
+
* --ts / --typescript TypeScript add-on
|
|
12
|
+
* --router router add-on
|
|
13
|
+
* --i18n i18n add-on
|
|
14
|
+
* --testing testing add-on
|
|
15
|
+
* -y, --yes accept all defaults, no prompts
|
|
16
|
+
* --local <path> use a local clarity-js (file: dependency) — for testing
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
const fs = require('node:fs');
|
|
22
|
+
const path = require('node:path');
|
|
23
|
+
const prompts = require('../lib/prompts');
|
|
24
|
+
const { scaffold, isNonEmptyDir } = require('../lib/scaffold');
|
|
25
|
+
|
|
26
|
+
const c = prompts.colors;
|
|
27
|
+
const PKG_VERSION = require('../package.json').version;
|
|
28
|
+
|
|
29
|
+
function parseArgs(argv) {
|
|
30
|
+
const args = { _: [], flags: {} };
|
|
31
|
+
for (let i = 0; i < argv.length; i++) {
|
|
32
|
+
const a = argv[i];
|
|
33
|
+
if (a === '--local') { args.flags.local = argv[++i]; }
|
|
34
|
+
else if (a.startsWith('--')) { args.flags[a.slice(2)] = true; }
|
|
35
|
+
else if (a === '-y') { args.flags.yes = true; }
|
|
36
|
+
else { args._.push(a); }
|
|
37
|
+
}
|
|
38
|
+
return args;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function validateProjectName(name) {
|
|
42
|
+
if (!name) return 'Project name is required';
|
|
43
|
+
if (!/^[a-z0-9._-]+$/i.test(name)) return 'Use only letters, numbers, dashes, dots and underscores';
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function main() {
|
|
48
|
+
const argv = parseArgs(process.argv.slice(2));
|
|
49
|
+
const yes = !!argv.flags.yes;
|
|
50
|
+
|
|
51
|
+
console.log(`\n${c.bold}${c.cyan}create-clarity${c.reset} ${c.gray}v${PKG_VERSION}${c.reset}`);
|
|
52
|
+
console.log(`${c.gray}Scaffold a Clarity.js app.${c.reset}\n`);
|
|
53
|
+
|
|
54
|
+
// ── Project name ───────────────────────────────────────────────────────────
|
|
55
|
+
let projectName = argv._[0];
|
|
56
|
+
if (!projectName) {
|
|
57
|
+
projectName = yes ? 'clarity-app' : await prompts.text('Project name', 'clarity-app');
|
|
58
|
+
}
|
|
59
|
+
const nameErr = validateProjectName(projectName);
|
|
60
|
+
if (nameErr) { console.error(`${c.yellow}✗ ${nameErr}${c.reset}`); process.exit(1); }
|
|
61
|
+
|
|
62
|
+
const targetDir = path.resolve(process.cwd(), projectName);
|
|
63
|
+
if (isNonEmptyDir(targetDir)) {
|
|
64
|
+
const ok = yes ? false : await prompts.confirm(`Directory "${projectName}" is not empty. Continue and overwrite files?`, false);
|
|
65
|
+
if (!ok) { console.log(`${c.yellow}Aborted.${c.reset}`); process.exit(1); }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ── Template ───────────────────────────────────────────────────────────────
|
|
69
|
+
let ssr;
|
|
70
|
+
if ('ssr' in argv.flags) ssr = true;
|
|
71
|
+
else if ('spa' in argv.flags) ssr = false;
|
|
72
|
+
else ssr = yes ? false : (await prompts.select('Which template?', [
|
|
73
|
+
{ value: 'spa', label: 'Basic (SPA)', hint: 'client-rendered single-page app' },
|
|
74
|
+
{ value: 'ssr', label: 'SSR', hint: 'server-side rendering + hydration' },
|
|
75
|
+
], 0)) === 'ssr';
|
|
76
|
+
|
|
77
|
+
// ── Add-ons ────────────────────────────────────────────────────────────────
|
|
78
|
+
let addons;
|
|
79
|
+
const flagAddons = ['typescript', 'ts', 'router', 'i18n', 'testing'].some(f => f in argv.flags);
|
|
80
|
+
if (flagAddons || yes) {
|
|
81
|
+
addons = new Set();
|
|
82
|
+
if (argv.flags.typescript || argv.flags.ts) addons.add('typescript');
|
|
83
|
+
if (argv.flags.router) addons.add('router');
|
|
84
|
+
if (argv.flags.i18n) addons.add('i18n');
|
|
85
|
+
if (argv.flags.testing) addons.add('testing');
|
|
86
|
+
} else {
|
|
87
|
+
const picked = await prompts.multiselect('Add-ons?', [
|
|
88
|
+
{ value: 'typescript', label: 'TypeScript', hint: 'typed config + .d.ts', selected: false },
|
|
89
|
+
{ value: 'router', label: 'Router', hint: 'multi-page routing', selected: false },
|
|
90
|
+
{ value: 'i18n', label: 'i18n', hint: 'translations', selected: false },
|
|
91
|
+
{ value: 'testing', label: 'Testing', hint: '@ozsarman/clarityjs/test setup', selected: false },
|
|
92
|
+
]);
|
|
93
|
+
addons = new Set(picked);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ── clarity-js dependency source ───────────────────────────────────────────
|
|
97
|
+
const clarityDep = argv.flags.local
|
|
98
|
+
? `file:${path.resolve(process.cwd(), argv.flags.local)}`
|
|
99
|
+
: `^0.6.0`;
|
|
100
|
+
|
|
101
|
+
const options = {
|
|
102
|
+
projectName,
|
|
103
|
+
ssr,
|
|
104
|
+
typescript: addons.has('typescript'),
|
|
105
|
+
router: addons.has('router'),
|
|
106
|
+
i18n: addons.has('i18n'),
|
|
107
|
+
testing: addons.has('testing'),
|
|
108
|
+
clarityDep,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// ── Scaffold ───────────────────────────────────────────────────────────────
|
|
112
|
+
const { written } = scaffold(targetDir, options);
|
|
113
|
+
|
|
114
|
+
console.log(`\n${c.green}✓${c.reset} Created ${c.bold}${projectName}${c.reset} ${c.gray}(${written.length} files)${c.reset}`);
|
|
115
|
+
const addonList = [
|
|
116
|
+
options.typescript && 'TypeScript',
|
|
117
|
+
options.router && 'Router',
|
|
118
|
+
options.i18n && 'i18n',
|
|
119
|
+
options.testing && 'Testing',
|
|
120
|
+
].filter(Boolean);
|
|
121
|
+
console.log(` ${c.gray}template:${c.reset} ${ssr ? 'SSR' : 'Basic (SPA)'}${addonList.length ? ` ${c.gray}add-ons:${c.reset} ${addonList.join(', ')}` : ''}`);
|
|
122
|
+
|
|
123
|
+
console.log(`\n${c.bold}Next steps:${c.reset}`);
|
|
124
|
+
console.log(` cd ${projectName}`);
|
|
125
|
+
console.log(` npm install`);
|
|
126
|
+
console.log(` npm run dev\n`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
main().catch(err => {
|
|
130
|
+
console.error(`\n${c.yellow}create-clarity failed:${c.reset} ${err.message}`);
|
|
131
|
+
process.exit(1);
|
|
132
|
+
});
|
package/lib/prompts.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiny zero-dependency interactive prompts built on Node's readline.
|
|
3
|
+
*
|
|
4
|
+
* Why no dependencies: `create-*` packages run via `npm create` / `npx`, which
|
|
5
|
+
* fetches the package and runs its bin with nothing installed. Keeping this
|
|
6
|
+
* dependency-free means instant, reliable scaffolding.
|
|
7
|
+
*
|
|
8
|
+
* Supports: text(), select(), multiselect(), confirm().
|
|
9
|
+
* Falls back gracefully to defaults when stdin is not a TTY (CI / piped input).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
'use strict';
|
|
13
|
+
|
|
14
|
+
const readline = require('node:readline');
|
|
15
|
+
|
|
16
|
+
const c = {
|
|
17
|
+
reset: '\x1b[0m',
|
|
18
|
+
dim: '\x1b[2m',
|
|
19
|
+
bold: '\x1b[1m',
|
|
20
|
+
cyan: '\x1b[36m',
|
|
21
|
+
green: '\x1b[32m',
|
|
22
|
+
yellow: '\x1b[33m',
|
|
23
|
+
gray: '\x1b[90m',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const isTTY = process.stdin.isTTY && process.stdout.isTTY;
|
|
27
|
+
|
|
28
|
+
function question(query) {
|
|
29
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
30
|
+
return new Promise(resolve => rl.question(query, answer => { rl.close(); resolve(answer); }));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Free-text prompt with a default value. */
|
|
34
|
+
async function text(message, defaultValue = '') {
|
|
35
|
+
if (!isTTY) return defaultValue;
|
|
36
|
+
const hint = defaultValue ? c.gray + ` (${defaultValue})` + c.reset : '';
|
|
37
|
+
const ans = (await question(`${c.cyan}?${c.reset} ${message}${hint} ${c.gray}›${c.reset} `)).trim();
|
|
38
|
+
return ans || defaultValue;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Single-choice list. `choices`: [{ value, label, hint }]. Returns a value. */
|
|
42
|
+
async function select(message, choices, defaultIndex = 0) {
|
|
43
|
+
if (!isTTY) return choices[defaultIndex].value;
|
|
44
|
+
console.log(`${c.cyan}?${c.reset} ${c.bold}${message}${c.reset}`);
|
|
45
|
+
choices.forEach((ch, i) => {
|
|
46
|
+
const num = c.cyan + (i + 1) + c.reset;
|
|
47
|
+
const hint = ch.hint ? c.gray + ' — ' + ch.hint + c.reset : '';
|
|
48
|
+
const def = i === defaultIndex ? c.green + ' (default)' + c.reset : '';
|
|
49
|
+
console.log(` ${num}. ${ch.label}${hint}${def}`);
|
|
50
|
+
});
|
|
51
|
+
const raw = (await question(` ${c.gray}Choose 1-${choices.length} ›${c.reset} `)).trim();
|
|
52
|
+
const idx = raw === '' ? defaultIndex : parseInt(raw, 10) - 1;
|
|
53
|
+
if (Number.isNaN(idx) || idx < 0 || idx >= choices.length) return choices[defaultIndex].value;
|
|
54
|
+
return choices[idx].value;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Multi-choice list. `choices`: [{ value, label, hint, selected }].
|
|
59
|
+
* Accepts comma/space separated numbers, e.g. "1,3 4". Returns array of values.
|
|
60
|
+
*/
|
|
61
|
+
async function multiselect(message, choices) {
|
|
62
|
+
const defaults = choices.filter(ch => ch.selected).map(ch => ch.value);
|
|
63
|
+
if (!isTTY) return defaults;
|
|
64
|
+
console.log(`${c.cyan}?${c.reset} ${c.bold}${message}${c.reset} ${c.gray}(comma-separated numbers, empty for none)${c.reset}`);
|
|
65
|
+
choices.forEach((ch, i) => {
|
|
66
|
+
const num = c.cyan + (i + 1) + c.reset;
|
|
67
|
+
const hint = ch.hint ? c.gray + ' — ' + ch.hint + c.reset : '';
|
|
68
|
+
const def = ch.selected ? c.green + ' (default on)' + c.reset : '';
|
|
69
|
+
console.log(` ${num}. ${ch.label}${hint}${def}`);
|
|
70
|
+
});
|
|
71
|
+
const raw = (await question(` ${c.gray}Select ›${c.reset} `)).trim();
|
|
72
|
+
if (raw === '') return defaults;
|
|
73
|
+
const picked = new Set(
|
|
74
|
+
raw.split(/[\s,]+/).map(s => parseInt(s, 10) - 1).filter(i => i >= 0 && i < choices.length)
|
|
75
|
+
);
|
|
76
|
+
return [...picked].map(i => choices[i].value);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Yes/no prompt. */
|
|
80
|
+
async function confirm(message, defaultValue = true) {
|
|
81
|
+
if (!isTTY) return defaultValue;
|
|
82
|
+
const hint = defaultValue ? 'Y/n' : 'y/N';
|
|
83
|
+
const ans = (await question(`${c.cyan}?${c.reset} ${message} ${c.gray}(${hint})${c.reset} ${c.gray}›${c.reset} `)).trim().toLowerCase();
|
|
84
|
+
if (ans === '') return defaultValue;
|
|
85
|
+
return ans === 'y' || ans === 'yes';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
module.exports = { text, select, multiselect, confirm, colors: c, isTTY };
|
package/lib/scaffold.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scaffolder — turns a resolved options object into a project on disk.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
'use strict';
|
|
6
|
+
|
|
7
|
+
const fs = require('node:fs');
|
|
8
|
+
const path = require('node:path');
|
|
9
|
+
const { buildFiles } = require('./templates');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Write a generated project to `targetDir`.
|
|
13
|
+
* @returns {{ written: string[], dir: string }}
|
|
14
|
+
*/
|
|
15
|
+
function scaffold(targetDir, options) {
|
|
16
|
+
const files = buildFiles(options);
|
|
17
|
+
const written = [];
|
|
18
|
+
|
|
19
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
20
|
+
|
|
21
|
+
for (const [rel, contents] of Object.entries(files)) {
|
|
22
|
+
const dest = path.join(targetDir, rel);
|
|
23
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
24
|
+
fs.writeFileSync(dest, contents, 'utf8');
|
|
25
|
+
written.push(rel);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return { written: written.sort(), dir: targetDir };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** True if a directory exists and contains entries. */
|
|
32
|
+
function isNonEmptyDir(dir) {
|
|
33
|
+
try {
|
|
34
|
+
return fs.statSync(dir).isDirectory() && fs.readdirSync(dir).length > 0;
|
|
35
|
+
} catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = { scaffold, isNonEmptyDir };
|
package/lib/templates.js
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Template file generators for create-clarity.
|
|
3
|
+
*
|
|
4
|
+
* buildFiles(options) returns a plain object mapping relative file paths to
|
|
5
|
+
* their string contents. The scaffolder writes them to disk. Keeping templates
|
|
6
|
+
* as code (not a copied directory) keeps the package self-contained and lets
|
|
7
|
+
* add-ons compose cleanly.
|
|
8
|
+
*
|
|
9
|
+
* options = {
|
|
10
|
+
* projectName: string,
|
|
11
|
+
* ssr: boolean, // server-side rendering template
|
|
12
|
+
* typescript: boolean, // TypeScript add-on
|
|
13
|
+
* router: boolean, // file/-component router add-on
|
|
14
|
+
* i18n: boolean, // internationalization add-on
|
|
15
|
+
* testing: boolean, // clarity-js/test add-on
|
|
16
|
+
* clarityDep: string, // value for the clarity-js dependency (version or file:path)
|
|
17
|
+
* }
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
'use strict';
|
|
21
|
+
|
|
22
|
+
function buildFiles(opts) {
|
|
23
|
+
const files = {};
|
|
24
|
+
const ext = opts.typescript ? 'ts' : 'js';
|
|
25
|
+
|
|
26
|
+
// ── package.json ───────────────────────────────────────────────────────────
|
|
27
|
+
const deps = { '@ozsarman/clarityjs': opts.clarityDep };
|
|
28
|
+
const devDeps = { vite: '^5.0.0' };
|
|
29
|
+
if (opts.typescript) devDeps.typescript = '^5.4.0';
|
|
30
|
+
|
|
31
|
+
const scripts = {
|
|
32
|
+
dev: 'vite',
|
|
33
|
+
build: 'vite build',
|
|
34
|
+
preview: 'vite preview',
|
|
35
|
+
};
|
|
36
|
+
if (opts.ssr) {
|
|
37
|
+
scripts['build:server'] = `vite build --ssr src/entry-server.${ext}`;
|
|
38
|
+
scripts.serve = `node server.${ext === 'ts' ? 'js' : 'js'}`;
|
|
39
|
+
}
|
|
40
|
+
if (opts.testing) scripts.test = 'node --test test/';
|
|
41
|
+
if (opts.typescript) scripts.typecheck = 'tsc --noEmit';
|
|
42
|
+
|
|
43
|
+
files['package.json'] = JSON.stringify({
|
|
44
|
+
name: opts.projectName,
|
|
45
|
+
private: true,
|
|
46
|
+
version: '0.0.0',
|
|
47
|
+
type: 'module',
|
|
48
|
+
scripts,
|
|
49
|
+
dependencies: deps,
|
|
50
|
+
devDependencies: devDeps,
|
|
51
|
+
}, null, 2) + '\n';
|
|
52
|
+
|
|
53
|
+
// ── vite.config ──────────────────────────────────────────────────────────--
|
|
54
|
+
const clarityPluginCall = opts.router ? 'clarity({ scanPages: true })' : 'clarity()';
|
|
55
|
+
files[`vite.config.${ext === 'ts' ? 'ts' : 'js'}`] =
|
|
56
|
+
`import { defineConfig } from 'vite';
|
|
57
|
+
import clarity from '@ozsarman/clarityjs/vite';
|
|
58
|
+
|
|
59
|
+
export default defineConfig({
|
|
60
|
+
plugins: [${clarityPluginCall}],
|
|
61
|
+
});
|
|
62
|
+
`;
|
|
63
|
+
|
|
64
|
+
// ── index.html ───────────────────────────────────────────────────────────--
|
|
65
|
+
files['index.html'] =
|
|
66
|
+
`<!DOCTYPE html>
|
|
67
|
+
<html lang="en">
|
|
68
|
+
<head>
|
|
69
|
+
<meta charset="UTF-8" />
|
|
70
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
71
|
+
<title>${opts.projectName}</title>
|
|
72
|
+
<link rel="stylesheet" href="/src/styles.css" />
|
|
73
|
+
</head>
|
|
74
|
+
<body>
|
|
75
|
+
<div id="app"></div>
|
|
76
|
+
<script type="module" src="/src/main.${ext}"></script>
|
|
77
|
+
</body>
|
|
78
|
+
</html>
|
|
79
|
+
`;
|
|
80
|
+
|
|
81
|
+
// ── styles ──────────────────────────────────────────────────────────────---
|
|
82
|
+
files['src/styles.css'] =
|
|
83
|
+
`:root { color-scheme: light dark; font-family: system-ui, sans-serif; }
|
|
84
|
+
body { margin: 0; display: grid; place-items: center; min-height: 100vh; }
|
|
85
|
+
.card { padding: 2rem 2.5rem; border-radius: 12px; box-shadow: 0 4px 24px rgba(0,0,0,.08); text-align: center; }
|
|
86
|
+
button { font: inherit; padding: .5rem 1rem; border-radius: 8px; border: 1px solid currentColor; background: transparent; cursor: pointer; }
|
|
87
|
+
nav a { margin-right: 1rem; }
|
|
88
|
+
`;
|
|
89
|
+
|
|
90
|
+
// ── entry point ────────────────────────────────────────────────────────────
|
|
91
|
+
if (opts.router) {
|
|
92
|
+
// File-based routing: the Vite plugin (scanPages) generates the route table
|
|
93
|
+
// from pages/ and exposes a ready-to-mount Routes component.
|
|
94
|
+
files[`src/main.${ext}`] =
|
|
95
|
+
`import { mount } from '@ozsarman/clarityjs';
|
|
96
|
+
import { Routes } from 'virtual:clarity-pages';
|
|
97
|
+
${opts.i18n ? "import './i18n.js';\n" : ''}
|
|
98
|
+
const container = document.getElementById('app');
|
|
99
|
+
mount(Routes, container);
|
|
100
|
+
`;
|
|
101
|
+
files['pages/index.clarity'] = indexPage(opts);
|
|
102
|
+
files['pages/about.clarity'] = aboutPage(opts);
|
|
103
|
+
files['pages/[slug].clarity'] = dynamicPage(opts);
|
|
104
|
+
files['pages/not-found.clarity'] = notFoundPage(opts);
|
|
105
|
+
} else {
|
|
106
|
+
files[`src/main.${ext}`] =
|
|
107
|
+
`import { mount } from '@ozsarman/clarityjs';
|
|
108
|
+
import { App } from './App.clarity';
|
|
109
|
+
${opts.i18n ? "import './i18n.js';\n" : ''}
|
|
110
|
+
const container = document.getElementById('app');
|
|
111
|
+
mount(App, container);
|
|
112
|
+
`;
|
|
113
|
+
files['src/App.clarity'] = appComponent(opts);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ── SSR extras ───────────────────────────────────────────────────────────--
|
|
117
|
+
if (opts.ssr) {
|
|
118
|
+
files[`src/entry-server.${ext}`] =
|
|
119
|
+
`import { renderToString } from '@ozsarman/clarityjs/ssr';
|
|
120
|
+
import { App } from './App.clarity';
|
|
121
|
+
|
|
122
|
+
// Called by the server (server.js) to produce HTML for each request.
|
|
123
|
+
export function render() {
|
|
124
|
+
return renderToString(App());
|
|
125
|
+
}
|
|
126
|
+
`;
|
|
127
|
+
files['server.js'] =
|
|
128
|
+
`import { createServer } from 'node:http';
|
|
129
|
+
import { render } from './src/entry-server.${ext === 'ts' ? 'js' : 'js'}';
|
|
130
|
+
|
|
131
|
+
// Minimal SSR server. In production, build with \`npm run build:server\`
|
|
132
|
+
// and serve the compiled output. This dev server renders on each request.
|
|
133
|
+
const PORT = process.env.PORT || 5173;
|
|
134
|
+
|
|
135
|
+
createServer((req, res) => {
|
|
136
|
+
const appHtml = render();
|
|
137
|
+
res.setHeader('content-type', 'text/html');
|
|
138
|
+
res.end(\`<!DOCTYPE html>
|
|
139
|
+
<html><head><meta charset="utf-8"><title>${opts.projectName}</title></head>
|
|
140
|
+
<body><div id="app">\${appHtml}</div></body></html>\`);
|
|
141
|
+
}).listen(PORT, () => console.log('SSR server on http://localhost:' + PORT));
|
|
142
|
+
`;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ── i18n add-on ──────────────────────────────────────────────────────────--
|
|
146
|
+
if (opts.i18n) {
|
|
147
|
+
files['src/i18n.js'] =
|
|
148
|
+
`import { createI18n } from '@ozsarman/clarityjs/i18n';
|
|
149
|
+
|
|
150
|
+
export const i18n = createI18n({
|
|
151
|
+
locale: 'en',
|
|
152
|
+
fallbackLocale: 'en',
|
|
153
|
+
messages: {
|
|
154
|
+
en: { greeting: 'Hello, Clarity!', count: 'Count is {n}' },
|
|
155
|
+
tr: { greeting: 'Merhaba, Clarity!', count: 'Sayaç: {n}' },
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
export const t = i18n.t;
|
|
160
|
+
`;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ── TypeScript add-on ──────────────────────────────────────────────────────
|
|
164
|
+
if (opts.typescript) {
|
|
165
|
+
files['tsconfig.json'] = JSON.stringify({
|
|
166
|
+
compilerOptions: {
|
|
167
|
+
target: 'ES2022',
|
|
168
|
+
module: 'ESNext',
|
|
169
|
+
moduleResolution: 'bundler',
|
|
170
|
+
strict: true,
|
|
171
|
+
jsx: 'preserve',
|
|
172
|
+
types: ['vite/client'],
|
|
173
|
+
skipLibCheck: true,
|
|
174
|
+
noEmit: true,
|
|
175
|
+
},
|
|
176
|
+
include: ['src', 'pages'],
|
|
177
|
+
}, null, 2) + '\n';
|
|
178
|
+
|
|
179
|
+
files['src/clarity.d.ts'] =
|
|
180
|
+
`// Let TypeScript understand .clarity imports.
|
|
181
|
+
declare module '*.clarity' {
|
|
182
|
+
const component: (props?: Record<string, unknown>) => Node;
|
|
183
|
+
export const App: typeof component;
|
|
184
|
+
export default component;
|
|
185
|
+
}
|
|
186
|
+
${opts.router ? `
|
|
187
|
+
// The Vite scanPages plugin generates this virtual module from pages/.
|
|
188
|
+
declare module 'virtual:clarity-pages' {
|
|
189
|
+
export const routes: Array<{ pattern: string; file: string; load: () => Promise<unknown> }>;
|
|
190
|
+
export const Routes: (props?: Record<string, unknown>) => Node;
|
|
191
|
+
const _default: typeof Routes;
|
|
192
|
+
export default _default;
|
|
193
|
+
}
|
|
194
|
+
` : ''}`;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// ── Testing add-on ───────────────────────────────────────────────────────--
|
|
198
|
+
if (opts.testing) {
|
|
199
|
+
const testImport = opts.router
|
|
200
|
+
? "import Home from '../pages/index.clarity';"
|
|
201
|
+
: "import { App } from '../src/App.clarity';";
|
|
202
|
+
const testTarget = opts.router ? 'Home' : 'App';
|
|
203
|
+
files['test/app.test.js'] =
|
|
204
|
+
`import { test } from 'node:test';
|
|
205
|
+
import assert from 'node:assert';
|
|
206
|
+
import { render, fireEvent } from '@ozsarman/clarityjs/test';
|
|
207
|
+
${testImport}
|
|
208
|
+
|
|
209
|
+
test('page renders and is interactive', async () => {
|
|
210
|
+
const { container, getByText } = render(${testTarget});
|
|
211
|
+
assert.ok(container, 'component mounted');
|
|
212
|
+
|
|
213
|
+
const button = getByText(/count|click/i) || container.querySelector('button');
|
|
214
|
+
if (button) {
|
|
215
|
+
fireEvent.click(button);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
`;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ── README ───────────────────────────────────────────────────────────────--
|
|
222
|
+
files['README.md'] = readme(opts);
|
|
223
|
+
|
|
224
|
+
// ── .gitignore ─────────────────────────────────────────────────────────────
|
|
225
|
+
files['.gitignore'] = 'node_modules\ndist\n.DS_Store\n*.local\n';
|
|
226
|
+
|
|
227
|
+
return files;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ─── Component templates ────────────────────────────────────────────────────--
|
|
231
|
+
|
|
232
|
+
function appComponent(opts) {
|
|
233
|
+
// Plain counter app (no router), optionally i18n-aware. Shows beforeMount + onMount.
|
|
234
|
+
const greeting = opts.i18n ? `{ t('greeting') }` : `{ "Hello, Clarity!" }`;
|
|
235
|
+
const label = opts.i18n ? `{ t('count', { n: count }) }` : `{ "Count: " } { count }`;
|
|
236
|
+
const imports = opts.i18n ? `import { t } from './i18n.js'\n\n` : '';
|
|
237
|
+
return `${imports}component App() {
|
|
238
|
+
state count = 0
|
|
239
|
+
|
|
240
|
+
beforeMount {
|
|
241
|
+
console.log('about to mount — initial count:', count)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
onMount {
|
|
245
|
+
console.log('mounted!')
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
render {
|
|
249
|
+
<div class="card">
|
|
250
|
+
<h1>${greeting}</h1>
|
|
251
|
+
<p>${label}</p>
|
|
252
|
+
<button on:click={ count++ }>Increment</button>
|
|
253
|
+
</div>
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
`;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// A small shared nav used across file-based pages.
|
|
260
|
+
const NAV = ` <nav>
|
|
261
|
+
<Link to="/">Home</Link>
|
|
262
|
+
<Link to="/about">About</Link>
|
|
263
|
+
<Link to="/hello-world">Dynamic</Link>
|
|
264
|
+
</nav>`;
|
|
265
|
+
|
|
266
|
+
function indexPage(opts) {
|
|
267
|
+
const greeting = opts.i18n ? `{ t('greeting') }` : `{ "Welcome home" }`;
|
|
268
|
+
const imports = opts.i18n
|
|
269
|
+
? `import { Link } from '@ozsarman/clarityjs/router'\nimport { t } from '../src/i18n.js'\n\n`
|
|
270
|
+
: `import { Link } from '@ozsarman/clarityjs/router'\n\n`;
|
|
271
|
+
return `${imports}component Home() {
|
|
272
|
+
state count = 0
|
|
273
|
+
render {
|
|
274
|
+
<div class="card">
|
|
275
|
+
${NAV}
|
|
276
|
+
<h1>${greeting}</h1>
|
|
277
|
+
<button on:click={ count++ }>Clicked { count } times</button>
|
|
278
|
+
</div>
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
`;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function aboutPage() {
|
|
285
|
+
return `import { Link } from '@ozsarman/clarityjs/router'
|
|
286
|
+
|
|
287
|
+
component About() {
|
|
288
|
+
render {
|
|
289
|
+
<div class="card">
|
|
290
|
+
${NAV}
|
|
291
|
+
<h1>{ "About" }</h1>
|
|
292
|
+
<p>{ "Built with Clarity.js file-based routing." }</p>
|
|
293
|
+
</div>
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
`;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// pages/[slug].clarity → /:slug — receives a `params` prop from the pages router.
|
|
300
|
+
function dynamicPage() {
|
|
301
|
+
return `import { Link } from '@ozsarman/clarityjs/router'
|
|
302
|
+
|
|
303
|
+
component DynamicPage(params) {
|
|
304
|
+
render {
|
|
305
|
+
<div class="card">
|
|
306
|
+
${NAV}
|
|
307
|
+
<h1>{ "Dynamic route" }</h1>
|
|
308
|
+
<p>{ "slug = " } { params.slug }</p>
|
|
309
|
+
</div>
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
`;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// pages/not-found.clarity → shown when no route matches; receives a `path` prop.
|
|
316
|
+
function notFoundPage() {
|
|
317
|
+
return `import { Link } from '@ozsarman/clarityjs/router'
|
|
318
|
+
|
|
319
|
+
component NotFound(path) {
|
|
320
|
+
render {
|
|
321
|
+
<div class="card">
|
|
322
|
+
${NAV}
|
|
323
|
+
<h1>{ "404" }</h1>
|
|
324
|
+
<p>{ "No page at " } { path }</p>
|
|
325
|
+
</div>
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
`;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function readme(opts) {
|
|
332
|
+
const addons = [];
|
|
333
|
+
if (opts.typescript) addons.push('TypeScript');
|
|
334
|
+
if (opts.router) addons.push('Router');
|
|
335
|
+
if (opts.i18n) addons.push('i18n');
|
|
336
|
+
if (opts.testing) addons.push('Testing');
|
|
337
|
+
const addonLine = addons.length ? `\n**Add-ons:** ${addons.join(', ')}\n` : '';
|
|
338
|
+
|
|
339
|
+
return `# ${opts.projectName}
|
|
340
|
+
|
|
341
|
+
A ${opts.ssr ? 'server-rendered' : 'single-page'} app built with [Clarity.js](https://gitlab.com/ozsarman/clarity.js).
|
|
342
|
+
${addonLine}
|
|
343
|
+
## Getting started
|
|
344
|
+
|
|
345
|
+
\`\`\`bash
|
|
346
|
+
npm install
|
|
347
|
+
npm run dev
|
|
348
|
+
\`\`\`
|
|
349
|
+
|
|
350
|
+
${opts.ssr ? '## SSR\n\n```bash\nnpm run build:server\nnpm run serve\n```\n\n' : ''}## Scripts
|
|
351
|
+
|
|
352
|
+
- \`npm run dev\` — start the Vite dev server
|
|
353
|
+
- \`npm run build\` — production build
|
|
354
|
+
- \`npm run preview\` — preview the production build
|
|
355
|
+
${opts.testing ? '- `npm test` — run the test suite\n' : ''}${opts.typescript ? '- `npm run typecheck` — type-check with tsc\n' : ''}
|
|
356
|
+
${opts.router ? `## Routing
|
|
357
|
+
|
|
358
|
+
File-based: every \`.clarity\` file under \`pages/\` becomes a route.
|
|
359
|
+
|
|
360
|
+
\`\`\`
|
|
361
|
+
pages/index.clarity → /
|
|
362
|
+
pages/about.clarity → /about
|
|
363
|
+
pages/[slug].clarity → /:slug (params.slug)
|
|
364
|
+
pages/not-found.clarity → 404 fallback
|
|
365
|
+
\`\`\`
|
|
366
|
+
|
|
367
|
+
The Vite plugin (\`scanPages: true\`) generates the route table and a \`Routes\`
|
|
368
|
+
component, imported in \`src/main.${opts.typescript ? 'ts' : 'js'}\` from \`virtual:clarity-pages\`.
|
|
369
|
+
|
|
370
|
+
` : ''}## Project structure
|
|
371
|
+
|
|
372
|
+
\`\`\`
|
|
373
|
+
${opts.router ? `pages/ # file-based routes
|
|
374
|
+
index.clarity
|
|
375
|
+
about.clarity
|
|
376
|
+
[slug].clarity
|
|
377
|
+
not-found.clarity
|
|
378
|
+
` : ''}src/
|
|
379
|
+
${opts.router ? '' : ` App.clarity # root component
|
|
380
|
+
`} main.${opts.typescript ? 'ts' : 'js'} # entry point
|
|
381
|
+
${opts.i18n ? ' i18n.js # translations\n' : ''} styles.css
|
|
382
|
+
\`\`\`
|
|
383
|
+
`;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
module.exports = { buildFiles };
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "create-clarity",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Scaffold a new Clarity.js app — npm create clarity@latest",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"bin": {
|
|
7
|
+
"create-clarity": "bin/create-clarity.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin/",
|
|
11
|
+
"lib/",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=16"
|
|
16
|
+
},
|
|
17
|
+
"keywords": ["clarity", "@ozsarman/clarityjs", "create", "scaffold", "starter", "template"],
|
|
18
|
+
"author": "Claude (Anthropic) + Özdemir Sarman",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://gitlab.com/ozsarman/clarity.js.git",
|
|
23
|
+
"directory": "packages/create-clarity"
|
|
24
|
+
}
|
|
25
|
+
}
|