@aerogel/cli 0.0.0 → 0.1.0

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 (72) hide show
  1. package/bin/gel +4 -0
  2. package/dist/aerogel-cli.d.ts +2 -2
  3. package/dist/aerogel-cli.js +790 -0
  4. package/dist/aerogel-cli.js.map +1 -0
  5. package/package.json +28 -33
  6. package/src/cli.ts +28 -6
  7. package/src/commands/Command.ts +14 -9
  8. package/src/commands/create.test.ts +4 -9
  9. package/src/commands/create.ts +40 -26
  10. package/src/commands/generate-component.test.ts +5 -7
  11. package/src/commands/generate-component.ts +114 -37
  12. package/src/commands/generate-model.test.ts +17 -6
  13. package/src/commands/generate-model.ts +47 -22
  14. package/src/commands/generate-service.test.ts +21 -0
  15. package/src/commands/generate-service.ts +151 -0
  16. package/src/commands/info.ts +15 -0
  17. package/src/commands/install.test.ts +90 -0
  18. package/src/commands/install.ts +47 -0
  19. package/src/lib/App.ts +76 -12
  20. package/src/lib/Editor.ts +57 -0
  21. package/src/lib/File.mock.ts +13 -11
  22. package/src/lib/File.ts +43 -3
  23. package/src/lib/Log.mock.ts +11 -6
  24. package/src/lib/Log.test.ts +22 -6
  25. package/src/lib/Log.ts +43 -27
  26. package/src/lib/Shell.mock.ts +7 -3
  27. package/src/lib/Shell.ts +2 -2
  28. package/src/lib/Template.ts +27 -19
  29. package/src/lib/utils/app.ts +15 -0
  30. package/src/lib/utils/edit.ts +44 -0
  31. package/src/lib/utils/paths.ts +46 -0
  32. package/src/plugins/LocalFirst.ts +9 -0
  33. package/src/plugins/Plugin.ts +176 -0
  34. package/src/plugins/Solid.ts +72 -0
  35. package/src/plugins/Soukai.ts +20 -0
  36. package/src/testing/setup.ts +62 -25
  37. package/src/utils/package.ts +23 -0
  38. package/templates/app/README.md +3 -0
  39. package/templates/service/[service.name].ts +8 -0
  40. package/.eslintrc.js +0 -7
  41. package/bin/ag +0 -4
  42. package/dist/aerogel-cli.cjs.js +0 -2
  43. package/dist/aerogel-cli.cjs.js.map +0 -1
  44. package/dist/aerogel-cli.esm.js +0 -2
  45. package/dist/aerogel-cli.esm.js.map +0 -1
  46. package/noeldemartin.config.js +0 -4
  47. package/src/lib/utils.test.ts +0 -33
  48. package/src/lib/utils.ts +0 -44
  49. package/templates/app/.github/workflows/ci.yml +0 -17
  50. package/templates/app/.nvmrc +0 -1
  51. package/templates/app/cypress/e2e/app.cy.ts +0 -9
  52. package/templates/app/cypress/support/e2e.ts +0 -3
  53. package/templates/app/cypress/tsconfig.json +0 -12
  54. package/templates/app/cypress.config.ts +0 -8
  55. package/templates/app/index.html +0 -12
  56. package/templates/app/package.json +0 -44
  57. package/templates/app/postcss.config.js +0 -6
  58. package/templates/app/src/App.vue +0 -10
  59. package/templates/app/src/assets/styles.css +0 -3
  60. package/templates/app/src/lang/en.yaml +0 -3
  61. package/templates/app/src/main.test.ts +0 -9
  62. package/templates/app/src/main.ts +0 -9
  63. package/templates/app/src/types/globals.d.ts +0 -3
  64. package/templates/app/src/types/shims.d.ts +0 -7
  65. package/templates/app/src/types/ts-reset.d.ts +0 -1
  66. package/templates/app/tailwind.config.js +0 -5
  67. package/templates/app/tsconfig.json +0 -18
  68. package/templates/app/vite.config.ts +0 -21
  69. package/templates/component-story/[component.name].story.vue +0 -7
  70. package/tsconfig.json +0 -11
  71. package/vite.config.ts +0 -14
  72. /package/src/{main.ts → index.ts} +0 -0
