@aerogel/cli 0.0.0-next.59bf5f7cc06e728d0cf6c00de28f1da48d7d6b8e → 0.0.0-next.702e17f12493b62feba1d0af54d7c3b1d07f934c

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 (74) hide show
  1. package/.prettierignore +1 -0
  2. package/dist/aerogel-cli.cjs.js +1 -1
  3. package/dist/aerogel-cli.cjs.js.map +1 -1
  4. package/dist/aerogel-cli.d.ts +1 -0
  5. package/dist/aerogel-cli.esm.js +1 -1
  6. package/dist/aerogel-cli.esm.js.map +1 -1
  7. package/package.json +12 -5
  8. package/src/cli.ts +24 -2
  9. package/src/commands/Command.ts +11 -6
  10. package/src/commands/create.test.ts +12 -4
  11. package/src/commands/create.ts +20 -9
  12. package/src/commands/generate-component.test.ts +50 -3
  13. package/src/commands/generate-component.ts +151 -21
  14. package/src/commands/generate-model.test.ts +2 -2
  15. package/src/commands/generate-model.ts +19 -22
  16. package/src/commands/generate-overrides.ts +85 -0
  17. package/src/commands/generate-service.test.ts +21 -0
  18. package/src/commands/generate-service.ts +151 -0
  19. package/src/commands/install.test.ts +41 -0
  20. package/src/commands/install.ts +33 -0
  21. package/src/lib/App.ts +60 -5
  22. package/src/lib/Editor.ts +58 -0
  23. package/src/lib/File.ts +6 -0
  24. package/src/lib/Log.mock.ts +2 -1
  25. package/src/lib/Log.ts +6 -4
  26. package/src/lib/Shell.mock.ts +1 -1
  27. package/src/lib/Template.ts +5 -1
  28. package/src/lib/utils/app.ts +15 -0
  29. package/src/lib/utils/edit.ts +44 -0
  30. package/src/lib/utils/paths.ts +34 -0
  31. package/src/plugins/Histoire.ts +105 -0
  32. package/src/plugins/Plugin.ts +178 -0
  33. package/src/plugins/Solid.ts +78 -0
  34. package/src/plugins/Soukai.ts +19 -0
  35. package/src/testing/setup.ts +36 -6
  36. package/templates/app/.github/workflows/ci.yml +14 -2
  37. package/templates/app/.vscode/launch.json +16 -0
  38. package/templates/app/.vscode/settings.json +10 -0
  39. package/templates/app/README.md +3 -0
  40. package/templates/app/cypress/cypress.config.ts +14 -0
  41. package/templates/app/cypress/support/e2e.ts +1 -3
  42. package/templates/app/index.html +4 -3
  43. package/templates/app/package.json +31 -9
  44. package/templates/app/src/App.vue +5 -3
  45. package/templates/app/src/assets/public/robots.txt +2 -0
  46. package/templates/app/src/main.ts +4 -4
  47. package/templates/app/src/types/globals.d.ts +0 -1
  48. package/templates/app/tailwind.config.js +1 -1
  49. package/templates/app/tsconfig.json +1 -0
  50. package/templates/app/vite.config.ts +10 -5
  51. package/templates/component-button/[component.name].vue +42 -0
  52. package/templates/component-button-story/[component.name].story.vue +77 -0
  53. package/templates/component-input/[component.name].vue +16 -0
  54. package/templates/component-input-story/[component.name].story.vue +63 -0
  55. package/templates/histoire/histoire.config.ts +7 -0
  56. package/templates/histoire/patches/histoire+0.17.6.patch +13 -0
  57. package/templates/histoire/src/main.histoire.ts +8 -0
  58. package/templates/overrides/components/index.ts +15 -0
  59. package/templates/overrides/components/overrides/AlertModal.vue +11 -0
  60. package/templates/overrides/components/overrides/ConfirmModal.vue +20 -0
  61. package/templates/overrides/components/overrides/ErrorReportModal.vue +35 -0
  62. package/templates/overrides/components/overrides/LoadingModal.vue +12 -0
  63. package/templates/overrides/components/overrides/ModalWrapper.vue +22 -0
  64. package/templates/overrides/components/overrides/SnackbarNotification.vue +34 -0
  65. package/templates/overrides-story/Overrides.story.vue +86 -0
  66. package/templates/postcss-pseudo-classes/postcss.config.js +15 -0
  67. package/templates/service/[service.name].ts +8 -0
  68. package/.eslintrc.js +0 -7
  69. package/noeldemartin.config.js +0 -4
  70. package/src/lib/utils.test.ts +0 -33
  71. package/src/lib/utils.ts +0 -44
  72. package/templates/app/cypress.config.ts +0 -8
  73. /package/bin/{ag → gel} +0 -0
  74. /package/templates/app/src/assets/{styles.css → css/styles.css} +0 -0
