@aerogel/cli 0.0.0-next.47ed8ee3c048720794026e45140e9b700cb428b9 → 0.0.0-next.5953e1862a7c89a8fc80da087467d67d4f4e8c73

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.
Files changed (58) hide show
  1. package/dist/aerogel-cli.cjs.js +1 -1
  2. package/dist/aerogel-cli.cjs.js.map +1 -1
  3. package/dist/aerogel-cli.esm.js +1 -1
  4. package/dist/aerogel-cli.esm.js.map +1 -1
  5. package/package.json +11 -4
  6. package/src/cli.ts +4 -0
  7. package/src/commands/create.test.ts +22 -5
  8. package/src/commands/create.ts +30 -12
  9. package/src/commands/generate-component.test.ts +40 -3
  10. package/src/commands/generate-component.ts +143 -20
  11. package/src/commands/generate-model.test.ts +7 -4
  12. package/src/commands/generate-model.ts +32 -15
  13. package/src/commands/generate-service.test.ts +21 -0
  14. package/src/commands/generate-service.ts +151 -0
  15. package/src/commands/install.test.ts +41 -0
  16. package/src/commands/install.ts +33 -0
  17. package/src/lib/App.ts +65 -3
  18. package/src/lib/Editor.ts +58 -0
  19. package/src/lib/File.ts +6 -0
  20. package/src/lib/Log.mock.ts +13 -4
  21. package/src/lib/Log.test.ts +19 -3
  22. package/src/lib/Log.ts +36 -20
  23. package/src/lib/Shell.mock.ts +1 -1
  24. package/src/lib/Template.ts +5 -1
  25. package/src/lib/utils/app.ts +15 -0
  26. package/src/lib/utils/edit.ts +44 -0
  27. package/src/lib/utils/paths.ts +34 -0
  28. package/src/plugins/Histoire.ts +93 -0
  29. package/src/plugins/Plugin.ts +178 -0
  30. package/src/plugins/Solid.ts +78 -0
  31. package/src/plugins/Soukai.ts +19 -0
  32. package/src/testing/setup.ts +38 -6
  33. package/templates/app/.github/workflows/ci.yml +14 -2
  34. package/templates/app/.vscode/launch.json +16 -0
  35. package/templates/app/.vscode/settings.json +10 -0
  36. package/templates/app/README.md +3 -0
  37. package/templates/app/cypress/cypress.config.ts +16 -0
  38. package/templates/app/index.html +4 -3
  39. package/templates/app/package.json +34 -12
  40. package/templates/app/src/App.vue +5 -3
  41. package/templates/app/src/assets/public/robots.txt +2 -0
  42. package/templates/app/src/main.ts +6 -2
  43. package/templates/app/src/types/globals.d.ts +0 -1
  44. package/templates/app/tailwind.config.js +1 -1
  45. package/templates/app/tsconfig.json +1 -0
  46. package/templates/app/vite.config.ts +14 -6
  47. package/templates/component-input/[component.name].vue +16 -0
  48. package/templates/component-input-story/[component.name].story.vue +63 -0
  49. package/templates/histoire/histoire.config.ts +7 -0
  50. package/templates/histoire/patches/histoire+0.17.6.patch +13 -0
  51. package/templates/histoire/src/main.histoire.ts +8 -0
  52. package/templates/service/[service.name].ts +8 -0
  53. package/.eslintrc.js +0 -7
  54. package/noeldemartin.config.js +0 -4
  55. package/src/lib/utils.test.ts +0 -33
  56. package/src/lib/utils.ts +0 -44
  57. package/templates/app/cypress.config.ts +0 -8
  58. /package/templates/app/src/assets/{styles.css → css/styles.css} +0 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aerogel/cli",
3
3
  "description": "Aerogel CLI",
4
- "version": "0.0.0-next.47ed8ee3c048720794026e45140e9b700cb428b9",
4
+ "version": "0.0.0-next.5953e1862a7c89a8fc80da087467d67d4f4e8c73",
5
5
  "main": "dist/aerogel-cli.cjs.js",
6
6
  "module": "dist/aerogel-cli.esm.js",
