create-foliwake 0.0.0-stage → 0.4.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/CHANGELOG.md +85 -0
- package/LICENSE +202 -0
- package/README.md +64 -2
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +16 -0
- package/dist/cli.d.ts +8 -0
- package/dist/cli.js +88 -0
- package/dist/create.d.ts +50 -0
- package/dist/create.js +250 -0
- package/dist/destination.d.ts +47 -0
- package/dist/destination.js +243 -0
- package/dist/editor-files.d.ts +6 -0
- package/dist/editor-files.js +39 -0
- package/dist/files.d.ts +26 -0
- package/dist/files.js +94 -0
- package/dist/git.d.ts +22 -0
- package/dist/git.js +64 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +7 -0
- package/dist/install.d.ts +12 -0
- package/dist/install.js +18 -0
- package/dist/options.d.ts +48 -0
- package/dist/options.js +54 -0
- package/dist/prompts.d.ts +16 -0
- package/dist/prompts.js +67 -0
- package/dist/starters.d.ts +20 -0
- package/dist/starters.js +61 -0
- package/dist/validate.d.ts +3 -0
- package/dist/validate.js +12 -0
- package/package.json +64 -3
- package/starters/examples/components/quiz/quiz.css +33 -0
- package/starters/examples/components/quiz/quiz.example.md +29 -0
- package/starters/examples/components/quiz/quiz.js +36 -0
- package/starters/examples/components/quiz/quiz.json +14 -0
- package/starters/examples/components/steps/steps.css +38 -0
- package/starters/examples/components/steps/steps.example.md +25 -0
- package/starters/examples/components/steps/steps.json +8 -0
- package/starters/examples/content/blocks.md +49 -0
- package/starters/examples/content/index.md +44 -0
- package/starters/minimal/components/note/note.css +15 -0
- package/starters/minimal/components/note/note.example.md +23 -0
- package/starters/minimal/components/note/note.json +8 -0
- package/starters/minimal/content/images/foliwake-logo.png +0 -0
- package/starters/minimal/content/index.md +32 -0
- package/starters/react/components/poll/poll.css +32 -0
- package/starters/react/components/poll/poll.example.md +26 -0
- package/starters/react/components/poll/poll.js +35 -0
- package/starters/react/components/poll/poll.json +13 -0
- package/starters/react/scripts/react-view.js +22 -0
- package/starters/shared/README.md +104 -0
- package/starters/shared/_github/actions/foliwake-request/action.yml +45 -0
- package/starters/shared/_github/scripts/foliwake-report.py +68 -0
- package/starters/shared/_github/workflows/branch-preview.yml +104 -0
- package/starters/shared/_github/workflows/frontend-checks.yml +28 -0
- package/starters/shared/_github/workflows/production.yml +134 -0
- package/starters/shared/_gitignore +8 -0
- package/starters/shared/jsconfig.json +15 -0
- package/starters/shared/scripts/components.js +284 -0
- package/starters/shared/site.yaml +2 -0
- package/starters/shared/styles/styles.css +106 -0
- package/starters/shared/tests/site.test.mjs +21 -0
- package/starters/shared/tests/workflows.test.mjs +84 -0
- package/starters/storybook/_storybook/main.js +49 -0
- package/starters/storybook/_storybook/preview.js +7 -0
- package/starters/storybook/_storybook/run.mjs +25 -0
- package/starters/storybook/pnpm-workspace.yaml +8 -0
- package/vendor/react.LICENSE.txt +21 -0
- package/vendor/react.js +32 -0
package/dist/files.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// create-foliwake: the files of a new project, planned in memory from the starters (starters.ts). Nothing is
|
|
2
|
+
// downloaded: React comes vendored with this package, and the project has no npm dependencies unless it has
|
|
3
|
+
// Storybook.
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { localSiteId, packageName } from './options.js';
|
|
6
|
+
import { layerFiles, packageVersion, projectPath, siteCheckVersion, starterFile, vendoredReact, } from './starters.js';
|
|
7
|
+
const TEMPLATE_LAYERS = {
|
|
8
|
+
minimal: ['shared', 'minimal'],
|
|
9
|
+
examples: ['shared', 'minimal', 'examples'],
|
|
10
|
+
};
|
|
11
|
+
/** Storybook's packages: exact versions, at least two weeks old when chosen. */
|
|
12
|
+
export const STORYBOOK_PACKAGES = Object.freeze({
|
|
13
|
+
'@storybook/html-vite': '10.6.3',
|
|
14
|
+
storybook: '10.6.3',
|
|
15
|
+
vite: '8.3.5',
|
|
16
|
+
});
|
|
17
|
+
/** The local configuration of the Foliwake CLI (`foliwake dev`); harmless without it. */
|
|
18
|
+
export const CONFIG_FILE_NAME = 'foliwake.config.json';
|
|
19
|
+
/** Starter files whose text has {{placeholders}}. */
|
|
20
|
+
const SUBSTITUTED = new Set(['README.md', '_github/workflows/frontend-checks.yml']);
|
|
21
|
+
const REACT_NOTE = '\n\nReact is vendored: `scripts/vendor/react.js` is React and ReactDOM (production build, its MIT license in ' +
|
|
22
|
+
"the file's header comment, which code releases keep, and in `scripts/vendor/react.LICENSE.txt`), committed " +
|
|
23
|
+
'with your code, so no npm install and no build step. The block `poll` renders with it through the block ' +
|
|
24
|
+
"kit's `ctx.mount` and `scripts/react-view.js`. A shell that gives its own React (`host.has('react')`: a " +
|
|
25
|
+
'React or Next app passing `react` in its host) is used instead.';
|
|
26
|
+
const STORYBOOK_NOTE = ' With the Foliwake CLI, Storybook shows them too: `npm install && npm run stories` (stories from ' +
|
|
27
|
+
'`foliwake blocks render`, configuration in `.storybook/`).';
|
|
28
|
+
function placeholders(input, version, checker) {
|
|
29
|
+
return {
|
|
30
|
+
name: path.basename(input.target),
|
|
31
|
+
template: input.template,
|
|
32
|
+
createVersion: version,
|
|
33
|
+
siteCheckVersion: checker,
|
|
34
|
+
siteIdNote: input.siteId ? ` — this project was created for the site id \`${input.siteId}\`` : '',
|
|
35
|
+
storybookNote: input.storybook ? STORYBOOK_NOTE : '',
|
|
36
|
+
reactNote: input.react ? REACT_NOTE : '',
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function substitute(bytes, values) {
|
|
40
|
+
return Buffer.from(bytes.toString('utf8').replace(/\{\{(\w+)\}\}/g, (all, key) => values[key] ?? all));
|
|
41
|
+
}
|
|
42
|
+
function localConfig(id) {
|
|
43
|
+
// `foliwake dev` and `page check` read only the local folders; the storage is named because the configuration
|
|
44
|
+
// format requires it (a local store, used only by `code prepare`).
|
|
45
|
+
const site = {
|
|
46
|
+
id,
|
|
47
|
+
code: { root: '.', repository: null },
|
|
48
|
+
contentRoot: './content',
|
|
49
|
+
storage: { endpoint: 'http://127.0.0.1:8333', bucket: `foliwake-${id}` },
|
|
50
|
+
stateDir: './.foliwake',
|
|
51
|
+
liveHosts: [`${id}.localhost`],
|
|
52
|
+
previewBaseHost: `preview.${id}.localhost`,
|
|
53
|
+
};
|
|
54
|
+
return `${JSON.stringify({ version: 1, sites: [site] }, null, 2)}\n`;
|
|
55
|
+
}
|
|
56
|
+
function packageJson(name, storybook) {
|
|
57
|
+
const stories = { stories: 'node .storybook/run.mjs', 'build-stories': 'foliwake blocks render && storybook build' };
|
|
58
|
+
const manifest = {
|
|
59
|
+
name,
|
|
60
|
+
private: true,
|
|
61
|
+
type: 'module',
|
|
62
|
+
description: 'The frontend of a Foliwake site: blocks, styles and local sample pages.',
|
|
63
|
+
scripts: {
|
|
64
|
+
check: 'npx @foliwake/site-check',
|
|
65
|
+
test: 'node --test "tests/*.test.mjs"',
|
|
66
|
+
dev: 'foliwake dev --watch',
|
|
67
|
+
'check:pages': 'foliwake page check',
|
|
68
|
+
...(storybook ? stories : {}),
|
|
69
|
+
},
|
|
70
|
+
...(storybook ? { devDependencies: STORYBOOK_PACKAGES } : {}),
|
|
71
|
+
};
|
|
72
|
+
return `${JSON.stringify(manifest, null, 2)}\n`;
|
|
73
|
+
}
|
|
74
|
+
/** Every file of the new project, sorted by path. */
|
|
75
|
+
export async function projectFiles(input) {
|
|
76
|
+
const values = placeholders(input, await packageVersion(), await siteCheckVersion());
|
|
77
|
+
const layers = [...TEMPLATE_LAYERS[input.template], ...(input.storybook ? ['storybook'] : [])];
|
|
78
|
+
if (input.react)
|
|
79
|
+
layers.push('react');
|
|
80
|
+
const files = new Map();
|
|
81
|
+
for (const layer of layers) {
|
|
82
|
+
for (const file of await layerFiles(layer)) {
|
|
83
|
+
const bytes = await starterFile(layer, file);
|
|
84
|
+
files.set(projectPath(file), SUBSTITUTED.has(file) ? substitute(bytes, values) : bytes);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (input.react)
|
|
88
|
+
for (const [file, bytes] of await vendoredReact())
|
|
89
|
+
files.set(file, bytes);
|
|
90
|
+
files.set(CONFIG_FILE_NAME, Buffer.from(localConfig(localSiteId(input.siteId, input.target))));
|
|
91
|
+
files.set('package.json', Buffer.from(packageJson(packageName(input.target), input.storybook ?? false)));
|
|
92
|
+
const sorted = [...files].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
93
|
+
return sorted.map(([file, bytes]) => ({ path: file, bytes }));
|
|
94
|
+
}
|
package/dist/git.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare const INITIAL_BRANCH = "main";
|
|
2
|
+
/** Whether `dir` is inside an existing Git work tree. Anything but a clear "no" is `unknown` (fail closed). */
|
|
3
|
+
export type Enclosing = {
|
|
4
|
+
kind: 'none';
|
|
5
|
+
} | {
|
|
6
|
+
kind: 'inside';
|
|
7
|
+
top: string;
|
|
8
|
+
} | {
|
|
9
|
+
kind: 'unknown';
|
|
10
|
+
reason: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function enclosingRepository(dir: string, env: NodeJS.ProcessEnv): Promise<Enclosing>;
|
|
13
|
+
export type CommitResult = {
|
|
14
|
+
ok: true;
|
|
15
|
+
sha: string;
|
|
16
|
+
} | {
|
|
17
|
+
ok: false;
|
|
18
|
+
step: 'init' | 'add' | 'commit';
|
|
19
|
+
reason: string;
|
|
20
|
+
};
|
|
21
|
+
/** `git init` (branch main), stage exactly `files`, commit with the user's identity. */
|
|
22
|
+
export declare function initAndCommit(dir: string, files: readonly string[], message: string, env: NodeJS.ProcessEnv): Promise<CommitResult>;
|
package/dist/git.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// create-foliwake: the new Git repository and its first commit (a code release
|
|
2
|
+
// is made only from a clean commit). The author is whatever the user's Git
|
|
3
|
+
// configuration says; nothing here sets a name, an e-mail or a remote. Only the
|
|
4
|
+
// files this run wrote are staged, so files kept in the folder are never committed.
|
|
5
|
+
import { execFile } from 'node:child_process';
|
|
6
|
+
// Inherited from a hook or script, these would point git at another repository.
|
|
7
|
+
const LOCATION_VARIABLES = [
|
|
8
|
+
'GIT_DIR',
|
|
9
|
+
'GIT_WORK_TREE',
|
|
10
|
+
'GIT_INDEX_FILE',
|
|
11
|
+
'GIT_COMMON_DIR',
|
|
12
|
+
'GIT_OBJECT_DIRECTORY',
|
|
13
|
+
'GIT_ALTERNATE_OBJECT_DIRECTORIES',
|
|
14
|
+
'GIT_NAMESPACE',
|
|
15
|
+
'GIT_PREFIX',
|
|
16
|
+
'GIT_CEILING_DIRECTORIES',
|
|
17
|
+
];
|
|
18
|
+
export const INITIAL_BRANCH = 'main';
|
|
19
|
+
function git(dir, args, env) {
|
|
20
|
+
// LC_ALL=C: git's messages in English, so "not a git repository" can be recognised.
|
|
21
|
+
const childEnv = { ...env, GIT_TERMINAL_PROMPT: '0', GIT_LITERAL_PATHSPECS: '1', LC_ALL: 'C' };
|
|
22
|
+
for (const name of LOCATION_VARIABLES)
|
|
23
|
+
delete childEnv[name];
|
|
24
|
+
return new Promise((resolve) => {
|
|
25
|
+
execFile('git', args, { cwd: dir, env: childEnv, maxBuffer: 16 << 20 }, (error, stdout, stderr) => {
|
|
26
|
+
if (!error)
|
|
27
|
+
return resolve({ ok: true, stdout: String(stdout).trim() });
|
|
28
|
+
if (error.code === 'ENOENT')
|
|
29
|
+
return resolve({ ok: false, reason: 'the `git` command was not found', missing: true });
|
|
30
|
+
const detail = String(stderr ?? '')
|
|
31
|
+
.trim()
|
|
32
|
+
.split('\n')
|
|
33
|
+
.filter(Boolean)
|
|
34
|
+
.slice(0, 3)
|
|
35
|
+
.join(' ');
|
|
36
|
+
resolve({ ok: false, reason: detail || error.message });
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
export async function enclosingRepository(dir, env) {
|
|
41
|
+
const found = await git(dir, ['rev-parse', '--show-toplevel'], env);
|
|
42
|
+
if (found.ok)
|
|
43
|
+
return found.stdout ? { kind: 'inside', top: found.stdout } : { kind: 'unknown', reason: 'inside Git metadata' };
|
|
44
|
+
if (found.missing)
|
|
45
|
+
return { kind: 'none' }; // no git: the commit step reports it
|
|
46
|
+
if (/not a git repository/i.test(found.reason))
|
|
47
|
+
return { kind: 'none' };
|
|
48
|
+
return { kind: 'unknown', reason: found.reason };
|
|
49
|
+
}
|
|
50
|
+
/** `git init` (branch main), stage exactly `files`, commit with the user's identity. */
|
|
51
|
+
export async function initAndCommit(dir, files, message, env) {
|
|
52
|
+
const init = await git(dir, ['init', '--quiet', `--initial-branch=${INITIAL_BRANCH}`], env);
|
|
53
|
+
if (!init.ok)
|
|
54
|
+
return { ok: false, step: 'init', reason: init.reason };
|
|
55
|
+
const add = await git(dir, ['add', '--', ...files], env);
|
|
56
|
+
if (!add.ok)
|
|
57
|
+
return { ok: false, step: 'add', reason: add.reason };
|
|
58
|
+
// useConfigOnly: the user's configured identity, never one git guesses from the host name.
|
|
59
|
+
const commit = await git(dir, ['-c', 'user.useConfigOnly=true', 'commit', '--quiet', '-m', message], env);
|
|
60
|
+
if (!commit.ok)
|
|
61
|
+
return { ok: false, step: 'commit', reason: commit.reason };
|
|
62
|
+
const sha = await git(dir, ['rev-parse', 'HEAD'], env);
|
|
63
|
+
return sha.ok ? { ok: true, sha: sha.stdout } : { ok: false, step: 'commit', reason: sha.reason };
|
|
64
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { type CreateIO, type CreateResult, EXIT_CANCELLED, createProject, nextSteps, printResult, runCreate, shellWord, } from './create.js';
|
|
2
|
+
export { type ProjectFile, projectFiles } from './files.js';
|
|
3
|
+
export { type CreateAnswers, type CreateRequest, CreateUsageError, DEFAULT_FOLDER, DEFAULT_TEMPLATE, TEMPLATES, type TemplateName, localSiteId, } from './options.js';
|
|
4
|
+
export { KIT_TYPES_PATH, LOCAL_SCHEMA_PATH, writeKitTypes, writeLocalSchema } from './editor-files.js';
|
|
5
|
+
export { projectProblems } from './validate.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// create-foliwake: create a Foliwake site frontend. The command is `create-foliwake` (bin.ts); createProject is
|
|
2
|
+
// the same run without printing, for a wrapper that adds its own steps (the Foliwake CLI's `foliwake create`).
|
|
3
|
+
export { EXIT_CANCELLED, createProject, nextSteps, printResult, runCreate, shellWord, } from './create.js';
|
|
4
|
+
export { projectFiles } from './files.js';
|
|
5
|
+
export { CreateUsageError, DEFAULT_FOLDER, DEFAULT_TEMPLATE, TEMPLATES, localSiteId, } from './options.js';
|
|
6
|
+
export { KIT_TYPES_PATH, LOCAL_SCHEMA_PATH, writeKitTypes, writeLocalSchema } from './editor-files.js';
|
|
7
|
+
export { projectProblems } from './validate.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const PACKAGE_MANAGERS: readonly ["npm", "pnpm", "yarn", "bun"];
|
|
2
|
+
export type PackageManager = (typeof PACKAGE_MANAGERS)[number];
|
|
3
|
+
/** The package manager named by npm_config_user_agent; npm when none is. */
|
|
4
|
+
export declare function packageManagerOf(env: NodeJS.ProcessEnv): PackageManager;
|
|
5
|
+
export type InstallResult = {
|
|
6
|
+
ok: true;
|
|
7
|
+
} | {
|
|
8
|
+
ok: false;
|
|
9
|
+
reason: string;
|
|
10
|
+
};
|
|
11
|
+
/** `<manager> install` in the project, its output shown as it runs. */
|
|
12
|
+
export declare function installPackages(dir: string, manager: PackageManager, env: NodeJS.ProcessEnv): Promise<InstallResult>;
|
package/dist/install.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// create-foliwake: installing the project's npm packages, only when the project has some (Storybook) and the
|
|
2
|
+
// user asked for it. The only step of create that uses the network. The package manager is the one that ran
|
|
3
|
+
// `create` (npm_config_user_agent: `pnpm/10.33.0 npm/? node/v22 …`), else npm.
|
|
4
|
+
import { spawn } from 'node:child_process';
|
|
5
|
+
export const PACKAGE_MANAGERS = ['npm', 'pnpm', 'yarn', 'bun'];
|
|
6
|
+
/** The package manager named by npm_config_user_agent; npm when none is. */
|
|
7
|
+
export function packageManagerOf(env) {
|
|
8
|
+
const name = (env.npm_config_user_agent ?? '').split('/')[0];
|
|
9
|
+
return PACKAGE_MANAGERS.includes(name) ? name : 'npm';
|
|
10
|
+
}
|
|
11
|
+
/** `<manager> install` in the project, its output shown as it runs. */
|
|
12
|
+
export function installPackages(dir, manager, env) {
|
|
13
|
+
return new Promise((resolve) => {
|
|
14
|
+
const child = spawn(manager, ['install'], { cwd: dir, env, stdio: 'inherit', shell: process.platform === 'win32' });
|
|
15
|
+
child.on('error', (error) => resolve({ ok: false, reason: error.message }));
|
|
16
|
+
child.on('exit', (code) => resolve(code === 0 ? { ok: true } : { ok: false, reason: `${manager} install exited with ${code}` }));
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export declare const TEMPLATES: readonly ["minimal", "examples"];
|
|
2
|
+
export type TemplateName = (typeof TEMPLATES)[number];
|
|
3
|
+
export declare const DEFAULT_FOLDER = "foliwake-site";
|
|
4
|
+
export declare const DEFAULT_TEMPLATE: TemplateName;
|
|
5
|
+
/** What to do with a folder that already has files: remove them (.git kept), or add next to them. */
|
|
6
|
+
export type ExistingFiles = 'clear' | 'keep';
|
|
7
|
+
/** The command line, as given: undefined means "not given" (asked in a terminal, else the default). */
|
|
8
|
+
export interface CreateRequest {
|
|
9
|
+
folder?: string | undefined;
|
|
10
|
+
template?: TemplateName | undefined;
|
|
11
|
+
/** The site id on a Foliwake server; "" means none. */
|
|
12
|
+
site?: string | undefined;
|
|
13
|
+
git?: boolean | undefined;
|
|
14
|
+
/** Add Storybook for the blocks (starters/storybook/); asked in a terminal, else no. */
|
|
15
|
+
storybook?: boolean | undefined;
|
|
16
|
+
/** Install the npm packages (only Storybook has any); asked in a terminal after Storybook, else no. */
|
|
17
|
+
install?: boolean | undefined;
|
|
18
|
+
/** Add React support: a React block and the vendored React (starters/react/); asked in a terminal, else no. */
|
|
19
|
+
react?: boolean | undefined;
|
|
20
|
+
existing?: ExistingFiles | undefined;
|
|
21
|
+
interactive?: boolean | undefined;
|
|
22
|
+
}
|
|
23
|
+
/** Every answer a run needs, after flags, prompts and defaults. */
|
|
24
|
+
export interface CreateAnswers {
|
|
25
|
+
folder: string;
|
|
26
|
+
template: TemplateName;
|
|
27
|
+
siteId: string | null;
|
|
28
|
+
git: boolean;
|
|
29
|
+
storybook: boolean;
|
|
30
|
+
install: boolean;
|
|
31
|
+
react: boolean;
|
|
32
|
+
existing: ExistingFiles | null;
|
|
33
|
+
}
|
|
34
|
+
/** Refused input; nothing was written. */
|
|
35
|
+
export declare class CreateUsageError extends Error {
|
|
36
|
+
}
|
|
37
|
+
/** A prompt was cancelled (Ctrl+C or Escape); nothing was written. */
|
|
38
|
+
export declare class CreateCancelled extends Error {
|
|
39
|
+
constructor();
|
|
40
|
+
}
|
|
41
|
+
/** Why a site id is not valid, or null. */
|
|
42
|
+
export declare function siteIdProblem(value: string): string | null;
|
|
43
|
+
export declare function templateProblem(value: string): string | null;
|
|
44
|
+
export declare function folderProblem(value: string): string | null;
|
|
45
|
+
/** The id of the site in the local configuration: the given site id, else one made from the folder name. */
|
|
46
|
+
export declare function localSiteId(siteId: string | null, target: string): string;
|
|
47
|
+
/** The package.json name for the project (npm rules: lowercase, URL-safe). */
|
|
48
|
+
export declare function packageName(target: string): string;
|
package/dist/options.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// create-foliwake: what a run is asked to do, its defaults and the name rules.
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { SITE_ID } from '@foliwake/site-check/formats/constants';
|
|
4
|
+
export const TEMPLATES = ['minimal', 'examples'];
|
|
5
|
+
export const DEFAULT_FOLDER = 'foliwake-site';
|
|
6
|
+
export const DEFAULT_TEMPLATE = 'minimal';
|
|
7
|
+
/** Refused input; nothing was written. */
|
|
8
|
+
export class CreateUsageError extends Error {
|
|
9
|
+
}
|
|
10
|
+
/** A prompt was cancelled (Ctrl+C or Escape); nothing was written. */
|
|
11
|
+
export class CreateCancelled extends Error {
|
|
12
|
+
constructor() {
|
|
13
|
+
super('Operation cancelled. Nothing was created.');
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Why a site id is not valid, or null. */
|
|
17
|
+
export function siteIdProblem(value) {
|
|
18
|
+
return SITE_ID.test(value)
|
|
19
|
+
? null
|
|
20
|
+
: `"${value}" is not a site id: 1–40 lowercase letters, digits and "-", starting with a letter or digit.`;
|
|
21
|
+
}
|
|
22
|
+
export function templateProblem(value) {
|
|
23
|
+
return TEMPLATES.includes(value)
|
|
24
|
+
? null
|
|
25
|
+
: `Unknown template "${value}": use ${TEMPLATES.join(' or ')}.`;
|
|
26
|
+
}
|
|
27
|
+
export function folderProblem(value) {
|
|
28
|
+
if (value.trim() === '')
|
|
29
|
+
return 'Give a folder name.';
|
|
30
|
+
if (value.includes('\0'))
|
|
31
|
+
return 'A folder name cannot contain a NUL character.';
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
/** A lowercase slug of a folder name, for the package name and the local site id. */
|
|
35
|
+
function slug(name, max) {
|
|
36
|
+
return name
|
|
37
|
+
.toLowerCase()
|
|
38
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
39
|
+
.replace(/^-+|-+$/g, '')
|
|
40
|
+
.slice(0, max)
|
|
41
|
+
.replace(/-+$/, '');
|
|
42
|
+
}
|
|
43
|
+
/** The id of the site in the local configuration: the given site id, else one made from the folder name. */
|
|
44
|
+
export function localSiteId(siteId, target) {
|
|
45
|
+
if (siteId)
|
|
46
|
+
return siteId;
|
|
47
|
+
const made = slug(path.basename(target), 40);
|
|
48
|
+
return made === '' ? 'site' : made;
|
|
49
|
+
}
|
|
50
|
+
/** The package.json name for the project (npm rules: lowercase, URL-safe). */
|
|
51
|
+
export function packageName(target) {
|
|
52
|
+
const made = slug(path.basename(target), 214);
|
|
53
|
+
return made === '' ? DEFAULT_FOLDER : made;
|
|
54
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Readable, Writable } from 'node:stream';
|
|
2
|
+
import { type ExistingFiles, type TemplateName } from './options.js';
|
|
3
|
+
export interface PromptStreams {
|
|
4
|
+
input: Readable;
|
|
5
|
+
output: Writable;
|
|
6
|
+
}
|
|
7
|
+
export declare function welcome(io: PromptStreams): void;
|
|
8
|
+
export declare function askFolder(io: PromptStreams): Promise<string>;
|
|
9
|
+
/** The folder has files: cancel, remove them (.git kept) or add next to them. */
|
|
10
|
+
export declare function askExisting(io: PromptStreams, shown: string, entries: readonly string[]): Promise<ExistingFiles>;
|
|
11
|
+
export declare function askTemplate(io: PromptStreams): Promise<TemplateName>;
|
|
12
|
+
export declare function askReact(io: PromptStreams): Promise<boolean>;
|
|
13
|
+
export declare function askStorybook(io: PromptStreams): Promise<boolean>;
|
|
14
|
+
/** Only after Storybook: its npm packages are the project's only ones, and installing them the only download. */
|
|
15
|
+
export declare function askInstall(io: PromptStreams, manager: string): Promise<boolean>;
|
|
16
|
+
export declare function askGit(io: PromptStreams): Promise<boolean>;
|
package/dist/prompts.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { confirm, intro, isCancel, select, text } from '@clack/prompts';
|
|
2
|
+
import { CreateCancelled, DEFAULT_FOLDER, DEFAULT_TEMPLATE, folderProblem, } from './options.js';
|
|
3
|
+
function answered(value) {
|
|
4
|
+
if (isCancel(value))
|
|
5
|
+
throw new CreateCancelled();
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
export function welcome(io) {
|
|
9
|
+
intro('Create a Foliwake site', io);
|
|
10
|
+
}
|
|
11
|
+
export async function askFolder(io) {
|
|
12
|
+
const value = answered(await text({
|
|
13
|
+
...io,
|
|
14
|
+
message: 'Project folder',
|
|
15
|
+
placeholder: DEFAULT_FOLDER,
|
|
16
|
+
defaultValue: DEFAULT_FOLDER,
|
|
17
|
+
validate: (v) => (v ? (folderProblem(v) ?? undefined) : undefined),
|
|
18
|
+
}));
|
|
19
|
+
return value === '' ? DEFAULT_FOLDER : value;
|
|
20
|
+
}
|
|
21
|
+
/** The folder has files: cancel, remove them (.git kept) or add next to them. */
|
|
22
|
+
export async function askExisting(io, shown, entries) {
|
|
23
|
+
const listed = entries.slice(0, 4).join(', ') + (entries.length > 4 ? ', …' : '');
|
|
24
|
+
const choice = answered(await select({
|
|
25
|
+
...io,
|
|
26
|
+
message: `${shown === '.' ? 'The current folder' : `Folder "${shown}"`} is not empty (${listed}). What now?`,
|
|
27
|
+
initialValue: 'cancel',
|
|
28
|
+
options: [
|
|
29
|
+
{ value: 'cancel', label: 'Cancel' },
|
|
30
|
+
{ value: 'clear', label: 'Remove the existing files and continue', hint: '.git is kept' },
|
|
31
|
+
{
|
|
32
|
+
value: 'keep',
|
|
33
|
+
label: 'Keep them and add the project next to them',
|
|
34
|
+
hint: 'stops if a file would be replaced',
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
}));
|
|
38
|
+
if (choice === 'cancel')
|
|
39
|
+
throw new CreateCancelled();
|
|
40
|
+
return choice;
|
|
41
|
+
}
|
|
42
|
+
export async function askTemplate(io) {
|
|
43
|
+
return answered(await select({
|
|
44
|
+
...io,
|
|
45
|
+
message: 'Template',
|
|
46
|
+
initialValue: DEFAULT_TEMPLATE,
|
|
47
|
+
options: [
|
|
48
|
+
{ value: 'minimal', label: 'minimal', hint: 'one page, one block' },
|
|
49
|
+
{ value: 'examples', label: 'examples', hint: 'two pages, three blocks, one with JavaScript' },
|
|
50
|
+
],
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
export async function askReact(io) {
|
|
54
|
+
const message = 'Add React support? (React vendored in scripts/vendor/, no npm install, and a React example block)';
|
|
55
|
+
return answered(await confirm({ ...io, message, initialValue: false }));
|
|
56
|
+
}
|
|
57
|
+
export async function askStorybook(io) {
|
|
58
|
+
const message = 'Add Storybook for your blocks? (needs the Foliwake CLI for `npm run stories`)';
|
|
59
|
+
return answered(await confirm({ ...io, message, initialValue: false }));
|
|
60
|
+
}
|
|
61
|
+
/** Only after Storybook: its npm packages are the project's only ones, and installing them the only download. */
|
|
62
|
+
export async function askInstall(io, manager) {
|
|
63
|
+
return answered(await confirm({ ...io, message: `Install its npm packages now with ${manager}?`, initialValue: false }));
|
|
64
|
+
}
|
|
65
|
+
export async function askGit(io) {
|
|
66
|
+
return answered(await confirm({ ...io, message: 'Create a Git repository and the first commit?', initialValue: true }));
|
|
67
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** The package root (dist/ → ..). */
|
|
2
|
+
export declare const PACKAGE_ROOT: string;
|
|
3
|
+
export declare const STARTERS_ROOT: string;
|
|
4
|
+
/** The vendored React of this package's build, and where a project gets it. */
|
|
5
|
+
export declare const VENDOR_ROOT: string;
|
|
6
|
+
export declare const VENDOR_REACT_FILES: Readonly<Record<string, string>>;
|
|
7
|
+
export declare const LAYERS: readonly ["shared", "minimal", "examples", "storybook", "react"];
|
|
8
|
+
export type Layer = (typeof LAYERS)[number];
|
|
9
|
+
/** The path a starter file has in a project (`_github/…` → `.github/…`). */
|
|
10
|
+
export declare function projectPath(starterPath: string): string;
|
|
11
|
+
/** The files of one layer, as starter paths (posix, relative to the layer). */
|
|
12
|
+
export declare function layerFiles(layer: Layer): Promise<string[]>;
|
|
13
|
+
/** The bytes of one starter file, e.g. `starterFile('react', 'scripts/react-view.js')`. */
|
|
14
|
+
export declare function starterFile(layer: Layer, starterPath: string): Promise<Buffer>;
|
|
15
|
+
/** The vendored React files as project paths and bytes; throws when this package was built without them. */
|
|
16
|
+
export declare function vendoredReact(): Promise<Array<[string, Buffer]>>;
|
|
17
|
+
/** The version of the @foliwake/site-check this package depends on (the projects pin it). */
|
|
18
|
+
export declare function siteCheckVersion(): Promise<string>;
|
|
19
|
+
/** This package's version. */
|
|
20
|
+
export declare function packageVersion(): Promise<string>;
|
package/dist/starters.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// The starters shipped in this package: one source of the files of a new project, for `create-foliwake` and for
|
|
2
|
+
// the Foliwake engine (`foliwake create`, `blocks new --react`). Layers: shared/, then minimal/, then the
|
|
3
|
+
// template's own folder, then the options (storybook/, react/); a later layer replaces a file of an earlier
|
|
4
|
+
// one. npm drops dotfiles such as .gitignore from a package, so they are stored as `_gitignore`, `_github/` and
|
|
5
|
+
// `_storybook/`. The vendored React is built when this package is built (vendor/, not committed).
|
|
6
|
+
import { readFile, readdir } from 'node:fs/promises';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
/** The package root (dist/ → ..). */
|
|
10
|
+
export const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
11
|
+
export const STARTERS_ROOT = path.join(PACKAGE_ROOT, 'starters');
|
|
12
|
+
/** The vendored React of this package's build, and where a project gets it. */
|
|
13
|
+
export const VENDOR_ROOT = path.join(PACKAGE_ROOT, 'vendor');
|
|
14
|
+
export const VENDOR_REACT_FILES = Object.freeze({
|
|
15
|
+
'react.js': 'scripts/vendor/react.js',
|
|
16
|
+
'react.LICENSE.txt': 'scripts/vendor/react.LICENSE.txt',
|
|
17
|
+
});
|
|
18
|
+
export const LAYERS = ['shared', 'minimal', 'examples', 'storybook', 'react'];
|
|
19
|
+
const RENAMED = {
|
|
20
|
+
_gitignore: '.gitignore',
|
|
21
|
+
_github: '.github',
|
|
22
|
+
_storybook: '.storybook',
|
|
23
|
+
};
|
|
24
|
+
/** The path a starter file has in a project (`_github/…` → `.github/…`). */
|
|
25
|
+
export function projectPath(starterPath) {
|
|
26
|
+
const [first, ...rest] = starterPath.split('/');
|
|
27
|
+
return [RENAMED[first] ?? first, ...rest].join('/');
|
|
28
|
+
}
|
|
29
|
+
/** The files of one layer, as starter paths (posix, relative to the layer). */
|
|
30
|
+
export async function layerFiles(layer) {
|
|
31
|
+
const root = path.join(STARTERS_ROOT, layer);
|
|
32
|
+
const entries = await readdir(root, { recursive: true, withFileTypes: true });
|
|
33
|
+
const files = entries.filter((entry) => entry.isFile());
|
|
34
|
+
return files.map((entry) => path.relative(root, path.join(entry.parentPath, entry.name)).split(path.sep).join('/'));
|
|
35
|
+
}
|
|
36
|
+
/** The bytes of one starter file, e.g. `starterFile('react', 'scripts/react-view.js')`. */
|
|
37
|
+
export function starterFile(layer, starterPath) {
|
|
38
|
+
return readFile(path.join(STARTERS_ROOT, layer, ...starterPath.split('/')));
|
|
39
|
+
}
|
|
40
|
+
/** The vendored React files as project paths and bytes; throws when this package was built without them. */
|
|
41
|
+
export async function vendoredReact() {
|
|
42
|
+
const files = [];
|
|
43
|
+
for (const [built, project] of Object.entries(VENDOR_REACT_FILES)) {
|
|
44
|
+
const file = path.join(VENDOR_ROOT, built);
|
|
45
|
+
const bytes = await readFile(file).catch((error) => {
|
|
46
|
+
throw new Error(`The vendored React of create-foliwake is missing (${file}: ${error.code ?? error.message}).`);
|
|
47
|
+
});
|
|
48
|
+
files.push([project, bytes]);
|
|
49
|
+
}
|
|
50
|
+
return files;
|
|
51
|
+
}
|
|
52
|
+
/** The version of the @foliwake/site-check this package depends on (the projects pin it). */
|
|
53
|
+
export async function siteCheckVersion() {
|
|
54
|
+
const manifest = new URL(import.meta.resolve('@foliwake/site-check/package.json'));
|
|
55
|
+
return JSON.parse(await readFile(manifest, 'utf8')).version;
|
|
56
|
+
}
|
|
57
|
+
/** This package's version. */
|
|
58
|
+
export async function packageVersion() {
|
|
59
|
+
const manifest = JSON.parse(await readFile(path.join(PACKAGE_ROOT, 'package.json'), 'utf8'));
|
|
60
|
+
return typeof manifest.version === 'string' ? manifest.version : 'unknown';
|
|
61
|
+
}
|
package/dist/validate.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// create-foliwake: the new project is checked with @foliwake/site-check before anything is written, over the
|
|
2
|
+
// planned files as a capture of the project folder would see them, so a broken starter never reaches a user.
|
|
3
|
+
// Every diagnostic counts (warnings and notes too): a starter must be clean. Its pages are rendered by the
|
|
4
|
+
// engine's own tests.
|
|
5
|
+
import { checkCodeRelease, formatDiagnostic } from '@foliwake/site-check';
|
|
6
|
+
import { CODE_ROOT_FILES, isUnderCodeRoot } from '@foliwake/site-check/formats/layout';
|
|
7
|
+
/** Every problem of the project's code, in the terminal form; empty when it is clean. */
|
|
8
|
+
export async function projectProblems(files) {
|
|
9
|
+
const code = files.filter((file) => isUnderCodeRoot(file.path) || CODE_ROOT_FILES.includes(file.path));
|
|
10
|
+
const result = await checkCodeRelease(new Map(code.map((file) => [file.path, file.bytes])));
|
|
11
|
+
return result.diagnostics.map(formatDiagnostic);
|
|
12
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-foliwake",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Create a Foliwake site frontend: npm create foliwake@latest. Blocks, styles, local sample pages, checks and GitHub workflows, checked with @foliwake/site-check before anything is written.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=22.22"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/foliwake/foliwake-js.git",
|
|
14
|
+
"directory": "packages/create-foliwake"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://docs.foliwake.dev/reference/packages/create-foliwake",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/foliwake/foliwake-js/issues"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"foliwake",
|
|
22
|
+
"cms",
|
|
23
|
+
"blocks",
|
|
24
|
+
"create",
|
|
25
|
+
"starter",
|
|
26
|
+
"scaffold"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public",
|
|
30
|
+
"provenance": true
|
|
31
|
+
},
|
|
32
|
+
"foliwake": {
|
|
33
|
+
"htmlContract": 2,
|
|
34
|
+
"blocksPack": 1,
|
|
35
|
+
"templates": 1,
|
|
36
|
+
"references": 1,
|
|
37
|
+
"query": 1
|
|
38
|
+
},
|
|
39
|
+
"bin": {
|
|
40
|
+
"create-foliwake": "./dist/bin.js"
|
|
41
|
+
},
|
|
42
|
+
"exports": {
|
|
43
|
+
".": {
|
|
44
|
+
"types": "./dist/index.d.ts",
|
|
45
|
+
"default": "./dist/index.js"
|
|
46
|
+
},
|
|
47
|
+
"./starters": {
|
|
48
|
+
"types": "./dist/starters.d.ts",
|
|
49
|
+
"default": "./dist/starters.js"
|
|
50
|
+
},
|
|
51
|
+
"./package.json": "./package.json"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@clack/prompts": "1.8.1",
|
|
55
|
+
"@foliwake/block-kit": "0.4.0",
|
|
56
|
+
"@foliwake/site-check": "0.4.0"
|
|
57
|
+
},
|
|
58
|
+
"files": [
|
|
59
|
+
"dist/",
|
|
60
|
+
"!dist/**/*.map",
|
|
61
|
+
"starters/",
|
|
62
|
+
"vendor/",
|
|
63
|
+
"LICENSE",
|
|
64
|
+
"README.md",
|
|
65
|
+
"CHANGELOG.md"
|
|
66
|
+
]
|
|
6
67
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/* quiz: one question with answers; quiz.js makes the answers clickable.
|
|
2
|
+
Without JavaScript the answers stay a readable list and the explanation stays visible.
|
|
3
|
+
HTML: <div data-component="quiz"><h3 data-field="question"><ul data-field="options" data-select="single|multiple">
|
|
4
|
+
<li data-item data-option-id data-correct="true|false">…</li></ul>[<div data-field="explanation"><p>…] */
|
|
5
|
+
|
|
6
|
+
@layer blocks {
|
|
7
|
+
[data-component="quiz"] {
|
|
8
|
+
& [data-field="question"] { margin: 0 0 0.75rem; font-size: 1.05rem; }
|
|
9
|
+
|
|
10
|
+
& [data-field="options"] { list-style: none; margin: 0 0 0.75rem; padding: 0; }
|
|
11
|
+
|
|
12
|
+
& [data-item] { margin: 0.4rem 0; }
|
|
13
|
+
|
|
14
|
+
& button[data-answer] {
|
|
15
|
+
width: 100%;
|
|
16
|
+
padding: 0.6rem 0.8rem;
|
|
17
|
+
border: 1px solid var(--color-border);
|
|
18
|
+
border-radius: 0.5rem;
|
|
19
|
+
font: inherit;
|
|
20
|
+
text-align: left;
|
|
21
|
+
color: inherit;
|
|
22
|
+
background: var(--color-page);
|
|
23
|
+
cursor: pointer;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
& button[data-answer]:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
|
|
27
|
+
& button[data-answer][data-result="right"] { border-color: #15803d; box-shadow: inset 4px 0 0 #15803d; }
|
|
28
|
+
& button[data-answer][data-result="wrong"] { border-color: #b91c1c; box-shadow: inset 4px 0 0 #b91c1c; }
|
|
29
|
+
|
|
30
|
+
&[data-quiz="ready"] [data-field="explanation"] { display: none; }
|
|
31
|
+
&[data-quiz="answered"] [data-field="explanation"] { display: block; }
|
|
32
|
+
}
|
|
33
|
+
}
|