@aerogel/cli 0.0.0-next.c8f032a868370824898e171969aec1bb6827688e → 0.0.0-next.f8cdd39997c56dcd46e07c26af8a84d04d610fce

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 (50) 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 +4 -3
  6. package/src/cli.ts +4 -0
  7. package/src/commands/create.test.ts +23 -4
  8. package/src/commands/create.ts +30 -12
  9. package/src/commands/generate-component.test.ts +12 -1
  10. package/src/commands/generate-component.ts +127 -20
  11. package/src/commands/generate-model.test.ts +16 -5
  12. package/src/commands/generate-model.ts +38 -12
  13. package/src/commands/generate-service.test.ts +21 -0
  14. package/src/commands/generate-service.ts +152 -0
  15. package/src/commands/install.test.ts +18 -0
  16. package/src/commands/install.ts +32 -0
  17. package/src/lib/App.ts +65 -3
  18. package/src/lib/Editor.ts +56 -0
  19. package/src/lib/File.ts +10 -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/Template.ts +4 -3
  24. package/src/lib/utils/app.ts +15 -0
  25. package/src/lib/utils/edit.ts +44 -0
  26. package/src/lib/{utils.test.ts → utils/format.test.ts} +2 -2
  27. package/src/lib/{utils.ts → utils/format.ts} +0 -6
  28. package/src/lib/utils/paths.ts +34 -0
  29. package/src/plugins/Plugin.ts +125 -0
  30. package/src/plugins/Solid.ts +112 -0
  31. package/src/plugins/Soukai.ts +19 -0
  32. package/src/testing/setup.ts +38 -6
  33. package/templates/app/.eslintrc.js +3 -0
  34. package/templates/app/.github/workflows/ci.yml +14 -2
  35. package/templates/app/.gitignore.template +2 -0
  36. package/templates/app/.vscode/launch.json +16 -0
  37. package/templates/app/.vscode/settings.json +10 -0
  38. package/templates/app/README.md +3 -0
  39. package/templates/app/cypress.config.ts +4 -0
  40. package/templates/app/index.html +1 -1
  41. package/templates/app/package.json +21 -10
  42. package/templates/app/prettier.config.js +5 -0
  43. package/templates/app/src/App.vue +3 -1
  44. package/templates/app/src/main.ts +5 -1
  45. package/templates/app/src/types/globals.d.ts +0 -1
  46. package/templates/app/tailwind.config.js +1 -1
  47. package/templates/app/tsconfig.json +1 -0
  48. package/templates/app/vite.config.ts +12 -5
  49. package/templates/service/[service.name].ts +8 -0
  50. package/noeldemartin.config.js +0 -4
@@ -7,19 +7,38 @@ 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
+ FileMock.expectCreated('./app/.gitignore').toContain('node_modules');
15
16
  FileMock.expectCreated('./app/package.json').toContain('"name": "my-app"');
17
+ FileMock.expectCreated('./app/package.json').toContain('"@aerogel/core": "next"');
16
18
  FileMock.expectCreated('./app/index.html').toContain('My App');
17
- FileMock.expectCreated('./app/src/App.vue').toContain(
18
- '<h1 class="text-4xl font-semibold">{{ $t(\'home.title\') }}</h1>',
19
- );
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\') }}');
20
22
 
21
23
  ShellMock.expectRan('npm install');
22
24
  ShellMock.expectRan('git init');
23
25
  });
24
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
+ FileMock.expectCreated('./my-app/index.html').toContain('My App');
42
+ });
43
+
25
44
  });
@@ -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,6 +17,17 @@ describe('Generate component command', () => {
17
17
  FileMock.expectCreated('src/components/FooBar.vue').toContain('<div>FooBar</div>');
18
18
  });
19
19
 