7
7
  "types": "dist/aerogel-cli.d.ts",
@@ -15,7 +15,8 @@
15
15
  "build:types": "noeldemartin-build-types",
16
16
  "lint": "noeldemartin-lint src",
17
17
  "publish-next": "noeldemartin-publish-next",
18
- "test": "vitest --run"
18
+ "test": "vitest --run",
19
+ "test:ci": "vitest --run --reporter verbose"
19
20
  },
20
21
  "engines": {
21
22
  "node": ">=18.x"
@@ -35,12 +36,18 @@
35
36
  "url": "https://github.com/NoelDeMartin/aerogeljs/issues"
36
37
  },
37
38
  "dependencies": {
38
- "@noeldemartin/utils": "0.4.0-next.ac00beaecf32bb02ed8e335225d7948d946d73bd",
39
+ "@noeldemartin/utils": "0.5.0-next.dce7f1ca99862fde01601bb39d71d0eaaed3abe5",
39
40
  "chalk": "^4.1.2",
40
41
  "commander": "^11.0.0",
41
- "mustache": "^4.2.0"
42
+ "mustache": "^4.2.0",
43
+ "ts-morph": "^20.0.0"
42
44
  },
43
45
  "devDependencies": {
44
46
  "@types/node": "^20.4.0"
47
+ },
48
+ "eslintConfig": {
49
+ "extends": [
50
+ "@noeldemartin/eslint-config-typescript"
51
+ ]
45
52
  }
46
53
  }
package/src/cli.ts CHANGED
@@ -3,6 +3,8 @@ import { CreateCommand } from '@/commands/create';
3
3
  import { facade } from '@noeldemartin/utils';
4
4
  import { GenerateComponentCommand } from '@/commands/generate-component';
5
5
  import { GenerateModelCommand } from '@/commands/generate-model';
6
+ import { GenerateServiceCommand } from '@/commands/generate-service';
7
+ import { InstallCommand } from '@/commands/install';
6
8
 