@@ -1,65 +1,142 @@
1
- import Command from '@/commands/Command';
2
- import File from '@/lib/File';
3
- import Log from '@/lib/Log';
4
- import Template from '@/lib/Template';
5
- import { basePath } from '@/lib/utils';
6
- import type { CommandOptions } from '@/commands/Command';
1
+ import { arrayFrom, stringToSlug } from '@noeldemartin/utils';
2
+ import { Node, SyntaxKind } from 'ts-morph';
3
+ import type { ArrayLiteralExpression, CallExpression, SourceFile } from 'ts-morph';
4
+
5
+ import Command from '@aerogel/cli/commands/Command';
6
+ import File from '@aerogel/cli/lib/File';
7
+ import Log from '@aerogel/cli/lib/Log';
8
+ import Template from '@aerogel/cli/lib/Template';
9
+ import { app } from '@aerogel/cli/lib/utils/app';
10
+ import { editFiles, findDescendant } from '@aerogel/cli/lib/utils/edit';
11
+ import { templatePath } from '@aerogel/cli/lib/utils/paths';
7
12
 
8
13
  export interface Options {
14
+ button?: boolean;
15
+ checkbox?: 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][] = [['name', 'Component name']];
17
- public static options: CommandOptions = {
18
- story: {
19
- description: 'Create component story using Histoire',
20
- type: 'boolean',
21
- },
22
- };
22
+ protected static override command: string = 'generate:component';
23
+ protected static override description: string = 'Generate an AerogelJS Component';
24
+ protected static override parameters: [string, string][] = [
25
+ ['path', 'Component path (relative to components folder; extension not necessary)'],
26
+ ];
23
27
 
24
- private name: string;
25
- private options: Options;
28
+ private path: string;
26
29
 
