@aerogel/cli 0.0.0-next.59bf5f7cc06e728d0cf6c00de28f1da48d7d6b8e → 0.0.0-next.6c02970f90d4e979a72530f1fa0c650785d6538f
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/.prettierignore +1 -0
- package/dist/aerogel-cli.cjs.js +1 -1
- package/dist/aerogel-cli.cjs.js.map +1 -1
- package/dist/aerogel-cli.d.ts +2 -2
- package/dist/aerogel-cli.esm.js +1 -1
- package/dist/aerogel-cli.esm.js.map +1 -1
- package/package.json +12 -5
- package/src/cli.ts +27 -3
- package/src/commands/Command.ts +11 -6
- package/src/commands/create.test.ts +12 -4
- package/src/commands/create.ts +20 -9
- package/src/commands/generate-component.test.ts +50 -3
- package/src/commands/generate-component.ts +162 -21
- package/src/commands/generate-model.test.ts +2 -2
- package/src/commands/generate-model.ts +19 -22
- package/src/commands/generate-overrides.ts +85 -0
- package/src/commands/generate-service.test.ts +21 -0
- package/src/commands/generate-service.ts +151 -0
- package/src/commands/info.ts +14 -0
- package/src/commands/install.test.ts +41 -0
- package/src/commands/install.ts +33 -0
- package/src/lib/App.ts +60 -5
- package/src/lib/Editor.ts +58 -0
- package/src/lib/File.mock.ts +1 -5
- package/src/lib/File.ts +7 -1
- package/src/lib/Log.mock.ts +3 -6
- package/src/lib/Log.ts +7 -5
- package/src/lib/Shell.mock.ts +2 -2
- package/src/lib/Shell.ts +1 -1
- package/src/lib/Template.ts +5 -1
- package/src/lib/utils/app.ts +15 -0
- package/src/lib/utils/edit.ts +44 -0
- package/src/lib/utils/paths.ts +34 -0
- package/src/plugins/Histoire.ts +113 -0
- package/src/plugins/Plugin.ts +178 -0
- package/src/plugins/Solid.ts +79 -0
- package/src/plugins/Soukai.ts +19 -0
- package/src/testing/setup.ts +39 -12
- package/templates/app/.github/workflows/ci.yml +16 -4
- 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/cypress.config.ts +14 -0
- package/templates/app/cypress/support/e2e.ts +1 -3
- package/templates/app/cypress/tsconfig.json +3 -1
- package/templates/app/index.html +4 -3
- package/templates/app/package.json +34 -11
- package/templates/app/src/App.vue +5 -3
- package/templates/app/src/assets/public/robots.txt +2 -0
- package/templates/app/src/main.ts +4 -4
- 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 +10 -5
- package/templates/component-button/[component.name].vue +42 -0
- package/templates/component-button-story/[component.name].story.vue +77 -0
- package/templates/component-checkbox/[component.name].vue +34 -0
- package/templates/component-checkbox-story/[component.name].story.vue +63 -0
- package/templates/component-input/[component.name].vue +17 -0
- package/templates/component-input-story/[component.name].story.vue +63 -0
- package/templates/histoire/histoire.config.ts +7 -0
- package/templates/histoire/patches/histoire+0.17.6.patch +13 -0
- package/templates/histoire/src/main.histoire.ts +8 -0
- package/templates/overrides/components/index.ts +15 -0
- package/templates/overrides/components/overrides/AlertModal.vue +11 -0
- package/templates/overrides/components/overrides/ConfirmModal.vue +20 -0
- package/templates/overrides/components/overrides/ErrorReportModal.vue +35 -0
- package/templates/overrides/components/overrides/LoadingModal.vue +12 -0
- package/templates/overrides/components/overrides/ModalWrapper.vue +22 -0
- package/templates/overrides/components/overrides/SnackbarNotification.vue +34 -0
- package/templates/overrides-story/Overrides.story.vue +86 -0
- package/templates/postcss-pseudo-classes/postcss.config.js +15 -0
- package/templates/service/[service.name].ts +8 -0
- package/.eslintrc.js +0 -7
- package/noeldemartin.config.js +0 -4
- package/src/lib/utils.test.ts +0 -33
- package/src/lib/utils.ts +0 -44
- package/templates/app/cypress.config.ts +0 -8
- /package/bin/{ag → gel} +0 -0
- /package/templates/app/src/assets/{styles.css → css/styles.css} +0 -0
package/src/lib/App.ts
CHANGED
|
@@ -3,10 +3,21 @@ 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
|
+
}
|
|
7
17
|
|
|
8
18
|
export interface Options {
|
|
9
19
|
local?: boolean;
|
|
20
|
+
linkedLocal?: boolean;
|
|
10
21
|
}
|
|
11
22
|
|
|
12
23
|
export default class App {
|
|
@@ -18,15 +29,59 @@ export default class App {
|
|
|
18
29
|
Log.fail(`Folder at '${path}' already exists!`);
|
|
19
30
|
}
|
|
20
31
|
|
|
21
|
-
Template.instantiate(
|
|
32
|
+
Template.instantiate(templatePath('app'), path, {
|
|
22
33
|
app: {
|
|
23
34
|
name: this.name,
|
|
24
35
|
slug: stringToSlug(this.name),
|
|
25
36
|
},
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
dependencies: this.getDependencies(),
|
|
38
|
+
contentPath: this.options.linkedLocal
|
|
39
|
+
? `${packagePath('core')}/dist/**/*.js`
|
|
40
|
+
: './node_modules/@aerogel/core/dist/**/*.js',
|
|
29
41
|
});
|
|
30
42
|
}
|
|
31
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
|
+
|
|
32
87
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
this.project.addSourceFilesAtPaths('package.json');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public addSourceFile(path: string): void {
|
|
25
|
+
this.project.addSourceFilesAtPaths(path);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public requireSourceFile(path: string): SourceFile {
|
|
29
|
+
return this.project.getSourceFileOrThrow(path);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public async format(): Promise<void> {
|
|
33
|
+
await Log.animate('Formatting modified files', async () => {
|
|
34
|
+
const usingPrettier = File.exists('prettier.config.js') || File.contains('package.json', '"prettier": {');
|
|
35
|
+
const usingESLint = File.exists('.eslintrc.js') || File.contains('package.json', '"eslintConfig"');
|
|
36
|
+
const usingPrettierESLint = File.contains('package.json', '"prettier-eslint-cli"');
|
|
37
|
+
const formatFile = usingPrettierESLint
|
|
38
|
+
? (file: string) => Shell.run(`npx prettier-eslint ${file} --write`)
|
|
39
|
+
: async (file: string) => {
|
|
40
|
+
usingPrettier && (await Shell.run(`npx prettier ${file} --write`));
|
|
41
|
+
file.match(/\.(ts|js|vue)$/) && usingESLint && (await Shell.run(`npx eslint ${file} --fix`));
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
await Promise.all(arrayFrom(this.modifiedFiles).map(async (file) => formatFile(file)));
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public async save(file: SourceFile): Promise<void> {
|
|
49
|
+
await file.save();
|
|
50
|
+
|
|
51
|
+
this.addModifiedFile(file.getFilePath());
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public addModifiedFile(path: string): void {
|
|
55
|
+
this.modifiedFiles.add(path);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
}
|
package/src/lib/File.mock.ts
CHANGED
|
@@ -41,10 +41,6 @@ export class FileMockService extends FileService {
|
|
|
41
41
|
this.virtualFilesystem[path] = contents;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
public reset(): void {
|
|
45
|
-
this.virtualFilesystem = {};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
44
|
public expectCreated(path: string, expectContent?: (contents: string) => void): Assertion<string> {
|
|
49
45
|
expect(typeof this.virtualFilesystem[path] === 'string', `expected '${path}' file to have been created`).toBe(
|
|
50
46
|
true,
|
|
@@ -63,4 +59,4 @@ export class FileMockService extends FileService {
|
|
|
63
59
|
|
|
64
60
|
}
|
|
65
61
|
|
|
66
|
-
export default facade(
|
|
62
|
+
export default facade(FileMockService);
|
package/src/lib/File.ts
CHANGED
|
@@ -12,6 +12,12 @@ export class FileService {
|
|
|
12
12
|
return existsSync(path);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
public isSymlink(path: string): boolean {
|
|
16
|
+
const stats = lstatSync(path);
|
|
17
|
+
|
|
18
|
+
return stats.isSymbolicLink();
|
|
19
|
+
}
|
|
20
|
+
|
|
15
21
|
public read(path: string): string | null {
|
|
16
22
|
if (!this.isFile(path)) {
|
|
17
23
|
return null;
|
|
@@ -67,4 +73,4 @@ export class FileService {
|
|
|
67
73
|
|
|
68
74
|
}
|
|
69
75
|
|
|
70
|
-
export default facade(
|
|
76
|
+
export default facade(FileService);
|
package/src/lib/Log.mock.ts
CHANGED
|
@@ -19,18 +19,15 @@ export class LogServiceMock extends LogService {
|
|
|
19
19
|
this.logs.push(message);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
|
+
public fail<T = any>(message: string): T {
|
|
23
24
|
throw new Error(`Fail: ${message}`);
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
public reset(): void {
|
|
27
|
-
this.logs = [];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
27
|
protected stdout(): void {
|
|
31
28
|
//
|
|
32
29
|
}
|
|
33
30
|
|
|
34
31
|
}
|
|
35
32
|
|
|
36
|
-
export default facade(
|
|
33
|
+
export default facade(LogServiceMock);
|
package/src/lib/Log.ts
CHANGED
|
@@ -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,7 +24,7 @@ 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
|
}
|
|
@@ -36,7 +37,8 @@ export class LogService {
|
|
|
36
37
|
this.log(this.renderMarkdown(message), this.renderError);
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
public fail<T = any>(message: string): T {
|
|
40
42
|
this.error(message);
|
|
41
43
|
|
|
42
44
|
process.exit(1);
|
|
@@ -90,4 +92,4 @@ export class LogService {
|
|
|
90
92
|
|
|
91
93
|
}
|
|
92
94
|
|
|
93
|
-
export default facade(
|
|
95
|
+
export default facade(LogService);
|
package/src/lib/Shell.mock.ts
CHANGED
|
@@ -8,7 +8,7 @@ export class ShellServiceMock extends ShellService {
|
|
|
8
8
|
private history: string[] = [];
|
|
9
9
|
|
|
10
10
|
public async run(command: string): Promise<void> {
|
|
11
|
-
this.history.push(command);
|
|
11
|
+
this.history.push(command.trim());
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
public expectRan(command: string): void {
|
|
@@ -17,4 +17,4 @@ export class ShellServiceMock extends ShellService {
|
|
|
17
17
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export default facade(
|
|
20
|
+
export default facade(ShellServiceMock);
|
package/src/lib/Shell.ts
CHANGED
package/src/lib/Template.ts
CHANGED
|
@@ -6,7 +6,11 @@ import File from '@/lib/File';
|
|
|
6
6
|
|
|
7
7
|
export default class Template {
|
|
8
8
|
|
|
9
|
-
public static instantiate(
|
|
9
|
+
public static instantiate(
|
|
10
|
+
path: string,
|
|
11
|
+
destination: string = './',
|
|
12
|
+
replacements: Record<string, unknown> = {},
|
|
13
|
+
): string[] {
|
|
10
14
|
const template = new Template(path);
|
|
11
15
|
|
|
12
16
|
return template.instantiate(destination, replacements);
|
|
@@ -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', '"@aerogel/core": "file:');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function isLinkedLocalApp(): boolean {
|
|
14
|
+
return File.isSymlink('node_modules/@aerogel/core');
|
|
15
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { arrayFrom } from '@noeldemartin/utils';
|
|
2
|
+
import type { Node, SyntaxKind } from 'ts-morph';
|
|
3
|
+
|
|
4
|
+
export function editFiles(): boolean {
|
|
5
|
+
// TODO mock editor instead of relying on this for unit tests
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function findDescendant<T extends Node>(
|
|
10
|
+
node: Node | undefined,
|
|
11
|
+
options: {
|
|
12
|
+
guard?: (node: Node | undefined) => node is T;
|
|
13
|
+
validate?: (node: T) => boolean;
|
|
14
|
+
skip?: SyntaxKind | SyntaxKind[];
|
|
15
|
+
} = {},
|
|
16
|
+
): T | undefined {
|
|
17
|
+
if (!node) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const guard = options.guard ?? (() => true);
|
|
22
|
+
const validate = options.validate ?? (() => true);
|
|
23
|
+
const skipKinds = arrayFrom(options.skip ?? []);
|
|
24
|
+
|
|
25
|
+
return node.forEachDescendant((descendant, traversal) => {
|
|
26
|
+
if (guard(descendant) && validate(descendant)) {
|
|
27
|
+
return descendant;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const descendantKind = descendant.getKind();
|
|
31
|
+
|
|
32
|
+
if (skipKinds.includes(descendantKind)) {
|
|
33
|
+
traversal.skip();
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function when<T extends Node>(node: Node | undefined, assertion: (node: Node) => node is T): T | undefined {
|
|
39
|
+
if (!node || !assertion(node)) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return node as T;
|
|
44
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { resolve } from 'path';
|
|
2
|
+
import { stringMatch } from '@noeldemartin/utils';
|
|
3
|
+
|
|
4
|
+
import File from '@/lib/File';
|
|
5
|
+
import Log from '@/lib/Log';
|
|
6
|
+
|
|
7
|
+
export function basePath(path: string = ''): string {
|
|
8
|
+
if (File.contains(resolve(__dirname, '../../../package.json'), '"name": "aerogel"')) {
|
|
9
|
+
return resolve(__dirname, '../', path);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const packageJson = File.read(resolve(__dirname, '../../../../package.json'));
|
|
13
|
+
const matches = stringMatch<2>(packageJson ?? '', /"@aerogel\/core": "file:(.*)\/aerogel-core-[\d.]*\.tgz"/);
|
|
14
|
+
const cliPath = matches?.[1] ?? Log.fail<string>('Could not determine base path');
|
|
15
|
+
|
|
16
|
+
return resolve(cliPath, path);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
+
export function packNotFound(packageName: string): any {
|
|
21
|
+
return Log.fail(`Could not find ${packageName} pack file, did you run 'npm pack'?`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function packagePackPath(packageName: string): string | null {
|
|
25
|
+
return File.getFiles(packagePath(packageName)).find((file) => file.endsWith('.tgz')) ?? null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function packagePath(packageName: string): string {
|
|
29
|
+
return basePath(`../${packageName}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function templatePath(name: string): string {
|
|
33
|
+
return resolve(__dirname, `../templates/${name}`);
|
|
34
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import File from '@/lib/File';
|
|
2
|
+
import Log from '@/lib/Log';
|
|
3
|
+
import Plugin from '@/plugins/Plugin';
|
|
4
|
+
import Shell from '@/lib/Shell';
|
|
5
|
+
import Template from '@/lib/Template';
|
|
6
|
+
import { isLinkedLocalApp } from '@/lib/utils/app';
|
|
7
|
+
import { packagePath, templatePath } from '@/lib/utils/paths';
|
|
8
|
+
import type { Editor } from '@/lib/Editor';
|
|
9
|
+
import { formatCodeBlock } from '@noeldemartin/utils';
|
|
10
|
+
|
|
11
|
+
export class Histoire extends Plugin {
|
|
12
|
+
|
|
13
|
+
private installedPatchPackage: boolean = false;
|
|
14
|
+
private installedPostCSSPseudoClasses: boolean = false;
|
|
15
|
+
|
|
16
|
+
constructor() {
|
|
17
|
+
super('histoire');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public async beforeInstall(): Promise<void> {
|
|
21
|
+
this.installedPatchPackage = false;
|
|
22
|
+
this.installedPostCSSPseudoClasses = false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
protected async afterInstall(): Promise<void> {
|
|
26
|
+
if (!this.installedPatchPackage) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
await Log.animate('Patching dependencies', async () => {
|
|
31
|
+
await Shell.run('npx patch-package');
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
protected async updateFiles(editor: Editor): Promise<void> {
|
|
36
|
+
await this.updateTailwindConfig(editor, {
|
|
37
|
+
content: isLinkedLocalApp()
|
|
38
|
+
? `'${packagePath('histoire')}/dist/**/*.js'`
|
|
39
|
+
: '\'./node_modules/@aerogel/histoire/dist/**/*.js\'',
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
await this.updateNpmScripts(editor);
|
|
43
|
+
await this.createConfigFiles();
|
|
44
|
+
await super.updateFiles(editor);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected async installNpmDependencies(): Promise<void> {
|
|
48
|
+
// We need this specific version because we're patching it using patch-package
|
|
49
|
+
await Shell.run('npm install histoire@0.17.6 --save-dev');
|
|
50
|
+
|
|
51
|
+
if (!File.contains('package.json', '"patch-package"')) {
|
|
52
|
+
await Shell.run('npm install patch-package --save-dev');
|
|
53
|
+
|
|
54
|
+
this.installedPatchPackage = true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (!File.contains('package.json', '"postcss-pseudo-classes"')) {
|
|
58
|
+
await Shell.run('npm install postcss-pseudo-classes --save-dev');
|
|
59
|
+
|
|
60
|
+
this.installedPostCSSPseudoClasses = true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
await super.installNpmDependencies();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
protected getLocalPackageName(): string {
|
|
67
|
+
return this.name;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected async updateNpmScripts(editor: Editor): Promise<void> {
|
|
71
|
+
Log.info('Updating npm scripts...');
|
|
72
|
+
|
|
73
|
+
const packageJson = File.read('package.json');
|
|
74
|
+
|
|
75
|
+
if (!packageJson) {
|
|
76
|
+
return Log.fail('Could not find package.json file');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
File.write(
|
|
80
|
+
'package.json',
|
|
81
|
+
packageJson.replace(
|
|
82
|
+
'"lint": "noeldemartin-lint src cypress",',
|
|
83
|
+
this.installedPatchPackage
|
|
84
|
+
? formatCodeBlock(`
|
|
85
|
+
"histoire": "histoire dev",
|
|
86
|
+
"lint": "noeldemartin-lint cypress src",
|
|
87
|
+
"postinstall": "patch-package",
|
|
88
|
+
`)
|
|
89
|
+
: formatCodeBlock(`
|
|
90
|
+
"histoire": "histoire dev",
|
|
91
|
+
"lint": "noeldemartin-lint cypress src",
|
|
92
|
+
`),
|
|
93
|
+
),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
editor.addModifiedFile('package.json');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
protected async createConfigFiles(): Promise<void> {
|
|
100
|
+
Log.info('Creating config files...');
|
|
101
|
+
|
|
102
|
+
Template.instantiate(templatePath('histoire'));
|
|
103
|
+
|
|
104
|
+
if (this.installedPostCSSPseudoClasses) {
|
|
105
|
+
Template.instantiate(templatePath('postcss-pseudo-classes'));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
protected isForDevelopment(): boolean {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { Node, SyntaxKind } from 'ts-morph';
|
|
2
|
+
import type { ArrayLiteralExpression, ImportDeclarationStructure, OptionalKind, SourceFile } from 'ts-morph';
|
|
3
|
+
|
|
4
|
+
import Log from '@/lib/Log';
|
|
5
|
+
import Shell from '@/lib/Shell';
|
|
6
|
+
import File from '@/lib/File';
|
|
7
|
+
import { app, isLinkedLocalApp, isLocalApp } from '@/lib/utils/app';
|
|
8
|
+
import { editFiles, findDescendant, when } from '@/lib/utils/edit';
|
|
9
|
+
import { packNotFound, packagePackPath, packagePath } from '@/lib/utils/paths';
|
|
10
|
+
import type { Editor } from '@/lib/Editor';
|
|
11
|
+
|
|
12
|
+
export default abstract class Plugin {
|
|
13
|
+
|
|
14
|
+
public readonly name: string;
|
|
15
|
+
|
|
16
|
+
constructor(name: string) {
|
|
17
|
+
this.name = name;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public async install(): Promise<void> {
|
|
21
|
+
this.assertNotInstalled();
|
|
22
|
+
|
|
23
|
+
await this.beforeInstall();
|
|
24
|
+
await this.installDependencies();
|
|
25
|
+
|
|
26
|
+
if (editFiles()) {
|
|
27
|
+
const editor = app().edit();
|
|
28
|
+
|
|
29
|
+
await this.updateFiles(editor);
|
|
30
|
+
await editor.format();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
await this.afterInstall();
|
|
34
|
+
|
|
35
|
+
Log.info(`Plugin ${this.name} installed!`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
protected assertNotInstalled(): void {
|
|
39
|
+
if (File.contains('package.json', `"${this.getNpmPackageName()}"`)) {
|
|
40
|
+
Log.fail(`${this.name} is already installed!`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
protected async beforeInstall(): Promise<void> {
|
|
45
|
+
// Placeholder for overrides, don't place any functionality here.
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected async afterInstall(): Promise<void> {
|
|
49
|
+
// Placeholder for overrides, don't place any functionality here.
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
protected async installDependencies(): Promise<void> {
|
|
53
|
+
await Log.animate('Installing plugin dependencies', async () => {
|
|
54
|
+
await this.installNpmDependencies();
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
protected async updateFiles(editor: Editor): Promise<void> {
|
|
59
|
+
if (!this.isForDevelopment()) {
|
|
60
|
+
await this.updateBootstrapConfig(editor);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
protected async installNpmDependencies(): Promise<void> {
|
|
65
|
+
const flags = this.isForDevelopment() ? '--save-dev' : '';
|
|
66
|
+
|
|
67
|
+
if (isLinkedLocalApp()) {
|
|
68
|
+
await Shell.run(`npm install file:${packagePath(this.getLocalPackageName())} ${flags}`);
|
|
69
|
+
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (isLocalApp()) {
|
|
74
|
+
const packPath = packagePackPath(this.getLocalPackageName()) ?? packNotFound(this.getLocalPackageName());
|
|
75
|
+
|
|
76
|
+
await Shell.run(`npm install file:${packPath} ${flags}`);
|
|
77
|
+
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
await Shell.run(`npm install ${this.getNpmPackageName()}@next --save-exact ${flags}`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
protected async updateBootstrapConfig(editor: Editor): Promise<void> {
|
|
85
|
+
await Log.animate('Injecting plugin in bootstrap configuration', async () => {
|
|
86
|
+
const mainConfig = editor.requireSourceFile('src/main.ts');
|
|
87
|
+
const pluginsArray = this.getBootstrapPluginsDeclaration(mainConfig);
|
|
88
|
+
|
|
89
|
+
if (!pluginsArray) {
|
|
90
|
+
return Log.fail(`
|
|
91
|
+
Could not find plugins array in bootstrap config, please add the following manually:
|
|
92
|
+
|
|
93
|
+
${this.getBootstrapConfig()}
|
|
94
|
+
`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
mainConfig.addImportDeclaration(this.getBootstrapImport());
|
|
98
|
+
pluginsArray.addElement(this.getBootstrapConfig());
|
|
99
|
+
|
|
100
|
+
await editor.save(mainConfig);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
protected async updateTailwindConfig(editor: Editor, options: { content: string }): Promise<void> {
|
|
105
|
+
await Log.animate('Updating tailwind configuration', async () => {
|
|
106
|
+
const tailwindConfig = editor.requireSourceFile('tailwind.config.js');
|
|
107
|
+
const contentArray = this.getTailwindContentArray(tailwindConfig);
|
|
108
|
+
|
|
109
|
+
if (!contentArray) {
|
|
110
|
+
return Log.fail(`
|
|
111
|
+
Could not find content array in tailwind config, please add the following manually:
|
|
112
|
+
|
|
113
|
+
${options.content}
|
|
114
|
+
`);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
contentArray.addElement(options.content);
|
|
118
|
+
|
|
119
|
+
await editor.save(tailwindConfig);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
protected getBootstrapPluginsDeclaration(mainConfig: SourceFile): ArrayLiteralExpression | null {
|
|
124
|
+
const bootstrapAppCall = findDescendant(mainConfig, {
|
|
125
|
+
guard: Node.isCallExpression,
|
|
126
|
+
validate: (callExpression) => callExpression.getExpression().getText() === 'bootstrap',
|
|
127
|
+
skip: SyntaxKind.ImportDeclaration,
|
|
128
|
+
});
|
|
129
|
+
const bootstrapOptions = bootstrapAppCall?.getArguments()[1];
|
|
130
|
+
const pluginsOption = when(bootstrapOptions, Node.isObjectLiteralExpression)?.getProperty('plugins');
|
|
131
|
+
const pluginsArray = when(pluginsOption, Node.isPropertyAssignment)?.getInitializer();
|
|
132
|
+
|
|
133
|
+
if (!Node.isArrayLiteralExpression(pluginsArray)) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return pluginsArray;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
protected getTailwindContentArray(tailwindConfig: SourceFile): ArrayLiteralExpression | null {
|
|
141
|
+
const contentAssignment = findDescendant(tailwindConfig, {
|
|
142
|
+
guard: Node.isPropertyAssignment,
|
|
143
|
+
validate: (propertyAssignment) => propertyAssignment.getName() === 'content',
|
|
144
|
+
skip: SyntaxKind.JSDoc,
|
|
145
|
+
});
|
|
146
|
+
const contentArray = contentAssignment?.getInitializer();
|
|
147
|
+
|
|
148
|
+
if (!Node.isArrayLiteralExpression(contentArray)) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return contentArray;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
protected getBootstrapImport(): OptionalKind<ImportDeclarationStructure> {
|
|
156
|
+
return {
|
|
157
|
+
defaultImport: this.name,
|
|
158
|
+
moduleSpecifier: `@aerogel/plugin-${this.name}`,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
protected getNpmPackageName(): string {
|
|
163
|
+
return `@aerogel/${this.getLocalPackageName()}`;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
protected getLocalPackageName(): string {
|
|
167
|
+
return `plugin-${this.name}`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
protected isForDevelopment(): boolean {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
protected getBootstrapConfig(): string {
|
|
175
|
+
return `${this.name}()`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
}
|