Skip to content

Commit a2ea9b6

Browse files
authored
fix(cli-daemon): support generators in windows
1 parent 64fb529 commit a2ea9b6

File tree

3 files changed

+40
-39
lines changed

3 files changed

+40
-39
lines changed

apps/cli-daemon/src/app/generators/generators.interface.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
type PropertyTypes = 'string' | 'boolean' | 'integer' | 'number';
2+
23
interface Property {
34
type: PropertyTypes;
45
format: string;
56
description: string;
67
default: string | Record<string, any>;
78
}
89

10+
export interface SchemaCollection {
11+
factory: string;
12+
schema: string;
13+
description: string;
14+
}
15+
916
export interface Schema {
1017
$schema: string;
1118
$id: string;

apps/cli-daemon/src/app/generators/generators.service.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { spawnSync, SpawnSyncOptionsWithBufferEncoding } from 'child_process';
2-
import { readFileSync } from 'fs';
32
import { resolve as pathResolve } from 'path';
3+
import * as process from 'process';
44

55
import { Injectable, Logger } from '@nestjs/common';
66

@@ -11,8 +11,8 @@ import { ExecResult } from './dto';
1111
import { GeneratorDefinition, Schema } from './generators.interface';
1212
import {
1313
convertKeyToArgument,
14-
getGeneratorDefinition,
15-
getGeneratorNames,
14+
formatJsonToJs,
15+
getGeneratorsDefinition,
1616
} from './utils';
1717

1818
@Injectable()
@@ -36,23 +36,13 @@ export class GeneratorsService {
3636
}
3737

3838
getAllGenerators(): GeneratorDefinition[] {
39-
const schemasPath = pathResolve(
40-
this.sessionService.cwd,
41-
'node_modules/@schematics/angular'
42-
);
43-
const generatorNames = getGeneratorNames(schemasPath);
44-
const generators = generatorNames
45-
.map((name) => getGeneratorDefinition(name, schemasPath))
46-
.filter(Boolean);
47-
return generators as GeneratorDefinition[];
39+
const collectionPath = this.getPath(['collection.json']);
40+
return getGeneratorsDefinition(collectionPath);
4841
}
4942

5043
getSchema(schemaName: string): Schema {
51-
const schemaPath = pathResolve(
52-
this.sessionService.cwd,
53-
`node_modules/@schematics/angular/${schemaName}/schema.json`
54-
);
55-
return JSON.parse(readFileSync(schemaPath, 'utf-8'));
44+
const schemaPath = this.getPath([schemaName, 'schema.json']);
45+
return formatJsonToJs<Schema>(schemaPath);
5646
}
5747

5848
private getArgsFromParams(params: Record<string, any>): string[] {
@@ -61,4 +51,15 @@ export class GeneratorsService {
6151
return `${convertedKey}=${value}`;
6252
});
6353
}
54+
55+
private getPath(extension: string[]): string {
56+
console.log('this.sessionService.cwd:', this.sessionService.cwd);
57+
return pathResolve(
58+
this.sessionService.cwd,
59+
'node_modules',
60+
'@schematics',
61+
'angular',
62+
...extension
63+
);
64+
}
6465
}

apps/cli-daemon/src/app/generators/utils.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { readdirSync, readFileSync } from 'fs';
2-
import { join } from 'path';
1+
import { readFileSync } from 'fs';
32

4-
import { GeneratorDefinition, Schema } from './generators.interface';
3+
import { GeneratorDefinition, SchemaCollection } from './generators.interface';
54

65
const capitalize = (s: string): string => s[0].toUpperCase() + s.slice(1);
76
const formatSchemaName = (name: string): string => {
@@ -10,29 +9,23 @@ const formatSchemaName = (name: string): string => {
109
.map((name) => capitalize(name))
1110
.join('');
1211
};
13-
export const getGeneratorDefinition = (
14-
generatorName: string,
12+
export const formatJsonToJs = <T>(path: string): T =>
13+
JSON.parse(readFileSync(path, 'utf-8'));
14+
export const getGeneratorsDefinition = (
1515
path: string
16-
): GeneratorDefinition | null => {
17-
try {
18-
const schema: Schema = JSON.parse(
19-
readFileSync(`${path}/${generatorName}/schema.json`, 'utf-8')
20-
);
21-
const displayName = formatSchemaName(generatorName);
16+
): GeneratorDefinition[] => {
17+
const collection = formatJsonToJs<{
18+
schematics: Record<string, SchemaCollection>;
19+
}>(path);
20+
return Object.values(collection.schematics).map((schemaCollection) => {
21+
const originalName = schemaCollection.factory.split('./')[1];
2222
return {
23-
displayName,
24-
originalName: generatorName,
25-
description: schema.description,
23+
originalName,
24+
description: schemaCollection.description,
25+
displayName: formatSchemaName(originalName),
2626
};
27-
} catch (e) {
28-
console.log(`There is no schema for ${generatorName}`);
29-
return null;
30-
}
27+
});
3128
};
32-
export const getGeneratorNames = (path: string): string[] =>
33-
readdirSync(path)
34-
.map((name) => join(path, name))
35-
.map((directoryName) => directoryName.split(path + '/')[1]);
3629

3730
export const convertKeyToArgument = (key: string): string => {
3831
// Getting all capital letters -> 'inlineStyle' -> ['S']

0 commit comments

Comments
 (0)