20
+ it('generates components in subfolders', async () => {
21
+ // Arrange
22
+ FileMock.stub('package.json', '@aerogel/core');
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
+
20
31
  it('generates components with stories', async () => {
21
32
  // Arrange
22
33
  FileMock.stub('package.json', '@aerogel/core');
@@ -1,8 +1,14 @@
1
+ import { arrayFrom } 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 {
@@ -12,8 +18,11 @@ export interface Options {
12
18
  export class GenerateComponentCommand extends Command {
13
19
 
14
20
  public static command: string = 'generate:component';
15
- public static description: string = 'Generate an AerogelJS component';
16
- public static parameters: [string, string][] = [['name', 'Component name']];
21
+ public static description: string = 'Generate an AerogelJS Component';
22
+ public static parameters: [string, string][] = [
23
+ ['path', 'Component path (relative to components folder; extension not necessary)'],
24
+ ];
25
+
17
26
  public static options: CommandOptions = {
18
27
  story: {
19
28
  description: 'Create component story using Histoire',
@@ -21,45 +30,143 @@ export class GenerateComponentCommand extends Command {
21
30
  },
22
31
  };
23
32
 
24
- private name: string;
33
+ private path: string;
25
34
  private options: Options;
26
35
 
27
- constructor(name: string, options: Options = {}) {
36
+ constructor(path: string, options: Options = {}) {
28
37
  super();
29
38
 
30
- this.name = name;
39
+ this.path = path;
31
40
  this.options = options;
32
41
  }
33
42
 
34
43
  public async run(): Promise<void> {
35
44
  this.assertAerogelOrDirectory('src/components');
36
- this.options.story && this.assertHistoireInstalled();
45
+ this.assertHistoireInstalled();
46
+
47
+ const files = new Set<string>();
48
+ const [directoryName, componentName] = this.parsePathComponents();
49
+
50
+ await this.createComponent(directoryName, componentName, files);
51
+ await this.createStory(componentName, files);
52
+ await this.declareComponents();
53
+
54
+ const filesList = arrayFrom(files)
55
+ .map((file) => `- ${file}`)
56
+ .join('\n');
57
+
58
+ Log.info(`${componentName} component created successfully! The following files were created:\n\n${filesList}`);
59
+ }
60
+
61
+ protected assertHistoireInstalled(): void {
62
+ if (!this.options.story) {
63
+ return;
64
+ }
37
65
 
38
- if (File.exists(`src/components/${this.name}.vue`)) {
39
- Log.fail(`${this.name} component already exists!`);
66
+ if (!File.exists('src/main.histoire.ts')) {
67
+ Log.fail('Histoire is not installed yet!');
40
68
  }
69
+ }
70
+
71
+ protected async createComponent(directoryName: string, componentName: string, files: Set<string>): Promise<void> {
72
+ await Log.animate('Creating component', async () => {
73
+ if (File.exists(`src/components/${this.path}.vue`)) {
74
+ Log.fail(`${this.path} component already exists!`);
75
+ }
76
+
77
+ const componentFiles = Template.instantiate(templatePath('component'), `src/components/${directoryName}`, {
78
+ component: { name: componentName },
79
+ });
41
80
 
42
- const files = Template.instantiate(basePath('templates/component'), 'src/components', {
43
- component: { name: this.name },
81
+ componentFiles.forEach((file) => files.add(file));
44
82
  });
83
+ }
84
+
85
+ protected async createStory(componentName: string, files: Set<string>): Promise<void> {
86
+ if (!this.options.story) {
87
+ return;
88
+ }
45
89
 
46
- if (this.options.story) {
47
- const storyFiles = Template.instantiate(basePath('templates/component-story'), 'src/components', {
48
- component: { name: this.name },
90
+ await Log.animate('Creating story', async () => {
91
+ const storyFiles = Template.instantiate(templatePath('component-story'), 'src/components', {
92
+ component: { name: componentName },
49
93
  });
50
94
 
51
- files.push(...storyFiles);
95
+ storyFiles.forEach((file) => files.add(file));
96
+ });
97
+ }
98
+
99
+ protected async declareComponents(): Promise<void> {
100
+ if (!editFiles()) {
101
+ return;
52
102
  }
53
103
 
54
- const filesList = files.map((file) => `- ${file}`).join('\n');
104
+ const editor = app().edit();
105
+ const viteConfig = editor.requireSourceFile('vite.config.ts');
106
+ const componentDirsArray = this.getComponentDirsArray(viteConfig);
107
+
108
+ if (!componentDirsArray) {
109
+ return Log.fail('Could not find component dirs declaration in vite config!');
110
+ }
55
111
 
56
- Log.info(`${this.name} component created successfully! The following files were created:\n\n${filesList}`);
112
+ if (
113
+ componentDirsArray
114
+ .getDescendantsOfKind(SyntaxKind.StringLiteral)
115
+ .some((literal) => literal.getText() === '\'src/components\'')
116
+ ) {
117
+ return;
118
+ }
119
+
120
+ await Log.animate('Updating vite config', async () => {
121
+ componentDirsArray.addElement('\'src/components\'');
122
+
123
+ await editor.save(viteConfig);
124
+ });
125
+
126
+ await editor.format();
57
127
  }
58
128
 
59
- protected assertHistoireInstalled(): void {
60
- if (!File.exists('src/main.histoire.ts')) {
61
- Log.fail('Histoire is not installed yet!');
129
+ protected getComponentDirsArray(viteConfig: SourceFile): ArrayLiteralExpression | null {
130
+ const pluginCall = findDescendant(viteConfig, {
131
+ guard: Node.isCallExpression,
132
+ validate: (callExpression) => callExpression.getText().startsWith('Components('),
133
+ skip: SyntaxKind.ImportDeclaration,
134
+ });
135
+
136
+ if (!pluginCall) {
137
+ return null;
62
138
  }
139
+
140
+ const dirsAssignment = findDescendant(pluginCall, {
141
+ guard: Node.isPropertyAssignment,
142
+ validate: (propertyAssignment) => propertyAssignment.getName() === 'dirs',
143
+ });
144
+
145
+ const dirsArray = dirsAssignment?.getInitializer();
146
+
147
+ if (!Node.isArrayLiteralExpression(dirsArray)) {
148
+ return this.declareComponentDirsArray(pluginCall);
149
+ }
150
+
151
+ return dirsArray;
152
+ }
153
+
154
+ protected declareComponentDirsArray(pluginCall: CallExpression): ArrayLiteralExpression | null {
155
+ const pluginOptions = findDescendant(pluginCall, { guard: Node.isObjectLiteralExpression });
156
+ const dirsAssignment = pluginOptions?.addPropertyAssignment({
157
+ name: 'dirs',
158
+ initializer: '[]',
159
+ });
160
+
161
+ return (dirsAssignment?.getInitializer() as ArrayLiteralExpression) ?? null;
162
+ }
163
+
164
+ protected parsePathComponents(): [string, string] {
165
+ const lastSlashIndex = this.path.lastIndexOf('/');
166
+
167
+ return lastSlashIndex === -1
168
+ ? ['', this.path]
169
+ : [this.path.substring(0, lastSlashIndex), this.path.substring(lastSlashIndex + 1)];
63
170
  }
64
171
 
65
172
  }
@@ -1,19 +1,27 @@
1
1
  import { describe, it } from 'vitest';
2
2
 
3
3
  import FileMock from '@/lib/File.mock';
4
- import { formatCodeBlock } from '@/lib/utils';
4
+ import { formatCodeBlock } from '@/lib/utils/format';
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
12
- FileMock.stub('package.json', '@aerogel/core');
12
+ FileMock.stub(
13
+ 'package.json',
14
+ `
15
+ "dependencies": {
16
+ "@aerogel/core": "next",
17
+ "soukai": "next"
18
+ }
19
+ `,
20
+ );
13
21
 
14
22
  // Act
15
23
  await GenerateModelCommand.run('FooBar', {
16
- fields: 'name:string,age:number,birth:Date',
24
+ fields: 'name:string:required,age:number,birth:Date',
17
25
  });
18
26
 
19
27
  // Assert
@@ -22,7 +30,10 @@ describe('Generate model command', () => {
22
30
  formatCodeBlock(`
23
31
  defineModelSchema({
24
32
  fields: {
25
- name: FieldType.String,
33
+ name: {
34
+ type: FieldType.String,
35
+ required: true,
36
+ },
26
37
  age: FieldType.Number,
27
38
  birth: FieldType.Date,
28
39
  },
@@ -4,7 +4,8 @@ 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
+ import { formatCodeBlock } from '@/lib/utils/format';
8
9
  import type { CommandOptions } from '@/commands/Command';
9
10
 
10
11
  interface Options {
@@ -14,7 +15,7 @@ interface Options {
14
15
  export class GenerateModelCommand extends Command {
15
16
 
16
17
  public static command: string = 'generate:model';
17
- public static description: string = 'Generate an AerogelJS model';
18
+ public static description: string = 'Generate an AerogelJS Model';
18
19
  public static parameters: [string, string][] = [['name', 'Model name']];
19
20
  public static options: CommandOptions = {
20
21
  fields: 'Create model with the given fields',
@@ -37,15 +38,19 @@ export class GenerateModelCommand extends Command {
37
38
  Log.fail(`${this.name} model already exists!`);
38
39
  }
39
40
 
40
- const files = Template.instantiate(basePath('templates/model'), 'src/models', {
41
- model: {
42
- name: this.name,
43
- fieldsDefinition: this.getFieldsDefinition(),
44
- },
45
- soukaiImports: this.options.fields ? 'FieldType, defineModelSchema' : 'defineModelSchema',
46
- });
41
+ this.assertSoukaiInstalled();
42
+
43
+ const filesList = await Log.animate('Creating model', async () => {
44
+ const files = Template.instantiate(templatePath('model'), 'src/models', {
45
+ model: {
46
+ name: this.name,
47
+ fieldsDefinition: this.getFieldsDefinition(),
48
+ },
49
+ soukaiImports: this.options.fields ? 'FieldType, defineModelSchema' : 'defineModelSchema',
50
+ });
47
51
 
48
- const filesList = files.map((file) => `- ${file}`).join('\n');
52
+ return files.map((file) => `- ${file}`).join('\n');
53
+ });
49
54
 
50
55
  Log.info(`${this.name} model created successfully! The following files were created:\n\n${filesList}`);
51
56
  }
@@ -58,16 +63,37 @@ export class GenerateModelCommand extends Command {
58
63
  const code = this.options.fields
59
64
  .split(',')
60
65
  .map((field) => {
61
- const [name, type] = field.split(':');
66
+ const [name, type, rules] = field.split(':');
62
67
 
63
68
  return {
64
69
  name,
65
70
  type: stringToStudlyCase(type ?? 'string'),
71
+ required: rules === 'required',
66
72
  };
67
73
  })
68
- .reduce((definition, field) => definition + `\n${field.name}: FieldType.${field.type},`, '');
74
+ .reduce((definition, field) => {
75
+ const fieldDefinition = field.required
76
+ ? formatCodeBlock(`
77
+ ${field.name}: {
78
+ type: FieldType.${field.type},
79
+ required: true,
80
+ }
81
+ `)
82
+ : `${field.name}: FieldType.${field.type}`;
83
+
84
+ return definition + `\n${fieldDefinition},`;
85
+ }, '');
69
86
 
70
87
  return formatCodeBlock(code, { indent: 8 });
71
88
  }
72
89
 
90
+ protected assertSoukaiInstalled(): void {
91
+ if (!File.contains('package.json', '"soukai"') && !File.contains('package.json', '"@aerogel/plugin-soukai"')) {
92
+ Log.fail(`
93
+ Soukai is not installed yet! You can install it running:
94
+ npx ag install soukai
95
+ `);
96
+ }
97
+ }
98
+
73
99
  }
@@ -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
+ });
@@ -0,0 +1,152 @@
1
+ import { arrayFrom, stringToCamelCase } from '@noeldemartin/utils';
2
+ import { Node, SyntaxKind } from 'ts-morph';
3
+ import type { ObjectLiteralExpression, SourceFile } from 'ts-morph';
4
+
5
+ import Command from '@/commands/Command';
6
+ import File from '@/lib/File';
7
+ import Log from '@/lib/Log';
8
+ import Template from '@/lib/Template';
9
+ import { app } from '@/lib/utils/app';
10
+ import { templatePath } from '@/lib/utils/paths';
11
+ import { editFiles, findDescendant } from '@/lib/utils/edit';
12
+ import { formatCodeBlock } from '@/lib/utils/format';
13
+ import type { Editor } from '@/lib/Editor';
14
+
15
+ export class GenerateServiceCommand extends Command {
16
+
17
+ public static command: string = 'generate:service';
18
+ public static description: string = 'Generate an AerogelJS Service';
19
+ public static parameters: [string, string][] = [['name', 'Service name']];
20
+
21
+ private name: string;
22
+
23
+ constructor(name: string) {
24
+ super();
25
+
26
+ this.name = name;
27
+ }
28
+
29
+ public async run(): Promise<void> {
30
+ this.assertAerogelOrDirectory('src/services');
31
+
32
+ const files = new Set<string>();
33
+ const editor = app().edit();
34
+
35
+ await this.createService(files);
36
+
37
+ if (editFiles()) {
38
+ await this.registerService(editor);
39
+ await editor.format();
40
+ }
41
+
42
+ const filesList = arrayFrom(files)
43
+ .map((file) => `- ${file}`)
44
+ .join('\n');
45
+
46
+ Log.info(`${this.name} service created successfully! The following files were created:\n\n${filesList}`);
47
+ }
48
+
49
+ protected async createService(files: Set<string>): Promise<void> {
50
+ await Log.animate('Creating service', async () => {
51
+ if (File.exists(`src/services/${this.name}.ts`)) {
52
+ Log.fail(`${this.name} service already exists!`);
53
+ }
54
+
55
+ const serviceFiles = Template.instantiate(templatePath('service'), 'src/services', {
56
+ service: {
57
+ name: this.name,
58
+ },
59
+ });
60
+
61
+ serviceFiles.forEach((file) => files.add(file));
62
+ });
63
+ }
64
+
65
+ protected async registerService(editor: Editor): Promise<void> {
66
+ await Log.animate('Registering service', async () => {
67
+ if (!File.exists('src/services/index.ts')) {
68
+ await this.createServicesIndex(editor);
69
+ }
70
+
71
+ const servicesIndex = editor.requireSourceFile('src/services/index.ts');
72
+ const servicesObject = this.getServicesObject(servicesIndex);
73
+
74
+ if (!servicesObject) {
75
+ return Log.fail('Could not find services object in services config, please add it manually.');
76
+ }
77
+
78
+ servicesIndex.addImportDeclaration({
79
+ defaultImport: this.name,
80
+ moduleSpecifier: `./${this.name}`,
81
+ });
82
+ servicesObject.addPropertyAssignment({
83
+ name: `$${stringToCamelCase(this.name)}`,
84
+ initializer: this.name,
85
+ });
86
+
87
+ await editor.save(servicesIndex);
88
+ });
89
+ }
90
+
91
+ protected async createServicesIndex(editor: Editor): Promise<void> {
92
+ File.write(
93
+ 'src/services/index.ts',
94
+ formatCodeBlock(`
95
+ export const services = {};
96
+
97
+ export type AppServices = typeof services;
98
+
99
+ declare module '@vue/runtime-core' {
100
+ interface ComponentCustomProperties extends AppServices {}
101
+ }
102
+ `),
103
+ );
104
+
105
+ editor.addSourceFile('src/services/index.ts');
106
+
107
+ const mainConfig = editor.requireSourceFile('src/main.ts');
108
+ const bootstrapOptions = this.getBootstrapOptions(mainConfig);
109
+
110
+ if (!bootstrapOptions) {
111
+ return Log.fail('Could not find options object in bootstrap config, please add the services manually.');
112
+ }
113
+
114
+ bootstrapOptions.insertShorthandPropertyAssignment(0, { name: 'services' });
115
+ mainConfig.addImportDeclaration({
116
+ namedImports: ['services'],
117
+ moduleSpecifier: './services',
118
+ });
119
+
120
+ await editor.save(mainConfig);
121
+ }
122
+
123
+ protected getBootstrapOptions(mainConfig: SourceFile): ObjectLiteralExpression | null {
124
+ const bootstrapAppCall = findDescendant(mainConfig, {
125
+ guard: Node.isCallExpression,
126
+ validate: (callExpression) => callExpression.getExpression().getText() === 'bootstrapApplication',
127
+ skip: SyntaxKind.ImportDeclaration,
128
+ });
129
+ const bootstrapOptions = bootstrapAppCall?.getArguments()[1];
130
+
131
+ if (!Node.isObjectLiteralExpression(bootstrapOptions)) {
132
+ return null;
133
+ }
134
+
135
+ return bootstrapOptions;
136
+ }
137
+
138
+ protected getServicesObject(servicesIndex: SourceFile): ObjectLiteralExpression | null {
139
+ const servicesDeclaration = findDescendant(servicesIndex, {
140
+ guard: Node.isVariableDeclaration,
141
+ validate: (variableDeclaration) => variableDeclaration.getName() === 'services',
142
+ });
143
+ const servicesObject = servicesDeclaration?.getInitializer();
144
+
145
+ if (!Node.isObjectLiteralExpression(servicesObject)) {
146
+ return null;
147
+ }
148
+
149
+ return servicesObject;
150
+ }
151
+
152
+ }
@@ -0,0 +1,18 @@
1
+ import { describe, it } from 'vitest';
2
+
3
+ import ShellMock from '@/lib/Shell.mock';
4
+
5
+ import { InstallCommand } from './install';
6
+
7
+ describe('Install plugin command', () => {
8
+
9
+ it('installs plugins', async () => {
10
+ // Act
11
+ await InstallCommand.run('solid');
12
+
13
+ // Assert
14
+ ShellMock.expectRan('npm install soukai-solid@next --save-exact');
15
+ ShellMock.expectRan('npm install @aerogel/plugin-solid@next --save-exact');
16
+ });
17
+
18
+ });