@soroush.design/cli 0.2.0-alpha.0 → 0.2.0-alpha.2
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 +30 -17
- package/dist/bin.mjs +16 -3
- package/dist/index.d.mts +36 -5
- package/dist/index.mjs +1 -1
- package/dist/{io-BuZk1Ebr.mjs → io-uDtjQuG6.mjs} +423 -71
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -7,31 +7,44 @@ an existing React app.
|
|
|
7
7
|
## Start a new app: `create`
|
|
8
8
|
|
|
9
9
|
```sh
|
|
10
|
-
npx @soroush.
|
|
10
|
+
npx @soroush.design/cli create my-app
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
It makes the app in a new folder, `my-app`, or in an empty one, and never writes in a folder that
|
|
14
|
-
holds anything. It asks for what you did not give it - the name
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
holds anything. It asks for what you did not give it - the name and the title typed, then the
|
|
15
|
+
language, the package manager, the default color scheme and the sample data pages picked with the
|
|
16
|
+
arrow keys - then shows every file and the install, and writes nothing before a yes. The package
|
|
17
|
+
manager starts on the one you ran it with (`npx`, `pnpm dlx`, `yarn dlx`, `bunx`). It installs with
|
|
17
18
|
no scripts, and runs `git init` with no commit, unless the folder is already in a repository. When
|
|
18
19
|
a write or the install fails, it takes the new app away.
|
|
19
20
|
|
|
20
|
-
| Option | What it does
|
|
21
|
-
| -------------------- |
|
|
22
|
-
| `-y`, `--yes` | Create without asking: TypeScript and light, unless given
|
|
23
|
-
| `--dry-run` | Show the files and exit
|
|
24
|
-
| `--
|
|
25
|
-
| `--
|
|
26
|
-
| `--
|
|
27
|
-
| `--
|
|
28
|
-
| `--
|
|
21
|
+
| Option | What it does |
|
|
22
|
+
| -------------------- | ----------------------------------------------------------------- |
|
|
23
|
+
| `-y`, `--yes` | Create without asking: TypeScript and light, unless given |
|
|
24
|
+
| `--dry-run` | Show the files and exit |
|
|
25
|
+
| `--title <text>` | What the app's pages call it: the name when not given |
|
|
26
|
+
| `--framework <name>` | The app's framework: `vike`, the default and only one yet |
|
|
27
|
+
| `--ts`, `--js` | TypeScript (the default) or JavaScript |
|
|
28
|
+
| `--pm <name>` | `npm`, `pnpm`, `yarn` or `bun`: the one that ran it, if not given |
|
|
29
|
+
| `--render <name>` | `isomorphic` (the default), `spa` or `ssg` |
|
|
30
|
+
| `--scheme <name>` | The default color scheme: `light` or `dark` |
|
|
31
|
+
| `--sample-data` | Add the sample data pages |
|
|
32
|
+
| `--no-git` | Do not run `git init` |
|
|
33
|
+
| `-h`, `--help` | Show the options of `create` |
|
|
29
34
|
|
|
30
35
|
### The Vike app
|
|
31
36
|
|
|
32
|
-
A [Vike](https://vike.dev) app,
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
A [Vike](https://vike.dev) app, rendered the way you pick:
|
|
38
|
+
|
|
39
|
+
- **Isomorphic** (the default): every page is prerendered to HTML at build time, with the styles
|
|
40
|
+
it uses, so it is drawn styled before any script runs. Then the app takes over in the browser
|
|
41
|
+
and moves between pages without a reload.
|
|
42
|
+
- **SPA**: every page is an empty HTML shell from the build, and the browser renders it, with its
|
|
43
|
+
styles. It moves between pages without a reload.
|
|
44
|
+
- **SSG**: every page is prerendered to HTML with its styles, and ships no JavaScript: a link loads
|
|
45
|
+
the next page. It has no light and dark switch, since that needs JavaScript.
|
|
46
|
+
|
|
47
|
+
All three build to static files that any static host serves.
|
|
35
48
|
|
|
36
49
|
- `src/pages/` holds the pages - a home page, a second page and a 404 page - built from the design
|
|
37
50
|
system's components and theme tokens only.
|
|
@@ -80,7 +93,7 @@ npx '@soroush.tech/design-system' init
|
|
|
80
93
|
| `--dry-run` | Show the changes and exit |
|
|
81
94
|
| `--force` | Run outside git, or with uncommitted changes |
|
|
82
95
|
| `--scheme <name>` | The default color scheme: `light` or `dark` |
|
|
83
|
-
| `-h`, `--help` | Show the
|
|
96
|
+
| `-h`, `--help` | Show the options of `init` |
|
|
84
97
|
|
|
85
98
|
### What `init` does
|
|
86
99
|
|
package/dist/bin.mjs
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { n as main, t as createNodeIo } from "./io-
|
|
2
|
+
import { n as main, r as version$1, t as createNodeIo } from "./io-uDtjQuG6.mjs";
|
|
3
3
|
// ../design-system/package.json
|
|
4
4
|
var name = "@soroush.tech/design-system";
|
|
5
|
-
var version = "2.
|
|
5
|
+
var version = "2.2.0";
|
|
6
6
|
var peerDependencies = {
|
|
7
7
|
"react": "^18.0.0 || ^19.0.0",
|
|
8
8
|
"react-dom": "^18.0.0 || ^19.0.0"
|
|
9
9
|
};
|
|
10
|
+
// src/designSystemVersion.ts
|
|
11
|
+
/**
|
|
12
|
+
* The design system version the cli run on its own installs: the one it was built next to, read
|
|
13
|
+
* from the workspace at build time. That is the target version, which a prerelease cli is
|
|
14
|
+
* published ahead of, so a prerelease cli asks for the design system prerelease of its own
|
|
15
|
+
* channel instead: `2.3.0-alpha.0` from a `0.2.0-alpha.N` cli, which `^` takes to the newest
|
|
16
|
+
* alpha, or to `2.3.0` once it is out.
|
|
17
|
+
*/
|
|
18
|
+
function getDesignSystemVersion(designSystemVersion, cliVersion) {
|
|
19
|
+
const channel = /-([a-z]+)\.\d+$/.exec(cliVersion)?.[1];
|
|
20
|
+
if (!channel || designSystemVersion.includes("-")) return designSystemVersion;
|
|
21
|
+
return `${designSystemVersion}-${channel}.0`;
|
|
22
|
+
}
|
|
10
23
|
// src/bin.ts
|
|
11
24
|
process.exitCode = await main(process.argv.slice(2), createNodeIo(), {
|
|
12
25
|
name,
|
|
13
|
-
version,
|
|
26
|
+
version: getDesignSystemVersion(version, version$1),
|
|
14
27
|
peerDependencies
|
|
15
28
|
});
|
|
16
29
|
export {};
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { Readable, Writable } from "node:stream";
|
|
2
|
+
/** One answer a choice offers. */
|
|
3
|
+
interface Choice {
|
|
4
|
+
/** What the command gets back. */
|
|
5
|
+
value: string;
|
|
6
|
+
/** What the person reads. */
|
|
7
|
+
label: string;
|
|
8
|
+
}
|
|
2
9
|
interface RunOptions {
|
|
3
10
|
cwd: string;
|
|
4
11
|
env?: Record<string, string>;
|
|
@@ -17,12 +24,22 @@ interface Io {
|
|
|
17
24
|
isInteractive: boolean;
|
|
18
25
|
write: (text: string) => void;
|
|
19
26
|
ask: (question: string) => Promise<string>;
|
|
27
|
+
/**
|
|
28
|
+
* Asks for one of `choices`, picked with the arrow keys, starting on `initial`. Rejects with a
|
|
29
|
+
* `CancelledError` on Ctrl+C or Escape.
|
|
30
|
+
*/
|
|
31
|
+
select: (question: string, choices: readonly Choice[], initial?: number) => Promise<string>;
|
|
32
|
+
/** Asks for any of `choices`, toggled with space, starting with `initial` picked. */
|
|
33
|
+
selectMany: (question: string, choices: readonly Choice[], initial?: readonly string[]) => Promise<string[]>;
|
|
20
34
|
run: (command: string, args: readonly string[], options: RunOptions) => RunResult;
|
|
21
35
|
}
|
|
36
|
+
type Terminal = Readable & {
|
|
37
|
+
isTTY?: boolean;
|
|
38
|
+
isRaw?: boolean;
|
|
39
|
+
setRawMode?: (mode: boolean) => unknown;
|
|
40
|
+
};
|
|
22
41
|
interface NodeIoOptions {
|
|
23
|
-
stdin?:
|
|
24
|
-
isTTY?: boolean;
|
|
25
|
-
};
|
|
42
|
+
stdin?: Terminal;
|
|
26
43
|
stdout?: Writable & {
|
|
27
44
|
isTTY?: boolean;
|
|
28
45
|
};
|
|
@@ -31,6 +48,7 @@ interface NodeIoOptions {
|
|
|
31
48
|
}
|
|
32
49
|
export declare function createNodeIo({ stdin, stdout, platform, cwd }?: NodeIoOptions): Io;
|
|
33
50
|
type Scheme = 'light' | 'dark';
|
|
51
|
+
type PackageManagerName = 'npm' | 'pnpm' | 'yarn' | 'bun';
|
|
34
52
|
interface PackageJson {
|
|
35
53
|
name?: string;
|
|
36
54
|
version?: string;
|
|
@@ -52,16 +70,29 @@ interface PackageJson {
|
|
|
52
70
|
export declare function readDesignSystemPackage(fromFile: string): PackageJson;
|
|
53
71
|
/**
|
|
54
72
|
* Reads the command line and runs the command, for `designSystem`: the package of
|
|
55
|
-
* `@soroush.tech/design-system` the command installs. Returns the exit code
|
|
73
|
+
* `@soroush.tech/design-system` the command installs. Returns the exit code: 130 when the person
|
|
74
|
+
* stops it at a question, which is before anything is written.
|
|
56
75
|
*/
|
|
57
76
|
declare function main(argv: readonly string[], io: Io, designSystem: PackageJson): Promise<number>;
|
|
77
|
+
/**
|
|
78
|
+
* How the app's pages are rendered: `isomorphic`, prerendered to HTML at build time and then
|
|
79
|
+
* moving between pages in the browser; `spa`, rendered in the browser only; `ssg`, HTML only, with
|
|
80
|
+
* no JavaScript in the browser.
|
|
81
|
+
*/
|
|
82
|
+
type Rendering = 'isomorphic' | 'spa' | 'ssg';
|
|
58
83
|
interface CreateOptions {
|
|
59
|
-
/** The app's
|
|
84
|
+
/** The app's folder: asked for, or `my-app`, when missing. */
|
|
60
85
|
name: string | undefined;
|
|
86
|
+
/** What the app's pages call it: asked for, or the name, when missing. */
|
|
87
|
+
title: string | undefined;
|
|
61
88
|
/** The starter, by its name in `STARTERS`: the first one when missing. */
|
|
62
89
|
framework: string | undefined;
|
|
63
90
|
/** TypeScript or JavaScript: asked for, or TypeScript, when missing. */
|
|
64
91
|
isTypeScript: boolean | undefined;
|
|
92
|
+
/** The package manager to install with: asked for, or the one that ran create, when missing. */
|
|
93
|
+
packageManager: PackageManagerName | undefined;
|
|
94
|
+
/** How the pages are rendered: asked for, or isomorphic, when missing. */
|
|
95
|
+
rendering: Rendering | undefined;
|
|
65
96
|
scheme: Scheme | undefined;
|
|
66
97
|
/** Adds the pages that read sample data: asked for, or not, when missing. */
|
|
67
98
|
hasSampleData: boolean | undefined;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as createNodeIo, n as default,
|
|
1
|
+
export { t as createNodeIo, n as default, i as readDesignSystemPackage } from "./io-uDtjQuG6.mjs";
|
|
@@ -2,6 +2,7 @@ import { parseArgs } from "node:util";
|
|
|
2
2
|
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, rmdirSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { dirname, isAbsolute, join, posix, relative, resolve, sep } from "node:path";
|
|
4
4
|
import { spawnSync } from "node:child_process";
|
|
5
|
+
import { emitKeypressEvents } from "node:readline";
|
|
5
6
|
import { createInterface } from "node:readline/promises";
|
|
6
7
|
// src/apply/fileSystem.ts
|
|
7
8
|
const nodeFileSystem = {
|
|
@@ -727,10 +728,15 @@ function findApp(cwd) {
|
|
|
727
728
|
folder
|
|
728
729
|
};
|
|
729
730
|
}
|
|
730
|
-
/** Asks for the default color scheme
|
|
731
|
+
/** Asks for the default color scheme, starting on light. */
|
|
731
732
|
async function askScheme(io) {
|
|
732
|
-
|
|
733
|
-
|
|
733
|
+
return await io.select("Default color scheme", [{
|
|
734
|
+
value: "light",
|
|
735
|
+
label: "light"
|
|
736
|
+
}, {
|
|
737
|
+
value: "dark",
|
|
738
|
+
label: "dark"
|
|
739
|
+
}]);
|
|
734
740
|
}
|
|
735
741
|
function collectFacts(app, scheme, designSystem) {
|
|
736
742
|
const { root, packageJson, rootCandidates } = app;
|
|
@@ -836,6 +842,13 @@ async function runInit(options, io, designSystem, fileSystem = nodeFileSystem) {
|
|
|
836
842
|
return applyAndInstall(plan, root, io, fileSystem);
|
|
837
843
|
}
|
|
838
844
|
// src/create/packageManager.ts
|
|
845
|
+
/** The package managers create installs with, in the order it offers them. */
|
|
846
|
+
const PACKAGE_MANAGERS = [
|
|
847
|
+
"npm",
|
|
848
|
+
"pnpm",
|
|
849
|
+
"yarn",
|
|
850
|
+
"bun"
|
|
851
|
+
];
|
|
839
852
|
/**
|
|
840
853
|
* The package manager that ran `create`, from the user agent it gives the commands it starts
|
|
841
854
|
* (`pnpm/10.4.1 npm/? node/v24.1.0 ...`), else npm. A new app has no lockfile to read it from.
|
|
@@ -869,6 +882,8 @@ function createAppInstallCommand({ name, isYarnBerry }) {
|
|
|
869
882
|
function formatRunScript({ name }, script) {
|
|
870
883
|
return name === "npm" || name === "bun" ? `${name} run ${script}` : `${name} ${script}`;
|
|
871
884
|
}
|
|
885
|
+
// package.json
|
|
886
|
+
var version = "0.2.0-alpha.2";
|
|
872
887
|
// src/create/vike/vike.ts
|
|
873
888
|
/** The versions the starter installs: the ones this package is tested with. */
|
|
874
889
|
const VERSIONS = {
|
|
@@ -891,9 +906,10 @@ const VERSIONS = {
|
|
|
891
906
|
/** A version from this package's own manifest, as a range an app takes. */
|
|
892
907
|
const toRange = (version) => /^\d/.test(version) ? `^${version}` : version;
|
|
893
908
|
const pickVersions = (names) => Object.fromEntries(names.map((name) => [name, toRange(VERSIONS[name])]));
|
|
894
|
-
function createPackageJson({
|
|
909
|
+
function createPackageJson({ packageName, isTypeScript, rendering, version }) {
|
|
910
|
+
const renderer = rendering === "spa" ? [] : ["@emotion/server"];
|
|
895
911
|
const packageJson = {
|
|
896
|
-
name,
|
|
912
|
+
name: packageName,
|
|
897
913
|
private: true,
|
|
898
914
|
version: "0.0.0",
|
|
899
915
|
type: "module",
|
|
@@ -906,7 +922,7 @@ function createPackageJson({ name, isTypeScript, version }) {
|
|
|
906
922
|
dependencies: {
|
|
907
923
|
"@soroush.tech/design-system": toRange(version),
|
|
908
924
|
...pickVersions([
|
|
909
|
-
|
|
925
|
+
...renderer,
|
|
910
926
|
"react",
|
|
911
927
|
"react-dom",
|
|
912
928
|
"vike"
|
|
@@ -982,13 +998,14 @@ const ITEMS_LINK = ` <Link href="/items" underline="hover">
|
|
|
982
998
|
Items
|
|
983
999
|
</Link>
|
|
984
1000
|
`;
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
1001
|
+
/** The layout's light and dark switch, in a page that has JavaScript. */
|
|
1002
|
+
const SCHEME_SWITCH = ` <Button size="sm" variant="outlined" onClick={() => setScheme(otherScheme)}>
|
|
1003
|
+
{otherScheme}
|
|
1004
|
+
</Button>
|
|
1005
|
+
`;
|
|
1006
|
+
/** The settings of each rendering, in +config. */
|
|
1007
|
+
const CONFIG_SETTINGS = {
|
|
1008
|
+
isomorphic: ` // Isomorphic: every page is HTML at build time, then the app moves between pages in the
|
|
992
1009
|
// browser, with no reload.
|
|
993
1010
|
prerender: true,
|
|
994
1011
|
clientRouting: true,
|
|
@@ -996,7 +1013,40 @@ export default {
|
|
|
996
1013
|
// +Layout is a setting of this app's own: the component every page renders inside.
|
|
997
1014
|
meta: {
|
|
998
1015
|
Layout: { env: { server: true, client: true } },
|
|
999
|
-
}
|
|
1016
|
+
},`,
|
|
1017
|
+
spa: ` // SPA: every page is an empty HTML shell from the build, which the browser renders; the app
|
|
1018
|
+
// moves between pages with no reload.
|
|
1019
|
+
prerender: true,
|
|
1020
|
+
clientRouting: true,
|
|
1021
|
+
// The page and +Layout, a setting of this app's own, load in the browser only.
|
|
1022
|
+
meta: {
|
|
1023
|
+
Page: { env: { server: false, client: true } },
|
|
1024
|
+
Layout: { env: { server: false, client: true } },
|
|
1025
|
+
},`,
|
|
1026
|
+
ssg: ` // SSG: every page is HTML at build time, with no JavaScript in the browser: a link loads the
|
|
1027
|
+
// next page.
|
|
1028
|
+
prerender: true,
|
|
1029
|
+
// The page and +Layout, a setting of this app's own, load at build time only.
|
|
1030
|
+
meta: {
|
|
1031
|
+
Page: { env: { server: true, client: false } },
|
|
1032
|
+
Layout: { env: { server: true, client: false } },
|
|
1033
|
+
},`
|
|
1034
|
+
};
|
|
1035
|
+
/** What the second page says of how it got there. */
|
|
1036
|
+
const ABOUT_TEXT = {
|
|
1037
|
+
isomorphic: `A second page: it is HTML with its styles from the build, and the app moves to it without a
|
|
1038
|
+
reload.`,
|
|
1039
|
+
spa: `A second page: the browser renders it, with its styles, and the app moves to it without a
|
|
1040
|
+
reload.`,
|
|
1041
|
+
ssg: "A second page: it is HTML with its styles from the build, and a link loads it as a new page."
|
|
1042
|
+
};
|
|
1043
|
+
function createSources({ title, isTypeScript, rendering, scheme, hasSampleData }) {
|
|
1044
|
+
const t = (typeScriptOnly) => isTypeScript ? typeScriptOnly : "";
|
|
1045
|
+
const x = isTypeScript ? "ts" : "js";
|
|
1046
|
+
return {
|
|
1047
|
+
config: `${t("import type { Config } from 'vike/types'\n\n")}// The settings of every page: https://vike.dev/config
|
|
1048
|
+
export default {
|
|
1049
|
+
${CONFIG_SETTINGS[rendering]}
|
|
1000
1050
|
}${t(" satisfies Config")}
|
|
1001
1051
|
`,
|
|
1002
1052
|
PageShell: `import { StrictMode } from 'react'
|
|
@@ -1004,9 +1054,8 @@ import { CacheProvider, styleCache } from '@soroush.tech/design-system/engine'
|
|
|
1004
1054
|
${t("import type { PageContext } from 'vike/types'\n")}import { PageContextProvider } from './usePageContext'
|
|
1005
1055
|
|
|
1006
1056
|
/**
|
|
1007
|
-
* The page inside its +Layout
|
|
1008
|
-
*
|
|
1009
|
-
* page can read its pageContext.
|
|
1057
|
+
* The page inside its +Layout. The Emotion cache is the design system's, so the styles the page
|
|
1058
|
+
* uses are found where it is drawn, and every component of the page can read its pageContext.
|
|
1010
1059
|
*/
|
|
1011
1060
|
export function PageShell({ pageContext }${t(": { readonly pageContext: PageContext }")}) {
|
|
1012
1061
|
const { Page, config } = pageContext
|
|
@@ -1027,6 +1076,34 @@ export function PageShell({ pageContext }${t(": { readonly pageContext: PageCont
|
|
|
1027
1076
|
</StrictMode>
|
|
1028
1077
|
)
|
|
1029
1078
|
}
|
|
1079
|
+
`,
|
|
1080
|
+
spaOnRenderHtml: `import { escapeInject } from 'vike/server'
|
|
1081
|
+
${t("import type { OnRenderHtmlSync } from 'vike/types'\n")}
|
|
1082
|
+
/** The page's HTML shell, from the build: the browser renders the page in #root. */
|
|
1083
|
+
export const onRenderHtml${t(": OnRenderHtmlSync")} = () => {
|
|
1084
|
+
return escapeInject\`<!doctype html>
|
|
1085
|
+
<html lang="en">
|
|
1086
|
+
<head>
|
|
1087
|
+
<meta charset="UTF-8" />
|
|
1088
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
1089
|
+
<title>${title}</title>
|
|
1090
|
+
</head>
|
|
1091
|
+
<body>
|
|
1092
|
+
<div id="root"></div>
|
|
1093
|
+
</body>
|
|
1094
|
+
</html>\`
|
|
1095
|
+
}
|
|
1096
|
+
`,
|
|
1097
|
+
spaOnRenderClient: `import { createRoot${t(", type Root")} } from 'react-dom/client'
|
|
1098
|
+
${t("import type { OnRenderClientSync } from 'vike/types'\n")}import { PageShell } from './PageShell'
|
|
1099
|
+
|
|
1100
|
+
let root${t(": Root | undefined")}
|
|
1101
|
+
|
|
1102
|
+
/** Draws the page in the browser, and each page the app moves to. */
|
|
1103
|
+
export const onRenderClient${t(": OnRenderClientSync")} = (pageContext) => {
|
|
1104
|
+
root ??= createRoot(document.getElementById('root')${t("!")})
|
|
1105
|
+
root.render(<PageShell pageContext={pageContext} />)
|
|
1106
|
+
}
|
|
1030
1107
|
`,
|
|
1031
1108
|
onRenderHtml: `import createEmotionServer from '@emotion/server/create-instance'
|
|
1032
1109
|
import { renderToString } from 'react-dom/server'
|
|
@@ -1047,7 +1124,7 @@ export const onRenderHtml${t(": OnRenderHtmlSync")} = (pageContext) => {
|
|
|
1047
1124
|
<head>
|
|
1048
1125
|
<meta charset="UTF-8" />
|
|
1049
1126
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
1050
|
-
<title>${
|
|
1127
|
+
<title>${title}</title>
|
|
1051
1128
|
\${dangerouslySkipEscape(styles)}
|
|
1052
1129
|
</head>
|
|
1053
1130
|
<body>
|
|
@@ -1073,7 +1150,19 @@ export const onRenderClient${t(": OnRenderClientSync")} = (pageContext) => {
|
|
|
1073
1150
|
}
|
|
1074
1151
|
}
|
|
1075
1152
|
`,
|
|
1076
|
-
Layout:
|
|
1153
|
+
Layout: `${rendering === "ssg" ? `${t("import type { ReactNode } from 'react'\n")}import { Flex } from '@soroush.tech/design-system/Flex'
|
|
1154
|
+
import { Link } from '@soroush.tech/design-system/Link'
|
|
1155
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
1156
|
+
import { View } from '@soroush.tech/design-system/View'
|
|
1157
|
+
import { Providers } from '../theme/Providers'
|
|
1158
|
+
|
|
1159
|
+
/**
|
|
1160
|
+
* The frame every page renders in, inside the app's providers. A page has no JavaScript in the
|
|
1161
|
+
* browser, so it is drawn in the default scheme, which src/theme/Providers.${x} sets.
|
|
1162
|
+
*/
|
|
1163
|
+
export default function Layout({ children }${t(": { readonly children: ReactNode }")}) {
|
|
1164
|
+
return (
|
|
1165
|
+
<Providers>` : `import { useState${t(", type ReactNode")} } from 'react'
|
|
1077
1166
|
import { Button } from '@soroush.tech/design-system/Button'
|
|
1078
1167
|
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
1079
1168
|
import { Link } from '@soroush.tech/design-system/Link'
|
|
@@ -1089,7 +1178,7 @@ export default function Layout({ children }${t(": { readonly children: ReactNode
|
|
|
1089
1178
|
const [scheme, setScheme] = useState${t("<'light' | 'dark'>")}('${scheme}')
|
|
1090
1179
|
const otherScheme = scheme === 'light' ? 'dark' : 'light'
|
|
1091
1180
|
return (
|
|
1092
|
-
<Providers scheme={scheme}
|
|
1181
|
+
<Providers scheme={scheme}>`}
|
|
1093
1182
|
<Flex
|
|
1094
1183
|
as="header"
|
|
1095
1184
|
flexDirection="row"
|
|
@@ -1101,7 +1190,7 @@ export default function Layout({ children }${t(": { readonly children: ReactNode
|
|
|
1101
1190
|
bg="paper"
|
|
1102
1191
|
>
|
|
1103
1192
|
<Typography variant="h6" as="span">
|
|
1104
|
-
${
|
|
1193
|
+
${title}
|
|
1105
1194
|
</Typography>
|
|
1106
1195
|
<Flex as="nav" flexDirection="row" alignItems="center" gap={2}>
|
|
1107
1196
|
<Link href="/" underline="hover">
|
|
@@ -1110,10 +1199,7 @@ export default function Layout({ children }${t(": { readonly children: ReactNode
|
|
|
1110
1199
|
<Link href="/about" underline="hover">
|
|
1111
1200
|
About
|
|
1112
1201
|
</Link>
|
|
1113
|
-
${hasSampleData ? ITEMS_LINK : ""}
|
|
1114
|
-
{otherScheme}
|
|
1115
|
-
</Button>
|
|
1116
|
-
</Flex>
|
|
1202
|
+
${hasSampleData ? ITEMS_LINK : ""}${rendering === "ssg" ? "" : SCHEME_SWITCH} </Flex>
|
|
1117
1203
|
</Flex>
|
|
1118
1204
|
<View as="main" px={3} py={4}>
|
|
1119
1205
|
{children}
|
|
@@ -1129,7 +1215,7 @@ import { Typography } from '@soroush.tech/design-system/Typography'
|
|
|
1129
1215
|
export default function Page() {
|
|
1130
1216
|
return (
|
|
1131
1217
|
<Flex gap={2} alignItems="flex-start">
|
|
1132
|
-
<Typography variant="h2">${
|
|
1218
|
+
<Typography variant="h2">${title}</Typography>
|
|
1133
1219
|
<Typography color="secondary">
|
|
1134
1220
|
A Vike app on the design system. Edit src/pages/index/+Page.${x}x, and make the themes your
|
|
1135
1221
|
own in src/theme/themes.${x}.
|
|
@@ -1152,8 +1238,7 @@ export default function Page() {
|
|
|
1152
1238
|
<Flex gap={2}>
|
|
1153
1239
|
<Typography variant="h2">About</Typography>
|
|
1154
1240
|
<Typography color="secondary">
|
|
1155
|
-
|
|
1156
|
-
reload.
|
|
1241
|
+
${ABOUT_TEXT[rendering]}
|
|
1157
1242
|
</Typography>
|
|
1158
1243
|
</Flex>
|
|
1159
1244
|
)
|
|
@@ -1376,19 +1461,34 @@ export default function Page() {
|
|
|
1376
1461
|
}
|
|
1377
1462
|
];
|
|
1378
1463
|
}
|
|
1379
|
-
|
|
1464
|
+
/** How the README says each rendering works, and which files render the pages. */
|
|
1465
|
+
const README_RENDERING = {
|
|
1466
|
+
isomorphic: {
|
|
1467
|
+
summary: "prerendered at build time with its styles in the HTML, then moving between pages with no reload.",
|
|
1468
|
+
renderers: "`+onRenderHtml` and `+onRenderClient` render the pages, with the styles each page uses in its HTML."
|
|
1469
|
+
},
|
|
1470
|
+
spa: {
|
|
1471
|
+
summary: "a single-page app: each page is an HTML shell from the build, rendered in the browser, which moves between pages with no reload.",
|
|
1472
|
+
renderers: "`+onRenderHtml` writes the HTML shell of each page, and `+onRenderClient` renders the page in it."
|
|
1473
|
+
},
|
|
1474
|
+
ssg: {
|
|
1475
|
+
summary: "every page prerendered to HTML at build time, with its styles, and no JavaScript in the browser.",
|
|
1476
|
+
renderers: "`+onRenderHtml` renders the pages to HTML, with the styles each page uses."
|
|
1477
|
+
}
|
|
1478
|
+
};
|
|
1479
|
+
function createReadme({ title, isTypeScript, rendering, hasSampleData }) {
|
|
1380
1480
|
const x = isTypeScript ? "ts" : "js";
|
|
1381
|
-
|
|
1481
|
+
const { summary, renderers } = README_RENDERING[rendering];
|
|
1482
|
+
return `# ${title}
|
|
1382
1483
|
|
|
1383
|
-
A [Vike](https://vike.dev) app on the [@soroush.tech design system](https://docs.soroush.tech)
|
|
1384
|
-
|
|
1484
|
+
A [Vike](https://vike.dev) app on the [@soroush.tech design system](https://docs.soroush.tech):
|
|
1485
|
+
${summary}
|
|
1385
1486
|
|
|
1386
1487
|
- \`npm run dev\` starts it, \`npm run build\` builds it, and \`npm run preview\` serves the build.
|
|
1387
1488
|
- \`src/pages/\` holds the pages: a folder per route, each with its \`+Page\`.
|
|
1388
1489
|
- \`src/pages/+Layout.${x}x\` is the frame every page renders in, inside the app's providers.
|
|
1389
1490
|
- \`src/theme/themes.${x}\` holds the light and the dark theme: make them your own.
|
|
1390
|
-
- \`src/pages
|
|
1391
|
-
page uses in its HTML.
|
|
1491
|
+
- In \`src/pages/\`, ${renderers}
|
|
1392
1492
|
${hasSampleData ? `- \`src/pages/items/\` lists the sample items of \`src/data/items.${x}\`, with a page for each.
|
|
1393
1493
|
Their \`+data\` hooks read the data at build time: make them fetch your own.
|
|
1394
1494
|
` : ""}
|
|
@@ -1398,9 +1498,11 @@ https://docs.soroush.tech/design-system/getting-started/installation/#fonts
|
|
|
1398
1498
|
}
|
|
1399
1499
|
/** The files of the Vike app `facts` describe. */
|
|
1400
1500
|
function createFiles(facts) {
|
|
1401
|
-
const { isTypeScript, scheme } = facts;
|
|
1501
|
+
const { isTypeScript, rendering, scheme } = facts;
|
|
1402
1502
|
const x = isTypeScript ? "ts" : "js";
|
|
1403
1503
|
const sources = createSources(facts);
|
|
1504
|
+
const isSpa = rendering === "spa";
|
|
1505
|
+
const onRenderClient = rendering === "ssg" ? void 0 : isSpa ? sources.spaOnRenderClient : sources.onRenderClient;
|
|
1404
1506
|
return [
|
|
1405
1507
|
{
|
|
1406
1508
|
path: "package.json",
|
|
@@ -1452,12 +1554,12 @@ function createFiles(facts) {
|
|
|
1452
1554
|
},
|
|
1453
1555
|
{
|
|
1454
1556
|
path: `src/pages/+onRenderHtml.${x}x`,
|
|
1455
|
-
content: sources.onRenderHtml
|
|
1557
|
+
content: isSpa ? sources.spaOnRenderHtml : sources.onRenderHtml
|
|
1456
1558
|
},
|
|
1457
|
-
{
|
|
1559
|
+
...onRenderClient ? [{
|
|
1458
1560
|
path: `src/pages/+onRenderClient.${x}x`,
|
|
1459
|
-
content:
|
|
1460
|
-
},
|
|
1561
|
+
content: onRenderClient
|
|
1562
|
+
}] : [],
|
|
1461
1563
|
{
|
|
1462
1564
|
path: `src/pages/+Layout.${x}x`,
|
|
1463
1565
|
content: sources.Layout
|
|
@@ -1477,20 +1579,37 @@ function createFiles(facts) {
|
|
|
1477
1579
|
...facts.hasSampleData ? createSampleDataFiles(facts) : []
|
|
1478
1580
|
];
|
|
1479
1581
|
}
|
|
1480
|
-
|
|
1481
|
-
/** The starters, by the name `--framework` takes. The first is the default. */
|
|
1482
|
-
const STARTERS = { vike: {
|
|
1582
|
+
const vikeStarter = {
|
|
1483
1583
|
title: "Vike",
|
|
1484
1584
|
createFiles
|
|
1485
|
-
}
|
|
1585
|
+
};
|
|
1586
|
+
// src/create/starters.ts
|
|
1587
|
+
/** The renderings, in the order create offers them. The first is the default. */
|
|
1588
|
+
const RENDERINGS = [
|
|
1589
|
+
"isomorphic",
|
|
1590
|
+
"spa",
|
|
1591
|
+
"ssg"
|
|
1592
|
+
];
|
|
1593
|
+
/** The starters, by the name `--framework` takes. The first is the default. */
|
|
1594
|
+
const STARTERS = { vike: vikeStarter };
|
|
1486
1595
|
// src/create/create.ts
|
|
1487
1596
|
const DEFAULT_NAME = "my-app";
|
|
1488
|
-
const APP_NAME = /^[
|
|
1597
|
+
const APP_NAME = /^[A-Za-z0-9][A-Za-z0-9._~-]*$/;
|
|
1489
1598
|
const MAX_NAME_LENGTH = 214;
|
|
1599
|
+
const TITLE_REFUSED = /[`${}<>\\]/;
|
|
1600
|
+
/** Whether `title` holds a character create cannot write into code and HTML as it is. */
|
|
1601
|
+
const isRefusedTitle = (title) => TITLE_REFUSED.test(title) || [...title].some((character) => character < " ");
|
|
1602
|
+
/**
|
|
1603
|
+
* The package name of the app in the folder `name`: lowercase, as npm takes it, with a `-` where a
|
|
1604
|
+
* capital started a word (`testApp` -> `test-app`).
|
|
1605
|
+
*/
|
|
1606
|
+
function toPackageName(name) {
|
|
1607
|
+
return name.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
1608
|
+
}
|
|
1490
1609
|
/** Why `name` cannot name a new app, or `undefined` when it can. */
|
|
1491
1610
|
function findNameRefusal(name) {
|
|
1492
|
-
if (APP_NAME.test(name) && name.length <= MAX_NAME_LENGTH) return void 0;
|
|
1493
|
-
return `${name} is not a name create can use: it names the folder
|
|
1611
|
+
if (APP_NAME.test(name) && toPackageName(name).length <= MAX_NAME_LENGTH) return void 0;
|
|
1612
|
+
return `${name} is not a name create can use: it names the folder, so it takes letters, digits, and . _ ~ -, and starts with a letter or a digit.`;
|
|
1494
1613
|
}
|
|
1495
1614
|
/**
|
|
1496
1615
|
* The state of the folder the app goes in: `missing` or `empty`, the two create writes in, or why
|
|
@@ -1525,16 +1644,64 @@ function isInsideRepository(folder, io) {
|
|
|
1525
1644
|
const { status, stdout } = io.run("git", ["rev-parse", "--is-inside-work-tree"], { cwd: folder });
|
|
1526
1645
|
return status === 0 && stdout.trim() === "true";
|
|
1527
1646
|
}
|
|
1647
|
+
const YES_OR_NO = [{
|
|
1648
|
+
value: "yes",
|
|
1649
|
+
label: "Yes"
|
|
1650
|
+
}, {
|
|
1651
|
+
value: "no",
|
|
1652
|
+
label: "No"
|
|
1653
|
+
}];
|
|
1528
1654
|
async function askLanguage(io) {
|
|
1529
|
-
|
|
1530
|
-
|
|
1655
|
+
return await io.select("Language", [{
|
|
1656
|
+
value: "ts",
|
|
1657
|
+
label: "TypeScript (recommended)"
|
|
1658
|
+
}, {
|
|
1659
|
+
value: "js",
|
|
1660
|
+
label: "JavaScript"
|
|
1661
|
+
}]) === "ts";
|
|
1662
|
+
}
|
|
1663
|
+
/** Asks for the package manager, starting on the one that ran create. */
|
|
1664
|
+
async function askPackageManager(io, detected) {
|
|
1665
|
+
const choices = PACKAGE_MANAGERS.map((name) => ({
|
|
1666
|
+
value: name,
|
|
1667
|
+
label: name === detected ? `${name} (the one that ran create)` : name
|
|
1668
|
+
}));
|
|
1669
|
+
return await io.select("Package manager", choices, PACKAGE_MANAGERS.indexOf(detected));
|
|
1670
|
+
}
|
|
1671
|
+
/**
|
|
1672
|
+
* The package manager to install with, or why it cannot be used: one other than the one that ran
|
|
1673
|
+
* create must answer `--version`, which also says whether a yarn is Berry.
|
|
1674
|
+
*/
|
|
1675
|
+
function resolvePackageManager(name, detected, io) {
|
|
1676
|
+
if (name === detected.name) return detected;
|
|
1677
|
+
const { status, stdout } = io.run(name, ["--version"], { cwd: io.cwd });
|
|
1678
|
+
if (status !== 0) return { refusal: `${name} is not installed: install it, or pick another package manager.` };
|
|
1679
|
+
return {
|
|
1680
|
+
name,
|
|
1681
|
+
isYarnBerry: name === "yarn" && Number.parseInt(stdout, 10) >= 2
|
|
1682
|
+
};
|
|
1683
|
+
}
|
|
1684
|
+
async function askTitle(io, name) {
|
|
1685
|
+
return (await io.ask(`App title? (${name}) `)).trim() || name;
|
|
1531
1686
|
}
|
|
1532
1687
|
async function askName(io) {
|
|
1533
1688
|
return (await io.ask(`App name? (${DEFAULT_NAME}) `)).trim() || DEFAULT_NAME;
|
|
1534
1689
|
}
|
|
1690
|
+
/** What the rendering question says of each one. */
|
|
1691
|
+
const RENDERING_LABELS = {
|
|
1692
|
+
isomorphic: "Isomorphic (recommended): HTML from the build, then no reload",
|
|
1693
|
+
spa: "SPA: rendered in the browser",
|
|
1694
|
+
ssg: "SSG: HTML only, no JavaScript"
|
|
1695
|
+
};
|
|
1696
|
+
async function askRendering(io) {
|
|
1697
|
+
const choices = RENDERINGS.map((value) => ({
|
|
1698
|
+
value,
|
|
1699
|
+
label: RENDERING_LABELS[value]
|
|
1700
|
+
}));
|
|
1701
|
+
return await io.select("Rendering", choices);
|
|
1702
|
+
}
|
|
1535
1703
|
async function askSampleData(io) {
|
|
1536
|
-
|
|
1537
|
-
return /^y(es)?$/i.test(answer.trim());
|
|
1704
|
+
return await io.select("Add sample data pages?", YES_OR_NO, 1) === "yes";
|
|
1538
1705
|
}
|
|
1539
1706
|
/** Whether to create the app: never on a dry run or with no terminal, else on --yes or a yes. */
|
|
1540
1707
|
async function isApproved(options, isPreviewOnly, io) {
|
|
@@ -1547,8 +1714,7 @@ async function isApproved(options, isPreviewOnly, io) {
|
|
|
1547
1714
|
return false;
|
|
1548
1715
|
}
|
|
1549
1716
|
if (options.isConfirmed) return true;
|
|
1550
|
-
const
|
|
1551
|
-
const isYes = answer === "" || /^y(es)?$/i.test(answer);
|
|
1717
|
+
const isYes = await io.select("Create it?", YES_OR_NO) === "yes";
|
|
1552
1718
|
if (!isYes) io.write("Nothing was written.\n");
|
|
1553
1719
|
return isYes;
|
|
1554
1720
|
}
|
|
@@ -1618,14 +1784,29 @@ async function runCreate(options, io, designSystem, { starters = STARTERS, fileS
|
|
|
1618
1784
|
io.write(`${folder.refusal}\n`);
|
|
1619
1785
|
return 1;
|
|
1620
1786
|
}
|
|
1787
|
+
const title = (options.title ?? (canAsk ? await askTitle(io, name) : name)).trim() || name;
|
|
1788
|
+
if (isRefusedTitle(title)) {
|
|
1789
|
+
io.write(`${title} is not a title create can use: it cannot hold \` $ { } < > or \\.\n`);
|
|
1790
|
+
return 1;
|
|
1791
|
+
}
|
|
1792
|
+
const packageName = toPackageName(name);
|
|
1621
1793
|
const isTypeScript = options.isTypeScript ?? (canAsk ? await askLanguage(io) : true);
|
|
1794
|
+
const detected = readPackageManager(userAgent);
|
|
1795
|
+
const packageManager = resolvePackageManager(options.packageManager ?? (canAsk ? await askPackageManager(io, detected.name) : detected.name), detected, io);
|
|
1796
|
+
if ("refusal" in packageManager) {
|
|
1797
|
+
io.write(`${packageManager.refusal}\n`);
|
|
1798
|
+
return 1;
|
|
1799
|
+
}
|
|
1800
|
+
const rendering = options.rendering ?? (canAsk ? await askRendering(io) : RENDERINGS[0]);
|
|
1622
1801
|
const scheme = options.scheme ?? (canAsk ? await askScheme(io) : "light");
|
|
1623
1802
|
const hasSampleData = options.hasSampleData ?? (canAsk ? await askSampleData(io) : false);
|
|
1624
|
-
const packageManager = readPackageManager(userAgent);
|
|
1625
1803
|
const plan = {
|
|
1626
1804
|
creates: starter.createFiles({
|
|
1627
1805
|
name,
|
|
1806
|
+
packageName,
|
|
1807
|
+
title,
|
|
1628
1808
|
isTypeScript,
|
|
1809
|
+
rendering,
|
|
1629
1810
|
scheme,
|
|
1630
1811
|
hasSampleData,
|
|
1631
1812
|
version: designSystem.version
|
|
@@ -1636,7 +1817,9 @@ async function runCreate(options, io, designSystem, { starters = STARTERS, fileS
|
|
|
1636
1817
|
install: createAppInstallCommand(packageManager)
|
|
1637
1818
|
};
|
|
1638
1819
|
const language = isTypeScript ? "TypeScript" : "JavaScript";
|
|
1639
|
-
|
|
1820
|
+
const packageNote = packageName === name ? "" : ` (package ${packageName})`;
|
|
1821
|
+
const renderingName = rendering === "isomorphic" ? rendering : rendering.toUpperCase();
|
|
1822
|
+
io.write(`create will make ${name}${packageNote}, a ${starter.title} app in ${language} (${renderingName}), in ./${name}:\n\n`);
|
|
1640
1823
|
io.write(`${formatPlan(plan)}\n`);
|
|
1641
1824
|
if (!await isApproved(options, isPreviewOnly, io)) return 0;
|
|
1642
1825
|
return writeAndInstall({
|
|
@@ -1647,30 +1830,112 @@ async function runCreate(options, io, designSystem, { starters = STARTERS, fileS
|
|
|
1647
1830
|
packageManager
|
|
1648
1831
|
}, options, io, fileSystem);
|
|
1649
1832
|
}
|
|
1833
|
+
// src/prompt.ts
|
|
1834
|
+
/** Stopped with Ctrl+C or Escape, before an answer. */
|
|
1835
|
+
var CancelledError = class extends Error {
|
|
1836
|
+
constructor() {
|
|
1837
|
+
super("Stopped before an answer.");
|
|
1838
|
+
this.name = "CancelledError";
|
|
1839
|
+
}
|
|
1840
|
+
};
|
|
1841
|
+
/** Reads a key as Node's `keypress` event names it. */
|
|
1842
|
+
function readChoiceKey(key) {
|
|
1843
|
+
if (!key) return "none";
|
|
1844
|
+
if (key.ctrl && key.name === "c" || key.name === "escape") return "cancel";
|
|
1845
|
+
if (key.name === "up" || key.name === "k") return "up";
|
|
1846
|
+
if (key.name === "down" || key.name === "j") return "down";
|
|
1847
|
+
if (key.name === "space") return "toggle";
|
|
1848
|
+
if (key.name === "return" || key.name === "enter") return "submit";
|
|
1849
|
+
return "none";
|
|
1850
|
+
}
|
|
1851
|
+
/** The state after `key`: the pointer wraps around the ends, and space picks or unpicks. */
|
|
1852
|
+
function updateChoice(state, key, choices) {
|
|
1853
|
+
const { cursor, picked } = state;
|
|
1854
|
+
if (key === "up") return {
|
|
1855
|
+
cursor: (cursor + choices.length - 1) % choices.length,
|
|
1856
|
+
picked
|
|
1857
|
+
};
|
|
1858
|
+
if (key === "down") return {
|
|
1859
|
+
cursor: (cursor + 1) % choices.length,
|
|
1860
|
+
picked
|
|
1861
|
+
};
|
|
1862
|
+
if (!picked) return state;
|
|
1863
|
+
const { value } = choices[cursor];
|
|
1864
|
+
const isPicked = picked.includes(value);
|
|
1865
|
+
return {
|
|
1866
|
+
cursor,
|
|
1867
|
+
picked: choices.map((choice) => choice.value).filter((candidate) => candidate === value ? !isPicked : picked.includes(candidate))
|
|
1868
|
+
};
|
|
1869
|
+
}
|
|
1870
|
+
/** The values a submitted choice answers with. */
|
|
1871
|
+
const getAnswer = ({ cursor, picked }, choices) => picked ? [...picked] : [choices[cursor].value];
|
|
1872
|
+
/** The question and its choices, as drawn while the person picks. Ends with a line break. */
|
|
1873
|
+
function formatChoice(question, { cursor, picked }, choices) {
|
|
1874
|
+
return `? ${question} (${picked ? "space to pick, enter when done" : "arrows to move, enter to pick"})\n${choices.map(({ value, label }, index) => {
|
|
1875
|
+
return `${index === cursor ? ">" : " "} ${picked ? picked.includes(value) ? "[x] " : "[ ] " : ""}${label}`;
|
|
1876
|
+
}).join("\n")}\n`;
|
|
1877
|
+
}
|
|
1878
|
+
/** The question with its answer, as it stays once picked. Ends with a line break. */
|
|
1879
|
+
function formatAnswer(question, values, choices) {
|
|
1880
|
+
return `? ${question}: ${choices.filter(({ value }) => values.includes(value)).map(({ label }) => label).join(", ") || "none"}\n`;
|
|
1881
|
+
}
|
|
1650
1882
|
// src/main.ts
|
|
1651
|
-
const
|
|
1883
|
+
const BIN = "npx @soroush.design/cli";
|
|
1884
|
+
/** The help with no command: the commands, and how to get each one's own help. */
|
|
1885
|
+
const USAGE = `Usage: ${BIN} <command> [options]
|
|
1652
1886
|
|
|
1653
1887
|
Commands:
|
|
1654
1888
|
init set up @soroush.tech/design-system in the React app in this folder
|
|
1655
1889
|
create [name] start a new app on the design system, in a new folder
|
|
1656
1890
|
|
|
1657
1891
|
Each shows every change first, and writes nothing until you say yes.
|
|
1892
|
+
For a command's options, run it with --help: ${BIN} create --help
|
|
1893
|
+
`;
|
|
1894
|
+
const [defaultFramework, ...otherFrameworks] = Object.keys(STARTERS);
|
|
1895
|
+
/** Each command's own help, by its name. */
|
|
1896
|
+
const HELP = {
|
|
1897
|
+
init: `Usage: ${BIN} init [options]
|
|
1898
|
+
|
|
1899
|
+
Sets the design system up in the React app in this folder, or in a workspace's root that holds
|
|
1900
|
+
one app. It shows every change first, and writes nothing until you say yes.
|
|
1658
1901
|
|
|
1659
1902
|
Options:
|
|
1660
|
-
-y, --yes
|
|
1661
|
-
--dry-run
|
|
1662
|
-
--
|
|
1663
|
-
|
|
1903
|
+
-y, --yes apply without asking
|
|
1904
|
+
--dry-run show the changes and exit
|
|
1905
|
+
--force run outside git, or with uncommitted changes
|
|
1906
|
+
--scheme <name> the default color scheme: light (the default) or dark
|
|
1907
|
+
-h, --help show this help
|
|
1908
|
+
|
|
1909
|
+
Example:
|
|
1910
|
+
${BIN} init --scheme dark
|
|
1911
|
+
`,
|
|
1912
|
+
create: `Usage: ${BIN} create [name] [options]
|
|
1664
1913
|
|
|
1665
|
-
|
|
1666
|
-
|
|
1914
|
+
Starts a new app on the design system in ./<name>, my-app when it is not given: a new folder, or
|
|
1915
|
+
an empty one. Its package.json takes the name in lowercase: testApp is test-app. It asks for what
|
|
1916
|
+
the options do not say, shows every file first, and writes nothing until you say yes.
|
|
1667
1917
|
|
|
1668
|
-
Options
|
|
1669
|
-
|
|
1918
|
+
Options:
|
|
1919
|
+
-y, --yes create without asking: TypeScript, light, no sample data
|
|
1920
|
+
--dry-run show the files and exit
|
|
1921
|
+
--title <text> what the app's pages call it: the name when not given
|
|
1922
|
+
--framework <name> the app's framework: ${[`${defaultFramework} (the default)`, ...otherFrameworks].join(", ")}
|
|
1670
1923
|
--ts, --js TypeScript (the default) or JavaScript
|
|
1671
|
-
--
|
|
1924
|
+
--pm <name> the package manager: npm, pnpm, yarn or bun; the one that ran create
|
|
1925
|
+
when not given
|
|
1926
|
+
--render <name> how pages render: isomorphic (the default), spa or ssg
|
|
1927
|
+
--scheme <name> the default color scheme: light (the default) or dark
|
|
1928
|
+
--sample-data add a list of sample items, and a page for each
|
|
1672
1929
|
--no-git do not run git init
|
|
1673
|
-
|
|
1930
|
+
-h, --help show this help
|
|
1931
|
+
|
|
1932
|
+
Examples:
|
|
1933
|
+
${BIN} create my-app
|
|
1934
|
+
${BIN} create my-app --yes --js --sample-data
|
|
1935
|
+
`
|
|
1936
|
+
};
|
|
1937
|
+
/** The help of `command`, or the one with no command. */
|
|
1938
|
+
const getHelp = (command) => command && Object.hasOwn(HELP, command) ? HELP[command] : USAGE;
|
|
1674
1939
|
const OPTIONS = {
|
|
1675
1940
|
yes: {
|
|
1676
1941
|
type: "boolean",
|
|
@@ -1684,8 +1949,11 @@ const OPTIONS = {
|
|
|
1684
1949
|
},
|
|
1685
1950
|
force: { type: "boolean" },
|
|
1686
1951
|
framework: { type: "string" },
|
|
1952
|
+
title: { type: "string" },
|
|
1687
1953
|
ts: { type: "boolean" },
|
|
1688
1954
|
js: { type: "boolean" },
|
|
1955
|
+
pm: { type: "string" },
|
|
1956
|
+
render: { type: "string" },
|
|
1689
1957
|
"sample-data": { type: "boolean" },
|
|
1690
1958
|
"no-git": { type: "boolean" }
|
|
1691
1959
|
};
|
|
@@ -1694,8 +1962,11 @@ const OWN_OPTIONS = {
|
|
|
1694
1962
|
init: ["force"],
|
|
1695
1963
|
create: [
|
|
1696
1964
|
"framework",
|
|
1965
|
+
"title",
|
|
1697
1966
|
"ts",
|
|
1698
1967
|
"js",
|
|
1968
|
+
"pm",
|
|
1969
|
+
"render",
|
|
1699
1970
|
"sample-data",
|
|
1700
1971
|
"no-git"
|
|
1701
1972
|
]
|
|
@@ -1712,9 +1983,19 @@ function findMisuse(command, extra, used) {
|
|
|
1712
1983
|
}
|
|
1713
1984
|
/**
|
|
1714
1985
|
* Reads the command line and runs the command, for `designSystem`: the package of
|
|
1715
|
-
* `@soroush.tech/design-system` the command installs. Returns the exit code
|
|
1986
|
+
* `@soroush.tech/design-system` the command installs. Returns the exit code: 130 when the person
|
|
1987
|
+
* stops it at a question, which is before anything is written.
|
|
1716
1988
|
*/
|
|
1717
1989
|
async function main(argv, io, designSystem) {
|
|
1990
|
+
try {
|
|
1991
|
+
return await runCommandLine(argv, io, designSystem);
|
|
1992
|
+
} catch (error) {
|
|
1993
|
+
if (!(error instanceof CancelledError)) throw error;
|
|
1994
|
+
io.write("Stopped: nothing was written.\n");
|
|
1995
|
+
return 130;
|
|
1996
|
+
}
|
|
1997
|
+
}
|
|
1998
|
+
async function runCommandLine(argv, io, designSystem) {
|
|
1718
1999
|
let parsed;
|
|
1719
2000
|
try {
|
|
1720
2001
|
parsed = parseArgs({
|
|
@@ -1723,15 +2004,16 @@ async function main(argv, io, designSystem) {
|
|
|
1723
2004
|
options: OPTIONS
|
|
1724
2005
|
});
|
|
1725
2006
|
} catch (error) {
|
|
1726
|
-
|
|
2007
|
+
const command = argv.find((arg) => Object.hasOwn(HELP, arg));
|
|
2008
|
+
io.write(`${error.message}\n\n${getHelp(command)}`);
|
|
1727
2009
|
return 1;
|
|
1728
2010
|
}
|
|
1729
2011
|
const { values, positionals } = parsed;
|
|
2012
|
+
const [command, ...extra] = positionals;
|
|
1730
2013
|
if (values.help) {
|
|
1731
|
-
io.write(
|
|
2014
|
+
io.write(getHelp(command));
|
|
1732
2015
|
return 0;
|
|
1733
2016
|
}
|
|
1734
|
-
const [command, ...extra] = positionals;
|
|
1735
2017
|
if (!command || !Object.hasOwn(OWN_OPTIONS, command)) {
|
|
1736
2018
|
const unknown = command ? `Unknown command: ${positionals.join(" ")}\n\n` : "";
|
|
1737
2019
|
io.write(unknown + USAGE);
|
|
@@ -1739,7 +2021,7 @@ async function main(argv, io, designSystem) {
|
|
|
1739
2021
|
}
|
|
1740
2022
|
const misuse = findMisuse(command, extra, Object.keys(values));
|
|
1741
2023
|
if (misuse) {
|
|
1742
|
-
io.write(`${misuse}\n\n${
|
|
2024
|
+
io.write(`${misuse}\n\n${getHelp(command)}`);
|
|
1743
2025
|
return 1;
|
|
1744
2026
|
}
|
|
1745
2027
|
const { scheme } = values;
|
|
@@ -1751,14 +2033,27 @@ async function main(argv, io, designSystem) {
|
|
|
1751
2033
|
io.write("--ts and --js do not go together: give one.\n");
|
|
1752
2034
|
return 1;
|
|
1753
2035
|
}
|
|
2036
|
+
const { render } = values;
|
|
2037
|
+
if (render !== void 0 && !RENDERINGS.includes(render)) {
|
|
2038
|
+
io.write(`--render is ${RENDERINGS.join(", ")}, not ${render}.\n`);
|
|
2039
|
+
return 1;
|
|
2040
|
+
}
|
|
2041
|
+
const { pm } = values;
|
|
2042
|
+
if (pm !== void 0 && !PACKAGE_MANAGERS.includes(pm)) {
|
|
2043
|
+
io.write(`--pm is ${PACKAGE_MANAGERS.join(", ")}, not ${pm}.\n`);
|
|
2044
|
+
return 1;
|
|
2045
|
+
}
|
|
1754
2046
|
const isConfirmed = Boolean(values.yes);
|
|
1755
2047
|
const isDryRun = Boolean(values["dry-run"]);
|
|
1756
2048
|
if (command === "create") {
|
|
1757
2049
|
const isTypeScript = values.ts || values.js ? Boolean(values.ts) : void 0;
|
|
1758
2050
|
return runCreate({
|
|
1759
2051
|
name: extra[0],
|
|
2052
|
+
title: values.title,
|
|
1760
2053
|
framework: values.framework,
|
|
1761
2054
|
isTypeScript,
|
|
2055
|
+
packageManager: pm,
|
|
2056
|
+
rendering: render,
|
|
1762
2057
|
scheme,
|
|
1763
2058
|
hasSampleData: values["sample-data"],
|
|
1764
2059
|
isConfirmed,
|
|
@@ -1783,6 +2078,52 @@ function toWindowsCommandLine(command, args) {
|
|
|
1783
2078
|
for (const part of [command, ...args]) if (!PLAIN_ARGUMENT.test(part)) throw new Error(`init does not run the argument ${part}`);
|
|
1784
2079
|
return [command, ...args.map((arg) => `"${arg}"`)].join(" ");
|
|
1785
2080
|
}
|
|
2081
|
+
const HIDE_CURSOR = "\x1B[?25l";
|
|
2082
|
+
const SHOW_CURSOR = "\x1B[?25h";
|
|
2083
|
+
const eraseLines = (count) => count > 0 ? `\u001B[${count}A\r\u001B[J` : "";
|
|
2084
|
+
/**
|
|
2085
|
+
* Draws a choice on the terminal and redraws it on each key, until Enter answers it or Ctrl+C or
|
|
2086
|
+
* Escape stops it. The terminal is in raw mode only while the choice is open.
|
|
2087
|
+
*/
|
|
2088
|
+
function runChoice(stdin, stdout, question, choices, start) {
|
|
2089
|
+
return new Promise((resolve, reject) => {
|
|
2090
|
+
let state = start;
|
|
2091
|
+
let drawnLines = 0;
|
|
2092
|
+
const draw = (text) => {
|
|
2093
|
+
stdout.write(eraseLines(drawnLines) + text);
|
|
2094
|
+
drawnLines = text.split("\n").length - 1;
|
|
2095
|
+
};
|
|
2096
|
+
const wasRaw = Boolean(stdin.isRaw);
|
|
2097
|
+
const close = (text) => {
|
|
2098
|
+
stdin.off("keypress", onKeypress);
|
|
2099
|
+
stdin.setRawMode?.(wasRaw);
|
|
2100
|
+
stdin.pause();
|
|
2101
|
+
draw(text);
|
|
2102
|
+
stdout.write(SHOW_CURSOR);
|
|
2103
|
+
};
|
|
2104
|
+
function onKeypress(_text, key) {
|
|
2105
|
+
const action = readChoiceKey(key);
|
|
2106
|
+
if (action === "none") return;
|
|
2107
|
+
if (action === "cancel") {
|
|
2108
|
+
close(`? ${question}\n`);
|
|
2109
|
+
reject(new CancelledError());
|
|
2110
|
+
} else if (action === "submit") {
|
|
2111
|
+
const answer = getAnswer(state, choices);
|
|
2112
|
+
close(formatAnswer(question, answer, choices));
|
|
2113
|
+
resolve(answer);
|
|
2114
|
+
} else {
|
|
2115
|
+
state = updateChoice(state, action, choices);
|
|
2116
|
+
draw(formatChoice(question, state, choices));
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
emitKeypressEvents(stdin);
|
|
2120
|
+
stdin.setRawMode?.(true);
|
|
2121
|
+
stdin.on("keypress", onKeypress);
|
|
2122
|
+
stdin.resume();
|
|
2123
|
+
stdout.write(HIDE_CURSOR);
|
|
2124
|
+
draw(formatChoice(question, state, choices));
|
|
2125
|
+
});
|
|
2126
|
+
}
|
|
1786
2127
|
function createNodeIo({ stdin = process.stdin, stdout = process.stdout, platform = process.platform, cwd = process.cwd() } = {}) {
|
|
1787
2128
|
return {
|
|
1788
2129
|
cwd,
|
|
@@ -1801,6 +2142,17 @@ function createNodeIo({ stdin = process.stdin, stdout = process.stdout, platform
|
|
|
1801
2142
|
prompt.close();
|
|
1802
2143
|
}
|
|
1803
2144
|
},
|
|
2145
|
+
select: async (question, choices, initial = 0) => {
|
|
2146
|
+
const [value] = await runChoice(stdin, stdout, question, choices, {
|
|
2147
|
+
cursor: initial,
|
|
2148
|
+
picked: void 0
|
|
2149
|
+
});
|
|
2150
|
+
return value;
|
|
2151
|
+
},
|
|
2152
|
+
selectMany: (question, choices, initial = []) => runChoice(stdin, stdout, question, choices, {
|
|
2153
|
+
cursor: 0,
|
|
2154
|
+
picked: initial
|
|
2155
|
+
}),
|
|
1804
2156
|
run: (command, args, { cwd: directory, env = {}, showOutput = false }) => {
|
|
1805
2157
|
const options = {
|
|
1806
2158
|
cwd: directory,
|
|
@@ -1822,4 +2174,4 @@ function createNodeIo({ stdin = process.stdin, stdout = process.stdout, platform
|
|
|
1822
2174
|
}
|
|
1823
2175
|
};
|
|
1824
2176
|
}
|
|
1825
|
-
export { main as n,
|
|
2177
|
+
export { readDesignSystemPackage as i, main as n, version as r, createNodeIo as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soroush.design/cli",
|
|
3
|
-
"version": "0.2.0-alpha.
|
|
3
|
+
"version": "0.2.0-alpha.2",
|
|
4
4
|
"description": "The command line of the @soroush.tech design system: init sets it up in an existing React app, and create starts a new one, each showing every change first.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"typecheck": "tsc --noEmit",
|
|
62
62
|
"test": "vitest run",
|
|
63
63
|
"test:coverage": "vitest run --coverage",
|
|
64
|
-
"test:types": "pnpm --filter @soroush.tech/design-system build && tsc -p type-tests/init/tsconfig.json && tsc -p type-tests/tsconfig.create-vike.json"
|
|
64
|
+
"test:types": "pnpm --filter @soroush.tech/design-system build && tsc -p type-tests/init/tsconfig.json && tsc -p type-tests/tsconfig.create-vike.json && tsc -p type-tests/tsconfig.create-vike-spa.json && tsc -p type-tests/tsconfig.create-vike-ssg.json"
|
|
65
65
|
},
|
|
66
66
|
"module": "./dist/index.mjs"
|
|
67
67
|
}
|