7
9
  export class CLIService {
8
10
 
@@ -14,6 +16,8 @@ export class CLIService {
14
16
  CreateCommand.define(program);
15
17
  GenerateComponentCommand.define(program);
16
18
  GenerateModelCommand.define(program);
19
+ GenerateServiceCommand.define(program);
20
+ InstallCommand.define(program);
17
21
 
18
22
  program.parse(argv);
19
23
  }
@@ -7,20 +7,37 @@ import { CreateCommand } from './create';
7
7
 
8
8
  describe('Create command', () => {
9
9
 
10
- it('works', async () => {
10
+ it('creates apps', async () => {
11
11
  // Act
12
12
  await CreateCommand.run('./app', { name: 'My App' });
13
13
 
14
14
  // Assert
15
15
  FileMock.expectCreated('./app/.gitignore').toContain('node_modules');
16
16
  FileMock.expectCreated('./app/package.json').toContain('"name": "my-app"');
17
- FileMock.expectCreated('./app/index.html').toContain('My App');
18
- FileMock.expectCreated('./app/src/App.vue').toContain(
19
- '<h1 class="text-4xl font-semibold">{{ $t(\'home.title\') }}</h1>',
20
- );
17
+ FileMock.expectCreated('./app/package.json').toContain('"@aerogel/core": "next"');
18
+ FileMock.expectCreated('./app/index.html').toContain('{{ app.name }}');
19
+ FileMock.expectCreated('./app/README.md').toContain('My App');
20
+ FileMock.expectCreated('./app/src/App.vue').toContain('<h1 class="text-4xl font-semibold">');
21
+ FileMock.expectCreated('./app/src/App.vue').toContain('{{ $t(\'home.title\') }}');
21
22
 
22
23
  ShellMock.expectRan('npm install');
23
24
  ShellMock.expectRan('git init');
24
25
  });
25
26
 
27
+ it('creates apps for local core development', async () => {
28
+ // Act
29
+ await CreateCommand.run('./app', { name: 'My App', local: true });
30
+
31
+ // Assert
32
+ FileMock.expectCreated('./app/package.json').toMatch(/"@aerogel\/core": "file:[^"]+\/packages\/core"/);
33
+ });
34
+
35
+ it('infers app name from path', async () => {
36
+ // Act
37
+ await CreateCommand.run('./my-app');
38
+
39
+ // Assert
40
+ FileMock.expectCreated('./my-app/package.json').toContain('"name": "my-app"');
41
+ });
42
+
26
43
  });
@@ -1,10 +1,16 @@
1
+ import { basename } from 'path';
2
+ import { stringToTitleCase } from '@noeldemartin/utils';
3
+
1
4
  import App from '@/lib/App';
2
5
  import Command from '@/commands/Command';
3
6
  import Log from '@/lib/Log';
4
7
  import Shell from '@/lib/Shell';
8
+ import type { CommandOptions } from '@/commands/Command';
5
9
 
6
10
  export interface Options {
7
11
  name?: string;
12
+ local?: boolean;
13
+ copy?: boolean;
8
14
  }
9
15
 
10
16
  export class CreateCommand extends Command {
@@ -12,14 +18,22 @@ export class CreateCommand extends Command {
12
18
  public static command: string = 'create';
13
19
  public static description: string = 'Create AerogelJS app';
14
20
  public static parameters: [string, string][] = [['path', 'Application path']];
15
- public static options: Record<string, string> = {
21
+ public static options: CommandOptions = {
16
22
  name: 'Application name',
23
+ local: {
24
+ type: 'boolean',
25
+ description: 'Whether to create an app using local Aerogel packages (used for core development)',
26
+ },
27
+ copy: {
28
+ type: 'boolean',
29
+ description: 'Whether to create an app linked to local Aerogel packages (used in CI)',
30
+ },
17
31
  };
18
32
 
19
33
  private path: string;
20
34
  private options: Options;
21
35
 
22
- constructor(path: string, options: Options) {
36
+ constructor(path: string, options: Options = {}) {
23
37
  super();
24
38
 
25
39
  this.path = path;
@@ -28,7 +42,7 @@ export class CreateCommand extends Command {
28
42
 
29
43
  public async run(): Promise<void> {
30
44
  const path = this.path;
31
- const name = this.options.name ?? 'Aerogel App';
45
+ const name = this.options.name ?? stringToTitleCase(basename(path));
32
46
 
33
47
  Shell.setWorkingDirectory(path);
34
48
 
@@ -36,20 +50,24 @@ export class CreateCommand extends Command {
36
50
  await this.installDependencies();
37
51
  await this.initializeGit();
38
52
 
39
- Log.success([
40
- '',
41
- `That's it! You can start working on **${name}** doing the following:`,
42
- ` cd ${path}`,
43
- ' npm run dev',
44
- '',
45
- 'Have fun!',
46
- ]);
53
+ Log.success(`
54
+
55
+ That's it! You can start working on **${name}** doing the following:
56
+
57
+ cd ${path}
58
+ npm run dev
59
+
60
+ Have fun!
61
+ `);
47
62
  }
48
63
 
49
64
  protected async createApp(name: string, path: string): Promise<void> {
50
65
  Log.info(`Creating **${name}**...`);
51
66
 
52
- new App(name).create(path);
67
+ new App(name, {
68
+ local: this.options.local,
69
+ linkedLocal: this.options.local && !this.options.copy,
70
+ }).create(path);
53
71
  }
54
72
 
55
73
  protected async installDependencies(): Promise<void> {
@@ -4,7 +4,7 @@ import FileMock from '@/lib/File.mock';
4
4
 
5
5
  import { GenerateComponentCommand } from './generate-component';
6
6
 
7
- describe('Generate component command', () => {
7
+ describe('Generate Component command', () => {
8
8
 
9
9
  it('generates components', async () => {
10
10
  // Arrange
@@ -17,10 +17,26 @@ describe('Generate component command', () => {
17
17
  FileMock.expectCreated('src/components/FooBar.vue').toContain('<div>FooBar</div>');
18
18
  });
19
19
 
20
- it('generates components with stories', async () => {
20
+ it('generates components in subfolders', async () => {
21
21
  // Arrange
22
22
  FileMock.stub('package.json', '@aerogel/core');
23
- FileMock.stub('src/main.histoire.ts');
23
+
24
+ // Act
25
+ await GenerateComponentCommand.run('module/FooBar');
26
+
27
+ // Assert
28
+ FileMock.expectCreated('src/components/module/FooBar.vue').toContain('<div>FooBar</div>');
29
+ });
30
+
31
+ it('generates components with stories', async () => {
32
+ // Arrange
33
+ FileMock.stub(
34
+ 'package.json',
35
+ `
36
+ "@aerogel/core": "*",
37
+ "histoire": "*"
38
+ `,
39
+ );
24
40
 
25
41
  // Act
26
42
  await GenerateComponentCommand.run('FooBar', { story: true });
@@ -30,4 +46,25 @@ describe('Generate component command', () => {
30
46
  FileMock.expectCreated('src/components/FooBar.story.vue').toContain('<FooBar />');
31
47
  });
32
48
 
49
+ it('generates input components with stories', async () => {
50
+ // Arrange
51
+ FileMock.stub(
52
+ 'package.json',
53
+ `
54
+ "@aerogel/core": "*",
55
+ "histoire": "*"
56
+ `,
57
+ );
58
+
59
+ // Act
60
+ await GenerateComponentCommand.run('FooBar', { input: true, story: true });
61
+
62
+ // Assert
63
+ FileMock.expectCreated('src/components/FooBar.vue').toContain('<AGHeadlessInputInput v-bind="attrs" />');
64
+ FileMock.expectCreated('src/components/FooBar.story.vue').toContain('.story-foobar .variant-playground');
65
+ FileMock.expectCreated('src/components/FooBar.story.vue').toContain(
66
+ '<FooBar name="food" :label="label" :placeholder="placeholder" />',
67
+ );
68
+ });
69
+
33
70
  });
@@ -1,65 +1,188 @@
1
+ import { arrayFrom, stringToSlug } from '@noeldemartin/utils';
2
+ import { Node, SyntaxKind } from 'ts-morph';
3
+ import type { ArrayLiteralExpression, CallExpression, SourceFile } from 'ts-morph';
4
+
1
5
  import Command from '@/commands/Command';
2
6
  import File from '@/lib/File';
3
7
  import Log from '@/lib/Log';
4
8
  import Template from '@/lib/Template';
5
- import { basePath } from '@/lib/utils';
9
+ import { app } from '@/lib/utils/app';
10
+ import { editFiles, findDescendant } from '@/lib/utils/edit';
11
+ import { templatePath } from '@/lib/utils/paths';
6
12
  import type { CommandOptions } from '@/commands/Command';
7
13
 
8
14
  export interface Options {
9
15
  story?: boolean;
16
+ input?: boolean;
10
17
  }
11
18
 
12
19
  export class GenerateComponentCommand extends Command {
13
20
 
14
21
  public static command: string = 'generate:component';
15
- public static description: string = 'Generate an AerogelJS component';
16
- public static parameters: [string, string][] = [['name', 'Component name']];
22
+ public static description: string = 'Generate an AerogelJS Component';
23
+ public static parameters: [string, string][] = [
24
+ ['path', 'Component path (relative to components folder; extension not necessary)'],
25
+ ];
26
+
17
27
  public static options: CommandOptions = {
18
28
  story: {
19
29
  description: 'Create component story using Histoire',
20
30
  type: 'boolean',
21
31
  },
32
+ input: {
33
+ description: 'Create a custom input',
34
+ type: 'boolean',
35
+ },
22
36
  };
23
37
 
24
- private name: string;
38
+ private path: string;
25
39
  private options: Options;
26
40
 
27
- constructor(name: string, options: Options = {}) {
41
+ constructor(path: string, options: Options = {}) {
28
42
  super();
29
43
 
30
- this.name = name;
44
+ this.path = path;
31
45
  this.options = options;
32
46
  }
33
47
 
34
48
  public async run(): Promise<void> {
35
49
  this.assertAerogelOrDirectory('src/components');
36
- this.options.story && this.assertHistoireInstalled();
50
+ this.assertHistoireInstalled();
51
+
52
+ const files = new Set<string>();
53
+ const [directoryName, componentName] = this.parsePathComponents();
54
+
55
+ await this.createComponent(directoryName, componentName, files);
56
+ await this.createStory(directoryName, componentName, files);
57
+ await this.declareComponents();
37
58
 
38
- if (File.exists(`src/components/${this.name}.vue`)) {
39
- Log.fail(`${this.name} component already exists!`);
59
+ const filesList = arrayFrom(files)
60
+ .map((file) => `- ${file}`)
61
+ .join('\n');
62
+
63
+ Log.info(`${componentName} component created successfully! The following files were created:\n\n${filesList}`);
64
+ }
65
+
66
+ protected assertHistoireInstalled(): void {
67
+ if (!this.options.story) {
68
+ return;
40
69
  }
41
70
 
42
- const files = Template.instantiate(basePath('templates/component'), 'src/components', {
43
- component: { name: this.name },
71
+ if (!File.contains('package.json', '"histoire"') && !File.contains('package.json', '"@aerogel/histoire"')) {
72
+ Log.fail(`
73
+ Histoire is not installed yet! You can install it running:
74
+ npx ag install histoire
75
+ `);
76
+ }
77
+ }
78
+
79
+ protected async createComponent(directoryName: string, componentName: string, files: Set<string>): Promise<void> {
80
+ await Log.animate('Creating component', async () => {
81
+ if (File.exists(`src/components/${this.path}.vue`)) {
82
+ Log.fail(`${this.path} component already exists!`);
83
+ }
84
+
85
+ const templateName = this.options.input ? 'component-input' : 'component';
86
+ const componentFiles = Template.instantiate(templatePath(templateName), `src/components/${directoryName}`, {
87
+ component: {
88
+ name: componentName,
89
+ slug: stringToSlug(componentName),
90
+ },
91
+ });
92
+
93
+ componentFiles.forEach((file) => files.add(file));
44
94
  });
95
+ }
45
96
 
46
- if (this.options.story) {
47
- const storyFiles = Template.instantiate(basePath('templates/component-story'), 'src/components', {
48
- component: { name: this.name },
97
+ protected async createStory(directoryName: string, componentName: string, files: Set<string>): Promise<void> {
98
+ if (!this.options.story) {
99
+ return;
100
+ }
101
+
102
+ await Log.animate('Creating story', async () => {
103
+ const templateName = this.options.input ? 'component-input-story' : 'component-story';
104
+ const storyFiles = Template.instantiate(templatePath(templateName), `src/components/${directoryName}`, {
105
+ component: {
106
+ name: componentName,
107
+ slug: stringToSlug(componentName),
108
+ },
49
109
  });
50
110
 
51
- files.push(...storyFiles);
111
+ storyFiles.forEach((file) => files.add(file));
112
+ });
113
+ }
114
+
115
+ protected async declareComponents(): Promise<void> {
116
+ if (!editFiles()) {
117
+ return;
52
118
  }
53
119
 
54
- const filesList = files.map((file) => `- ${file}`).join('\n');
120
+ const editor = app().edit();
121
+ const viteConfig = editor.requireSourceFile('vite.config.ts');
122
+ const componentDirsArray = this.getComponentDirsArray(viteConfig);
55
123
 
56
- Log.info(`${this.name} component created successfully! The following files were created:\n\n${filesList}`);
124
+ if (!componentDirsArray) {
125
+ return Log.fail('Could not find component dirs declaration in vite config!');
126
+ }
127
+
128
+ if (
129
+ componentDirsArray
130
+ .getDescendantsOfKind(SyntaxKind.StringLiteral)
131
+ .some((literal) => literal.getText() === '\'src/components\'')
132
+ ) {
133
+ return;
134
+ }
135
+
136
+ await Log.animate('Updating vite config', async () => {
137
+ componentDirsArray.addElement('\'src/components\'');
138
+
139
+ await editor.save(viteConfig);
140
+ });
141
+
142
+ await editor.format();
57
143
  }
58
144
 
59
- protected assertHistoireInstalled(): void {
60
- if (!File.exists('src/main.histoire.ts')) {
61
- Log.fail('Histoire is not installed yet!');
145
+ protected getComponentDirsArray(viteConfig: SourceFile): ArrayLiteralExpression | null {
146
+ const pluginCall = findDescendant(viteConfig, {
147
+ guard: Node.isCallExpression,
148
+ validate: (callExpression) => callExpression.getText().startsWith('Components('),
149
+ skip: SyntaxKind.ImportDeclaration,
150
+ });
151
+
152
+ if (!pluginCall) {
153
+ return null;
154
+ }
155
+
156
+ const dirsAssignment = findDescendant(pluginCall, {
157
+ guard: Node.isPropertyAssignment,
158
+ validate: (propertyAssignment) => propertyAssignment.getName() === 'dirs',
159
+ });
160
+
161
+ const dirsArray = dirsAssignment?.getInitializer();
162
+
163
+ if (!Node.isArrayLiteralExpression(dirsArray)) {
164
+ return this.declareComponentDirsArray(pluginCall);
62
165
  }
166
+
167
+ return dirsArray;
168
+ }
169
+
170
+ protected declareComponentDirsArray(pluginCall: CallExpression): ArrayLiteralExpression | null {
171
+ const pluginOptions = findDescendant(pluginCall, { guard: Node.isObjectLiteralExpression });
172
+ const dirsAssignment = pluginOptions?.addPropertyAssignment({
173
+ name: 'dirs',
174
+ initializer: '[]',
175
+ });
176
+
177
+ return (dirsAssignment?.getInitializer() as ArrayLiteralExpression) ?? null;
178
+ }
179
+
180
+ protected parsePathComponents(): [string, string] {
181
+ const lastSlashIndex = this.path.lastIndexOf('/');
182
+
183
+ return lastSlashIndex === -1
184
+ ? ['', this.path]
185
+ : [this.path.substring(0, lastSlashIndex), this.path.substring(lastSlashIndex + 1)];
63
186
  }
64
187
 
65
188
  }
@@ -1,11 +1,11 @@
1
1
  import { describe, it } from 'vitest';
2
+ import { formatCodeBlock } from '@noeldemartin/utils';
2
3
 
3
4
  import FileMock from '@/lib/File.mock';
4
- import { formatCodeBlock } from '@/lib/utils';
5
5
 
6
6
  import { GenerateModelCommand } from './generate-model';
7
7
 
8
- describe('Generate model command', () => {
8
+ describe('Generate Model command', () => {
9
9
 
10
10
  it('generates models', async () => {
11
11
  // Arrange
@@ -21,7 +21,7 @@ describe('Generate model command', () => {
21
21
 
22
22
  // Act
23
23
  await GenerateModelCommand.run('FooBar', {
24
- fields: 'name:string,age:number,birth:Date',
24
+ fields: 'name:string:required,age:number,birth:Date',
25
25
  });
26
26
 
27
27
  // Assert
@@ -30,7 +30,10 @@ describe('Generate model command', () => {
30
30
  formatCodeBlock(`
31
31
  defineModelSchema({
32
32
  fields: {
33
- name: FieldType.String,
33
+ name: {
34
+ type: FieldType.String,
35
+ required: true,
36
+ },
34
37
  age: FieldType.Number,
35
38
  birth: FieldType.Date,
36
39
  },
@@ -1,10 +1,10 @@
1
- import { stringToStudlyCase } from '@noeldemartin/utils';
1
+ import { formatCodeBlock, stringToStudlyCase } from '@noeldemartin/utils';
2
2
 
3
3
  import Command from '@/commands/Command';
4
4
  import File from '@/lib/File';
5
5
  import Log from '@/lib/Log';
6
6
  import Template from '@/lib/Template';
7
- import { basePath, formatCodeBlock } from '@/lib/utils';
7
+ import { templatePath } from '@/lib/utils/paths';
8
8
  import type { CommandOptions } from '@/commands/Command';
9
9
 
10
10
  interface Options {
@@ -14,7 +14,7 @@ interface Options {
14
14
  export class GenerateModelCommand extends Command {
15
15
 
16
16
  public static command: string = 'generate:model';
17
- public static description: string = 'Generate an AerogelJS model';
17
+ public static description: string = 'Generate an AerogelJS Model';
18
18
  public static parameters: [string, string][] = [['name', 'Model name']];
19
19
  public static options: CommandOptions = {
20
20
  fields: 'Create model with the given fields',
@@ -39,15 +39,17 @@ export class GenerateModelCommand extends Command {
39
39
 
40
40
  this.assertSoukaiInstalled();
41
41
 
42
- const files = Template.instantiate(basePath('templates/model'), 'src/models', {
43
- model: {
44
- name: this.name,
45
- fieldsDefinition: this.getFieldsDefinition(),
46
- },
47
- soukaiImports: this.options.fields ? 'FieldType, defineModelSchema' : 'defineModelSchema',
48
- });
42
+ const filesList = await Log.animate('Creating model', async () => {
43
+ const files = Template.instantiate(templatePath('model'), 'src/models', {
44
+ model: {
45
+ name: this.name,
46
+ fieldsDefinition: this.getFieldsDefinition(),
47
+ },
48
+ soukaiImports: this.options.fields ? 'FieldType, defineModelSchema' : 'defineModelSchema',
49
+ });
49
50
 
50
- const filesList = files.map((file) => `- ${file}`).join('\n');
51
+ return files.map((file) => `- ${file}`).join('\n');
52
+ });
51
53
 
52
54
  Log.info(`${this.name} model created successfully! The following files were created:\n\n${filesList}`);
53
55
  }
@@ -60,21 +62,36 @@ export class GenerateModelCommand extends Command {
60
62
  const code = this.options.fields
61
63
  .split(',')
62
64
  .map((field) => {
63
- const [name, type] = field.split(':');
65
+ const [name, type, rules] = field.split(':');
64
66
 
65
67
  return {
66
68
  name,
67
69
  type: stringToStudlyCase(type ?? 'string'),
70
+ required: rules === 'required',
68
71
  };
69
72
  })
70
- .reduce((definition, field) => definition + `\n${field.name}: FieldType.${field.type},`, '');
73
+ .reduce((definition, field) => {
74
+ const fieldDefinition = field.required
75
+ ? formatCodeBlock(`
76
+ ${field.name}: {
77
+ type: FieldType.${field.type},
78
+ required: true,
79
+ }
80
+ `)
81
+ : `${field.name}: FieldType.${field.type}`;
82
+
83
+ return definition + `\n${fieldDefinition},`;
84
+ }, '');
71
85
 
72
86
  return formatCodeBlock(code, { indent: 8 });
73
87
  }
74
88
 
75
89
  protected assertSoukaiInstalled(): void {
76
- if (!File.contains('package.json', '"soukai"')) {
77
- Log.fail('Soukai is not installed yet!');
90
+ if (!File.contains('package.json', '"soukai"') && !File.contains('package.json', '"@aerogel/plugin-soukai"')) {
91
+ Log.fail(`
92
+ Soukai is not installed yet! You can install it running:
93
+ npx ag install soukai
94
+ `);
78
95
  }
79
96
  }
80
97
 
@@ -0,0 +1,21 @@
1
+ import { describe, it } from 'vitest';
2
+
3
+ import FileMock from '@/lib/File.mock';
4
+
5
+ import { GenerateServiceCommand } from './generate-service';
6
+
7
+ describe('Generate Service command', () => {
8
+
9
+ it('generates services', async () => {
10
+ // Arrange
11
+ FileMock.stub('package.json', '@aerogel/core');
12
+
13
+ // Act
14
+ await GenerateServiceCommand.run('FooBar');
15
+
16
+ // Assert
17
+ FileMock.expectCreated('src/services/FooBar.ts').toContain('class FooBarService extends Service');
18
+ FileMock.expectCreated('src/services/FooBar.ts').toContain('export default facade(new FooBarService());');
19
+ });
20
+
21
+ });