@abgov/nx-adsp 13.18.0-beta.5 → 13.18.0-beta.6

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/generators.json CHANGED
@@ -70,6 +70,11 @@
70
70
  "schema": "./src/generators/vue-workspace-view/schema.json",
71
71
  "description": "Generator that adds a staff-facing, paginated list view (WorkspaceTable + a --columns spec) to an existing vue-app project."
72
72
  },
73
+ "vue-admin-crud": {
74
+ "factory": "./src/generators/vue-admin-crud/vue-admin-crud",
75
+ "schema": "./src/generators/vue-admin-crud/schema.json",
76
+ "description": "Generator that adds a simple admin CRUD screen pair (WorkspaceTable list + create/update Edit view) to an existing vue-app project."
77
+ },
73
78
  "mean": {
74
79
  "factory": "./src/generators/mean/mean",
75
80
  "schema": "./src/generators/mean/schema.json",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abgov/nx-adsp",
3
- "version": "13.18.0-beta.5",
3
+ "version": "13.18.0-beta.6",
4
4
  "license": "Apache-2.0",
5
5
  "main": "src/index.js",
6
6
  "description": "Government of Alberta - Nx plugin for ADSP apps.",
@@ -0,0 +1,136 @@
1
+ <script setup lang="ts">
2
+ import { reactive, ref, computed, onMounted } from 'vue';
3
+ import { useRoute, useRouter } from 'vue-router';
4
+ import { useKeycloak } from '@dsb-norge/vue-keycloak-js';
5
+ import { GoabInput, GoabCheckbox } from '<%= goaImportPath %>';
6
+
7
+ const route = useRoute();
8
+ const router = useRouter();
9
+ const kc = useKeycloak();
10
+
11
+ const idParam = computed(() => String(route.params.id ?? ''));
12
+ const isNew = computed(() => idParam.value === 'new' || idParam.value === '');
13
+
14
+ const form = reactive({
15
+ <% fields.forEach(function (field) { -%>
16
+ <%- field.key %>: <%- field.type === 'checkbox' ? 'false' : "''" %>,
17
+ <% }); -%>
18
+ });
19
+
20
+ const errors = reactive<Record<string, string | undefined>>({});
21
+ const loading = ref(!isNew.value);
22
+ const saving = ref(false);
23
+ // Separate load vs. save errors -- a save failure must keep the form on
24
+ // screen (with an inline "Save failed" callout), not replace it with the
25
+ // top-level "Unable to load" state a load failure shows instead of the form.
26
+ const loadError = ref<string | null>(null);
27
+ const saveError = ref<string | null>(null);
28
+ const successMessage = ref<string | null>(null);
29
+
30
+ async function load() {
31
+ if (isNew.value) return;
32
+ loading.value = true;
33
+ loadError.value = null;
34
+ try {
35
+ const res = await fetch(`/api/<%= resource %>/${idParam.value}`);
36
+ if (!res.ok) throw new Error(`Failed to load (${res.status})`);
37
+ const data = await res.json();
38
+ <% fields.forEach(function (field) { -%>
39
+ if (data['<%= field.key %>'] !== undefined) form.<%= field.key %> = data['<%= field.key %>'];
40
+ <% }); -%>
41
+ } catch (e) {
42
+ loadError.value = e instanceof Error ? e.message : 'Failed to load.';
43
+ } finally {
44
+ loading.value = false;
45
+ }
46
+ }
47
+
48
+ onMounted(load);
49
+
50
+ function validate(): boolean {
51
+ let valid = true;
52
+ <% fields.forEach(function (field) { -%>
53
+ <% if (field.type !== 'checkbox' && field.required !== false) { -%>
54
+ if (!form.<%= field.key %> || !form.<%= field.key %>.trim()) {
55
+ errors.<%= field.key %> = '<%= field.label %> is required.';
56
+ valid = false;
57
+ } else {
58
+ errors.<%= field.key %> = undefined;
59
+ }
60
+ <% } -%>
61
+ <% }); -%>
62
+ return valid;
63
+ }
64
+
65
+ async function onSubmit() {
66
+ if (!validate()) return;
67
+ saving.value = true;
68
+ saveError.value = null;
69
+ try {
70
+ const res = await fetch(
71
+ isNew.value ? '/api/<%= resource %>' : `/api/<%= resource %>/${idParam.value}`,
72
+ {
73
+ method: isNew.value ? 'POST' : 'PUT',
74
+ headers: { 'Content-Type': 'application/json' },
75
+ body: JSON.stringify(form),
76
+ },
77
+ );
78
+ if (!res.ok) throw new Error(`Failed to save (${res.status})`);
79
+ successMessage.value = isNew.value ? '<%= singularLabel %> created.' : '<%= singularLabel %> saved.';
80
+ setTimeout(() => router.push('<%= route %>'), 600);
81
+ } catch (e) {
82
+ saveError.value = e instanceof Error ? e.message : 'Failed to save.';
83
+ } finally {
84
+ saving.value = false;
85
+ }
86
+ }
87
+
88
+ function goBack() {
89
+ router.push('<%= route %>');
90
+ }
91
+ </script>
92
+
93
+ <template>
94
+ <div>
95
+ <h1>{{ isNew ? 'Create <%= singularLabel %>' : 'Edit <%= singularLabel %>' }}</h1>
96
+
97
+ <goa-callout v-if="successMessage" type="success" heading="Saved">
98
+ <p>{{ successMessage }}</p>
99
+ </goa-callout>
100
+
101
+ <div v-else-if="loading" aria-label="Loading">
102
+ <goa-skeleton type="text" size="3" />
103
+ </div>
104
+
105
+ <goa-callout v-else-if="loadError" type="emergency" heading="Unable to load">
106
+ <p>{{ loadError }}</p>
107
+ </goa-callout>
108
+
109
+ <form v-else @submit.prevent="onSubmit">
110
+ <% fields.forEach(function (field) { -%>
111
+ <% if (field.type === 'checkbox') { -%>
112
+ <goa-form-item label="<%= field.label %>">
113
+ <GoabCheckbox v-model="form.<%= field.key %>" name="<%= field.key %>" text="<%= field.label %>" />
114
+ </goa-form-item>
115
+ <% } else { -%>
116
+ <goa-form-item label="<%= field.label %>"<% if (field.required !== false) { %> requirement="required"<% } %> :error="errors.<%= field.key %>">
117
+ <GoabInput v-model="form.<%= field.key %>" name="<%= field.key %>" type="text" />
118
+ </goa-form-item>
119
+ <% } -%>
120
+ <% }); -%>
121
+
122
+ <goa-callout v-if="saveError" type="emergency" heading="Save failed">
123
+ <p>{{ saveError }}</p>
124
+ </goa-callout>
125
+
126
+ <goa-spacer vspacing="l" />
127
+
128
+ <goa-button-group gap="relaxed">
129
+ <goa-button v-if="kc.authenticated" type="primary" :disabled="saving || undefined" @_click="onSubmit">
130
+ {{ isNew ? 'Create <%= singularLabel %>' : 'Save changes' }}
131
+ </goa-button>
132
+ <goa-button type="secondary" @_click="goBack">Cancel</goa-button>
133
+ </goa-button-group>
134
+ </form>
135
+ </div>
136
+ </template>
@@ -0,0 +1,76 @@
1
+ <script setup lang="ts">
2
+ import { ref, onMounted } from 'vue';
3
+ import { useKeycloak } from '@dsb-norge/vue-keycloak-js';
4
+ import { WorkspaceTable } from '<%= goaImportPath %>';
5
+
6
+ const kc = useKeycloak();
7
+
8
+ const columns = [
9
+ <% fields.forEach(function (field) { -%>
10
+ { key: '<%= field.key %>', label: '<%= field.label %>' },
11
+ <% }); -%>
12
+ ];
13
+
14
+ // The fetched rows' shape isn't known to this generator -- read fields
15
+ // defensively rather than declaring (and likely getting wrong) a fake interface.
16
+ const rows = ref<Record<string, unknown>[]>([]);
17
+ const loading = ref(true);
18
+ const error = ref<string | null>(null);
19
+
20
+ async function load() {
21
+ loading.value = true;
22
+ error.value = null;
23
+ try {
24
+ const res = await fetch('/api/<%= resource %>');
25
+ if (!res.ok) throw new Error(`Failed to load (${res.status})`);
26
+ const data = await res.json();
27
+ // Accept either a bare array or a { results } envelope.
28
+ rows.value = Array.isArray(data) ? data : (data.results ?? []);
29
+ } catch (e) {
30
+ error.value = e instanceof Error ? e.message : 'Failed to load.';
31
+ } finally {
32
+ loading.value = false;
33
+ }
34
+ }
35
+
36
+ onMounted(load);
37
+ </script>
38
+
39
+ <template>
40
+ <div>
41
+ <div class="admin-crud-topbar">
42
+ <h1><%= heading %></h1>
43
+ <router-link v-if="kc.authenticated" to="<%= route %>/new">
44
+ <goa-button type="primary">Create <%= singularLabel %></goa-button>
45
+ </router-link>
46
+ </div>
47
+
48
+ <goa-spacer vspacing="m" />
49
+
50
+ <!-- No page/item-count/per-page-count props -- an admin lookup table this
51
+ small doesn't need WorkspaceTable's pagination. -->
52
+ <WorkspaceTable :columns="columns" :rows="rows" :loading="loading" :error="error" @retry="load">
53
+ <% fields.forEach(function (field) { -%>
54
+ <% if (field.type === 'checkbox') { -%>
55
+ <template #cell-<%= field.key %>="{ row }">
56
+ <goa-badge :type="row['<%= field.key %>'] ? 'success' : 'midtone'" :content="row['<%= field.key %>'] ? 'Yes' : 'No'" />
57
+ </template>
58
+ <% } -%>
59
+ <% }); -%>
60
+ <template #actions="{ row }">
61
+ <router-link :to="`<%= route %>/${row.id}`">
62
+ <goa-button type="tertiary" size="compact">Edit</goa-button>
63
+ </router-link>
64
+ </template>
65
+ </WorkspaceTable>
66
+ </div>
67
+ </template>
68
+
69
+ <style scoped>
70
+ .admin-crud-topbar {
71
+ display: flex;
72
+ justify-content: space-between;
73
+ align-items: center;
74
+ gap: var(--goa-space-m);
75
+ }
76
+ </style>
@@ -0,0 +1,34 @@
1
+ export interface AdminCrudField {
2
+ key: string;
3
+ label: string;
4
+ type?: 'text' | 'checkbox';
5
+ required?: boolean;
6
+ }
7
+
8
+ export interface Schema {
9
+ project: string;
10
+ name: string;
11
+ resource: string;
12
+ route: string;
13
+ /**
14
+ * JSON string on the real CLI (Nx's array-typed CLI coercion only supports
15
+ * comma-separated primitives, not JSON). A real array is also accepted for
16
+ * programmatic callers (e.g. tests).
17
+ */
18
+ fields: string | AdminCrudField[];
19
+ heading?: string;
20
+ singularLabel?: string;
21
+ requiresAuth?: boolean;
22
+ }
23
+
24
+ export interface NormalizedSchema extends Omit<Schema, 'fields'> {
25
+ projectRoot: string;
26
+ /** PascalCase view name with a "ListView" suffix, e.g. RegionsListView. */
27
+ listViewFileName: string;
28
+ /** PascalCase view name with an "EditView" suffix, e.g. RegionsEditView. */
29
+ editViewFileName: string;
30
+ fields: AdminCrudField[];
31
+ heading: string;
32
+ singularLabel: string;
33
+ requiresAuth: boolean;
34
+ }
@@ -0,0 +1,54 @@
1
+ {
2
+ "$schema": "http://json-schema.org/schema",
3
+ "id": "NxAdspVueAdminCrud",
4
+ "title": "Vue Admin CRUD (List + Edit)",
5
+ "description": "Generates a simple admin CRUD screen pair (a WorkspaceTable list view with a Create action and per-row Edit, plus a create/update Edit view) into an existing vue-app project. Suited to small lookup-table style admin screens, not large paginated workspaces -- see vue-workspace-view for that.",
6
+ "type": "object",
7
+ "properties": {
8
+ "project": {
9
+ "type": "string",
10
+ "description": "The vue-app project to add the views to.",
11
+ "$default": {
12
+ "$source": "argv",
13
+ "index": 0
14
+ },
15
+ "x-prompt": "Which project should the admin CRUD screens be added to?"
16
+ },
17
+ "name": {
18
+ "type": "string",
19
+ "description": "View name, e.g. 'regions' generates src/views/RegionsListView.vue and src/views/RegionsEditView.vue.",
20
+ "$default": {
21
+ "$source": "argv",
22
+ "index": 1
23
+ },
24
+ "x-prompt": "What should the views be called?"
25
+ },
26
+ "resource": {
27
+ "type": "string",
28
+ "description": "API resource path segment -- fetches /api/<resource> (list), /api/<resource>/:id (load one), POST /api/<resource> (create), PUT /api/<resource>/:id (update)."
29
+ },
30
+ "route": {
31
+ "type": "string",
32
+ "description": "List route path added to router/index.ts, e.g. /regions. The edit/create route is added as `${route}/:id` (visiting `${route}/new` creates)."
33
+ },
34
+ "fields": {
35
+ "type": "string",
36
+ "description": "JSON array of fields, in display/form order -- e.g. '[{\"key\":\"name\",\"label\":\"Name\"},{\"key\":\"active\",\"label\":\"Active\",\"type\":\"checkbox\"}]'. Each item: { key, label, type?: \"text\"|\"checkbox\" (default \"text\"), required?: boolean (default true for \"text\", ignored for \"checkbox\") }. A plain array is also accepted when this generator is invoked programmatically."
37
+ },
38
+ "heading": {
39
+ "type": "string",
40
+ "description": "List page heading. Defaults to the view name, title-cased."
41
+ },
42
+ "singularLabel": {
43
+ "type": "string",
44
+ "description": "Singular label used in \"Create <label>\"/\"Edit <label>\" headings and buttons. Defaults to --heading (override for irregular plurals, e.g. --heading=Regions --singularLabel=Region)."
45
+ },
46
+ "requiresAuth": {
47
+ "type": "boolean",
48
+ "description": "Whether the generated routes require authentication.",
49
+ "default": true
50
+ }
51
+ },
52
+ "required": ["project", "name", "resource", "route", "fields"],
53
+ "additionalProperties": false
54
+ }
@@ -0,0 +1,3 @@
1
+ import { Tree } from '@nx/devkit';
2
+ import { Schema } from './schema';
3
+ export default function (host: Tree, options: Schema): Promise<void>;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = default_1;
4
+ const tslib_1 = require("tslib");
5
+ const devkit_1 = require("@nx/devkit");
6
+ const path = require("path");
7
+ const vue_router_1 = require("../../utils/vue-router");
8
+ const vue_components_1 = require("../vue-components/vue-components");
9
+ // Nx's own CLI option coercion (coerceTypesInOptions in nx/src/utils/params)
10
+ // only knows how to split an array-typed option on commas -- it has no JSON
11
+ // support, so a real `"type": "array"` schema for --fields silently mangles a
12
+ // JSON array into garbage fragments when invoked from the actual CLI (only
13
+ // programmatic callers, like this generator's own unit tests, ever pass a
14
+ // real array). --fields is `"type": "string"` in schema.json specifically so
15
+ // Nx leaves it alone, and this generator parses the JSON itself.
16
+ function parseFields(fields) {
17
+ const parsed = typeof fields === 'string' ? JSON.parse(fields) : fields;
18
+ if (!Array.isArray(parsed) || parsed.length === 0) {
19
+ throw new Error('--fields must be a non-empty JSON array of { key, label, type?, required? } objects.');
20
+ }
21
+ return parsed;
22
+ }
23
+ function normalizeOptions(host, options) {
24
+ var _a, _b, _c;
25
+ const { root: projectRoot } = (0, devkit_1.readProjectConfiguration)(host, options.project);
26
+ const className = (0, devkit_1.names)(options.name).className;
27
+ // className is PascalCase (e.g. "Regions") -- space it out for a readable
28
+ // default heading ("Regions").
29
+ const heading = (_a = options.heading) !== null && _a !== void 0 ? _a : className.replace(/([A-Z])/g, ' $1').trim();
30
+ return Object.assign(Object.assign({}, options), { projectRoot, fields: parseFields(options.fields), listViewFileName: `${className}ListView`, editViewFileName: `${className}EditView`, heading, singularLabel: (_b = options.singularLabel) !== null && _b !== void 0 ? _b : heading, requiresAuth: (_c = options.requiresAuth) !== null && _c !== void 0 ? _c : true });
31
+ }
32
+ function default_1(host, options) {
33
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
34
+ const normalizedOptions = normalizeOptions(host, options);
35
+ // Idempotent -- ensures WorkspaceTable exists even in a project scaffolded
36
+ // before vue-components carried it.
37
+ yield (0, vue_components_1.default)(host);
38
+ (0, devkit_1.generateFiles)(host, path.join(__dirname, 'files'), normalizedOptions.projectRoot, Object.assign(Object.assign({}, normalizedOptions), { goaImportPath: (0, vue_components_1.vueComponentsImportPath)(host), tmpl: '' }));
39
+ // The edit route also serves create: visiting `${route}/new` matches the
40
+ // same `:id` param (id === 'new'), same convention the real reference
41
+ // implementation this was modeled on uses -- no separate create route.
42
+ (0, vue_router_1.insertVueRoute)(host, normalizedOptions.projectRoot, normalizedOptions.project, {
43
+ path: `${normalizedOptions.route}/:id`,
44
+ componentImportPath: `../views/${normalizedOptions.editViewFileName}.vue`,
45
+ requiresAuth: normalizedOptions.requiresAuth,
46
+ });
47
+ (0, vue_router_1.insertVueRoute)(host, normalizedOptions.projectRoot, normalizedOptions.project, {
48
+ path: normalizedOptions.route,
49
+ componentImportPath: `../views/${normalizedOptions.listViewFileName}.vue`,
50
+ requiresAuth: normalizedOptions.requiresAuth,
51
+ });
52
+ yield (0, devkit_1.formatFiles)(host);
53
+ });
54
+ }
55
+ //# sourceMappingURL=vue-admin-crud.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vue-admin-crud.js","sourceRoot":"","sources":["../../../../../../packages/nx-adsp/src/generators/vue-admin-crud/vue-admin-crud.ts"],"names":[],"mappings":";;AAkDA,4BAiCC;;AAnFD,uCAMoB;AACpB,6BAA6B;AAC7B,uDAAwD;AACxD,qEAE0C;AAG1C,6EAA6E;AAC7E,4EAA4E;AAC5E,8EAA8E;AAC9E,2EAA2E;AAC3E,0EAA0E;AAC1E,6EAA6E;AAC7E,iEAAiE;AACjE,SAAS,WAAW,CAAC,MAAwB;IAC3C,MAAM,MAAM,GAAG,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IACxE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAClD,MAAM,IAAI,KAAK,CACb,sFAAsF,CACvF,CAAC;IACJ,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,gBAAgB,CAAC,IAAU,EAAE,OAAe;;IACnD,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,GAAG,IAAA,iCAAwB,EAAC,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAE9E,MAAM,SAAS,GAAG,IAAA,cAAK,EAAC,OAAO,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC;IAChD,0EAA0E;IAC1E,+BAA+B;IAC/B,MAAM,OAAO,GAAG,MAAA,OAAO,CAAC,OAAO,mCAAI,SAAS,CAAC,OAAO,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC;IAC/E,uCACK,OAAO,KACV,WAAW,EACX,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,EACnC,gBAAgB,EAAE,GAAG,SAAS,UAAU,EACxC,gBAAgB,EAAE,GAAG,SAAS,UAAU,EACxC,OAAO,EACP,aAAa,EAAE,MAAA,OAAO,CAAC,aAAa,mCAAI,OAAO,EAC/C,YAAY,EAAE,MAAA,OAAO,CAAC,YAAY,mCAAI,IAAI,IAC1C;AACJ,CAAC;AAED,mBAA+B,IAAU,EAAE,OAAe;;QACxD,MAAM,iBAAiB,GAAG,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAE1D,2EAA2E;QAC3E,oCAAoC;QACpC,MAAM,IAAA,wBAAsB,EAAC,IAAI,CAAC,CAAC;QAEnC,IAAA,sBAAa,EACX,IAAI,EACJ,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,EAC7B,iBAAiB,CAAC,WAAW,kCAExB,iBAAiB,KACpB,aAAa,EAAE,IAAA,wCAAuB,EAAC,IAAI,CAAC,EAC5C,IAAI,EAAE,EAAE,IAEX,CAAC;QAEF,yEAAyE;QACzE,sEAAsE;QACtE,uEAAuE;QACvE,IAAA,2BAAc,EAAC,IAAI,EAAE,iBAAiB,CAAC,WAAW,EAAE,iBAAiB,CAAC,OAAO,EAAE;YAC7E,IAAI,EAAE,GAAG,iBAAiB,CAAC,KAAK,MAAM;YACtC,mBAAmB,EAAE,YAAY,iBAAiB,CAAC,gBAAgB,MAAM;YACzE,YAAY,EAAE,iBAAiB,CAAC,YAAY;SAC7C,CAAC,CAAC;QACH,IAAA,2BAAc,EAAC,IAAI,EAAE,iBAAiB,CAAC,WAAW,EAAE,iBAAiB,CAAC,OAAO,EAAE;YAC7E,IAAI,EAAE,iBAAiB,CAAC,KAAK;YAC7B,mBAAmB,EAAE,YAAY,iBAAiB,CAAC,gBAAgB,MAAM;YACzE,YAAY,EAAE,iBAAiB,CAAC,YAAY;SAC7C,CAAC,CAAC;QAEH,MAAM,IAAA,oBAAW,EAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;CAAA"}
@@ -0,0 +1,186 @@
1
+ import {
2
+ addProjectConfiguration,
3
+ readProjectConfiguration,
4
+ Tree,
5
+ } from '@nx/devkit';
6
+ import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
7
+ import generator from './vue-admin-crud';
8
+ import { Schema } from './schema';
9
+
10
+ // Mirrors the shape vue-app's own template generates -- vue-admin-crud retrofits
11
+ // into this file, so the fixture must match what it actually looks for.
12
+ const ROUTER_FIXTURE = `import { createRouter, createWebHistory } from 'vue-router';
13
+ import HomeView from '../views/HomeView.vue';
14
+
15
+ const router = createRouter({
16
+ history: createWebHistory(import.meta.env.BASE_URL),
17
+ routes: [
18
+ { path: '/', component: HomeView },
19
+ ],
20
+ });
21
+
22
+ export default router;
23
+ `;
24
+
25
+ describe('Vue Admin CRUD Generator', () => {
26
+ let host: Tree;
27
+ const baseOptions: Schema = {
28
+ project: 'test',
29
+ name: 'regions',
30
+ resource: 'regions',
31
+ route: '/regions',
32
+ fields: [
33
+ { key: 'name', label: 'Name' },
34
+ { key: 'active', label: 'Active', type: 'checkbox' },
35
+ ],
36
+ };
37
+
38
+ beforeEach(() => {
39
+ host = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
40
+ addProjectConfiguration(host, 'test', { root: 'apps/test' });
41
+ host.write('apps/test/src/router/index.ts', ROUTER_FIXTURE);
42
+ });
43
+
44
+ it('throws when --project does not exist', async () => {
45
+ await expect(
46
+ generator(host, { ...baseOptions, project: 'no-such-app' }),
47
+ ).rejects.toThrow();
48
+ });
49
+
50
+ it("throws a clear error when the project isn't a vue-app (no router/index.ts)", async () => {
51
+ addProjectConfiguration(host, 'not-vue', { root: 'apps/not-vue' });
52
+ await expect(
53
+ generator(host, { ...baseOptions, project: 'not-vue' }),
54
+ ).rejects.toThrow(/router\/index\.ts/);
55
+ });
56
+
57
+ it('generates the list view with a Create action and per-row Edit, no pagination props', async () => {
58
+ await generator(host, baseOptions);
59
+
60
+ const view = host
61
+ .read('apps/test/src/views/RegionsListView.vue')
62
+ .toString();
63
+ expect(view).toContain('<h1>Regions</h1>');
64
+ expect(view).toContain("fetch('/api/regions')");
65
+ expect(view).toContain("import { WorkspaceTable } from '@proj/vue-components';");
66
+ expect(view).toContain('<WorkspaceTable');
67
+ // No pagination props bound -- this is the "reused without its pagination/
68
+ // filter props" case, unlike vue-workspace-view (the WorkspaceTable tag
69
+ // itself contains a comment explaining why, hence checking for the bound
70
+ // attribute specifically rather than the bare word).
71
+ expect(view).not.toContain(':item-count=');
72
+ expect(view).not.toContain(':per-page-count=');
73
+ expect(view).toContain('to="/regions/new"');
74
+ expect(view).toContain('Create Regions');
75
+ expect(view).toContain(':to="`/regions/${row.id}`"');
76
+ // Checkbox field renders as a Yes/No badge in the list.
77
+ expect(view).toContain("#cell-active=\"{ row }\"");
78
+ expect(view).toContain("row['active'] ? 'Yes' : 'No'");
79
+ }, 30000);
80
+
81
+ it('generates the edit view with create/update, field-level validation, and success + redirect', async () => {
82
+ await generator(host, baseOptions);
83
+
84
+ const view = host
85
+ .read('apps/test/src/views/RegionsEditView.vue')
86
+ .toString();
87
+ expect(view).toContain("idParam.value === 'new' || idParam.value === ''");
88
+ expect(view).toContain("import { GoabInput, GoabCheckbox } from '@proj/vue-components';");
89
+ expect(view).toContain("name.trim()");
90
+ expect(view).toContain("errors.name = 'Name is required.';");
91
+ // Checkbox has no required-validation block.
92
+ expect(view).not.toContain('errors.active');
93
+ expect(view).toContain("method: isNew.value ? 'POST' : 'PUT'");
94
+ expect(view).toContain("isNew.value ? '/api/regions'");
95
+ expect(view).toContain('fetch(`/api/regions/${idParam.value}`');
96
+ expect(view).toContain("router.push('/regions')");
97
+ expect(view).toContain('Create Regions');
98
+ expect(view).toContain('Edit Regions');
99
+ }, 30000);
100
+
101
+ it('uses --singularLabel over --heading for Create/Edit headings when both are set', async () => {
102
+ await generator(host, {
103
+ ...baseOptions,
104
+ heading: 'Regions',
105
+ singularLabel: 'Region',
106
+ });
107
+ const list = host.read('apps/test/src/views/RegionsListView.vue').toString();
108
+ const edit = host.read('apps/test/src/views/RegionsEditView.vue').toString();
109
+ expect(list).toContain('<h1>Regions</h1>');
110
+ expect(list).toContain('Create Region');
111
+ expect(edit).toContain('Create Region');
112
+ expect(edit).toContain('Edit Region');
113
+ }, 30000);
114
+
115
+ it('marks a field as not required with --fields[].required=false', async () => {
116
+ await generator(host, {
117
+ ...baseOptions,
118
+ fields: [{ key: 'name', label: 'Name', required: false }],
119
+ });
120
+ const edit = host.read('apps/test/src/views/RegionsEditView.vue').toString();
121
+ expect(edit).not.toContain("errors.name = 'Name is required.'");
122
+ expect(edit).not.toContain('requirement="required"');
123
+ }, 30000);
124
+
125
+ it('accepts --fields as a JSON string, the form the real CLI produces', async () => {
126
+ await generator(host, {
127
+ ...baseOptions,
128
+ fields: JSON.stringify(baseOptions.fields),
129
+ });
130
+ const view = host
131
+ .read('apps/test/src/views/RegionsListView.vue')
132
+ .toString();
133
+ expect(view).toContain("{ key: 'name', label: 'Name' }");
134
+ }, 30000);
135
+
136
+ it('throws a clear error when --fields is not valid JSON', async () => {
137
+ await expect(
138
+ generator(host, { ...baseOptions, fields: '{not json' }),
139
+ ).rejects.toThrow();
140
+ });
141
+
142
+ it('throws a clear error when --fields parses to an empty array', async () => {
143
+ await expect(
144
+ generator(host, { ...baseOptions, fields: '[]' }),
145
+ ).rejects.toThrow(/non-empty/);
146
+ });
147
+
148
+ it('inserts both the list and edit routes, requiring auth by default', async () => {
149
+ await generator(host, baseOptions);
150
+
151
+ const routerTs = host.read('apps/test/src/router/index.ts').toString();
152
+ expect(routerTs).toContain("path: '/regions'");
153
+ expect(routerTs).toContain("path: '/regions/:id'");
154
+ expect(routerTs).toContain(
155
+ "component: () => import('../views/RegionsListView.vue')",
156
+ );
157
+ expect(routerTs).toContain(
158
+ "component: () => import('../views/RegionsEditView.vue')",
159
+ );
160
+ expect(
161
+ routerTs.split('meta: { requiresAuth: true }').length - 1,
162
+ ).toBe(2);
163
+ // The existing route is untouched, not replaced.
164
+ expect(routerTs).toContain("{ path: '/', component: HomeView }");
165
+ }, 30000);
166
+
167
+ it('omits the requiresAuth meta when --requiresAuth=false', async () => {
168
+ await generator(host, { ...baseOptions, requiresAuth: false });
169
+ const routerTs = host.read('apps/test/src/router/index.ts').toString();
170
+ expect(routerTs).not.toContain('requiresAuth');
171
+ }, 30000);
172
+
173
+ it('ensures the shared WorkspaceTable pattern component exists', async () => {
174
+ await generator(host, baseOptions);
175
+ expect(
176
+ host.exists('libs/vue-components/src/lib/patterns/WorkspaceTable.vue'),
177
+ ).toBeTruthy();
178
+ }, 30000);
179
+
180
+ it('does not touch the target project configuration', async () => {
181
+ const before = readProjectConfiguration(host, 'test');
182
+ await generator(host, baseOptions);
183
+ const after = readProjectConfiguration(host, 'test');
184
+ expect(after).toEqual(before);
185
+ }, 30000);
186
+ });