@aerogel/cli 0.0.0-next.c8f032a868370824898e171969aec1bb6827688e → 0.0.0-next.f16bd1d894543c5303039c49f6f33488a1ffe931
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.esm.js +1 -1
- package/package.json +3 -2
- package/src/cli.ts +4 -0
- package/src/commands/create.test.ts +13 -4
- package/src/commands/create.ts +25 -10
- package/src/commands/generate-component.test.ts +12 -1
- package/src/commands/generate-component.ts +127 -20
- package/src/commands/generate-model.test.ts +16 -5
- package/src/commands/generate-model.ts +38 -12
- 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 +51 -0
- package/src/lib/File.ts +10 -0
- package/src/lib/Log.mock.ts +13 -4
- package/src/lib/Log.test.ts +19 -3
- package/src/lib/Log.ts +36 -20
- package/src/lib/Template.ts +4 -3
- 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 +65 -0
- package/src/plugins/Soukai.ts +19 -0
- package/src/testing/setup.ts +38 -6
- package/templates/app/.eslintrc.js +3 -0
- package/templates/app/.gitignore.template +2 -0
- package/templates/app/.vscode/launch.json +16 -0
- package/templates/app/.vscode/settings.json +10 -0
- package/templates/app/cypress.config.ts +4 -0
- package/templates/app/index.html +1 -1
- package/templates/app/package.json +20 -10
- 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/vite.config.ts +8 -5
- package/templates/service/[service.name].ts +8 -0
- package/noeldemartin.config.js +0 -4
|
@@ -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',
|
|
@@ -37,15 +38,19 @@ export class GenerateModelCommand extends Command {
|
|
|
37
38
|
Log.fail(`${this.name} model already exists!`);
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
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) =>
|
|
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
|
+
});
|
|
@@ -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
|
+
}
|
package/src/lib/App.ts
CHANGED
|
@@ -3,23 +3,85 @@ import { stringToSlug } from '@noeldemartin/utils';
|
|
|
3
3
|
import File from '@/lib/File';
|
|
4
4
|
import Log from '@/lib/Log';
|
|
5
5
|
import Template from '@/lib/Template';
|
|
6
|
-
import {
|
|
6
|
+
import { packNotFound, packagePackPath, packagePath, templatePath } from '@/lib/utils/paths';
|
|
7
|
+
import { Editor } from '@/lib/Editor';
|
|
8
|
+
|
|
9
|
+
interface Dependencies {
|
|
10
|
+
aerogelCli: string;
|
|
11
|
+
aerogelCore: string;
|
|
12
|
+
aerogelCypress: string;
|
|
13
|
+
aerogelPluginI18n: string;
|
|
14
|
+
aerogelPluginSoukai: string;
|
|
15
|
+
aerogelVite: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Options {
|
|
19
|
+
local?: boolean;
|
|
20
|
+
linkedLocal?: boolean;
|
|
21
|
+
}
|
|
7
22
|
|
|
8
23
|
export default class App {
|
|
9
24
|
|
|
10
|
-
constructor(
|
|
25
|
+
constructor(protected name: string, protected options: Options = {}) {}
|
|
11
26
|
|
|
12
27
|
public create(path: string): void {
|
|
13
28
|
if (File.exists(path) && (!File.isDirectory(path) || !File.isEmptyDirectory(path))) {
|
|
14
29
|
Log.fail(`Folder at '${path}' already exists!`);
|
|
15
30
|
}
|
|
16
31
|
|
|
17
|
-
Template.instantiate(
|
|
32
|
+
Template.instantiate(templatePath('app'), path, {
|
|
18
33
|
app: {
|
|
19
34
|
name: this.name,
|
|
20
35
|
slug: stringToSlug(this.name),
|
|
21
36
|
},
|
|
37
|
+
dependencies: this.getDependencies(),
|
|
38
|
+
contentPath: this.options.linkedLocal
|
|
39
|
+
? `${packagePath('core')}/dist/**/*.js`
|
|
40
|
+
: './node_modules/@aerogel/core/dist/**/*.js',
|
|
22
41
|
});
|
|
23
42
|
}
|
|
24
43
|
|
|
44
|
+
public edit(): Editor {
|
|
45
|
+
return new Editor();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected getDependencies(): Dependencies {
|
|
49
|
+
const withFilePrefix = <T extends Record<string, string>>(paths: T) =>
|
|
50
|
+
Object.entries(paths).reduce(
|
|
51
|
+
(pathsWithFile, [name, path]) => Object.assign(pathsWithFile, { [name]: `file:${path}` }) as T,
|
|
52
|
+
{} as T,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
if (this.options.linkedLocal) {
|
|
56
|
+
return withFilePrefix({
|
|
57
|
+
aerogelCli: packagePath('cli'),
|
|
58
|
+
aerogelCore: packagePath('core'),
|
|
59
|
+
aerogelCypress: packagePath('cypress'),
|
|
60
|
+
aerogelPluginI18n: packagePath('plugin-i18n'),
|
|
61
|
+
aerogelPluginSoukai: packagePath('plugin-soukai'),
|
|
62
|
+
aerogelVite: packagePath('vite'),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (this.options.local) {
|
|
67
|
+
return withFilePrefix({
|
|
68
|
+
aerogelCli: packagePackPath('cli') ?? packNotFound('cli'),
|
|
69
|
+
aerogelCore: packagePackPath('core') ?? packNotFound('core'),
|
|
70
|
+
aerogelCypress: packagePackPath('cypress') ?? packNotFound('cypress'),
|
|
71
|
+
aerogelPluginI18n: packagePackPath('plugin-i18n') ?? packNotFound('plugin-i18n'),
|
|
72
|
+
aerogelPluginSoukai: packagePackPath('plugin-soukai') ?? packNotFound('plugin-soukai'),
|
|
73
|
+
aerogelVite: packagePackPath('vite') ?? packNotFound('vite'),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
aerogelCli: 'next',
|
|
79
|
+
aerogelCore: 'next',
|
|
80
|
+
aerogelCypress: 'next',
|
|
81
|
+
aerogelPluginI18n: 'next',
|
|
82
|
+
aerogelPluginSoukai: 'next',
|
|
83
|
+
aerogelVite: 'next',
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
25
87
|
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { arrayFrom } from '@noeldemartin/utils';
|
|
2
|
+
import { Project } from 'ts-morph';
|
|
3
|
+
import type { SourceFile } from 'ts-morph';
|
|
4
|
+
|
|
5
|
+
import File from '@/lib/File';
|
|
6
|
+
import Log from '@/lib/Log';
|
|
7
|
+
import Shell from '@/lib/Shell';
|
|
8
|
+
|
|
9
|
+
export class Editor {
|
|
10
|
+
|
|
11
|
+
private project: Project;
|
|
12
|
+
private modifiedFiles: Set<string>;
|
|
13
|
+
|
|
14
|
+
constructor() {
|
|
15
|
+
this.project = new Project({ tsConfigFilePath: 'tsconfig.json' });
|
|
16
|
+
this.modifiedFiles = new Set();
|
|
17
|
+
|
|
18
|
+
this.project.addSourceFilesAtPaths('src/**/*.ts');
|
|
19
|
+
this.project.addSourceFilesAtPaths('tailwind.config.js');
|
|
20
|
+
this.project.addSourceFilesAtPaths('vite.config.ts');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public addSourceFile(path: string): void {
|
|
24
|
+
this.project.addSourceFilesAtPaths(path);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public requireSourceFile(path: string): SourceFile {
|
|
28
|
+
return this.project.getSourceFileOrThrow(path);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public async format(): Promise<void> {
|
|
32
|
+
await Log.animate('Formatting modified files', async () => {
|
|
33
|
+
const usingPrettier = File.exists('prettier.config.js');
|
|
34
|
+
const usingESLint = File.exists('.eslintrc.js');
|
|
35
|
+
|
|
36
|
+
await Promise.all(
|
|
37
|
+
arrayFrom(this.modifiedFiles).map(async (file) => {
|
|
38
|
+
usingPrettier && (await Shell.run(`npx prettier ${file} --write`));
|
|
39
|
+
usingESLint && (await Shell.run(`npx eslint ${file} --fix`));
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public async save(file: SourceFile): Promise<void> {
|
|
46
|
+
await file.save();
|
|
47
|
+
|
|
48
|
+
this.modifiedFiles.add(file.getFilePath());
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
}
|
package/src/lib/File.ts
CHANGED
|
@@ -4,10 +4,20 @@ import { dirname, resolve } from 'path';
|
|
|
4
4
|
|
|
5
5
|
export class FileService {
|
|
6
6
|
|
|
7
|
+
public contains(path: string, contents: string): boolean {
|
|
8
|
+
return !!this.read(path)?.includes(contents);
|
|
9
|
+
}
|
|
10
|
+
|
|
7
11
|
public exists(path: string): boolean {
|
|
8
12
|
return existsSync(path);
|
|
9
13
|
}
|
|
10
14
|
|
|
15
|
+
public isSymlink(path: string): boolean {
|
|
16
|
+
const stats = lstatSync(path);
|
|
17
|
+
|
|
18
|
+
return stats.isSymbolicLink();
|
|
19
|
+
}
|
|
20
|
+
|
|
11
21
|
public read(path: string): string | null {
|
|
12
22
|
if (!this.isFile(path)) {
|
|
13
23
|
return null;
|
package/src/lib/Log.mock.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { expect } from 'vitest';
|
|
2
|
-
import {
|
|
2
|
+
import { facade } from '@noeldemartin/utils';
|
|
3
3
|
|
|
4
4
|
import { LogService } from './Log';
|
|
5
5
|
|
|
@@ -11,14 +11,23 @@ export class LogServiceMock extends LogService {
|
|
|
11
11
|
expect(this.logs, `Expected message "${message}" to have been logged`).toContain(message);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
this.logs.
|
|
14
|
+
public expectLogLength(count: number): void {
|
|
15
|
+
expect(this.logs, `Expected log to have length ${count}`).toHaveLength(count);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
protected logLine(message: string): void {
|
|
19
|
+
this.logs.push(message);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
|
+
public fail<T = any>(message: string): T {
|
|
19
24
|
throw new Error(`Fail: ${message}`);
|
|
20
25
|
}
|
|
21
26
|
|
|
27
|
+
public reset(): void {
|
|
28
|
+
this.logs = [];
|
|
29
|
+
}
|
|
30
|
+
|
|
22
31
|
protected stdout(): void {
|
|
23
32
|
//
|
|
24
33
|
}
|
package/src/lib/Log.test.ts
CHANGED
|
@@ -5,12 +5,11 @@ import LogMock from '@/lib/Log.mock';
|
|
|
5
5
|
|
|
6
6
|
import Log from './Log';
|
|
7
7
|
|
|
8
|
+
const info = hex('#00ffff');
|
|
9
|
+
|
|
8
10
|
describe('Log', () => {
|
|
9
11
|
|
|
10
12
|
it('renders markdown bold', () => {
|
|
11
|
-
// Arrange
|
|
12
|
-
const info = hex('#00ffff');
|
|
13
|
-
|
|
14
13
|
// Act
|
|
15
14
|
Log.info('Foo **bar**');
|
|
16
15
|
|
|
@@ -18,4 +17,21 @@ describe('Log', () => {
|
|
|
18
17
|
LogMock.expectLogged(info(`Foo ${bold('bar')}`));
|
|
19
18
|
});
|
|
20
19
|
|
|
20
|
+
it('renders multiline messages', () => {
|
|
21
|
+
// Act
|
|
22
|
+
Log.info(`
|
|
23
|
+
This is multiline,
|
|
24
|
+
but the indentation should be respected.
|
|
25
|
+
|
|
26
|
+
As well as the new lines.
|
|
27
|
+
`);
|
|
28
|
+
|
|
29
|
+
// Assert
|
|
30
|
+
LogMock.expectLogLength(4);
|
|
31
|
+
LogMock.expectLogged(info('This is multiline,'));
|
|
32
|
+
LogMock.expectLogged(info(' but the indentation should be respected.'));
|
|
33
|
+
LogMock.expectLogged(info(''));
|
|
34
|
+
LogMock.expectLogged(info('As well as the new lines.'));
|
|
35
|
+
});
|
|
36
|
+
|
|
21
37
|
});
|
package/src/lib/Log.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { facade, stringMatchAll } from '@noeldemartin/utils';
|
|
2
2
|
import { bold, hex } from 'chalk';
|
|
3
3
|
import { clearLine, cursorTo } from 'readline';
|
|
4
4
|
|
|
@@ -9,8 +9,9 @@ export class LogService {
|
|
|
9
9
|
protected renderError = hex('#ff0000');
|
|
10
10
|
|
|
11
11
|
public async animate<T>(message: string, operation: () => Promise<T>): Promise<T> {
|
|
12
|
-
const updateStdout = (end: string = '') => {
|
|
13
|
-
const progress =
|
|
12
|
+
const updateStdout = (end: string = '', done: boolean = false) => {
|
|
13
|
+
const progress =
|
|
14
|
+
this.renderInfo(this.renderMarkdown(message) + (done ? '...' : '.'.repeat(frame % 4))) + end;
|
|
14
15
|
|
|
15
16
|
this.stdout(progress);
|
|
16
17
|
};
|
|
@@ -23,33 +24,28 @@ export class LogService {
|
|
|
23
24
|
const result = await operation();
|
|
24
25
|
|
|
25
26
|
clearInterval(interval);
|
|
26
|
-
updateStdout('\n');
|
|
27
|
+
updateStdout('\n', true);
|
|
27
28
|
|
|
28
29
|
return result;
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
public info(
|
|
32
|
-
|
|
33
|
-
this.log(this.renderInfo(this.renderMarkdown(message)));
|
|
34
|
-
});
|
|
32
|
+
public info(message: string): void {
|
|
33
|
+
this.log(this.renderMarkdown(message), this.renderInfo);
|
|
35
34
|
}
|
|
36
35
|
|
|
37
|
-
public error(
|
|
38
|
-
|
|
39
|
-
this.log(this.renderError(this.renderMarkdown(message)));
|
|
40
|
-
});
|
|
36
|
+
public error(message: string): void {
|
|
37
|
+
this.log(this.renderMarkdown(message), this.renderError);
|
|
41
38
|
}
|
|
42
39
|
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
public fail<T = any>(message: string): T {
|
|
42
|
+
this.error(message);
|
|
45
43
|
|
|
46
44
|
process.exit(1);
|
|
47
45
|
}
|
|
48
46
|
|
|
49
|
-
public success(
|
|
50
|
-
|
|
51
|
-
this.log(this.renderSuccess(this.renderMarkdown(message)));
|
|
52
|
-
});
|
|
47
|
+
public success(message: string): void {
|
|
48
|
+
this.log(this.renderMarkdown(message), this.renderSuccess);
|
|
53
49
|
}
|
|
54
50
|
|
|
55
51
|
protected renderMarkdown(message: string): string {
|
|
@@ -62,9 +58,29 @@ export class LogService {
|
|
|
62
58
|
return message;
|
|
63
59
|
}
|
|
64
60
|
|
|
65
|
-
protected log(
|
|
61
|
+
protected log(message: string, formatMessage?: (message: string) => string): void {
|
|
62
|
+
this.formatMessage(message).forEach((line) => {
|
|
63
|
+
this.logLine(formatMessage ? formatMessage(line) : line);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected formatMessage(message: string): string[] {
|
|
68
|
+
if (message[0] === '\n') {
|
|
69
|
+
message = message.slice(1).trimEnd();
|
|
70
|
+
|
|
71
|
+
const lines = message.split('\n');
|
|
72
|
+
const firstLetter = message.trim()[0] ?? '';
|
|
73
|
+
const indentation = lines.find((line) => line.trim().length > 0)?.indexOf(firstLetter) ?? 0;
|
|
74
|
+
|
|
75
|
+
return lines.map((line) => line.slice(indentation));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return [message];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
protected logLine(line: string): void {
|
|
66
82
|
// eslint-disable-next-line no-console
|
|
67
|
-
|
|
83
|
+
console.log(line);
|
|
68
84
|
}
|
|
69
85
|
|
|
70
86
|
protected stdout(message: string): void {
|
package/src/lib/Template.ts
CHANGED
|
@@ -25,10 +25,11 @@ export default class Template {
|
|
|
25
25
|
file.substring(this.path.length + 1),
|
|
26
26
|
);
|
|
27
27
|
const fileContents = readFileSync(file).toString();
|
|
28
|
+
const filePath =
|
|
29
|
+
destination + (relativePath.endsWith('.template') ? relativePath.slice(0, -9) : relativePath);
|
|
28
30
|
|
|
29
|
-
File.write(
|
|
30
|
-
|
|
31
|
-
files.push(destination + relativePath);
|
|
31
|
+
File.write(filePath, render(fileContents, replacements, undefined, ['<%', '%>']));
|
|
32
|
+
files.push(filePath);
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
return files;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import App from '@/lib/App';
|
|
2
|
+
import File from '@/lib/File';
|
|
3
|
+
|
|
4
|
+
export function app(): App {
|
|
5
|
+
// TODO parse app name
|
|
6
|
+
return new App('');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function isLocalApp(): boolean {
|
|
10
|
+
return File.contains('package.json', 'file');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function isLinkedLocalApp(): boolean {
|
|
14
|
+
return File.isSymlink('node_modules/@aerogel/core');
|
|
15
|
+
}
|