@aerogel/cli 0.0.0-next.926bde19326fe7b6b24b277666936862b64d8295 → 0.0.0-next.b58141fee5d2fe7d25debdbca6b1d2bf1c13e48e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/aerogel-cli.cjs.js +1 -1
- package/dist/aerogel-cli.cjs.js.map +1 -1
- package/dist/aerogel-cli.esm.js +1 -1
- package/dist/aerogel-cli.esm.js.map +1 -1
- package/package.json +4 -3
- package/src/cli.ts +4 -0
- package/src/commands/create.test.ts +22 -4
- package/src/commands/create.ts +21 -4
- package/src/commands/generate-component.test.ts +12 -1
- package/src/commands/generate-component.ts +127 -20
- package/src/commands/generate-model.test.ts +7 -4
- package/src/commands/generate-model.ts +30 -20
- package/src/commands/generate-service.test.ts +21 -0
- package/src/commands/generate-service.ts +152 -0
- package/src/commands/install.test.ts +18 -0
- package/src/commands/install.ts +32 -0
- package/src/lib/App.ts +65 -3
- package/src/lib/Editor.ts +56 -0
- package/src/lib/File.ts +6 -0
- package/src/lib/Log.mock.ts +2 -1
- package/src/lib/Log.ts +6 -4
- package/src/lib/utils/app.ts +15 -0
- package/src/lib/utils/edit.ts +44 -0
- package/src/lib/{utils.test.ts → utils/format.test.ts} +2 -2
- package/src/lib/{utils.ts → utils/format.ts} +0 -6
- package/src/lib/utils/paths.ts +34 -0
- package/src/plugins/Plugin.ts +125 -0
- package/src/plugins/Solid.ts +114 -0
- package/src/plugins/Soukai.ts +19 -0
- package/src/testing/setup.ts +36 -6
- package/templates/app/.eslintrc.js +3 -0
- package/templates/app/.github/workflows/ci.yml +14 -2
- package/templates/app/.vscode/launch.json +16 -0
- package/templates/app/.vscode/settings.json +10 -0
- package/templates/app/README.md +3 -0
- package/templates/app/cypress.config.ts +8 -0
- package/templates/app/index.html +1 -1
- package/templates/app/package.json +20 -7
- package/templates/app/prettier.config.js +5 -0
- package/templates/app/src/App.vue +3 -1
- package/templates/app/src/main.ts +5 -1
- package/templates/app/src/types/globals.d.ts +0 -1
- package/templates/app/tailwind.config.js +1 -1
- package/templates/app/tsconfig.json +1 -0
- package/templates/app/vite.config.ts +12 -5
- package/templates/service/[service.name].ts +8 -0
- package/noeldemartin.config.js +0 -4
|
@@ -7,20 +7,38 @@ import { CreateCommand } from './create';
|
|
|
7
7
|
|
|
8
8
|
describe('Create command', () => {
|
|
9
9
|
|
|
10
|
-
it('
|
|
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/package.json').toContain('"@aerogel/core": "next"');
|
|
17
18
|
FileMock.expectCreated('./app/index.html').toContain('My App');
|
|
18
|
-
FileMock.expectCreated('./app/
|
|
19
|
-
|
|
20
|
-
);
|
|
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
|
+
FileMock.expectCreated('./my-app/index.html').toContain('My App');
|
|
42
|
+
});
|
|
43
|
+
|
|
26
44
|
});
|
package/src/commands/create.ts
CHANGED
|
@@ -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:
|
|
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 ??
|
|
45
|
+
const name = this.options.name ?? stringToTitleCase(basename(path));
|
|
32
46
|
|
|
33
47
|
Shell.setWorkingDirectory(path);
|
|
34
48
|
|
|
@@ -50,7 +64,10 @@ export class CreateCommand extends Command {
|
|
|
50
64
|
protected async createApp(name: string, path: string): Promise<void> {
|
|
51
65
|
Log.info(`Creating **${name}**...`);
|
|
52
66
|
|
|
53
|
-
new App(name
|
|
67
|
+
new App(name, {
|
|
68
|
+
local: this.options.local,
|
|
69
|
+
linkedLocal: this.options.local && !this.options.copy,
|
|
70
|
+
}).create(path);
|
|
54
71
|
}
|
|
55
72
|
|
|
56
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
|
|
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 {
|
|
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
|
|
16
|
-
public static parameters: [string, string][] = [
|
|
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
|
|
33
|
+
private path: string;
|
|
25
34
|
private options: Options;
|
|
26
35
|
|
|
27
|
-
constructor(
|
|
36
|
+
constructor(path: string, options: Options = {}) {
|
|
28
37
|
super();
|
|
29
38
|
|
|
30
|
-
this.
|
|
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.
|
|
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(
|
|
39
|
-
Log.fail(
|
|
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
|
-
|
|
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
|
-
|
|
47
|
-
const storyFiles = Template.instantiate(
|
|
48
|
-
component: { 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.
|
|
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
|
|
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
|
-
|
|
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
|
|
60
|
-
|
|
61
|
-
|
|
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,11 +1,11 @@
|
|
|
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
|
|
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:
|
|
33
|
+
name: {
|
|
34
|
+
type: FieldType.String,
|
|
35
|
+
required: true,
|
|
36
|
+
},
|
|
34
37
|
age: FieldType.Number,
|
|
35
38
|
birth: FieldType.Date,
|
|
36
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 {
|
|
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
|
|
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',
|
|
@@ -39,15 +40,17 @@ export class GenerateModelCommand extends Command {
|
|
|
39
40
|
|
|
40
41
|
this.assertSoukaiInstalled();
|
|
41
42
|
|
|
42
|
-
const
|
|
43
|
-
model
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
+
});
|
|
49
51
|
|
|
50
|
-
|
|
52
|
+
return files.map((file) => `- ${file}`).join('\n');
|
|
53
|
+
});
|
|
51
54
|
|
|
52
55
|
Log.info(`${this.name} model created successfully! The following files were created:\n\n${filesList}`);
|
|
53
56
|
}
|
|
@@ -60,28 +63,35 @@ export class GenerateModelCommand extends Command {
|
|
|
60
63
|
const code = this.options.fields
|
|
61
64
|
.split(',')
|
|
62
65
|
.map((field) => {
|
|
63
|
-
const [name, type] = field.split(':');
|
|
66
|
+
const [name, type, rules] = field.split(':');
|
|
64
67
|
|
|
65
68
|
return {
|
|
66
69
|
name,
|
|
67
70
|
type: stringToStudlyCase(type ?? 'string'),
|
|
71
|
+
required: rules === 'required',
|
|
68
72
|
};
|
|
69
73
|
})
|
|
70
|
-
.reduce((definition, field) =>
|
|
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
|
+
}, '');
|
|
71
86
|
|
|
72
87
|
return formatCodeBlock(code, { indent: 8 });
|
|
73
88
|
}
|
|
74
89
|
|
|
75
90
|
protected assertSoukaiInstalled(): void {
|
|
76
|
-
if (!File.contains('package.json', '"soukai"')) {
|
|
91
|
+
if (!File.contains('package.json', '"soukai"') && !File.contains('package.json', '"@aerogel/plugin-soukai"')) {
|
|
77
92
|
Log.fail(`
|
|
78
|
-
Soukai is not installed yet! You can install it
|
|
79
|
-
|
|
80
|
-
1. Run the following command:
|
|
81
|
-
npm install soukai @aerogel/plugin-soukai"
|
|
82
|
-
|
|
83
|
-
2. Add this to your plugins array:
|
|
84
|
-
soukai({ models: import.meta.glob('@/models/*', { eager: true }) })
|
|
93
|
+
Soukai is not installed yet! You can install it running:
|
|
94
|
+
npx ag install soukai
|
|
85
95
|
`);
|
|
86
96
|
}
|
|
87
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
|
+
});
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import Command from '@/commands/Command';
|
|
2
|
+
import Log from '@/lib/Log';
|
|
3
|
+
import { Solid } from '@/plugins/Solid';
|
|
4
|
+
import { Soukai } from '@/plugins/Soukai';
|
|
5
|
+
import type Plugin from '@/plugins/Plugin';
|
|
6
|
+
|
|
7
|
+
const plugins = [new Soukai(), new Solid()].reduce(
|
|
8
|
+
(pluginsObject, plugin) => Object.assign(pluginsObject, { [plugin.name]: plugin }),
|
|
9
|
+
{} as Record<string, Plugin>,
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
export class InstallCommand extends Command {
|
|
13
|
+
|
|
14
|
+
public static command: string = 'install';
|
|
15
|
+
public static description: string = 'Install an AerogelJS plugin';
|
|
16
|
+
public static parameters: [string, string][] = [['plugin', 'Plugin to install']];
|
|
17
|
+
|
|
18
|
+
private plugin: Plugin;
|
|
19
|
+
|
|
20
|
+
constructor(plugin: string) {
|
|
21
|
+
super();
|
|
22
|
+
|
|
23
|
+
this.plugin =
|
|
24
|
+
plugins[plugin] ??
|
|
25
|
+
Log.fail(`Plugin '${plugin}' doesn't exist. Available plugins: ${Object.keys(plugins).join(', ')}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public async run(): Promise<void> {
|
|
29
|
+
await this.plugin.install();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
}
|