create-usebecky 0.0.0-stage → 0.2.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 +80 -2
- package/dist/create.js +842 -0
- package/dist/extension-skill/usebecky-extension/SKILL.md +181 -0
- package/dist/extension-skill/usebecky-extension/references/accounts.md +125 -0
- package/dist/extension-skill/usebecky-extension/references/dev-server.md +102 -0
- package/dist/extension-skill/usebecky-extension/references/manifest.md +104 -0
- package/dist/extension-skill/usebecky-extension/references/oauth-and-tokens.md +179 -0
- package/dist/extension-skill/usebecky-extension/references/partner-api.md +87 -0
- package/dist/extension-skill/usebecky-extension/references/submit-and-pricing.md +31 -0
- package/dist/extension-skill/usebecky-extension/references/ui.md +172 -0
- package/dist/extension-skill/usebecky-extension/references/webhooks.md +97 -0
- package/dist/index.js +10 -0
- package/dist/keys.js +72 -0
- package/dist/platform.js +104 -0
- package/dist/pm.js +69 -0
- package/dist/prompts.js +242 -0
- package/dist/scaffold.js +108 -0
- package/dist/setup.js +130 -0
- package/dist/shared/ai.js +203 -0
- package/dist/shared/extension-skill.js +107 -0
- package/dist/shared/framework.js +460 -0
- package/dist/shared/hosted.js +8 -0
- package/dist/shared/ui.js +141 -0
- package/dist/skill/usebecky/SKILL.md +339 -0
- package/dist/skill/usebecky/references/access-rules.md +307 -0
- package/dist/skill/usebecky/references/addons.md +68 -0
- package/dist/skill/usebecky/references/analytics.md +166 -0
- package/dist/skill/usebecky/references/android.md +434 -0
- package/dist/skill/usebecky/references/auth.md +379 -0
- package/dist/skill/usebecky/references/cache.md +51 -0
- package/dist/skill/usebecky/references/components.md +127 -0
- package/dist/skill/usebecky/references/emails.md +105 -0
- package/dist/skill/usebecky/references/expo.md +147 -0
- package/dist/skill/usebecky/references/functions.md +99 -0
- package/dist/skill/usebecky/references/hosting.md +119 -0
- package/dist/skill/usebecky/references/http-api.md +233 -0
- package/dist/skill/usebecky/references/ios.md +355 -0
- package/dist/skill/usebecky/references/payments.md +493 -0
- package/dist/skill/usebecky/references/realtime.md +143 -0
- package/dist/skill/usebecky/references/scheduled-jobs.md +82 -0
- package/dist/skill/usebecky/references/sdk.md +838 -0
- package/dist/skill/usebecky/references/storage.md +210 -0
- package/dist/skill/usebecky/references/templates.md +191 -0
- package/dist/skill/usebecky/references/wordpress.md +87 -0
- package/dist/templates.js +74 -0
- package/package.json +37 -4
- package/templates/expo/App.tsx +48 -0
- package/templates/expo/README.md +59 -0
- package/templates/expo/_env.example +4 -0
- package/templates/expo/_gitignore +12 -0
- package/templates/expo/app.json +25 -0
- package/templates/expo/assets/icon.png +0 -0
- package/templates/expo/brand.ts +7 -0
- package/templates/expo/components/Avatar.tsx +75 -0
- package/templates/expo/components/ui.tsx +57 -0
- package/templates/expo/index.ts +4 -0
- package/templates/expo/lib/theme.ts +43 -0
- package/templates/expo/lib/usebecky.ts +15 -0
- package/templates/expo/package.json +29 -0
- package/templates/expo/screens/Home.tsx +187 -0
- package/templates/expo/screens/SignIn.tsx +112 -0
- package/templates/expo/tsconfig.json +6 -0
- package/templates/expo/usebecky.template.json +36 -0
- package/templates/extension/README.md +128 -0
- package/templates/extension/_env.example +21 -0
- package/templates/extension/_gitignore +11 -0
- package/templates/extension/logo.svg +1 -0
- package/templates/extension/package.json +25 -0
- package/templates/extension/src/accounts.ts +90 -0
- package/templates/extension/src/app.ts +178 -0
- package/templates/extension/src/pages.ts +108 -0
- package/templates/extension/src/replay-guard.ts +17 -0
- package/templates/extension/src/server.ts +57 -0
- package/templates/extension/src/settings.ts +69 -0
- package/templates/extension/src/ui.ts +70 -0
- package/templates/extension/test/app.test.ts +222 -0
- package/templates/extension/tsconfig.json +16 -0
- package/templates/extension/usebecky-extension.json +36 -0
- package/templates/extension/usebecky.template.json +22 -0
- package/templates/landing/README.md +43 -0
- package/templates/landing/_env.example +4 -0
- package/templates/landing/_gitignore +9 -0
- package/templates/landing/index.html +16 -0
- package/templates/landing/package.json +28 -0
- package/templates/landing/public/icon.svg +1 -0
- package/templates/landing/src/App.tsx +97 -0
- package/templates/landing/src/components/Contact.tsx +43 -0
- package/templates/landing/src/components/Counter.tsx +34 -0
- package/templates/landing/src/components/Feedback.tsx +36 -0
- package/templates/landing/src/components/Logo.tsx +11 -0
- package/templates/landing/src/components/Receipt.tsx +128 -0
- package/templates/landing/src/components/Setup.tsx +20 -0
- package/templates/landing/src/components/fields.tsx +63 -0
- package/templates/landing/src/content.ts +22 -0
- package/templates/landing/src/index.css +119 -0
- package/templates/landing/src/lib/usebecky.ts +11 -0
- package/templates/landing/src/lib/waitlist.ts +46 -0
- package/templates/landing/src/main.tsx +12 -0
- package/templates/landing/src/vite-env.d.ts +6 -0
- package/templates/landing/tsconfig.json +19 -0
- package/templates/landing/usebecky.template.json +20 -0
- package/templates/landing/vite.config.ts +10 -0
- package/templates/notes/README.md +68 -0
- package/templates/notes/_env.example +4 -0
- package/templates/notes/_gitignore +9 -0
- package/templates/notes/index.html +15 -0
- package/templates/notes/package.json +28 -0
- package/templates/notes/public/icon.svg +1 -0
- package/templates/notes/src/App.tsx +35 -0
- package/templates/notes/src/brand.ts +7 -0
- package/templates/notes/src/components/Composer.tsx +113 -0
- package/templates/notes/src/components/NoteEntry.tsx +151 -0
- package/templates/notes/src/components/Notes.tsx +163 -0
- package/templates/notes/src/components/Setup.tsx +44 -0
- package/templates/notes/src/components/SignIn.tsx +112 -0
- package/templates/notes/src/components/icons.tsx +54 -0
- package/templates/notes/src/index.css +105 -0
- package/templates/notes/src/lib/notes.ts +68 -0
- package/templates/notes/src/lib/usebecky.ts +9 -0
- package/templates/notes/src/main.tsx +22 -0
- package/templates/notes/src/vite-env.d.ts +6 -0
- package/templates/notes/tsconfig.json +19 -0
- package/templates/notes/usebecky.template.json +34 -0
- package/templates/notes/vite.config.ts +10 -0
- package/templates/saas/README.md +69 -0
- package/templates/saas/_env.example +10 -0
- package/templates/saas/_gitignore +11 -0
- package/templates/saas/app/(app)/account/ManageBilling.tsx +26 -0
- package/templates/saas/app/(app)/account/page.tsx +71 -0
- package/templates/saas/app/(app)/dashboard/Projects.tsx +227 -0
- package/templates/saas/app/(app)/dashboard/page.tsx +21 -0
- package/templates/saas/app/(app)/layout.tsx +29 -0
- package/templates/saas/app/(app)/premium/page.tsx +62 -0
- package/templates/saas/app/auth/callback/page.tsx +33 -0
- package/templates/saas/app/globals.css +143 -0
- package/templates/saas/app/icon.svg +1 -0
- package/templates/saas/app/layout.tsx +20 -0
- package/templates/saas/app/page.tsx +87 -0
- package/templates/saas/app/pricing/Plans.tsx +133 -0
- package/templates/saas/app/pricing/TestCardHint.tsx +18 -0
- package/templates/saas/app/pricing/page.tsx +21 -0
- package/templates/saas/app/setup/page.tsx +34 -0
- package/templates/saas/app/sign-in/SignInForm.tsx +195 -0
- package/templates/saas/app/sign-in/page.tsx +21 -0
- package/templates/saas/brand.ts +10 -0
- package/templates/saas/components/AppNav.tsx +45 -0
- package/templates/saas/components/Logo.tsx +22 -0
- package/templates/saas/components/SiteHeader.tsx +50 -0
- package/templates/saas/components/Status.tsx +15 -0
- package/templates/saas/lib/session.ts +26 -0
- package/templates/saas/lib/usebecky.ts +11 -0
- package/templates/saas/next-env.d.ts +5 -0
- package/templates/saas/next.config.ts +5 -0
- package/templates/saas/package.json +26 -0
- package/templates/saas/postcss.config.mjs +3 -0
- package/templates/saas/tsconfig.json +21 -0
- package/templates/saas/usebecky.template.json +31 -0
package/dist/keys.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Writing the project URL and publishable key into the new app.
|
|
2
|
+
// node starters: the same env file and variable names as `npx usebecky init` (shared/framework.ts)
|
|
3
|
+
// xcode/gradle starters: `config.file` in xcconfig / properties / dotenv format
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { ensureGitignore, envPlanFor, writeEnvFile } from "./shared/framework.js";
|
|
7
|
+
/** Env prefix the framework uses for browser-visible variables. */
|
|
8
|
+
export function publicPrefix(framework) {
|
|
9
|
+
switch (framework) {
|
|
10
|
+
case 'next':
|
|
11
|
+
return 'NEXT_PUBLIC_';
|
|
12
|
+
case 'vite':
|
|
13
|
+
case 'remix':
|
|
14
|
+
return 'VITE_';
|
|
15
|
+
case 'expo':
|
|
16
|
+
return 'EXPO_PUBLIC_';
|
|
17
|
+
case 'nuxt':
|
|
18
|
+
return 'NUXT_PUBLIC_';
|
|
19
|
+
case 'sveltekit':
|
|
20
|
+
return 'PUBLIC_';
|
|
21
|
+
default:
|
|
22
|
+
return '';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export async function writeKeys(dir, manifest, keys, extra = {}) {
|
|
26
|
+
const extraVars = (prefix) => Object.entries(extra).map(([name, value]) => ({ name: `${prefix}${name}`, value }));
|
|
27
|
+
if (manifest.kind === 'node') {
|
|
28
|
+
const plan = envPlanFor(manifest.framework, keys);
|
|
29
|
+
const vars = [...plan.vars, ...extraVars(publicPrefix(manifest.framework))];
|
|
30
|
+
// A brand-new folder has no env file yet, so nothing is ever replaced without asking.
|
|
31
|
+
const result = await writeEnvFile(path.join(dir, plan.file), vars, () => false);
|
|
32
|
+
const gi = ensureGitignore(dir, ['.env', '.env.local']);
|
|
33
|
+
return { file: plan.file, created: result.created, names: [...result.added, ...result.updated], gitignoreAdded: gi.added };
|
|
34
|
+
}
|
|
35
|
+
const spec = manifest.config;
|
|
36
|
+
const vars = [{ name: spec.url, value: keys.url }, { name: spec.publishableKey, value: keys.publishableKey }, ...extraVars('')];
|
|
37
|
+
const created = !fs.existsSync(path.join(dir, spec.file));
|
|
38
|
+
if (spec.format === 'dotenv')
|
|
39
|
+
await writeEnvFile(path.join(dir, spec.file), vars, () => true);
|
|
40
|
+
else
|
|
41
|
+
writeKeyValueFile(path.join(dir, spec.file), spec.format, vars);
|
|
42
|
+
const gi = ensureGitignore(dir, [path.basename(spec.file)]);
|
|
43
|
+
return { file: spec.file, created, names: vars.map((v) => v.name), gitignoreAdded: gi.added };
|
|
44
|
+
}
|
|
45
|
+
/** `KEY = value` (xcconfig) or `key=value` (properties): replace existing keys, append new ones. */
|
|
46
|
+
export function writeKeyValueFile(file, format, vars) {
|
|
47
|
+
const lines = fs.existsSync(file) ? fs.readFileSync(file, 'utf8').split(/\r?\n/) : [];
|
|
48
|
+
if (lines.length && lines[lines.length - 1] === '')
|
|
49
|
+
lines.pop();
|
|
50
|
+
const fmt = (v) => (format === 'xcconfig' ? `${v.name} = ${xcconfigValue(v.value)}` : `${v.name}=${v.value.replace(/\\/g, '\\\\')}`);
|
|
51
|
+
const comment = format === 'xcconfig' ? '//' : '#';
|
|
52
|
+
const toAppend = [];
|
|
53
|
+
for (const v of vars) {
|
|
54
|
+
const re = new RegExp(`^\\s*${v.name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*=`);
|
|
55
|
+
const i = lines.findIndex((l) => re.test(l));
|
|
56
|
+
if (i >= 0)
|
|
57
|
+
lines[i] = fmt(v);
|
|
58
|
+
else
|
|
59
|
+
toAppend.push(fmt(v));
|
|
60
|
+
}
|
|
61
|
+
if (toAppend.length) {
|
|
62
|
+
if (lines.length)
|
|
63
|
+
lines.push('');
|
|
64
|
+
lines.push(`${comment} usebecky: values from Dashboard → Project → API keys (written by create-usebecky)`, ...toAppend);
|
|
65
|
+
}
|
|
66
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
67
|
+
fs.writeFileSync(file, lines.join('\n') + '\n', { mode: 0o600 });
|
|
68
|
+
}
|
|
69
|
+
/** In .xcconfig files `//` starts a comment, so URLs are written as `https:/$()/host`. */
|
|
70
|
+
export function xcconfigValue(value) {
|
|
71
|
+
return value.replace(/\/\//g, '/$()/');
|
|
72
|
+
}
|
package/dist/platform.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// The platform API (the one the dashboard and `npx usebecky` use): sign in, projects, keys,
|
|
2
|
+
// and the admin APIs for tables, buckets, realtime and forms. Same conventions as the SDK CLI.
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { HOSTED_API_URL } from "./shared/hosted.js";
|
|
6
|
+
export const VERSION = '0.2.0';
|
|
7
|
+
/** The platform API: the hosted one, unless --api-url / USEBECKY_API_URL say otherwise. */
|
|
8
|
+
export const DEFAULT_API = HOSTED_API_URL;
|
|
9
|
+
/** "create-usebecky/0.1.0 (macOS; Node.js 22.11.0)": the dashboard lists its sessions as "usebecky CLI on macOS". */
|
|
10
|
+
const USER_AGENT = `create-usebecky/${VERSION} (${process.platform === 'darwin' ? 'macOS' : process.platform === 'win32' ? 'Windows' : process.platform === 'linux' ? 'Linux' : process.platform}; Node.js ${process.versions.node})`;
|
|
11
|
+
export function readConfig(file) {
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(fs.readFileSync(file, 'utf8')) ?? {};
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return {};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function saveConfig(file, next) {
|
|
20
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
21
|
+
fs.writeFileSync(file, JSON.stringify(next, null, 2) + '\n', { mode: 0o600 });
|
|
22
|
+
}
|
|
23
|
+
/** The stored token only counts for the API it was issued by (same rule as the SDK CLI). */
|
|
24
|
+
export function tokenFor(config, apiUrl, env) {
|
|
25
|
+
if (env.USEBECKY_ACCESS_TOKEN)
|
|
26
|
+
return env.USEBECKY_ACCESS_TOKEN;
|
|
27
|
+
if (config.apiUrl && config.apiUrl.replace(/\/+$/, '') !== apiUrl)
|
|
28
|
+
return undefined;
|
|
29
|
+
return config.token;
|
|
30
|
+
}
|
|
31
|
+
export function createPlatform(apiUrl, fetchImpl = fetch) {
|
|
32
|
+
let token;
|
|
33
|
+
async function request(method, p, body, opts = {}) {
|
|
34
|
+
let res;
|
|
35
|
+
try {
|
|
36
|
+
res = await fetchImpl(`${apiUrl}${opts.base ?? '/platform/v1'}${p}`, {
|
|
37
|
+
method,
|
|
38
|
+
headers: {
|
|
39
|
+
'content-type': 'application/json',
|
|
40
|
+
'x-client-info': `create-usebecky/${VERSION}`,
|
|
41
|
+
'user-agent': USER_AGENT,
|
|
42
|
+
...(opts.auth !== false && token ? { authorization: `Bearer ${token}` } : {}),
|
|
43
|
+
},
|
|
44
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
45
|
+
signal: AbortSignal.timeout(60_000),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
const timedOut = err?.name === 'TimeoutError';
|
|
50
|
+
return {
|
|
51
|
+
data: null,
|
|
52
|
+
error: timedOut
|
|
53
|
+
? { code: 'timeout', message: `usebecky at ${apiUrl} did not answer in time.`, hint: 'Try again in a moment.', status: 0 }
|
|
54
|
+
: { code: 'network_error', message: `Could not reach usebecky at ${apiUrl}.`, hint: offlineHint(apiUrl), status: 0 },
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const text = await res.text();
|
|
58
|
+
let json = null;
|
|
59
|
+
try {
|
|
60
|
+
json = text ? JSON.parse(text) : null;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
json = text;
|
|
64
|
+
}
|
|
65
|
+
if (!res.ok) {
|
|
66
|
+
const e = json?.error ?? {};
|
|
67
|
+
return { data: null, error: { code: e.code ?? `http_${res.status}`, message: e.message ?? `Request failed (${res.status}).`, hint: e.hint, status: res.status } };
|
|
68
|
+
}
|
|
69
|
+
return { data: json, error: null };
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
url: apiUrl,
|
|
73
|
+
setToken: (t) => (token = t),
|
|
74
|
+
get token() {
|
|
75
|
+
return token;
|
|
76
|
+
},
|
|
77
|
+
request,
|
|
78
|
+
health: async () => {
|
|
79
|
+
try {
|
|
80
|
+
const r = await fetchImpl(`${apiUrl}/healthz`, { signal: AbortSignal.timeout(8000) });
|
|
81
|
+
return r.ok;
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
login: (email, password, deviceId) => request('POST', '/auth/login', { email, password, device_id: deviceId }, { auth: false }),
|
|
88
|
+
signup: (email, password, deviceId) => request('POST', '/auth/signup', { email, password, device_id: deviceId }, { auth: false }),
|
|
89
|
+
/** The browser sign-in: start it, then ask every `interval` seconds until it is allowed, cancelled or expired. */
|
|
90
|
+
startCliLogin: (deviceId) => request('POST', '/auth/cli/sessions', { device_id: deviceId }, { auth: false }),
|
|
91
|
+
pollCliLogin: (id) => request('POST', '/auth/cli/sessions/token', { id }, { auth: false }),
|
|
92
|
+
config: () => request('GET', '/config', undefined, { auth: false }),
|
|
93
|
+
templates: () => request('GET', '/templates', undefined, { auth: false }),
|
|
94
|
+
projects: () => request('GET', '/projects'),
|
|
95
|
+
project: (ref) => request('GET', `/projects/${encodeURIComponent(ref)}`),
|
|
96
|
+
createProject: (name, template) => request('POST', '/projects', { name, template }),
|
|
97
|
+
keys: (ref) => request('GET', `/projects/${encodeURIComponent(ref)}/keys`),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
export function offlineHint(apiUrl) {
|
|
101
|
+
return apiUrl === HOSTED_API_URL
|
|
102
|
+
? 'Check your internet connection and try again.'
|
|
103
|
+
: `Check your internet connection and the address (${apiUrl}, from --api-url or USEBECKY_API_URL).`;
|
|
104
|
+
}
|
package/dist/pm.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Package managers: detect the one that ran us, install, and build the "run dev" command.
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
export const PACKAGE_MANAGERS = ['npm', 'pnpm', 'yarn', 'bun'];
|
|
4
|
+
/** `npm create`, `pnpm create`, `yarn create` and `bunx` set npm_config_user_agent ("pnpm/9.12.0 npm/? node/v22..."). */
|
|
5
|
+
export function detectPackageManager(userAgent) {
|
|
6
|
+
const name = (userAgent ?? '').split(' ')[0]?.split('/')[0];
|
|
7
|
+
return PACKAGE_MANAGERS.includes(name) ? name : 'npm';
|
|
8
|
+
}
|
|
9
|
+
export function installArgs(pm) {
|
|
10
|
+
return pm === 'yarn' ? [] : ['install'];
|
|
11
|
+
}
|
|
12
|
+
/** "npm run dev", "pnpm dev", "yarn dev", "bun run dev". */
|
|
13
|
+
export function runScript(pm, script) {
|
|
14
|
+
if (pm === 'npm')
|
|
15
|
+
return `npm run ${script}`;
|
|
16
|
+
if (pm === 'bun')
|
|
17
|
+
return `bun run ${script}`;
|
|
18
|
+
return `${pm} ${script}`;
|
|
19
|
+
}
|
|
20
|
+
export function installCommand(pm) {
|
|
21
|
+
return [pm, ...installArgs(pm)].join(' ');
|
|
22
|
+
}
|
|
23
|
+
/** Run a command, capturing its output (shown only if it fails). */
|
|
24
|
+
export const runCommand = (command, args, cwd) => new Promise((resolve) => {
|
|
25
|
+
let output = '';
|
|
26
|
+
let child;
|
|
27
|
+
try {
|
|
28
|
+
child = spawn(command, args, {
|
|
29
|
+
cwd,
|
|
30
|
+
shell: process.platform === 'win32',
|
|
31
|
+
env: { ...process.env, ADBLOCK: '1', DISABLE_OPENCOLLECTIVE: '1', NODE_ENV: undefined },
|
|
32
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
resolve({ code: 1, output: String(err), notFound: true });
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const keep = (chunk) => {
|
|
40
|
+
output += chunk.toString();
|
|
41
|
+
if (output.length > 200_000)
|
|
42
|
+
output = output.slice(-100_000);
|
|
43
|
+
};
|
|
44
|
+
child.stdout?.on('data', keep);
|
|
45
|
+
child.stderr?.on('data', keep);
|
|
46
|
+
child.on('error', (err) => resolve({ code: 1, output: output + String(err), notFound: err.code === 'ENOENT' }));
|
|
47
|
+
child.on('close', (code) => resolve({ code: code ?? 1, output }));
|
|
48
|
+
});
|
|
49
|
+
/** The starters guide (install troubleshooting). */
|
|
50
|
+
export const STARTERS_DOCS = 'https://usebecky.com/docs/getting-started/starters';
|
|
51
|
+
/** Plain-English reason for a failed install, from its output. */
|
|
52
|
+
export function explainInstallFailure(output) {
|
|
53
|
+
// The registry couldn't find the usebecky package (a mirror or a private registry without it).
|
|
54
|
+
if (/E404|\b404\b/.test(output) && /registry\.[^\s/]+\/usebecky\b(?!-)|['"]usebecky@/i.test(output)) {
|
|
55
|
+
return `Your package registry doesn't have the usebecky package. Check that npm uses the public registry (npm config get registry), then run npm install usebecky in your app's folder (help: ${STARTERS_DOCS}).`;
|
|
56
|
+
}
|
|
57
|
+
if (/ENOTFOUND|EAI_AGAIN|ECONNREFUSED|ETIMEDOUT|ECONNRESET|network|getaddrinfo|socket hang up/i.test(output)) {
|
|
58
|
+
return 'Could not download packages. It looks like you are offline, or the package registry is unreachable.';
|
|
59
|
+
}
|
|
60
|
+
if (/EACCES|EPERM/i.test(output))
|
|
61
|
+
return 'Could not write to the folder (permission denied).';
|
|
62
|
+
if (/ENOSPC/i.test(output))
|
|
63
|
+
return 'Your disk is full.';
|
|
64
|
+
if (/E404|404 Not Found/i.test(output))
|
|
65
|
+
return 'A package could not be found on the registry.';
|
|
66
|
+
if (/ERESOLVE|peer dep/i.test(output))
|
|
67
|
+
return 'Some package versions do not fit together.';
|
|
68
|
+
return 'Installing packages failed.';
|
|
69
|
+
}
|
package/dist/prompts.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// Prompts with zero dependencies. In a terminal: arrow-key menus and hidden passwords. With piped
|
|
2
|
+
// input (tests, CI): one answer per line, menus answered by number. With `yes`: defaults, no input.
|
|
3
|
+
import readline from 'node:readline';
|
|
4
|
+
import { c } from "./shared/ui.js";
|
|
5
|
+
const CLEAR_LINE = '\u001b[2K';
|
|
6
|
+
const up = (n) => (n > 0 ? `\u001b[${n}A` : '');
|
|
7
|
+
const HIDE_CURSOR = '\u001b[?25l';
|
|
8
|
+
const SHOW_CURSOR = '\u001b[?25h';
|
|
9
|
+
export function createPrompts(io) {
|
|
10
|
+
const write = (s = '') => io.output.write(s + '\n');
|
|
11
|
+
const tty = !!io.input.isTTY && typeof io.input.setRawMode === 'function' && !!io.output.isTTY;
|
|
12
|
+
// ---- piped input: a queue of lines ---------------------------------------------------
|
|
13
|
+
let rl = null;
|
|
14
|
+
const queue = [];
|
|
15
|
+
let waiting = null;
|
|
16
|
+
let ended = false;
|
|
17
|
+
function readLine() {
|
|
18
|
+
if (!rl) {
|
|
19
|
+
rl = readline.createInterface({ input: io.input, terminal: false });
|
|
20
|
+
rl.on('line', (line) => (waiting ? (waiting(line), (waiting = null)) : queue.push(line)));
|
|
21
|
+
rl.on('close', () => {
|
|
22
|
+
ended = true;
|
|
23
|
+
if (waiting)
|
|
24
|
+
waiting(null), (waiting = null);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
if (queue.length)
|
|
28
|
+
return Promise.resolve(queue.shift());
|
|
29
|
+
if (ended)
|
|
30
|
+
return Promise.resolve(null);
|
|
31
|
+
return new Promise((resolve) => (waiting = resolve));
|
|
32
|
+
}
|
|
33
|
+
// ---- terminal: one readline per question ------------------------------------------------
|
|
34
|
+
function question(prompt) {
|
|
35
|
+
return new Promise((resolve) => {
|
|
36
|
+
const q = readline.createInterface({ input: io.input, output: io.output, terminal: true });
|
|
37
|
+
q.on('SIGINT', () => {
|
|
38
|
+
q.close();
|
|
39
|
+
cancel();
|
|
40
|
+
});
|
|
41
|
+
q.question(prompt, (answer) => {
|
|
42
|
+
q.close();
|
|
43
|
+
resolve(answer);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function cancel() {
|
|
48
|
+
io.output.write(`${SHOW_CURSOR}\n${c.dim('Cancelled. Nothing else was changed.')}\n`);
|
|
49
|
+
process.exit(130);
|
|
50
|
+
}
|
|
51
|
+
async function text(label, opts = {}) {
|
|
52
|
+
const def = opts.default ?? '';
|
|
53
|
+
if (io.yes)
|
|
54
|
+
return def;
|
|
55
|
+
for (;;) {
|
|
56
|
+
const prompt = `${c.cyan('?')} ${c.bold(label)}${def ? c.dim(` (${def})`) : ''} `;
|
|
57
|
+
let answer;
|
|
58
|
+
if (tty)
|
|
59
|
+
answer = await question(prompt);
|
|
60
|
+
else {
|
|
61
|
+
io.output.write(prompt);
|
|
62
|
+
const line = await readLine();
|
|
63
|
+
if (line === null) {
|
|
64
|
+
write();
|
|
65
|
+
return def;
|
|
66
|
+
}
|
|
67
|
+
write(line);
|
|
68
|
+
answer = line;
|
|
69
|
+
}
|
|
70
|
+
const value = answer.trim() || def;
|
|
71
|
+
const problem = opts.validate?.(value) ?? null;
|
|
72
|
+
if (!problem)
|
|
73
|
+
return value;
|
|
74
|
+
write(` ${c.yellow(problem)}`);
|
|
75
|
+
if (!tty && ended)
|
|
76
|
+
return def;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function password(label) {
|
|
80
|
+
if (io.yes)
|
|
81
|
+
return '';
|
|
82
|
+
if (!tty)
|
|
83
|
+
return text(label);
|
|
84
|
+
io.output.write(`${c.cyan('?')} ${c.bold(label)} `);
|
|
85
|
+
const input = io.input;
|
|
86
|
+
return new Promise((resolve) => {
|
|
87
|
+
let value = '';
|
|
88
|
+
input.setRawMode(true);
|
|
89
|
+
input.resume();
|
|
90
|
+
const onData = (buf) => {
|
|
91
|
+
for (const ch of buf.toString('utf8')) {
|
|
92
|
+
if (ch === '\r' || ch === '\n' || ch === '\u0004') {
|
|
93
|
+
input.setRawMode(false);
|
|
94
|
+
input.removeListener('data', onData);
|
|
95
|
+
input.pause();
|
|
96
|
+
io.output.write('\n');
|
|
97
|
+
return resolve(value);
|
|
98
|
+
}
|
|
99
|
+
if (ch === '\u0003') {
|
|
100
|
+
input.setRawMode(false);
|
|
101
|
+
cancel();
|
|
102
|
+
}
|
|
103
|
+
if (ch === '\u007f' || ch === '\b') {
|
|
104
|
+
if (value.length) {
|
|
105
|
+
value = value.slice(0, -1);
|
|
106
|
+
io.output.write('\b \b');
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
else if (ch >= ' ') {
|
|
110
|
+
value += ch;
|
|
111
|
+
io.output.write('•');
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
input.on('data', onData);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
async function confirm(label, defaultYes = true) {
|
|
119
|
+
if (io.yes)
|
|
120
|
+
return defaultYes;
|
|
121
|
+
const answer = (await text(`${label} ${c.dim(defaultYes ? '[Y/n]' : '[y/N]')}`)).toLowerCase();
|
|
122
|
+
if (!answer)
|
|
123
|
+
return defaultYes;
|
|
124
|
+
return answer === 'y' || answer === 'yes';
|
|
125
|
+
}
|
|
126
|
+
async function select(label, choices, defaultIndex = 0) {
|
|
127
|
+
if (io.yes || choices.length === 1)
|
|
128
|
+
return choices[defaultIndex].value;
|
|
129
|
+
const width = Math.max(...choices.map((ch) => visible(ch.label).length));
|
|
130
|
+
// Keep every row on one line: a wrapped row would break the arrow-key redraw.
|
|
131
|
+
const columns = io.output.columns || 80;
|
|
132
|
+
const row = (ch) => {
|
|
133
|
+
const room = columns - 4 - width - 2;
|
|
134
|
+
const hint = ch.hint && room > 12 ? (ch.hint.length > room ? `${ch.hint.slice(0, room - 1).trimEnd()}…` : ch.hint) : '';
|
|
135
|
+
return `${pad(ch.label, width)}${hint ? ` ${c.dim(hint)}` : ''}`;
|
|
136
|
+
};
|
|
137
|
+
if (!tty) {
|
|
138
|
+
write(`${c.cyan('?')} ${c.bold(label)}`);
|
|
139
|
+
choices.forEach((ch, i) => write(` ${c.bold(String(i + 1))}) ${row(ch)}`));
|
|
140
|
+
for (;;) {
|
|
141
|
+
const answer = await text('Enter a number', { default: String(defaultIndex + 1) });
|
|
142
|
+
const n = Number(answer);
|
|
143
|
+
if (Number.isInteger(n) && n >= 1 && n <= choices.length)
|
|
144
|
+
return choices[n - 1].value;
|
|
145
|
+
write(c.yellow(` Please enter a number from 1 to ${choices.length}.`));
|
|
146
|
+
if (ended)
|
|
147
|
+
return choices[defaultIndex].value;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const input = io.input;
|
|
151
|
+
let index = defaultIndex;
|
|
152
|
+
let drawn = 0;
|
|
153
|
+
const draw = () => {
|
|
154
|
+
let out = up(drawn);
|
|
155
|
+
const lines = [`${c.cyan('?')} ${c.bold(label)} ${c.dim('(use ↑ ↓ and Enter)')}`, ...choices.map((ch, i) => (i === index ? `${c.cyan('❯')} ${c.cyan(row(ch))}` : ` ${row(ch)}`))];
|
|
156
|
+
for (const line of lines)
|
|
157
|
+
out += `\r${CLEAR_LINE}${line}\n`;
|
|
158
|
+
io.output.write(out);
|
|
159
|
+
drawn = lines.length;
|
|
160
|
+
};
|
|
161
|
+
return new Promise((resolve) => {
|
|
162
|
+
readline.emitKeypressEvents(input);
|
|
163
|
+
input.setRawMode(true);
|
|
164
|
+
input.resume();
|
|
165
|
+
io.output.write(HIDE_CURSOR);
|
|
166
|
+
draw();
|
|
167
|
+
const onKey = (str, key = {}) => {
|
|
168
|
+
if (key.ctrl && key.name === 'c') {
|
|
169
|
+
input.setRawMode(false);
|
|
170
|
+
cancel();
|
|
171
|
+
}
|
|
172
|
+
if (key.name === 'up' || key.name === 'k')
|
|
173
|
+
index = (index - 1 + choices.length) % choices.length;
|
|
174
|
+
else if (key.name === 'down' || key.name === 'j' || key.name === 'tab')
|
|
175
|
+
index = (index + 1) % choices.length;
|
|
176
|
+
else if (str && /^[1-9]$/.test(str) && Number(str) <= choices.length)
|
|
177
|
+
index = Number(str) - 1;
|
|
178
|
+
else if (key.name === 'return' || key.name === 'enter') {
|
|
179
|
+
input.removeListener('keypress', onKey);
|
|
180
|
+
input.setRawMode(false);
|
|
181
|
+
input.pause();
|
|
182
|
+
// Replace the menu with a one-line answer.
|
|
183
|
+
let out = up(drawn);
|
|
184
|
+
for (let i = 0; i < drawn; i++)
|
|
185
|
+
out += `\r${CLEAR_LINE}\n`;
|
|
186
|
+
out += up(drawn) + `\r${CLEAR_LINE}${c.green('✔')} ${c.bold(label)} ${c.cyan(visible(choices[index].label).trim())}\n`;
|
|
187
|
+
io.output.write(out + SHOW_CURSOR);
|
|
188
|
+
return resolve(choices[index].value);
|
|
189
|
+
}
|
|
190
|
+
else
|
|
191
|
+
return;
|
|
192
|
+
draw();
|
|
193
|
+
};
|
|
194
|
+
input.on('keypress', onKey);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
/** A one-line progress indicator. In a terminal it animates; otherwise it prints the result line only. */
|
|
198
|
+
function spinner(textNow) {
|
|
199
|
+
const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
|
|
200
|
+
let i = 0;
|
|
201
|
+
let current = textNow;
|
|
202
|
+
const started = Date.now();
|
|
203
|
+
const timer = tty
|
|
204
|
+
? setInterval(() => io.output.write(`\r${CLEAR_LINE}${c.cyan(frames[i++ % frames.length])} ${current}${elapsed()}`), 80)
|
|
205
|
+
: null;
|
|
206
|
+
if (tty)
|
|
207
|
+
io.output.write(HIDE_CURSOR);
|
|
208
|
+
function elapsed() {
|
|
209
|
+
const s = Math.round((Date.now() - started) / 1000);
|
|
210
|
+
return s >= 3 ? c.dim(` ${s}s`) : '';
|
|
211
|
+
}
|
|
212
|
+
const finish = (symbol, msg) => {
|
|
213
|
+
if (timer)
|
|
214
|
+
clearInterval(timer);
|
|
215
|
+
io.output.write(`${tty ? `\r${CLEAR_LINE}` : ''}${symbol} ${msg}\n${tty ? SHOW_CURSOR : ''}`);
|
|
216
|
+
};
|
|
217
|
+
return {
|
|
218
|
+
update: (t) => (current = t),
|
|
219
|
+
seconds: () => Math.max(1, Math.round((Date.now() - started) / 1000)),
|
|
220
|
+
succeed: (msg = current) => finish(c.green('✔'), msg),
|
|
221
|
+
warn: (msg = current) => finish(c.yellow('!'), msg),
|
|
222
|
+
fail: (msg = current) => finish(c.red('✖'), msg),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
tty,
|
|
227
|
+
write,
|
|
228
|
+
text,
|
|
229
|
+
password,
|
|
230
|
+
confirm,
|
|
231
|
+
select,
|
|
232
|
+
spinner,
|
|
233
|
+
close: () => {
|
|
234
|
+
rl?.close();
|
|
235
|
+
rl = null;
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
// eslint-disable-next-line no-control-regex
|
|
240
|
+
const ANSI = /\u001b\[[0-9;?]*[A-Za-z]/g;
|
|
241
|
+
export const visible = (s) => s.replace(ANSI, '');
|
|
242
|
+
const pad = (s, width) => s + ' '.repeat(Math.max(0, width - visible(s).length));
|
package/dist/scaffold.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// Copying a starter into the new app folder. Pure file-system code, easy to test.
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { MANIFEST } from "./templates.js";
|
|
5
|
+
/** npm strips some dotfiles from published packages, so starters ship them with a "_" prefix. */
|
|
6
|
+
export const RENAME = {
|
|
7
|
+
_gitignore: '.gitignore',
|
|
8
|
+
'_env.example': '.env.example',
|
|
9
|
+
_npmrc: '.npmrc',
|
|
10
|
+
};
|
|
11
|
+
/** Never copied, even if they exist in the starter folder (e.g. after testing it locally). */
|
|
12
|
+
const SKIP = new Set([MANIFEST, 'node_modules', '.next', 'dist', '.expo', '.turbo', '.vite', 'package-lock.json', 'pnpm-lock.yaml', 'yarn.lock', 'bun.lockb', 'bun.lock', '.env', '.env.local', '.DS_Store', '.claude', 'AGENTS.md', 'tsconfig.tsbuildinfo']);
|
|
13
|
+
/** Files that may already be in a folder we still treat as empty. */
|
|
14
|
+
const HARMLESS = new Set(['.git', '.DS_Store', 'Thumbs.db', '.idea', '.vscode']);
|
|
15
|
+
export function inspectDir(dir) {
|
|
16
|
+
let stat;
|
|
17
|
+
try {
|
|
18
|
+
stat = fs.statSync(dir);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return { state: 'missing' };
|
|
22
|
+
}
|
|
23
|
+
if (!stat.isDirectory())
|
|
24
|
+
return { state: 'file' };
|
|
25
|
+
const entries = fs.readdirSync(dir).filter((e) => !HARMLESS.has(e));
|
|
26
|
+
return entries.length ? { state: 'not-empty', entries } : { state: 'empty' };
|
|
27
|
+
}
|
|
28
|
+
/** "Acme Notes" → "acme-notes": a valid npm package name and folder name ("" when nothing is left). */
|
|
29
|
+
export function toSlug(input) {
|
|
30
|
+
const slug = input
|
|
31
|
+
.trim()
|
|
32
|
+
.toLowerCase()
|
|
33
|
+
.normalize('NFKD')
|
|
34
|
+
.replace(/[̀-ͯ]/g, '')
|
|
35
|
+
.replace(/[^a-z0-9._~-]+/g, '-')
|
|
36
|
+
.replace(/-+/g, '-')
|
|
37
|
+
.replace(/^[-._~]+|[-._~]+$/g, '');
|
|
38
|
+
return slug.slice(0, 214);
|
|
39
|
+
}
|
|
40
|
+
/** "my-app" → "My App". Keeps words the user already capitalised ("ACME" stays "ACME"). */
|
|
41
|
+
export function toDisplayName(input) {
|
|
42
|
+
const cleaned = input
|
|
43
|
+
.trim()
|
|
44
|
+
.replace(/'/g, '’')
|
|
45
|
+
.replace(/[^\p{L}\p{N}’ ._-]+/gu, ' ');
|
|
46
|
+
const words = cleaned.split(/[\s_-]+/).filter(Boolean);
|
|
47
|
+
if (!words.length)
|
|
48
|
+
return 'My App';
|
|
49
|
+
return words
|
|
50
|
+
.map((w) => w[0].toUpperCase() + w.slice(1))
|
|
51
|
+
.join(' ')
|
|
52
|
+
.slice(0, 60);
|
|
53
|
+
}
|
|
54
|
+
/** Copy `from` (a starter folder) into `to`, renaming dotfiles and applying the app name. */
|
|
55
|
+
export function copyTemplate(from, to, manifest, opts) {
|
|
56
|
+
let files = 0;
|
|
57
|
+
const walk = (rel) => {
|
|
58
|
+
const src = path.join(from, rel);
|
|
59
|
+
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
60
|
+
if (SKIP.has(entry.name))
|
|
61
|
+
continue;
|
|
62
|
+
const childRel = rel ? path.join(rel, entry.name) : entry.name;
|
|
63
|
+
const destName = RENAME[entry.name] ?? entry.name;
|
|
64
|
+
const dest = path.join(to, rel, destName);
|
|
65
|
+
if (entry.isDirectory()) {
|
|
66
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
67
|
+
walk(childRel);
|
|
68
|
+
}
|
|
69
|
+
else if (entry.isFile()) {
|
|
70
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
71
|
+
fs.copyFileSync(path.join(from, childRel), dest);
|
|
72
|
+
files++;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
fs.mkdirSync(to, { recursive: true });
|
|
77
|
+
walk('');
|
|
78
|
+
applyBrand(to, manifest, opts.appName, opts.slug);
|
|
79
|
+
rewritePackageJson(to, opts);
|
|
80
|
+
return { files };
|
|
81
|
+
}
|
|
82
|
+
function applyBrand(dir, manifest, appName, slug) {
|
|
83
|
+
for (const rel of manifest.brandFiles ?? []) {
|
|
84
|
+
const file = path.join(dir, rel);
|
|
85
|
+
if (!fs.existsSync(file))
|
|
86
|
+
continue;
|
|
87
|
+
let text = fs.readFileSync(file, 'utf8');
|
|
88
|
+
if (manifest.brandSlug)
|
|
89
|
+
text = text.split(manifest.brandSlug).join(slug);
|
|
90
|
+
if (manifest.brandName)
|
|
91
|
+
text = text.split(manifest.brandName).join(appName);
|
|
92
|
+
fs.writeFileSync(file, text);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function rewritePackageJson(dir, opts) {
|
|
96
|
+
const file = path.join(dir, 'package.json');
|
|
97
|
+
if (!fs.existsSync(file))
|
|
98
|
+
return;
|
|
99
|
+
const pkg = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
100
|
+
pkg.name = opts.slug;
|
|
101
|
+
if (opts.sdkSpec) {
|
|
102
|
+
for (const field of ['dependencies', 'devDependencies']) {
|
|
103
|
+
if (pkg[field]?.usebecky)
|
|
104
|
+
pkg[field].usebecky = opts.sdkSpec;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
fs.writeFileSync(file, JSON.stringify(pkg, null, 2) + '\n');
|
|
108
|
+
}
|