27
- constructor(name: string, options: Options = {}) {
30
+ constructor(path: string) {
28
31
  super();
29
32
 
30
- this.name = name;
31
- this.options = options;
33
+ this.path = path;
32
34
  }
33
35
 
34
- public async run(): Promise<void> {
36
+ protected override async run(): Promise<void> {
35
37
  this.assertAerogelOrDirectory('src/components');
36
- this.options.story && this.assertHistoireInstalled();
37
38
 
38
- if (File.exists(`src/components/${this.name}.vue`)) {
39
- Log.fail(`${this.name} component already exists!`);
40
- }
39
+ const files = new Set<string>();
40
+ const [directoryName, componentName] = this.parsePathComponents();
41
41
 
42
- const files = Template.instantiate(basePath('templates/component'), 'src/components', {
43
- component: { name: this.name },
44
- });
42
+ await this.createComponent(directoryName, componentName, files);
43
+ await this.declareComponents();
44
+
45
+ const filesList = arrayFrom(files)
46
+ .map((file) => `- ${file}`)
47
+ .join('\n');
45
48
 
46
- if (this.options.story) {
47
- const storyFiles = Template.instantiate(basePath('templates/component-story'), 'src/components', {
48
- component: { name: this.name },
49
+ Log.info(`${componentName} component created successfully! The following files were created:\n\n${filesList}`);
50
+ }
51
+
52
+ protected async createComponent(directoryName: string, componentName: string, files: Set<string>): Promise<void> {
53
+ await Log.animate('Creating component', async () => {
54
+ if (File.exists(`src/components/${this.path}.vue`)) {
55
+ Log.fail(`${this.path} component already exists!`);
56
+ }
57
+
58
+ const componentFiles = Template.instantiate(templatePath('component'), `src/components/${directoryName}`, {
59
+ component: {
60
+ name: componentName,
61
+ slug: stringToSlug(componentName),
62
+ },
49
63
  });
50
64
 
51
- files.push(...storyFiles);
65
+ componentFiles.forEach((file) => files.add(file));
66
+ });
67
+ }
68
+
69
+ protected async declareComponents(): Promise<void> {
70
+ if (!editFiles()) {
71
+ return;
72
+ }
73
+
74
+ const editor = app().edit();
75
+ const viteConfig = editor.requireSourceFile('vite.config.ts');
76
+ const componentDirsArray = this.getComponentDirsArray(viteConfig);
77
+
78
+ if (!componentDirsArray) {
79
+ return Log.fail('Could not find component dirs declaration in vite config!');
80
+ }
81
+
82
+ if (
83
+ componentDirsArray
84
+ .getDescendantsOfKind(SyntaxKind.StringLiteral)
85
+ .some((literal) => literal.getText() === '\'src/components\'')
86
+ ) {
87
+ return;
52
88
  }
53
89
 
54
- const filesList = files.map((file) => `- ${file}`).join('\n');
90
+ await Log.animate('Updating vite config', async () => {
91
+ componentDirsArray.addElement('\'src/components\'');
92
+
93
+ await editor.save(viteConfig);
94
+ });
55
95
 
56
- Log.info(`${this.name} component created successfully! The following files were created:\n\n${filesList}`);
96
+ await editor.format();
57
97
  }
58
98
 
59
- protected assertHistoireInstalled(): void {
60
- if (!File.exists('src/main.histoire.ts')) {
61
- Log.fail('Histoire is not installed yet!');
99
+ protected getComponentDirsArray(viteConfig: SourceFile): ArrayLiteralExpression | null {
100
+ const pluginCall = findDescendant(viteConfig, {
101
+ guard: Node.isCallExpression,
102
+ validate: (callExpression) => callExpression.getText().startsWith('Components('),
103
+ skip: SyntaxKind.ImportDeclaration,
104
+ });
105
+
106
+ if (!pluginCall) {
107
+ return null;
108
+ }
109
+
110
+ const dirsAssignment = findDescendant(pluginCall, {
111
+ guard: Node.isPropertyAssignment,
112
+ validate: (propertyAssignment) => propertyAssignment.getName() === 'dirs',
113
+ });
114
+
115
+ const dirsArray = dirsAssignment?.getInitializer();
116
+
117
+ if (!Node.isArrayLiteralExpression(dirsArray)) {
118
+ return this.declareComponentDirsArray(pluginCall);
62
119
  }
120
+
121
+ return dirsArray;
122
+ }
123
+
124
+ protected declareComponentDirsArray(pluginCall: CallExpression): ArrayLiteralExpression | null {
125
+ const pluginOptions = findDescendant(pluginCall, { guard: Node.isObjectLiteralExpression });
126
+ const dirsAssignment = pluginOptions?.addPropertyAssignment({
127
+ name: 'dirs',
128
+ initializer: '[]',
129
+ });
130
+
131
+ return (dirsAssignment?.getInitializer() as ArrayLiteralExpression) ?? null;
132
+ }
133
+
134
+ protected parsePathComponents(): [string, string] {
135
+ const lastSlashIndex = this.path.lastIndexOf('/');
136
+
137
+ return lastSlashIndex === -1
138
+ ? ['', this.path]
139
+ : [this.path.substring(0, lastSlashIndex), this.path.substring(lastSlashIndex + 1)];
63
140
  }
64
141
 
65
142
  }
@@ -1,19 +1,27 @@
1
1
  import { describe, it } from 'vitest';
2
+ import { formatCodeBlock } from '@noeldemartin/utils';
2
3
 
3
- import FileMock from '@/lib/File.mock';
4
- import { formatCodeBlock } from '@/lib/utils';
4
+ import FileMock from '@aerogel/cli/lib/File.mock';
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
  },
@@ -1,11 +1,11 @@
1
- import { stringToStudlyCase } from '@noeldemartin/utils';
1
+ import { formatCodeBlock, stringToStudlyCase } from '@noeldemartin/utils';
2
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 { basePath, formatCodeBlock } from '@/lib/utils';
8
- import type { CommandOptions } from '@/commands/Command';
3
+ import Command from '@aerogel/cli/commands/Command';
4
+ import File from '@aerogel/cli/lib/File';
5
+ import Log from '@aerogel/cli/lib/Log';
6
+ import Template from '@aerogel/cli/lib/Template';
7
+ import { templatePath } from '@aerogel/cli/lib/utils/paths';
8
+ import type { CommandOptions } from '@aerogel/cli/commands/Command';
9
9
 
10
10
  interface Options {
11
11
  fields?: string;
@@ -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 override command: string = 'generate:model';
17
+ protected static override description: string = 'Generate an AerogelJS Model';
18
+ protected static override parameters: [string, string][] = [['name', 'Model name']];
19
+ protected static override options: CommandOptions = {
20
20
  fields: 'Create model with the given fields',
21
21
  };
22
22
 
@@ -30,22 +30,26 @@ export class GenerateModelCommand extends Command {
30
30
  this.options = options;
31
31
  }
32
32
 
33
- public async run(): Promise<void> {
33
+ protected override async run(): Promise<void> {
34
34
  this.assertAerogelOrDirectory('src/models');
35
35
 
36
36
  if (File.exists(`src/models/${this.name}.ts`)) {
37
37
  Log.fail(`${this.name} model already exists!`);
38
38
  }
39
39
 
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
- });
40
+ this.assertSoukaiInstalled();
41
+
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
+ });
47
50
 
48
- const filesList = files.map((file) => `- ${file}`).join('\n');
51
+ return files.map((file) => `- ${file}`).join('\n');
52
+ });
49
53
 
50
54
  Log.info(`${this.name} model created successfully! The following files were created:\n\n${filesList}`);
51
55
  }
@@ -58,16 +62,37 @@ export class GenerateModelCommand extends Command {
58
62
  const code = this.options.fields
59
63
  .split(',')
60
64
  .map((field) => {
61
- const [name, type] = field.split(':');
65
+ const [name, type, rules] = field.split(':');
62
66
 
63
67
  return {
64
68
  name,
65
69
  type: stringToStudlyCase(type ?? 'string'),
70
+ required: rules === 'required',
66
71
  };
67
72
  })
68
- .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
+ }, '');
69
85
 
