@navette-js/cli 1.0.0-beta.3 → 1.0.0-beta.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @navette-js/cli
2
2
 
3
- Command-line tools for Navette projects: migrations, users, and generators for resources, pages and widgets (npx navette).
3
+ Command-line tools for Navette projects: migrations, users, and generators for resources, pages, widgets and form fields (npx navette).
4
4
 
5
5
  Part of [Navette](https://github.com/NavetteJS/navette), admin panels for Node.js. Start a project with `npm create navette@latest`, or install the framework with `npm install @navette-js/panels`.
6
6
 
package/lib/cli.js CHANGED
@@ -7,14 +7,14 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
7
7
  }
8
8
  return path;
9
9
  };
10
- // Project CLI (`npx navette …`): migrations, users, and generators for resources, pages and widgets.
10
+ // Project CLI (`npx navette …`): migrations, users, and generators for resources, pages, widgets and form fields.
11
11
  // Run it at the root of a project created with `npm create navette`.
12
12
  import { existsSync, mkdirSync, readdirSync, writeFileSync } from 'node:fs';
13
13
  import { randomBytes } from 'node:crypto';
14
14
  import { join, relative } from 'node:path';
15
15
  import { createInterface } from 'node:readline/promises';
16
16
  import { pathToFileURL } from 'node:url';
17
- import { generateResource, generateMigration, generatePage, generateWidget, studly, tableName } from "./generators.js";
17
+ import { generateField, generateResource, generateMigration, generatePage, generateWidget, studly, tableName } from "./generators.js";
18
18
  const cwd = process.cwd();
19
19
  if (existsSync(join(cwd, '.env')))
20
20
  process.loadEnvFile(join(cwd, '.env'));
@@ -132,6 +132,13 @@ const commands = {
132
132
  write(join(cwd, 'src', panelOf(), 'pages', `${name}.ts`), generatePage(name));
133
133
  },
134
134
  },
135
+ 'make:field': {
136
+ usage: 'navette make:field <Name> Create a custom form field (src/fields)',
137
+ async run() {
138
+ const name = studly(positional[0] ?? fail('Give the field a name, e.g. RatingInput'));
139
+ write(join(cwd, 'src', 'fields', `${name}.ts`), generateField(name));
140
+ },
141
+ },
135
142
  'make:widget': {
136
143
  usage: 'navette make:widget <Name> [--panel=admin] [--type=stats|chart|table] Create a dashboard widget',
137
144
  async run() {
@@ -15,3 +15,5 @@ export declare function generateResource(model: string, table: string, columns:
15
15
  export declare function generateMigration(name: string): string;
16
16
  export declare function generatePage(name: string): string;
17
17
  export declare function generateWidget(name: string, type: 'stats' | 'chart' | 'table'): string;
18
+ /** A custom form field: an Alpine.js control entangled with the field's state, validated on the server. */
19
+ export declare function generateField(name: string): string;
package/lib/generators.js CHANGED
@@ -175,3 +175,39 @@ export class ${name} extends StatsOverviewWidget {
175
175
  }
176
176
  `;
177
177
  }
178
+ /** A custom form field: an Alpine.js control entangled with the field's state, validated on the server. */
179
+ export function generateField(name) {
180
+ return `import { Field, type Rule } from '@navette-js/forms';
181
+ import { configured, e, js } from '@navette-js/support';
182
+
183
+ /**
184
+ * ${headline(snake(name))} field. Use it like any field: \`${name}.make('name').required()\`.
185
+ * The state comes from the browser: check its shape in getValidationRules() before it is saved.
186
+ */
187
+ export class ${name} extends Field {
188
+ static make(name: string): ${name} {
189
+ return configured(new ${name}(name));
190
+ }
191
+
192
+ async getValidationRules(): Promise<Rule[]> {
193
+ return [
194
+ ...(await super.getValidationRules()),
195
+ { name: 'string', passes: (value) => typeof value === 'string', message: (attribute) => \`The \${attribute} field must be a string.\` },
196
+ ];
197
+ }
198
+
199
+ async toHtml(): Promise<string> {
200
+ const id = this.getId();
201
+ const disabled = await this.isDisabled();
202
+
203
+ return this.wrap(
204
+ \`<div class="fi-input-wrp\${this.hasError() ? ' fi-invalid' : ''}" x-data="{ state: $wire.$entangle(\${e(js(this.getStatePath()))}) }">
205
+ <div class="fi-input-wrp-content-ctn">
206
+ <input id="\${e(id)}" type="text" class="fi-input" x-model="state"\${disabled ? ' disabled' : ''}\${this.hasError() ? \` aria-invalid="true" aria-describedby="\${e(id)}-error"\` : ''} />
207
+ </div>
208
+ </div>\`,
209
+ );
210
+ }
211
+ }
212
+ `;
213
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@navette-js/cli",
3
- "version": "1.0.0-beta.3",
4
- "description": "Command-line tools for Navette projects: migrations, users, and generators for resources, pages and widgets (npx navette).",
3
+ "version": "1.0.0-beta.4",
4
+ "description": "Command-line tools for Navette projects: migrations, users, and generators for resources, pages, widgets and form fields (npx navette).",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "exports": {
@@ -42,8 +42,8 @@
42
42
  "scaffold"
43
43
  ],
44
44
  "dependencies": {
45
- "@navette-js/panels": "1.0.0-beta.3",
46
- "@navette-js/database": "1.0.0-beta.3"
45
+ "@navette-js/panels": "1.0.0-beta.4",
46
+ "@navette-js/database": "1.0.0-beta.4"
47
47
  },
48
48
  "publishConfig": {
49
49
  "access": "public",