@nectary/cli 1.0.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 +111 -0
- package/dist/add-helpers.js +43 -0
- package/dist/add.js +68 -0
- package/dist/config.js +20 -0
- package/dist/index.js +13 -0
- package/dist/install.js +33 -0
- package/dist/list.js +19 -0
- package/dist/registry/button.json +11 -0
- package/dist/registry/phone-input.json +13 -0
- package/dist/registry/select.json +11 -0
- package/dist/registry/table.json +11 -0
- package/dist/registry.js +104 -0
- package/dist/validate.js +39 -0
- package/dist/view.js +26 -0
- package/package.json +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Nectary CLI
|
|
2
|
+
|
|
3
|
+
Add Nectary compositions (higher-order React components built on `@nectary/components`) directly into your project. This follows the same “copy source into your repo” approach as [Shadcn UI](https://ui.shadcn.com/), so you own the code and can edit it. It is aimed at React 16–18 apps where using these compositions as web components would require awkward serialization of objects/arrays.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Node ^18.19.0 or >=20.5.0 (see `engines` in package.json)
|
|
8
|
+
- A project with `@nectary/components` installed
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
### List compositions
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npx nectary@latest list
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Prints each available composition name and its one-line description.
|
|
19
|
+
|
|
20
|
+
### View a composition
|
|
21
|
+
|
|
22
|
+
Preview a composition before adding it (description, dependencies, and files that would be written):
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npx nectary@latest view <name>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Example:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npx nectary@latest view table
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Add a composition
|
|
35
|
+
|
|
36
|
+
From your project root:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npx nectary@latest add <name>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Examples:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npx nectary@latest add button
|
|
46
|
+
npx nectary@latest add select
|
|
47
|
+
npx nectary@latest add phone-input
|
|
48
|
+
npx nectary@latest add table
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Files are written under `src/components/nectary/` by default (see [Configuration](#configuration)). The CLI will also install any npm dependencies required by the composition (e.g. `countries-phone-masks` for `phone-input`).
|
|
52
|
+
|
|
53
|
+
### Options
|
|
54
|
+
|
|
55
|
+
- **`--path <path>`** – Override the output directory (default: `src/components/nectary`).
|
|
56
|
+
- **`--overwrite`** – Replace existing files. Without this, existing files are skipped.
|
|
57
|
+
|
|
58
|
+
### Configuration
|
|
59
|
+
|
|
60
|
+
Optional: create a `nectary.json` file at your project root to set the default components path:
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"componentsPath": "src/components/nectary"
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
If `nectary.json` is missing, the CLI uses `src/components/nectary`.
|
|
69
|
+
|
|
70
|
+
## Available compositions
|
|
71
|
+
|
|
72
|
+
Compositions live in this package under `registry/` and can be customized (e.g. add props) without touching the docs.
|
|
73
|
+
|
|
74
|
+
| Name | Description | Dependencies |
|
|
75
|
+
|------|-------------|--------------|
|
|
76
|
+
| `button` | Simple Button (sinch-button). Props: `text`, `aria-label`, `type`, `size`, `disabled`, `toggled`, `formType`, `onClick`. | — |
|
|
77
|
+
| `select` | Select with search. Props: `options`, `placeholder`, `searchPlaceholder`, `value`, `onChange`, `style`, `ariaLabel`. | — |
|
|
78
|
+
| `phone-input` | Phone input with country code selector. Props: `placeholder`, `value`, `onChange`, `style`, `ariaLabel`. | countries-phone-masks |
|
|
79
|
+
| `table` | Table composition. Props: `columns`, `data`, `getRowKey`. | — |
|
|
80
|
+
|
|
81
|
+
## Running locally (development)
|
|
82
|
+
|
|
83
|
+
From the monorepo root or from `packages/nectary-cli`:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
pnpm --filter nectary build
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Then from the project where you want to add a composition:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
node /path/to/nectary/packages/nectary-cli/dist/index.js add select
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Or from `packages/nectary-cli`:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
node dist/index.js add select
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Adding a new composition to the registry
|
|
102
|
+
|
|
103
|
+
1. **Add the composition source** in this package under `registry/<name>/` (e.g. `registry/select/Search.tsx`). You can add whatever props you want; these files are the single source of truth for the CLI and are independent of the docs.
|
|
104
|
+
2. **Edit `registry.json`** in this package. Add an item with:
|
|
105
|
+
- `name` – Kebab-case id used in `nectary add <name>`.
|
|
106
|
+
- `description` – Short description.
|
|
107
|
+
- `dependencies` – Array of npm package names (e.g. `"zod"`, `"@hookform/resolvers"`).
|
|
108
|
+
- `files` – Array of `{ "path": "OutputPath/File.tsx", "source": "registry/<name>/File.tsx" }`.
|
|
109
|
+
`path` is where the file will be written in the user's project (relative to their components path). `source` is relative to **this package root** (e.g. `registry/select/Search.tsx`).
|
|
110
|
+
3. **Rebuild the registry**: from `packages/nectary-cli`, run `pnpm run build` (or `pnpm run build:registry` after `tsc`). This reads the manifest and writes `dist/registry/<name>.json`.
|
|
111
|
+
4. **Publish** the `nectary` package so users can run `npx nectary@latest add <name>`.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
/**
|
|
4
|
+
* Resolves file.path relative to baseDir and ensures the result is inside baseDir.
|
|
5
|
+
* Prevents path traversal (e.g. ../../../etc/passwd) when using remote registry items.
|
|
6
|
+
* Returns null if the path would escape baseDir.
|
|
7
|
+
*/
|
|
8
|
+
export function resolvePathWithinBase(baseDir, filePath) {
|
|
9
|
+
const normalizedBase = path.resolve(baseDir);
|
|
10
|
+
if (filePath.includes('\0')) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
const resolved = path.resolve(normalizedBase, filePath);
|
|
14
|
+
const relative = path.relative(normalizedBase, resolved);
|
|
15
|
+
if (relative.startsWith('..') || path.isAbsolute(relative)) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return resolved;
|
|
19
|
+
}
|
|
20
|
+
/** Extract package name and full spec from a dependency spec (e.g. "zod@^3.20.0" or "@hookform/resolvers"). */
|
|
21
|
+
export function parseDepSpec(spec) {
|
|
22
|
+
const lastAt = spec.lastIndexOf('@');
|
|
23
|
+
if (lastAt <= 0) {
|
|
24
|
+
return { name: spec, spec };
|
|
25
|
+
}
|
|
26
|
+
const afterAt = spec.slice(lastAt + 1);
|
|
27
|
+
if (/^[\d^~]/.test(afterAt) || /^\d/.test(afterAt)) {
|
|
28
|
+
return { name: spec.slice(0, lastAt), spec };
|
|
29
|
+
}
|
|
30
|
+
return { name: spec, spec };
|
|
31
|
+
}
|
|
32
|
+
export function filterMissingDependencies(cwd, deps) {
|
|
33
|
+
const pkgPath = path.join(cwd, 'package.json');
|
|
34
|
+
if (!fs.existsSync(pkgPath)) {
|
|
35
|
+
return deps;
|
|
36
|
+
}
|
|
37
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
38
|
+
const all = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
39
|
+
return deps.filter((spec) => {
|
|
40
|
+
const { name } = parseDepSpec(spec);
|
|
41
|
+
return !Object.prototype.hasOwnProperty.call(all, name);
|
|
42
|
+
});
|
|
43
|
+
}
|
package/dist/add.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { Command } from 'commander';
|
|
5
|
+
import { filterMissingDependencies, resolvePathWithinBase } from './add-helpers.js';
|
|
6
|
+
import { getComponentsPath, loadNectaryConfig } from './config.js';
|
|
7
|
+
import { installDependencies } from './install.js';
|
|
8
|
+
import { loadRegistryItem } from './registry.js';
|
|
9
|
+
import { validateProjectDependencies } from './validate.js';
|
|
10
|
+
export function addCommand() {
|
|
11
|
+
return new Command('add')
|
|
12
|
+
.description('Add a composition (e.g. select, phone-input, table)')
|
|
13
|
+
.argument('<name>', 'Registry item name or URL to a registry item JSON')
|
|
14
|
+
.option('-p, --path <path>', 'Override components output path')
|
|
15
|
+
.option('-o, --overwrite', 'Overwrite existing files', false)
|
|
16
|
+
.option('-y, --yes', 'Skip any confirmation prompts (non-interactive, for scripting)', false)
|
|
17
|
+
.action(async (name, options) => {
|
|
18
|
+
const cwd = process.cwd();
|
|
19
|
+
validateProjectDependencies(cwd);
|
|
20
|
+
const config = loadNectaryConfig(cwd);
|
|
21
|
+
const basePath = options.path ?? getComponentsPath(config);
|
|
22
|
+
const cliDir = path.dirname(fileURLToPath(import.meta.url));
|
|
23
|
+
const item = await loadRegistryItem(name, cliDir);
|
|
24
|
+
if (item == null) {
|
|
25
|
+
console.error(`Error: composition "${name}" not found.`);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
const baseDir = path.resolve(cwd, basePath);
|
|
29
|
+
const written = [];
|
|
30
|
+
for (const file of item.files) {
|
|
31
|
+
const targetPath = resolvePathWithinBase(baseDir, file.path);
|
|
32
|
+
if (targetPath === null) {
|
|
33
|
+
console.error(`Error: invalid file path "${file.path}" (path traversal not allowed).`);
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
const dir = path.dirname(targetPath);
|
|
37
|
+
if (!fs.existsSync(dir)) {
|
|
38
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
39
|
+
}
|
|
40
|
+
if (fs.existsSync(targetPath) && options.overwrite !== true) {
|
|
41
|
+
console.warn(`Skip (exists): ${path.relative(cwd, targetPath)}`);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
fs.writeFileSync(targetPath, file.content, 'utf8');
|
|
45
|
+
written.push(path.relative(cwd, targetPath));
|
|
46
|
+
console.log(`Added: ${path.relative(cwd, targetPath)}`);
|
|
47
|
+
}
|
|
48
|
+
if (item.dependencies.length > 0) {
|
|
49
|
+
const toInstall = filterMissingDependencies(cwd, item.dependencies);
|
|
50
|
+
if (toInstall.length > 0) {
|
|
51
|
+
try {
|
|
52
|
+
await installDependencies(cwd, toInstall);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
56
|
+
console.error(`Failed to install dependencies: ${message}`);
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
console.log('All dependencies already present.');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (written.length === 0 && options.overwrite !== true) {
|
|
65
|
+
console.log('No files written (use --overwrite to replace existing).');
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
const DEFAULT_COMPONENTS_PATH = 'src/components/nectary';
|
|
4
|
+
const CONFIG_FILENAME = 'nectary.json';
|
|
5
|
+
export function loadNectaryConfig(cwd) {
|
|
6
|
+
const configPath = path.join(cwd, CONFIG_FILENAME);
|
|
7
|
+
if (!fs.existsSync(configPath)) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
try {
|
|
11
|
+
const raw = fs.readFileSync(configPath, 'utf8');
|
|
12
|
+
return JSON.parse(raw);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function getComponentsPath(config) {
|
|
19
|
+
return config?.componentsPath ?? DEFAULT_COMPONENTS_PATH;
|
|
20
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { program } from 'commander';
|
|
3
|
+
import { addCommand } from './add.js';
|
|
4
|
+
import { listCommand } from './list.js';
|
|
5
|
+
import { viewCommand } from './view.js';
|
|
6
|
+
program
|
|
7
|
+
.name('nectary')
|
|
8
|
+
.description('Add Nectary compositions as React components to your project')
|
|
9
|
+
.version('0.0.1');
|
|
10
|
+
program.addCommand(addCommand());
|
|
11
|
+
program.addCommand(listCommand());
|
|
12
|
+
program.addCommand(viewCommand());
|
|
13
|
+
program.parse();
|
package/dist/install.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { detect } from '@antfu/ni';
|
|
2
|
+
import { execa } from 'execa';
|
|
3
|
+
function normalizePackageManager(detected) {
|
|
4
|
+
if (detected === undefined || detected === null || typeof detected !== 'string') {
|
|
5
|
+
return 'npm';
|
|
6
|
+
}
|
|
7
|
+
const id = detected.toLowerCase();
|
|
8
|
+
if (id.startsWith('yarn')) {
|
|
9
|
+
return 'yarn';
|
|
10
|
+
}
|
|
11
|
+
if (id.startsWith('pnpm')) {
|
|
12
|
+
return 'pnpm';
|
|
13
|
+
}
|
|
14
|
+
if (id.startsWith('bun')) {
|
|
15
|
+
return 'bun';
|
|
16
|
+
}
|
|
17
|
+
return 'npm';
|
|
18
|
+
}
|
|
19
|
+
export async function getPackageManager(cwd) {
|
|
20
|
+
const detected = await detect({ programmatic: true, cwd });
|
|
21
|
+
return normalizePackageManager(detected ?? undefined);
|
|
22
|
+
}
|
|
23
|
+
export async function installDependencies(cwd, specs) {
|
|
24
|
+
if (specs.length === 0) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const pm = await getPackageManager(cwd);
|
|
28
|
+
const args = pm === 'npm'
|
|
29
|
+
? ['install', '--save', ...specs]
|
|
30
|
+
: ['add', ...specs];
|
|
31
|
+
console.log(`Running ${pm} ${args.join(' ')}...`);
|
|
32
|
+
await execa(pm, args, { cwd, stdio: 'inherit' });
|
|
33
|
+
}
|
package/dist/list.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
import { Command } from 'commander';
|
|
4
|
+
import { listRegistryItems } from './registry.js';
|
|
5
|
+
export function listCommand() {
|
|
6
|
+
return new Command('list')
|
|
7
|
+
.description('List available compositions (name and description)')
|
|
8
|
+
.action(() => {
|
|
9
|
+
const cliDir = path.dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const items = listRegistryItems(cliDir);
|
|
11
|
+
if (items.length === 0) {
|
|
12
|
+
console.log('No compositions found.');
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
for (const { name, description } of items) {
|
|
16
|
+
console.log(`${name} - ${description}`);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "button",
|
|
3
|
+
"description": "Simple Button wrapper (sinch-button). Props: text, aria-label, type, size, disabled, toggled, formType, onClick.",
|
|
4
|
+
"dependencies": [],
|
|
5
|
+
"files": [
|
|
6
|
+
{
|
|
7
|
+
"path": "Button/Button.tsx",
|
|
8
|
+
"content": "import type { FC } from 'react'\nimport '@nectary/components/button'\n\nexport type ButtonType =\n | 'primary'\n | 'secondary'\n | 'subtle-primary'\n | 'subtle-secondary'\n | 'cta-primary'\n | 'cta-secondary'\n | 'destructive'\n\nexport type ButtonSize = 's' | 'm' | 'l'\n\nexport interface ButtonProps {\n /** Button label (visible text). */\n text: string,\n /** Accessible name for the button. */\n 'aria-label': string,\n /** Visual type. Default: primary. */\n type?: ButtonType,\n /** Size: s, m, l. Default: m. */\n size?: ButtonSize,\n isDisabled?: boolean,\n isToggled?: boolean,\n /** Form association: button, submit, reset. Default: button. */\n formType?: 'button' | 'submit' | 'reset',\n onClick?: () => void,\n}\n\nexport const Button: FC<ButtonProps> = ({\n text,\n 'aria-label': ariaLabel,\n type = 'primary',\n size = 'm',\n isDisabled = false,\n isToggled = false,\n formType = 'button',\n onClick,\n}) => {\n return (\n <sinch-button\n text={text}\n aria-label={ariaLabel}\n type={type as 'primary' | 'secondary' | 'subtle-primary' | 'subtle-secondary' | 'cta-primary' | 'cta-secondary' | 'destructive'}\n size={size as 's' | 'm' | 'l'}\n disabled={isDisabled}\n toggled={isToggled}\n form-type={formType}\n on-click={onClick}\n />\n )\n}"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "phone-input",
|
|
3
|
+
"description": "Phone input with country code selector. Accepts placeholder, value, onChange, style and ariaLabel props.",
|
|
4
|
+
"dependencies": [
|
|
5
|
+
"countries-phone-masks@^1.1.0"
|
|
6
|
+
],
|
|
7
|
+
"files": [
|
|
8
|
+
{
|
|
9
|
+
"path": "PhoneInput/PhoneInput.tsx",
|
|
10
|
+
"content": "import countries from 'countries-phone-masks'\nimport { useState } from 'react'\nimport type { CSSProperties, FC } from 'react'\nimport '@nectary/components/popover'\nimport '@nectary/components/input'\nimport '@nectary/components/select-menu'\nimport '@nectary/components/select-menu-option'\nimport '@nectary/components/select-button'\nimport '@nectary/components/flag'\n\nconst defaultInputStyles: CSSProperties = {\n width: 300,\n}\n\nexport interface PhoneInputProps {\n placeholder?: string,\n value?: string,\n onChange?: (value: string) => void,\n style?: CSSProperties,\n ariaLabel?: string,\n}\n\nexport const PhoneInput: FC<PhoneInputProps> = ({\n placeholder = 'Phone Number',\n value: controlledValue,\n onChange,\n style = defaultInputStyles,\n ariaLabel = 'Phone number',\n}) => {\n const [isOpen, setIsOpen] = useState(false)\n const [menuValue, setMenuValue] = useState('')\n const [internalValue, setInternalValue] = useState('')\n const inputValue = controlledValue ?? internalValue\n const setInputValue = onChange != null\n ? (v: string) => {\n onChange(v); setInternalValue(v)\n }\n : setInternalValue\n\n const onClose = () => setIsOpen(false)\n const onOpen = () => setIsOpen(true)\n const onMenuChange = (e: CustomEvent<string>) => {\n onClose()\n setMenuValue(e.detail)\n }\n const onInputChange = (e: CustomEvent<string>) => {\n setInputValue(e.detail)\n }\n const phoneCode = countries.find((country) => country.iso === menuValue)?.code ?? ''\n const phoneMask = countries.find((country) => country.iso === menuValue)?.mask ?? ''\n\n return (\n <sinch-popover\n open={isOpen}\n aria-label=\"Select\"\n orientation=\"bottom\"\n modal\n on-close={onClose}\n >\n <sinch-input\n slot=\"target\"\n aria-label={ariaLabel}\n mask={(Array.isArray(phoneMask) ? phoneMask[0] : phoneMask).replaceAll('#', '0')}\n placeholder={placeholder}\n value={inputValue}\n style={style}\n on-change={onInputChange}\n >\n <sinch-select-button\n slot=\"left\"\n size=\"s\"\n text={phoneCode}\n placeholder=\"+0\"\n aria-label=\"Open area code select\"\n on-click={onOpen}\n >\n {menuValue.length > 0 && (\n <sinch-flag slot=\"icon\" code={menuValue.toLowerCase()}/>\n )}\n </sinch-select-button>\n </sinch-input>\n <sinch-select-menu\n slot=\"content\"\n rows={5}\n aria-label={menuValue !== '' ? `Area code selected ${menuValue}` : 'Select area code'}\n value={menuValue}\n on-change={onMenuChange}\n >\n {countries.map(({ code, iso, name }) => (\n <sinch-select-menu-option\n key={iso}\n value={iso}\n text={`(${code}) ${name}`}\n aria-label={`Select ${name}`}\n >\n <sinch-flag slot=\"icon\" code={iso.toLowerCase()}/>\n </sinch-select-menu-option>\n ))}\n </sinch-select-menu>\n </sinch-popover>\n )\n}"
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "select",
|
|
3
|
+
"description": "Select composition with search (Popover + SelectButton + SelectMenu). Accepts options, placeholder, value, onChange and style props.",
|
|
4
|
+
"dependencies": [],
|
|
5
|
+
"files": [
|
|
6
|
+
{
|
|
7
|
+
"path": "Select/Search.tsx",
|
|
8
|
+
"content": "import { useState } from 'react'\nimport type { CSSProperties, FC } from 'react'\nimport '@nectary/components/popover'\nimport '@nectary/components/select-button'\nimport '@nectary/components/select-menu'\nimport '@nectary/components/select-menu-option'\n\nconst defaultSelectStyles: CSSProperties = {\n width: 300,\n}\n\nexport interface SelectSearchProps {\n /** Options to display. */\n options?: string[],\n placeholder?: string,\n searchPlaceholder?: string,\n /** Controlled value */\n value?: string,\n /** Callback when selection changes */\n onChange?: (value: string) => void,\n style?: CSSProperties,\n ariaLabel?: string,\n}\n\nexport const SelectSearch: FC<SelectSearchProps> = ({\n options = [],\n placeholder = 'Select option',\n searchPlaceholder = 'Search',\n value: controlledValue,\n onChange,\n style = defaultSelectStyles,\n ariaLabel = 'Select',\n}) => {\n const [isOpen, setIsOpen] = useState(false)\n const [internalValue, setInternalValue] = useState('')\n const value = controlledValue ?? internalValue\n const setValue = onChange != null ? (v: string) => {\n onChange(v); setInternalValue(v)\n } : setInternalValue\n\n const onSelectChange = (e: CustomEvent<string>) => {\n setValue(e.detail)\n setIsOpen(false)\n }\n const onClose = () => setIsOpen(false)\n const onOpen = () => setIsOpen(true)\n\n return (\n <sinch-popover\n open={isOpen}\n aria-label={ariaLabel}\n orientation=\"bottom\"\n modal\n on-close={onClose}\n >\n <sinch-select-button\n slot=\"target\"\n text={value}\n placeholder={placeholder}\n aria-label={`Open ${ariaLabel}`}\n style={style}\n on-click={onOpen}\n />\n <sinch-select-menu\n slot=\"content\"\n aria-label={`${ariaLabel} menu`}\n rows={3}\n value={value}\n on-change={onSelectChange}\n search-placeholder={searchPlaceholder}\n >\n {options.map((option) => (\n <sinch-select-menu-option\n key={option}\n value={option}\n text={option}\n aria-label={option}\n />\n ))}\n </sinch-select-menu>\n </sinch-popover>\n )\n}"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "table",
|
|
3
|
+
"description": "Table composition (Table + TableHead + TableRow + TableHeadCell + TableCell + TableBody). Accepts columns and data props.",
|
|
4
|
+
"dependencies": [],
|
|
5
|
+
"files": [
|
|
6
|
+
{
|
|
7
|
+
"path": "Table/Table.tsx",
|
|
8
|
+
"content": "import type { FC, ReactNode } from 'react'\nimport '@nectary/components/table'\nimport '@nectary/components/table-head'\nimport '@nectary/components/table-row'\nimport '@nectary/components/table-head-cell'\nimport '@nectary/components/table-cell'\nimport '@nectary/components/table-body'\nimport '@nectary/components/text'\n\nexport interface TableColumn {\n key: string,\n header: string,\n align?: 'start' | 'end',\n}\n\nexport interface TableProps {\n /** Column definitions (key, header, optional align). */\n columns: TableColumn[],\n /** Row data: array of objects keyed by column keys. */\n data: Record<string, unknown>[],\n /** Optional row key for React list (default: index). */\n getRowKey?: (row: Record<string, unknown>, index: number) => string,\n}\n\nfunction formatCell(value: unknown): ReactNode {\n if (value === null || value === undefined) {\n return ''\n }\n\n if (typeof value === 'object' && typeof (value as { toString?: () => string }).toString === 'function') {\n return String((value as { toString: () => string }).toString())\n }\n\n return String(value)\n}\n\nexport const Table: FC<TableProps> = ({\n columns,\n data,\n getRowKey = (_, index) => String(index),\n}) => {\n return (\n <sinch-table>\n <sinch-table-head>\n <sinch-table-row>\n {columns.map((col) => (\n <sinch-table-head-cell\n key={col.key}\n text={col.header}\n align={col.align}\n />\n ))}\n </sinch-table-row>\n </sinch-table-head>\n\n <sinch-table-body>\n {data.map((row, index) => (\n <sinch-table-row key={getRowKey(row, index)}>\n {columns.map((col) => (\n <sinch-table-cell key={col.key} align={col.align}>\n <sinch-text type=\"s\">{formatCell(row[col.key])}</sinch-text>\n </sinch-table-cell>\n ))}\n </sinch-table-row>\n ))}\n </sinch-table-body>\n </sinch-table>\n )\n}"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
function isRegistryFile(value) {
|
|
4
|
+
return (typeof value === 'object' &&
|
|
5
|
+
value !== null &&
|
|
6
|
+
'path' in value &&
|
|
7
|
+
'content' in value &&
|
|
8
|
+
typeof value.path === 'string' &&
|
|
9
|
+
typeof value.content === 'string');
|
|
10
|
+
}
|
|
11
|
+
function parseRegistryItem(data) {
|
|
12
|
+
if (data === null || typeof data !== 'object' || !('name' in data) || !('files' in data)) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const obj = data;
|
|
16
|
+
const name = obj.name;
|
|
17
|
+
const description = obj.description;
|
|
18
|
+
const dependencies = obj.dependencies;
|
|
19
|
+
const files = obj.files;
|
|
20
|
+
if (typeof name !== 'string' || name === '') {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
if (description !== undefined && typeof description !== 'string') {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
if (!Array.isArray(dependencies)) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
if (!dependencies.every((d) => typeof d === 'string')) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
if (!Array.isArray(files)) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (!files.every(isRegistryFile)) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
name,
|
|
40
|
+
description: typeof description === 'string' ? description : '',
|
|
41
|
+
dependencies,
|
|
42
|
+
files,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function isUrl(name) {
|
|
46
|
+
return name.startsWith('http://') || name.startsWith('https://');
|
|
47
|
+
}
|
|
48
|
+
async function loadFromUrl(url) {
|
|
49
|
+
try {
|
|
50
|
+
const res = await fetch(url);
|
|
51
|
+
if (!res.ok) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
const data = await res.json();
|
|
55
|
+
return parseRegistryItem(data);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function loadFromBundle(name, cliDir) {
|
|
62
|
+
const registryDir = path.join(cliDir, 'registry');
|
|
63
|
+
const filePath = path.join(registryDir, `${name}.json`);
|
|
64
|
+
if (!fs.existsSync(filePath)) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
68
|
+
const data = JSON.parse(raw);
|
|
69
|
+
return parseRegistryItem(data);
|
|
70
|
+
}
|
|
71
|
+
export function loadRegistryItem(name, cliDir) {
|
|
72
|
+
if (isUrl(name)) {
|
|
73
|
+
return loadFromUrl(name);
|
|
74
|
+
}
|
|
75
|
+
return Promise.resolve(loadFromBundle(name, cliDir));
|
|
76
|
+
}
|
|
77
|
+
export function listRegistryItems(cliDir) {
|
|
78
|
+
const registryDir = path.join(cliDir, 'registry');
|
|
79
|
+
if (!fs.existsSync(registryDir)) {
|
|
80
|
+
return [];
|
|
81
|
+
}
|
|
82
|
+
const files = fs.readdirSync(registryDir);
|
|
83
|
+
const items = [];
|
|
84
|
+
for (const file of files) {
|
|
85
|
+
if (!file.endsWith('.json')) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const filePath = path.join(registryDir, file);
|
|
89
|
+
try {
|
|
90
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
91
|
+
const data = JSON.parse(raw);
|
|
92
|
+
if (data.name != null) {
|
|
93
|
+
items.push({
|
|
94
|
+
name: data.name,
|
|
95
|
+
description: data.description ?? '',
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
// skip invalid or unreadable files
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return items.sort((a, b) => a.name.localeCompare(b.name));
|
|
104
|
+
}
|
package/dist/validate.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
const REQUIRED_DEPS = ['react', '@nectary/components'];
|
|
4
|
+
export function readPackageJson(cwd) {
|
|
5
|
+
const pkgPath = path.join(cwd, 'package.json');
|
|
6
|
+
if (!fs.existsSync(pkgPath)) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const raw = fs.readFileSync(pkgPath, 'utf8');
|
|
11
|
+
return JSON.parse(raw);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function getDependencyNames(pkg) {
|
|
18
|
+
const names = new Set();
|
|
19
|
+
for (const key of Object.keys(pkg.dependencies ?? {})) {
|
|
20
|
+
names.add(key);
|
|
21
|
+
}
|
|
22
|
+
for (const key of Object.keys(pkg.devDependencies ?? {})) {
|
|
23
|
+
names.add(key);
|
|
24
|
+
}
|
|
25
|
+
return names;
|
|
26
|
+
}
|
|
27
|
+
export function validateProjectDependencies(cwd) {
|
|
28
|
+
const pkg = readPackageJson(cwd);
|
|
29
|
+
if (pkg == null) {
|
|
30
|
+
console.error('Error: No package.json found in this directory. Run nectary add from your project root.');
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
const names = getDependencyNames(pkg);
|
|
34
|
+
const missing = REQUIRED_DEPS.filter((dep) => !names.has(dep));
|
|
35
|
+
if (missing.length > 0) {
|
|
36
|
+
console.error(`Error: This project must have 'react' and '@nectary/components' in package.json. Missing: ${missing.join(', ')}. Add them first, then run nectary add again.`);
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
}
|
package/dist/view.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
import { Command } from 'commander';
|
|
4
|
+
import { loadRegistryItem } from './registry.js';
|
|
5
|
+
export function viewCommand() {
|
|
6
|
+
return new Command('view')
|
|
7
|
+
.description('Preview a composition (description, dependencies, files) without adding it')
|
|
8
|
+
.argument('<name>', 'Registry item name or URL to a registry item JSON')
|
|
9
|
+
.action(async (name) => {
|
|
10
|
+
const cliDir = path.dirname(fileURLToPath(import.meta.url));
|
|
11
|
+
const item = await loadRegistryItem(name, cliDir);
|
|
12
|
+
if (item == null) {
|
|
13
|
+
console.error(`Error: composition "${name}" not found.`);
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
console.log(item.name);
|
|
17
|
+
console.log(item.description);
|
|
18
|
+
console.log(item.dependencies.length === 0
|
|
19
|
+
? 'Dependencies: none'
|
|
20
|
+
: `Dependencies: ${item.dependencies.join(', ')}`);
|
|
21
|
+
console.log('Files:');
|
|
22
|
+
for (const file of item.files) {
|
|
23
|
+
console.log(` - ${file.path}`);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nectary/cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "CLI to add Nectary compositions as React components to your project",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"nectary": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"main": "dist/index.js",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc && node scripts/build-registry.mjs",
|
|
19
|
+
"build:registry": "node scripts/build-registry.mjs",
|
|
20
|
+
"prepublishOnly": "pnpm run build",
|
|
21
|
+
"test": "pnpm run build && node --test test/add.select.smoke.test.mjs"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@antfu/ni": "^0.21.0",
|
|
25
|
+
"commander": "^12.0.0",
|
|
26
|
+
"execa": "^9.0.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"react": "^18.2.0",
|
|
30
|
+
"@nectary/components": "5.18.1",
|
|
31
|
+
"@types/node": "^18.17.17",
|
|
32
|
+
"typescript": "^5.2.2"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": "^18.19.0 || >=20.5.0"
|
|
36
|
+
}
|
|
37
|
+
}
|