@@ -1,23 +1,39 @@
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 {
15
+ button?: boolean;
16
+ input?: boolean;
9
17
  story?: boolean;
10
18
  }
11
19
 
12
20
  export class GenerateComponentCommand extends Command {
13
21
 
14
- public static command: string = 'generate:component';
15
- public static description: string = 'Generate an AerogelJS component';
16
- public static parameters: [string, string][] = [
22
+ protected static command: string = 'generate:component';
23
+ protected static description: string = 'Generate an AerogelJS Component';
24
+ protected static parameters: [string, string][] = [
17
25
  ['path', 'Component path (relative to components folder; extension not necessary)'],
18
26
  ];
19
27
 
20
- public static options: CommandOptions = {
28
+ protected static options: CommandOptions = {
29
+ button: {
30
+ description: 'Create a custom button',
31
+ type: 'boolean',
32
+ },
33
+ input: {
34
+ description: 'Create a custom input',
35
+ type: 'boolean',
36
+ },
21
37
  story: {
22
38
  description: 'Create component story using Histoire',
23
39
  type: 'boolean',
@@ -34,36 +50,150 @@ export class GenerateComponentCommand extends Command {
34
50
  this.options = options;
35
51
  }
36
52
 
37
- public async run(): Promise<void> {
53
+ protected async validate(): Promise<void> {
54
+ if (this.options.button && this.options.input) {
55
+ Log.fail('Cannot use both \'button\' and \'input\' flags!');
56
+ }
57
+ }
58
+
59
+ protected async run(): Promise<void> {
38
60
  this.assertAerogelOrDirectory('src/components');
39
- this.options.story && this.assertHistoireInstalled();
61
+ this.assertHistoireInstalled();
62
+
63
+ const files = new Set<string>();
64
+ const [directoryName, componentName] = this.parsePathComponents();
40
65
 
41
- if (File.exists(`src/components/${this.path}.vue`)) {
42
- Log.fail(`${this.path} component already exists!`);
66
+ await this.createComponent(directoryName, componentName, files);
67
+ await this.createStory(directoryName, componentName, files);
68
+ await this.declareComponents();
69
+
70
+ const filesList = arrayFrom(files)
71
+ .map((file) => `- ${file}`)
72
+ .join('\n');
73
+
74
+ Log.info(`${componentName} component created successfully! The following files were created:\n\n${filesList}`);
75
+ }
76
+
77
+ protected assertHistoireInstalled(): void {
78
+ if (!this.options.story) {
79
+ return;
43
80
  }
44
81
 
45
- const [directoryName, componentName] = this.parsePathComponents();
46
- const files = Template.instantiate(basePath('templates/component'), `src/components/${directoryName}`, {
47
- component: { name: componentName },
82
+ if (!File.contains('package.json', '"histoire"') && !File.contains('package.json', '"@aerogel/histoire"')) {
83
+ Log.fail(`
84
+ Histoire is not installed yet! You can install it running:
85
+ npx gel install histoire
86
+ `);
87
+ }
88
+ }
89
+
90
+ protected async createComponent(directoryName: string, componentName: string, files: Set<string>): Promise<void> {
91
+ await Log.animate('Creating component', async () => {
92
+ if (File.exists(`src/components/${this.path}.vue`)) {
93
+ Log.fail(`${this.path} component already exists!`);
94
+ }
95
+
96
+ const templateName = this.options.input
97
+ ? 'component-input'
98
+ : this.options.button
99
+ ? 'component-button'
100
+ : 'component';
101
+ const componentFiles = Template.instantiate(templatePath(templateName), `src/components/${directoryName}`, {
102
+ component: {
103
+ name: componentName,
104
+ slug: stringToSlug(componentName),
105
+ },
106
+ });
107
+
108
+ componentFiles.forEach((file) => files.add(file));
48
109
  });
110
+ }
111
+
112
+ protected async createStory(directoryName: string, componentName: string, files: Set<string>): Promise<void> {
113
+ if (!this.options.story) {
114
+ return;
115
+ }
49
116
 
50
- if (this.options.story) {
51
- const storyFiles = Template.instantiate(basePath('templates/component-story'), 'src/components', {
52
- component: { name: componentName },
117
+ await Log.animate('Creating story', async () => {
118
+ const templateName = this.options.input
119
+ ? 'component-input-story'
120
+ : this.options.button
121
+ ? 'component-button-story'
122
+ : 'component-story';
123
+ const storyFiles = Template.instantiate(templatePath(templateName), `src/components/${directoryName}`, {
124
+ component: {
125
+ name: componentName,
126
+ slug: stringToSlug(componentName),
127
+ },
53
128
  });
54
129
 
55
- files.push(...storyFiles);
130
+ storyFiles.forEach((file) => files.add(file));
131
+ });
132
+ }
133
+
134
+ protected async declareComponents(): Promise<void> {
135
+ if (!editFiles()) {
136
+ return;
56
137
  }
57
138
 
58
- const filesList = files.map((file) => `- ${file}`).join('\n');
139
+ const editor = app().edit();
140
+ const viteConfig = editor.requireSourceFile('vite.config.ts');
141
+ const componentDirsArray = this.getComponentDirsArray(viteConfig);
59
142
 
60
- Log.info(`${componentName} component created successfully! The following files were created:\n\n${filesList}`);
143
+ if (!componentDirsArray) {
144
+ return Log.fail('Could not find component dirs declaration in vite config!');
145
+ }
146
+
147
+ if (
148
+ componentDirsArray
149
+ .getDescendantsOfKind(SyntaxKind.StringLiteral)
150
+ .some((literal) => literal.getText() === '\'src/components\'')
151
+ ) {
152
+ return;
153
+ }
154
+
155
+ await Log.animate('Updating vite config', async () => {
156
+ componentDirsArray.addElement('\'src/components\'');
157
+
158
+ await editor.save(viteConfig);
159
+ });
160
+
161
+ await editor.format();
61
162
  }
62
163
 
63
- protected assertHistoireInstalled(): void {
64
- if (!File.exists('src/main.histoire.ts')) {
65
- Log.fail('Histoire is not installed yet!');
164
+ protected getComponentDirsArray(viteConfig: SourceFile): ArrayLiteralExpression | null {
165
+ const pluginCall = findDescendant(viteConfig, {
166
+ guard: Node.isCallExpression,
167
+ validate: (callExpression) => callExpression.getText().startsWith('Components('),
168
+ skip: SyntaxKind.ImportDeclaration,
169
+ });
170
+
171
+ if (!pluginCall) {
172
+ return null;
173
+ }
174
+
175
+ const dirsAssignment = findDescendant(pluginCall, {
176
+ guard: Node.isPropertyAssignment,
177
+ validate: (propertyAssignment) => propertyAssignment.getName() === 'dirs',
178
+ });
179
+
180
+ const dirsArray = dirsAssignment?.getInitializer();
181
+
182
+ if (!Node.isArrayLiteralExpression(dirsArray)) {
183
+ return this.declareComponentDirsArray(pluginCall);
66
184
  }
185
+
186
+ return dirsArray;
187
+ }
188
+
189
+ protected declareComponentDirsArray(pluginCall: CallExpression): ArrayLiteralExpression | null {
190
+ const pluginOptions = findDescendant(pluginCall, { guard: Node.isObjectLiteralExpression });
191
+ const dirsAssignment = pluginOptions?.addPropertyAssignment({
192
+ name: 'dirs',
193
+ initializer: '[]',
194
+ });
195
+
196
+ return (dirsAssignment?.getInitializer() as ArrayLiteralExpression) ?? null;
67
197
  }
68
198
 
69
199
  protected parsePathComponents(): [string, string] {
@@ -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
@@ -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 {
@@ -13,10 +13,10 @@ interface Options {
13
13
 
14
14
  export class GenerateModelCommand extends Command {
15
15
 
16
- public static command: string = 'generate:model';
17
- public static description: string = 'Generate an AerogelJS model';
18
- public static parameters: [string, string][] = [['name', 'Model name']];
19
- public static options: CommandOptions = {
16
+ protected static command: string = 'generate:model';
17
+ protected static description: string = 'Generate an AerogelJS Model';
18
+ protected static parameters: [string, string][] = [['name', 'Model name']];
19
+ protected static options: CommandOptions = {
20
20
  fields: 'Create model with the given fields',
21
21
  };
22
22
 
@@ -30,7 +30,7 @@ export class GenerateModelCommand extends Command {
30
30
  this.options = options;
31
31
  }
32
32
 
33
- public async run(): Promise<void> {
33
+ protected async run(): Promise<void> {
34
34
  this.assertAerogelOrDirectory('src/models');
35
35
 
36
36
  if (File.exists(`src/models/${this.name}.ts`)) {
@@ -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
  }
@@ -87,13 +89,8 @@ export class GenerateModelCommand extends Command {
87
89
  protected assertSoukaiInstalled(): void {
88
90
  if (!File.contains('package.json', '"soukai"') && !File.contains('package.json', '"@aerogel/plugin-soukai"')) {
89
91
  Log.fail(`
90
- Soukai is not installed yet! You can install it doing the following:
91
-
92
- 1. Run the following command:
93
- npm install soukai @aerogel/plugin-soukai@next
94
-
95
- 2. Add this to your plugins array in src/main.ts:
96
- soukai({ models: import.meta.glob('@/models/*', { eager: true }) })
92
+ Soukai is not installed yet! You can install it running:
93
+ npx gel install soukai
97
94
  `);
98
95
  }
99
96
  }
@@ -0,0 +1,85 @@
1
+ import { arrayFrom } from '@noeldemartin/utils';
2
+
3
+ import Command from '@/commands/Command';
4
+ import File from '@/lib/File';
5
+ import Log from '@/lib/Log';
6
+ import Template from '@/lib/Template';
7
+ import { templatePath } from '@/lib/utils/paths';
8
+ import type { CommandOptions } from '@/commands/Command';
9
+
10
+ export interface Options {
11
+ story?: boolean;
12
+ }
13
+
14
+ export class GenerateOverridesCommand extends Command {
15
+
16
+ protected static command: string = 'generate:overrides';
17
+ protected static description: string = 'Generate AerogelJS component overrides';
18
+
19
+ protected static options: CommandOptions = {
20
+ story: {
21
+ description: 'Create overrides story using Histoire',
22
+ type: 'boolean',
23
+ },
24
+ };
25
+
26
+ private options: Options;
27
+
28
+ constructor(options: Options = {}) {
29
+ super();
30
+
31
+ this.options = options;
32
+ }
33
+
34
+ protected async run(): Promise<void> {
35
+ this.assertAerogelOrDirectory('src/components');
36
+ this.assertHistoireInstalled();
37
+
38
+ const files = new Set<string>();
39
+
40
+ await this.createComponents(files);
41
+ await this.createStory(files);
42
+
43
+ const filesList = arrayFrom(files)
44
+ .map((file) => `- ${file}`)
45
+ .join('\n');
46
+
47
+ Log.info(`Overrides created successfully! The following files were created:\n\n${filesList}`);
48
+ Log.info('\nRemember to declare your components in main.ts and main.histoire.ts!');
49
+ }
50
+
51
+ protected assertHistoireInstalled(): void {
52
+ if (!this.options.story) {
53
+ return;
54
+ }
55
+
56
+ if (!File.contains('package.json', '"histoire"') && !File.contains('package.json', '"@aerogel/histoire"')) {
57
+ Log.fail(`
58
+ Histoire is not installed yet! You can install it running:
59
+ npx gel install histoire
60
+ `);
61
+ }
62
+ }
63
+
64
+ protected async createComponents(files: Set<string>): Promise<void> {
65
+ await Log.animate('Creating components', async () => {
66
+ if (File.exists('src/components/ModalWrapper.vue')) {
67
+ Log.fail('ModalWrapper component already exists!');
68
+ }
69
+
70
+ Template.instantiate(templatePath('overrides'), 'src').forEach((file) => files.add(file));
71
+ });
72
+ }
73
+
74
+ protected async createStory(files: Set<string>): Promise<void> {
75
+ if (!this.options.story) {
76
+ return;
77
+ }
78
+
79
+ await Log.animate('Creating story', async () => {
80
+ Template.instantiate(templatePath('overrides-story'), 'src/components/overrides/').forEach((file) =>
81
+ files.add(file));
82
+ });
83
+ }
84
+
85
+ }
@@ -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,151 @@
1
+ import { arrayFrom, formatCodeBlock, 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 type { Editor } from '@/lib/Editor';
13
+
14
+ export class GenerateServiceCommand extends Command {
15
+
16
+ protected static command: string = 'generate:service';
17
+ protected static description: string = 'Generate an AerogelJS Service';
18
+ protected static parameters: [string, string][] = [['name', 'Service name']];
19
+
20
+ private name: string;
21
+
22
+ constructor(name: string) {
23
+ super();
24
+
25
+ this.name = name;
26
+ }
27
+
28
+ protected async run(): Promise<void> {
29
+ this.assertAerogelOrDirectory('src/services');
30
+
31
+ const files = new Set<string>();
32
+ const editor = app().edit();
33
+
34
+ await this.createService(files);
35
+
36
+ if (editFiles()) {
37
+ await this.registerService(editor);
38
+ await editor.format();
39
+ }
40
+
41
+ const filesList = arrayFrom(files)
42
+ .map((file) => `- ${file}`)
43
+ .join('\n');
44
+
45
+ Log.info(`${this.name} service created successfully! The following files were created:\n\n${filesList}`);
46
+ }
47
+
48
+ protected async createService(files: Set<string>): Promise<void> {
49
+ await Log.animate('Creating service', async () => {
50
+ if (File.exists(`src/services/${this.name}.ts`)) {
51
+ Log.fail(`${this.name} service already exists!`);
52
+ }
53
+
54
+ const serviceFiles = Template.instantiate(templatePath('service'), 'src/services', {
55
+ service: {
56
+ name: this.name,
57
+ },
58
+ });
59
+
60
+ serviceFiles.forEach((file) => files.add(file));
61
+ });
62
+ }
63
+
64
+ protected async registerService(editor: Editor): Promise<void> {
65
+ await Log.animate('Registering service', async () => {
66
+ if (!File.exists('src/services/index.ts')) {
67
+ await this.createServicesIndex(editor);
68
+ }
69
+
70
+ const servicesIndex = editor.requireSourceFile('src/services/index.ts');
71
+ const servicesObject = this.getServicesObject(servicesIndex);
72
+
73
+ if (!servicesObject) {
74
+ return Log.fail('Could not find services object in services config, please add it manually.');
75
+ }
76
+
77
+ servicesIndex.addImportDeclaration({
78
+ defaultImport: this.name,
79
+ moduleSpecifier: `./${this.name}`,
80
+ });
81
+ servicesObject.addPropertyAssignment({
82
+ name: `$${stringToCamelCase(this.name)}`,
83
+ initializer: this.name,
84
+ });
85
+
86
+ await editor.save(servicesIndex);
87
+ });
88
+ }
89
+
90
+ protected async createServicesIndex(editor: Editor): Promise<void> {
91
+ File.write(
92
+ 'src/services/index.ts',
93
+ formatCodeBlock(`
94
+ export const services = {};
95
+
96
+ export type AppServices = typeof services;
97
+
98
+ declare module '@vue/runtime-core' {
99
+ interface ComponentCustomProperties extends AppServices {}
100
+ }
101
+ `),
102
+ );
103
+
104
+ editor.addSourceFile('src/services/index.ts');
105
+
106
+ const mainConfig = editor.requireSourceFile('src/main.ts');
107
+ const bootstrapOptions = this.getBootstrapOptions(mainConfig);
108
+
109
+ if (!bootstrapOptions) {
110
+ return Log.fail('Could not find options object in bootstrap config, please add the services manually.');
111
+ }
112
+
113
+ bootstrapOptions.insertShorthandPropertyAssignment(0, { name: 'services' });
114
+ mainConfig.addImportDeclaration({
115
+ namedImports: ['services'],
116
+ moduleSpecifier: './services',
117
+ });
118
+
119
+ await editor.save(mainConfig);
120
+ }
121
+
122
+ protected getBootstrapOptions(mainConfig: SourceFile): ObjectLiteralExpression | null {
123
+ const bootstrapAppCall = findDescendant(mainConfig, {
124
+ guard: Node.isCallExpression,
125
+ validate: (callExpression) => callExpression.getExpression().getText() === 'bootstrap',
126
+ skip: SyntaxKind.ImportDeclaration,
127
+ });
128
+ const bootstrapOptions = bootstrapAppCall?.getArguments()[1];
129
+
130
+ if (!Node.isObjectLiteralExpression(bootstrapOptions)) {
131
+ return null;
132
+ }
133
+
134
+ return bootstrapOptions;
135
+ }
136
+
137
+ protected getServicesObject(servicesIndex: SourceFile): ObjectLiteralExpression | null {
138
+ const servicesDeclaration = findDescendant(servicesIndex, {
139
+ guard: Node.isVariableDeclaration,
140
+ validate: (variableDeclaration) => variableDeclaration.getName() === 'services',
141
+ });
142
+ const servicesObject = servicesDeclaration?.getInitializer();
143
+
144
+ if (!Node.isObjectLiteralExpression(servicesObject)) {
145
+ return null;
146
+ }
147
+
148
+ return servicesObject;
149
+ }
150
+
151
+ }
@@ -0,0 +1,41 @@
1
+ import { describe, it } from 'vitest';
2
+
3
+ import FileMock from '@/lib/File.mock';
4
+ import ShellMock from '@/lib/Shell.mock';
5
+
6
+ import { InstallCommand } from './install';
7
+
8
+ describe('Install plugin command', () => {
9
+
10
+ it('installs solid', async () => {
11
+ // Act
12
+ await InstallCommand.run('solid');
13
+
14
+ // Assert
15
+ ShellMock.expectRan('npm install soukai-solid@next --save-exact');
16
+ ShellMock.expectRan('npm install @aerogel/plugin-solid@next --save-exact');
17
+ });
18
+
19
+ it('installs soukai', async () => {
20
+ // Act
21
+ await InstallCommand.run('soukai');
22
+
23
+ // Assert
24
+ ShellMock.expectRan('npm install soukai@next --save-exact');
25
+ ShellMock.expectRan('npm install @aerogel/plugin-soukai@next --save-exact');
26
+ });
27
+
28
+ it('installs histoire', async () => {
29
+ // Arrange
30
+ FileMock.stub('package.json', '"@aerogel/core"');
31
+
32
+ // Act
33
+ await InstallCommand.run('histoire');
34
+
35
+ // Assert
36
+ ShellMock.expectRan('npm install histoire@0.17.6 --save-dev');
37
+ ShellMock.expectRan('npm install @aerogel/histoire@next --save-exact --save-dev');
38
+ ShellMock.expectRan('npm install patch-package --save-dev');
39
+ });
40
+
41
+ });
@@ -0,0 +1,33 @@
1
+ import Command from '@/commands/Command';
2
+ import Log from '@/lib/Log';
3
+ import { Histoire } from '@/plugins/Histoire';
4
+ import { Solid } from '@/plugins/Solid';
5
+ import { Soukai } from '@/plugins/Soukai';
6
+ import type Plugin from '@/plugins/Plugin';
7
+
8
+ const plugins = [new Soukai(), new Solid(), new Histoire()].reduce(
9
+ (pluginsObject, plugin) => Object.assign(pluginsObject, { [plugin.name]: plugin }),
10
+ {} as Record<string, Plugin>,
11
+ );
12
+
13
+ export class InstallCommand extends Command {
14
+
15
+ protected static command: string = 'install';
16
+ protected static description: string = 'Install an AerogelJS plugin';
17
+ protected static parameters: [string, string][] = [['plugin', 'Plugin to install']];
18
+
19
+ private plugin: Plugin;
20
+
21
+ constructor(plugin: string) {
22
+ super();
23
+
24
+ this.plugin =
25
+ plugins[plugin] ??
26
+ Log.fail(`Plugin '${plugin}' doesn't exist. Available plugins: ${Object.keys(plugins).join(', ')}`);
27
+ }
28
+
29
+ protected async run(): Promise<void> {
30
+ await this.plugin.install();
31
+ }
32
+
33
+ }