70
86
  return formatCodeBlock(code, { indent: 8 });
71
87
  }
72
88
 
89
+ protected assertSoukaiInstalled(): void {
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 gel install soukai
94
+ `);
95
+ }
96
+ }
97
+
73
98
  }
@@ -0,0 +1,21 @@
1
+ import { describe, it } from 'vitest';
2
+
3
+ import FileMock from '@aerogel/cli/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(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 '@aerogel/cli/commands/Command';
6
+ import File from '@aerogel/cli/lib/File';
7
+ import Log from '@aerogel/cli/lib/Log';
8
+ import Template from '@aerogel/cli/lib/Template';
9
+ import { app } from '@aerogel/cli/lib/utils/app';
10
+ import { templatePath } from '@aerogel/cli/lib/utils/paths';
11
+ import { editFiles, findDescendant } from '@aerogel/cli/lib/utils/edit';
12
+ import type { Editor } from '@aerogel/cli/lib/Editor';
13
+
14
+ export class GenerateServiceCommand extends Command {
15
+
16
+ protected static override command: string = 'generate:service';
17
+ protected static override description: string = 'Generate an AerogelJS Service';
18
+ protected static override 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 override 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 '@aerogel/core' {
99
+ interface Services 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,15 @@
1
+ import { URL, fileURLToPath } from 'node:url';
2
+
3
+ import Command from '@aerogel/cli/commands/Command';
4
+ import Log from '@aerogel/cli/lib/Log';
5
+ export class InfoCommand extends Command {
6
+
7
+ protected static override command: string = 'info';
8
+ protected static override description: string = 'Show debugging information about the CLI';
9
+
10
+ protected override async run(): Promise<void> {
11
+ Log.info('[AerogelJS CLI info]');
12
+ Log.info('Installation directory: ' + fileURLToPath(new URL(/* @vite-ignore */ './', import.meta.url)));
13
+ }
14
+
15
+ }
@@ -0,0 +1,90 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import ShellMock from '@aerogel/cli/lib/Shell.mock';
4
+ import FileMock from '@aerogel/cli/lib/File.mock';
5
+
6
+ import { InstallCommand } from './install';
7
+
8
+ describe('Install plugin command', () => {
9
+
10
+ it('installs solid', async () => {
11
+ // Arrange
12
+ stubPackageJson();
13
+
14
+ // Act
15
+ await InstallCommand.run('solid');
16
+
17
+ // Assert
18
+ ShellMock.expectRan('pnpm install --no-save');
19
+
20
+ expectPackageJson({
21
+ dependencies: {
22
+ '@aerogel/core': 'next',
23
+ '@aerogel/plugin-solid': 'next',
24
+ '@noeldemartin/solid-utils': 'next',
25
+ 'soukai-solid': 'next',
26
+ 'vue': '^3.5.13',
27
+ },
28
+ });
29
+ });
30
+
31
+ it('installs soukai', async () => {
32
+ // Arrange
33
+ stubPackageJson();
34
+
35
+ // Act
36
+ await InstallCommand.run('soukai', { skipInstall: true });
37
+
38
+ // Assert
39
+ ShellMock.expectNotRan('pnpm install --no-save');
40
+
41
+ expectPackageJson({
42
+ dependencies: {
43
+ '@aerogel/core': 'next',
44
+ '@aerogel/plugin-soukai': 'next',
45
+ 'soukai': 'next',
46
+ 'vue': '^3.5.13',
47
+ },
48
+ });
49
+ });
50
+
51
+ it('installs local-first', async () => {
52
+ // Arrange
53
+ stubPackageJson();
54
+
55
+ // Act
56
+ await InstallCommand.run('local-first');
57
+
58
+ // Assert
59
+ ShellMock.expectRan('pnpm install --no-save');
60
+
61
+ expectPackageJson({
62
+ dependencies: {
63
+ '@aerogel/core': 'next',
64
+ '@aerogel/plugin-local-first': 'next',
65
+ 'vue': '^3.5.13',
66
+ },
67
+ });
68
+ });
69
+
70
+ });
71
+
72
+ function stubPackageJson(): void {
73
+ FileMock.stub(
74
+ 'package.json',
75
+ JSON.stringify(
76
+ {
77
+ dependencies: {
78
+ '@aerogel/core': 'next',
79
+ 'vue': '^3.5.13',
80
+ },
81
+ },
82
+ null,
83
+ 2,
84
+ ),
85
+ );
86
+ }
87
+
88
+ function expectPackageJson(packageJson: object): void {
89
+ expect(JSON.parse(FileMock.read('package.json') ?? '{}')).toEqual(packageJson);
90
+ }
@@ -0,0 +1,47 @@
1
+ import Command from '@aerogel/cli/commands/Command';
2
+ import LocalFirst from '@aerogel/cli/plugins/LocalFirst';
3
+ import Log from '@aerogel/cli/lib/Log';
4
+ import Solid from '@aerogel/cli/plugins/Solid';
5
+ import Soukai from '@aerogel/cli/plugins/Soukai';
6
+ import type Plugin from '@aerogel/cli/plugins/Plugin';
7
+ import type { CommandOptions } from '@aerogel/cli/commands/Command';
8
+
9
+ const plugins = [new Soukai(), new Solid(), new LocalFirst()].reduce(
10
+ (pluginsObject, plugin) => Object.assign(pluginsObject, { [plugin.name]: plugin }),
11
+ {} as Record<string, Plugin>,
12
+ );
13
+ export interface Options {
14
+ skipInstall?: boolean;
15
+ }
16
+
17
+ export class InstallCommand extends Command {
18
+
19
+ protected static override command: string = 'install';
20
+ protected static override description: string = 'Install an AerogelJS plugin';
21
+ protected static override parameters: [string, string][] = [['plugin', 'Plugin to install']];
22
+ protected static override options: CommandOptions = {
23
+ skipInstall: {
24
+ type: 'boolean',
25
+ description: 'Skip installing dependencies, just add them to package.json',
26
+ },
27
+ };
28
+
29
+ private options: Options;
30
+ private plugin: Plugin;
31
+
32
+ constructor(plugin: string, options: Options = {}) {
33
+ super();
34
+
35
+ this.options = options;
36
+ this.plugin =
37
+ plugins[plugin] ??
38
+ Log.fail(`Plugin '${plugin}' doesn't exist. Available plugins: ${Object.keys(plugins).join(', ')}`);
39
+ }
40
+
41
+ protected override async run(): Promise<void> {
42
+ await this.plugin.install({
43
+ skipInstall: this.options.skipInstall,
44
+ });
45
+ }
46
+
47
+ }