@typeix/schematics 8.8.0-rc.1 → 8.9.1

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/README.md CHANGED
@@ -30,7 +30,6 @@ Typeix is an MIT-licensed open source project. It can grow thanks to the sponsor
30
30
  If you'd like to join them, please [read more here](https://docs.typeix.com/support).
31
31
 
32
32
  ## Stay in touch
33
- * Author - [Igor Ivanovic](https://twitter.com/igorzg1987)
34
33
  * Website - [https://typeix.com](https://typeix.com)
35
34
  * Twitter - [@typeixframework](https://twitter.com/typeixframework)
36
35
 
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "language": "js",
3
- "collection": "@nestjs/schematics",
3
+ "collection": "@typeix/schematics",
4
4
  "sourceRoot": "src"
5
5
  }
@@ -6,10 +6,13 @@ export declare class JSONFile {
6
6
  private readonly host;
7
7
  private readonly path;
8
8
  content: string;
9
- constructor(host: Tree, path: string);
10
9
  private _jsonAst;
11
- private get JsonAst();
10
+ private readonly formattingOptions;
11
+ constructor(host: Tree, path: string);
12
+ private get jsonAst();
12
13
  get(jsonPath: JSONPath): unknown;
13
14
  modify(jsonPath: JSONPath, value: JsonValue | undefined, insertInOrder?: InsertionIndex | false): void;
14
15
  remove(jsonPath: JSONPath): void;
16
+ private _initializeContent;
17
+ private _resolveInsertionIndex;
15
18
  }
@@ -6,15 +6,13 @@ class JSONFile {
6
6
  constructor(host, path) {
7
7
  this.host = host;
8
8
  this.path = path;
9
- const buffer = this.host.read(this.path);
10
- if (buffer) {
11
- this.content = buffer.toString();
12
- }
13
- else {
14
- throw new Error(`Could not read '${path}'.`);
15
- }
9
+ this.formattingOptions = {
10
+ insertSpaces: true,
11
+ tabSize: 2
12
+ };
13
+ this.content = this._initializeContent();
16
14
  }
17
- get JsonAst() {
15
+ get jsonAst() {
18
16
  if (this._jsonAst) {
19
17
  return this._jsonAst;
20
18
  }
@@ -29,7 +27,7 @@ class JSONFile {
29
27
  return this._jsonAst;
30
28
  }
31
29
  get(jsonPath) {
32
- const jsonAstNode = this.JsonAst;
30
+ const jsonAstNode = this.jsonAst;
33
31
  if (!jsonAstNode) {
34
32
  return undefined;
35
33
  }
@@ -40,29 +38,36 @@ class JSONFile {
40
38
  return node === undefined ? undefined : (0, jsonc_parser_1.getNodeValue)(node);
41
39
  }
42
40
  modify(jsonPath, value, insertInOrder) {
43
- let getInsertionIndex;
44
- if (insertInOrder === undefined) {
45
- const property = jsonPath.slice(-1)[0];
46
- getInsertionIndex = (properties) => [...properties, property].sort().findIndex((p) => p === property);
47
- }
48
- else if (insertInOrder !== false) {
49
- getInsertionIndex = insertInOrder;
50
- }
41
+ const getInsertionIndex = this._resolveInsertionIndex(jsonPath, insertInOrder);
51
42
  const edits = (0, jsonc_parser_1.modify)(this.content, jsonPath, value, {
52
43
  getInsertionIndex,
53
- formattingOptions: {
54
- insertSpaces: true,
55
- tabSize: 2
56
- }
44
+ formattingOptions: this.formattingOptions
57
45
  });
58
46
  this.content = (0, jsonc_parser_1.applyEdits)(this.content, edits);
59
47
  this.host.overwrite(this.path, this.content);
60
48
  this._jsonAst = undefined;
61
49
  }
62
50
  remove(jsonPath) {
63
- if (this.get(jsonPath) !== undefined) {
64
- this.modify(jsonPath, undefined);
51
+ this.modify(jsonPath, undefined);
52
+ }
53
+ _initializeContent() {
54
+ const buffer = this.host.read(this.path);
55
+ if (buffer) {
56
+ return buffer.toString();
57
+ }
58
+ else {
59
+ throw new Error(`Could not read '${this.path}'.`);
60
+ }
61
+ }
62
+ _resolveInsertionIndex(jsonPath, insertInOrder) {
63
+ if (insertInOrder === undefined) {
64
+ const property = jsonPath.slice(-1)[0];
65
+ return (properties) => [...properties, property].sort().findIndex((p) => p === property);
66
+ }
67
+ else if (insertInOrder !== false) {
68
+ return insertInOrder;
65
69
  }
70
+ return undefined;
66
71
  }
67
72
  }
68
73
  exports.JSONFile = JSONFile;
@@ -1,13 +1,18 @@
1
1
  import { DeclarationOptions } from "./module.declarator";
2
2
  export declare class MetadataManager {
3
3
  private content;
4
+ private static readonly PROPERTY_KIND;
5
+ private readonly source;
4
6
  constructor(content: string);
5
7
  insert(metadata: string, symbol: string, staticOptions?: DeclarationOptions["staticOptions"]): string | undefined;
6
- private findFirstDecoratorMetadata;
7
- private getSourceNodes;
8
+ private findModuleDecorator;
9
+ private findMatchingMetadataProperties;
10
+ private findDecoratorMetadata;
8
11
  private insertMetadataToEmptyModuleDecorator;
9
12
  private insertNewMetadataToDecorator;
10
13
  private insertSymbolToMetadata;
11
14
  private mergeSymbolAndExpr;
12
15
  private addBlankLines;
16
+ private getSourceNodes;
17
+ private insertTextAtPosition;
13
18
  }
@@ -5,171 +5,112 @@ const typescript_1 = require("typescript");
5
5
  class MetadataManager {
6
6
  constructor(content) {
7
7
  this.content = content;
8
+ this.source = (0, typescript_1.createSourceFile)("filename.ts", this.content, typescript_1.ScriptTarget.ES2017);
8
9
  }
9
10
  insert(metadata, symbol, staticOptions) {
10
- var _a;
11
- const source = (0, typescript_1.createSourceFile)("filename.ts", this.content, typescript_1.ScriptTarget.ES2017);
12
- const moduleDecoratorNode = (_a = this.findFirstDecoratorMetadata(source, "Module")) !== null && _a !== void 0 ? _a : this.findFirstDecoratorMetadata(source, "RootModule");
11
+ const moduleDecoratorNode = this.findModuleDecorator();
13
12
  if (!moduleDecoratorNode) {
14
- return;
13
+ return undefined;
15
14
  }
16
- const matchingProperties = moduleDecoratorNode.properties
17
- .filter((prop) => prop.kind === typescript_1.SyntaxKind.PropertyAssignment)
18
- .filter((prop) => {
19
- const name = prop.name;
20
- switch (name.kind) {
21
- case typescript_1.SyntaxKind.Identifier:
22
- return name.getText(source) === metadata;
23
- case typescript_1.SyntaxKind.StringLiteral:
24
- return name.text === metadata;
25
- default:
26
- return false;
27
- }
28
- });
15
+ const matchingProperties = this.findMatchingMetadataProperties(moduleDecoratorNode, metadata);
29
16
  symbol = this.mergeSymbolAndExpr(symbol, staticOptions);
30
- const addBlankLinesIfDynamic = () => {
31
- symbol = staticOptions ? this.addBlankLines(symbol) : symbol;
32
- };
33
17
  if (matchingProperties.length === 0) {
34
- const expr = moduleDecoratorNode;
35
- if (expr.properties.length === 0) {
36
- addBlankLinesIfDynamic();
37
- return this.insertMetadataToEmptyModuleDecorator(expr, metadata, symbol);
38
- }
39
- else {
40
- addBlankLinesIfDynamic();
41
- return this.insertNewMetadataToDecorator(expr, source, metadata, symbol);
42
- }
43
- }
44
- else {
45
- return this.insertSymbolToMetadata(source, matchingProperties, symbol, staticOptions);
18
+ return moduleDecoratorNode.properties.length === 0
19
+ ? this.insertMetadataToEmptyModuleDecorator(moduleDecoratorNode, metadata, symbol, staticOptions)
20
+ : this.insertNewMetadataToDecorator(moduleDecoratorNode, metadata, symbol, staticOptions);
46
21
  }
22
+ return this.insertSymbolToMetadata(matchingProperties, symbol, staticOptions);
47
23
  }
48
- findFirstDecoratorMetadata(source, identifier) {
49
- var _a;
50
- for (const node of this.getSourceNodes(source)) {
51
- const isDecoratorFactoryNode = node.kind === typescript_1.SyntaxKind.Decorator &&
52
- node.expression.kind === typescript_1.SyntaxKind.CallExpression;
53
- if (!isDecoratorFactoryNode) {
54
- continue;
55
- }
56
- const expr = node.expression;
57
- const isExpectedExpression = ((_a = expr.arguments[0]) === null || _a === void 0 ? void 0 : _a.kind) === typescript_1.SyntaxKind.ObjectLiteralExpression;
58
- if (!isExpectedExpression) {
59
- continue;
24
+ findModuleDecorator() {
25
+ return (this.findDecoratorMetadata(this.source, "Module") ||
26
+ this.findDecoratorMetadata(this.source, "RootModule"));
27
+ }
28
+ findMatchingMetadataProperties(node, key) {
29
+ return node.properties
30
+ .filter((prop) => prop.kind === MetadataManager.PROPERTY_KIND)
31
+ .filter((prop) => {
32
+ const name = prop.name;
33
+ if (name.kind === typescript_1.SyntaxKind.Identifier) {
34
+ return name.getText(this.source) === key;
60
35
  }
61
- if (expr.expression.kind === typescript_1.SyntaxKind.Identifier) {
62
- const escapedText = expr.expression.escapedText;
63
- const isTargetIdentifier = escapedText
64
- ? escapedText.toLowerCase() === identifier.toLowerCase()
65
- : true;
66
- if (isTargetIdentifier) {
67
- return expr.arguments[0];
68
- }
36
+ if (name.kind === typescript_1.SyntaxKind.StringLiteral) {
37
+ return name.text === key;
69
38
  }
70
- }
39
+ return false;
40
+ });
71
41
  }
72
- getSourceNodes(sourceFile) {
73
- const nodes = [sourceFile];
74
- const result = [];
75
- while (nodes.length > 0) {
76
- const node = nodes.shift();
77
- if (node) {
78
- result.push(node);
79
- if (node.getChildCount(sourceFile) >= 0) {
80
- nodes.unshift(...node.getChildren(sourceFile));
42
+ findDecoratorMetadata(source, identifier) {
43
+ var _a, _b;
44
+ for (const node of this.getSourceNodes(source)) {
45
+ if (node.kind === typescript_1.SyntaxKind.Decorator && node.expression.kind === typescript_1.SyntaxKind.CallExpression) {
46
+ const expr = node.expression;
47
+ if (((_a = expr.arguments[0]) === null || _a === void 0 ? void 0 : _a.kind) === typescript_1.SyntaxKind.ObjectLiteralExpression) {
48
+ const exprText = (_b = expr.expression.escapedText) === null || _b === void 0 ? void 0 : _b.toString().toLowerCase();
49
+ if (identifier.toLowerCase() === exprText) {
50
+ return expr.arguments[0];
51
+ }
81
52
  }
82
53
  }
83
54
  }
84
- return result;
55
+ return undefined;
85
56
  }
86
- insertMetadataToEmptyModuleDecorator(expr, metadata, symbol) {
57
+ insertMetadataToEmptyModuleDecorator(expr, metadata, symbol, staticOptions) {
87
58
  const position = expr.getEnd() - 1;
59
+ symbol = staticOptions ? this.addBlankLines(symbol) : symbol;
88
60
  const toInsert = ` ${metadata}: [${symbol}]`;
89
- return this.content.split("").reduce((content, char, index) => {
90
- if (index === position) {
91
- return `${content}\n${toInsert}\n${char}`;
92
- }
93
- else {
94
- return `${content}${char}`;
95
- }
96
- }, "");
61
+ return this.insertTextAtPosition(this.content, position, `\n${toInsert}\n`);
97
62
  }
98
- insertNewMetadataToDecorator(expr, source, metadata, symbol) {
99
- const node = expr.properties[expr.properties.length - 1];
100
- const position = node.getEnd();
101
- const text = node.getFullText(source);
102
- const matches = text.match(/^\r?\n\s*/);
103
- let toInsert;
104
- if (matches) {
105
- toInsert = `,${matches[0]}${metadata}: [${symbol}]`;
106
- }
107
- else {
108
- toInsert = `, ${metadata}: [${symbol}]`;
109
- }
110
- return this.content.split("").reduce((content, char, index) => {
111
- if (index === position) {
112
- return `${content}${toInsert}${char}`;
113
- }
114
- else {
115
- return `${content}${char}`;
116
- }
117
- }, "");
63
+ insertNewMetadataToDecorator(expr, metadata, symbol, staticOptions) {
64
+ var _a;
65
+ const lastProperty = expr.properties[expr.properties.length - 1];
66
+ const position = lastProperty.getEnd();
67
+ symbol = staticOptions ? this.addBlankLines(symbol) : symbol;
68
+ const separator = ((_a = lastProperty.getFullText(this.source).match(/^\r?\n\s+/)) === null || _a === void 0 ? void 0 : _a[0]) || " ";
69
+ const toInsert = `,${separator}${metadata}: [${symbol}]`;
70
+ return this.insertTextAtPosition(this.content, position, toInsert);
118
71
  }
119
- insertSymbolToMetadata(source, matchingProperties, symbol, staticOptions) {
120
- const assignment = matchingProperties[0];
121
- let node;
122
- const arrLiteral = assignment.initializer;
123
- if (!arrLiteral.elements) {
72
+ insertSymbolToMetadata(properties, symbol, staticOptions) {
73
+ var _a;
74
+ const assignment = properties[0];
75
+ const arrayLiteral = assignment.initializer;
76
+ if (!arrayLiteral.elements) {
124
77
  return this.content;
125
78
  }
126
- if (arrLiteral.elements.length === 0) {
127
- node = arrLiteral;
128
- }
129
- else {
130
- node = arrLiteral.elements;
131
- }
132
- if (Array.isArray(node)) {
133
- const nodeArray = node;
134
- const symbolsArray = nodeArray.map((childNode) => childNode.getText(source));
135
- if (symbolsArray.includes(symbol)) {
136
- return this.content;
137
- }
138
- node = node[node.length - 1];
139
- }
140
- let toInsert;
141
- let position = node.getEnd();
142
- if (node.kind === typescript_1.SyntaxKind.ArrayLiteralExpression) {
143
- position--;
144
- toInsert = staticOptions ? this.addBlankLines(symbol) : `${symbol}`;
145
- }
146
- else {
147
- const text = node.getFullText(source);
148
- const itemSeparator = (text.match(/^\r?\n(\r?)\s+/) ||
149
- text.match(/^\r?\n/) ||
150
- " ")[0];
151
- toInsert = `,${itemSeparator}${symbol}`;
152
- }
153
- return this.content.split("").reduce((content, char, index) => {
154
- if (index === position) {
155
- return `${content}${toInsert}${char}`;
156
- }
157
- else {
158
- return `${content}${char}`;
159
- }
160
- }, "");
79
+ const lastElement = arrayLiteral.elements.length > 0
80
+ ? arrayLiteral.elements[arrayLiteral.elements.length - 1]
81
+ : arrayLiteral;
82
+ const position = lastElement.getEnd() + (lastElement.kind === typescript_1.SyntaxKind.ArrayLiteralExpression ? -1 : 0);
83
+ const separator = ((_a = lastElement.getFullText(this.source).match(/^\r?\n\s+/)) === null || _a === void 0 ? void 0 : _a[0]) || " ";
84
+ const toInsert = arrayLiteral.elements.length === 0
85
+ ? staticOptions ? this.addBlankLines(symbol) : `${symbol}`
86
+ : `,${separator}${symbol}`;
87
+ return this.insertTextAtPosition(this.content, position, toInsert);
161
88
  }
162
89
  mergeSymbolAndExpr(symbol, staticOptions) {
163
90
  if (!staticOptions) {
164
91
  return symbol;
165
92
  }
166
- const spacing = 6;
167
- let options = JSON.stringify(staticOptions.value, null, spacing);
168
- symbol += `.${staticOptions.name}(${options})`;
169
- return symbol;
93
+ const options = JSON.stringify(staticOptions.value, null, 2);
94
+ return `${symbol}.${staticOptions.name}(${options})`;
170
95
  }
171
96
  addBlankLines(expr) {
172
97
  return `\n ${expr}\n `;
173
98
  }
99
+ getSourceNodes(source) {
100
+ const nodes = [source];
101
+ const result = [];
102
+ while (nodes.length > 0) {
103
+ const node = nodes.shift();
104
+ if (node) {
105
+ result.push(node);
106
+ nodes.unshift(...node.getChildren(source));
107
+ }
108
+ }
109
+ return result;
110
+ }
111
+ insertTextAtPosition(content, position, text) {
112
+ return content.substring(0, position) + text + content.substring(position);
113
+ }
174
114
  }
175
115
  exports.MetadataManager = MetadataManager;
116
+ MetadataManager.PROPERTY_KIND = typescript_1.SyntaxKind.PropertyAssignment;
@@ -2,9 +2,11 @@ import { DeclarationOptions } from "./module.declarator";
2
2
  import { PathSolver } from "./path.solver";
3
3
  export declare class ModuleImportDeclarator {
4
4
  private solver;
5
+ private static readonly IMPORT_STATEMENT_REGEX;
5
6
  constructor(solver?: PathSolver);
6
7
  declare(content: string, options: DeclarationOptions): string;
7
- private findImportsEndpoint;
8
- private buildLineToInsert;
8
+ private buildImportStatement;
9
9
  private computeRelativePath;
10
+ private normalizeImportModulePath;
11
+ private findLastImportIndex;
10
12
  }
@@ -8,32 +8,30 @@ class ModuleImportDeclarator {
8
8
  this.solver = solver;
9
9
  }
10
10
  declare(content, options) {
11
- const toInsert = this.buildLineToInsert(options);
11
+ const importStatement = this.buildImportStatement(options);
12
12
  const contentLines = content.split("\n");
13
- const finalImportIndex = this.findImportsEndpoint(contentLines);
14
- contentLines.splice(finalImportIndex + 1, 0, toInsert);
13
+ const lastImportIndex = this.findLastImportIndex(contentLines);
14
+ contentLines.splice(lastImportIndex + 1, 0, importStatement);
15
15
  return contentLines.join("\n");
16
16
  }
17
- findImportsEndpoint(contentLines) {
18
- const reversedContent = Array.from(contentLines).reverse();
19
- const reverseImports = reversedContent.filter(line => line.match(/\} from ('|")/));
20
- if (reverseImports.length <= 0) {
21
- return 0;
22
- }
23
- return contentLines.indexOf(reverseImports[0]);
24
- }
25
- buildLineToInsert(options) {
17
+ buildImportStatement(options) {
26
18
  return `import {${options.symbol}} from "${this.computeRelativePath(options)}";`;
27
19
  }
28
20
  computeRelativePath(options) {
29
- let importModulePath;
30
- if (options.type !== undefined) {
31
- importModulePath = (0, core_1.normalize)(`/${options.path}/${options.name}.${options.type}`);
32
- }
33
- else {
34
- importModulePath = (0, core_1.normalize)(`/${options.path}/${options.name}`);
35
- }
21
+ const importModulePath = this.normalizeImportModulePath(options);
36
22
  return this.solver.relative(options.module, importModulePath);
37
23
  }
24
+ normalizeImportModulePath(options) {
25
+ const filePath = `/${options.path}/${options.name}`;
26
+ return (0, core_1.normalize)(options.type ? `${filePath}.${options.type}` : filePath);
27
+ }
28
+ findLastImportIndex(contentLines) {
29
+ const reverseImports = contentLines
30
+ .slice()
31
+ .reverse()
32
+ .find(line => ModuleImportDeclarator.IMPORT_STATEMENT_REGEX.test(line));
33
+ return reverseImports ? contentLines.indexOf(reverseImports) : 0;
34
+ }
38
35
  }
39
36
  exports.ModuleImportDeclarator = ModuleImportDeclarator;
37
+ ModuleImportDeclarator.IMPORT_STATEMENT_REGEX = /\} from ('|")/;
@@ -1,4 +1,5 @@
1
1
  import { DeclarationOptions } from "./module.declarator";
2
2
  export declare class ModuleMetadataDeclarator {
3
- declare(content: string, options: DeclarationOptions): string;
3
+ declare(moduleSource: string, declarationOpts: DeclarationOptions): string;
4
+ private insertMetadata;
4
5
  }
@@ -3,10 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ModuleMetadataDeclarator = void 0;
4
4
  const metadata_manager_1 = require("./metadata.manager");
5
5
  class ModuleMetadataDeclarator {
6
- declare(content, options) {
6
+ declare(moduleSource, declarationOpts) {
7
+ return this.insertMetadata(moduleSource, declarationOpts) || moduleSource;
8
+ }
9
+ insertMetadata(content, options) {
7
10
  const manager = new metadata_manager_1.MetadataManager(content);
8
- const inserted = manager.insert(options.metadata, options.symbol, options.staticOptions);
9
- return inserted !== null && inserted !== void 0 ? inserted : content;
11
+ return manager.insert(options.metadata, options.symbol, options.staticOptions);
10
12
  }
11
13
  }
12
14
  exports.ModuleMetadataDeclarator = ModuleMetadataDeclarator;
@@ -17,7 +17,10 @@ export interface DeclarationOptions {
17
17
  export declare class ModuleDeclarator {
18
18
  private imports;
19
19
  private metadata;
20
+ private static readonly DEFAULT_IMPORTS;
21
+ private static readonly DEFAULT_METADATA;
20
22
  constructor(imports?: ModuleImportDeclarator, metadata?: ModuleMetadataDeclarator);
21
23
  declare(content: string, options: DeclarationOptions): string;
22
24
  private computeSymbol;
25
+ private determineSymbol;
23
26
  }
@@ -5,28 +5,31 @@ const strings_1 = require("@angular-devkit/core/src/utils/strings");
5
5
  const module_import_declarator_1 = require("./module-import.declarator");
6
6
  const module_metadata_declarator_1 = require("./module-metadata.declarator");
7
7
  class ModuleDeclarator {
8
- constructor(imports = new module_import_declarator_1.ModuleImportDeclarator(), metadata = new module_metadata_declarator_1.ModuleMetadataDeclarator()) {
8
+ constructor(imports = ModuleDeclarator.DEFAULT_IMPORTS, metadata = ModuleDeclarator.DEFAULT_METADATA) {
9
9
  this.imports = imports;
10
10
  this.metadata = metadata;
11
11
  }
12
12
  declare(content, options) {
13
- options = this.computeSymbol(options);
14
- content = this.imports.declare(content, options);
15
- content = this.metadata.declare(content, options);
16
- return content;
13
+ const enrichedOptions = this.computeSymbol(options);
14
+ const updatedContent = this.metadata.declare(this.imports.declare(content, enrichedOptions), enrichedOptions);
15
+ return updatedContent;
17
16
  }
18
17
  computeSymbol(options) {
19
- const target = Object.assign({}, options);
20
- if (options.className) {
21
- target.symbol = options.className;
22
- }
23
- else if (options.type !== undefined) {
24
- target.symbol = (0, strings_1.classify)(options.name).concat((0, strings_1.capitalize)(options.type));
18
+ return {
19
+ ...options,
20
+ symbol: this.determineSymbol(options)
21
+ };
22
+ }
23
+ determineSymbol({ className, type, name }) {
24
+ if (className) {
25
+ return className;
25
26
  }
26
- else {
27
- target.symbol = (0, strings_1.classify)(options.name);
27
+ if (type !== undefined) {
28
+ return (0, strings_1.classify)(name).concat((0, strings_1.capitalize)(type));
28
29
  }
29
- return target;
30
+ return (0, strings_1.classify)(name);
30
31
  }
31
32
  }
32
33
  exports.ModuleDeclarator = ModuleDeclarator;
34
+ ModuleDeclarator.DEFAULT_IMPORTS = new module_import_declarator_1.ModuleImportDeclarator();
35
+ ModuleDeclarator.DEFAULT_METADATA = new module_metadata_declarator_1.ModuleMetadataDeclarator();
@@ -9,5 +9,6 @@ export declare class ModuleFinder {
9
9
  private tree;
10
10
  constructor(tree: Tree);
11
11
  find(options: FindOptions): Path | null;
12
- private findIn;
12
+ private findModuleInDirectory;
13
+ private getModuleFilename;
13
14
  }
@@ -7,18 +7,21 @@ class ModuleFinder {
7
7
  this.tree = tree;
8
8
  }
9
9
  find(options) {
10
- const generatedDirectoryPath = options.path;
11
- const generatedDirectory = this.tree.getDir(generatedDirectoryPath);
12
- return this.findIn(generatedDirectory);
10
+ const dirPath = options.path;
11
+ const directory = this.tree.getDir(dirPath);
12
+ return this.findModuleInDirectory(directory);
13
13
  }
14
- findIn(directory) {
14
+ findModuleInDirectory(directory) {
15
15
  if (!directory) {
16
16
  return null;
17
17
  }
18
- const moduleFilename = directory.subfiles.find(filename => /\.module\.(t|j)s$/.test(filename));
19
- return moduleFilename !== undefined
18
+ const moduleFilename = this.getModuleFilename(directory);
19
+ return moduleFilename
20
20
  ? (0, core_1.join)(directory.path, moduleFilename.valueOf())
21
- : this.findIn(directory.parent);
21
+ : this.findModuleInDirectory(directory.parent);
22
+ }
23
+ getModuleFilename(directory) {
24
+ return directory.subfiles.find((filename) => /\.module\.(t|j)s$/.test(filename));
22
25
  }
23
26
  }
24
27
  exports.ModuleFinder = ModuleFinder;
@@ -8,5 +8,7 @@ export interface Location {
8
8
  path: Path;
9
9
  }
10
10
  export declare class NameParser {
11
+ private static readonly DEFAULT_PATH_SEPARATOR;
11
12
  parse(options: ParseOptions): Location;
13
+ private constructNamePath;
12
14
  }
@@ -4,14 +4,19 @@ exports.NameParser = void 0;
4
4
  const core_1 = require("@angular-devkit/core");
5
5
  class NameParser {
6
6
  parse(options) {
7
- const nameWithoutPath = (0, core_1.basename)(options.name);
8
- const namePath = (0, core_1.dirname)((options.path === undefined ? "" : options.path)
9
- .concat("/")
10
- .concat(options.name));
7
+ const baseName = (0, core_1.basename)(options.name);
8
+ const namePath = this.constructNamePath(options);
11
9
  return {
12
- name: nameWithoutPath,
13
- path: (0, core_1.normalize)("/".concat(namePath))
10
+ name: baseName,
11
+ path: (0, core_1.normalize)(NameParser.DEFAULT_PATH_SEPARATOR.concat(namePath))
14
12
  };
15
13
  }
14
+ constructNamePath(options) {
15
+ const combinedPath = (options.path || "")
16
+ .concat(NameParser.DEFAULT_PATH_SEPARATOR)
17
+ .concat(options.name);
18
+ return (0, core_1.dirname)(combinedPath);
19
+ }
16
20
  }
17
21
  exports.NameParser = NameParser;
22
+ NameParser.DEFAULT_PATH_SEPARATOR = "/";
@@ -1,4 +1,6 @@
1
1
  import { Path } from "@angular-devkit/core";
2
2
  export declare class PathSolver {
3
+ private static readonly PLACEHOLDER;
3
4
  relative(from: Path, to: Path): string;
5
+ private getRelativeDirectory;
4
6
  }
@@ -4,11 +4,14 @@ exports.PathSolver = void 0;
4
4
  const core_1 = require("@angular-devkit/core");
5
5
  class PathSolver {
6
6
  relative(from, to) {
7
- const placeholder = "/placeholder";
8
- const relativeDir = (0, core_1.relative)((0, core_1.dirname)((placeholder + from)), (0, core_1.dirname)((placeholder + to)));
9
- return (relativeDir.startsWith(".")
10
- ? relativeDir
11
- : "./" + relativeDir).concat(relativeDir.length === 0 ? (0, core_1.basename)(to) : "/" + (0, core_1.basename)(to));
7
+ const relativePath = this.getRelativeDirectory(from, to);
8
+ return `${relativePath.startsWith(".") ? relativePath : `./${relativePath}`}`.concat(relativePath.length === 0 ? "" : "/", (0, core_1.basename)(to));
9
+ }
10
+ getRelativeDirectory(from, to) {
11
+ const baseFrom = (0, core_1.dirname)((PathSolver.PLACEHOLDER + from));
12
+ const baseTo = (0, core_1.dirname)((PathSolver.PLACEHOLDER + to));
13
+ return (0, core_1.relative)(baseFrom, baseTo);
12
14
  }
13
15
  }
14
16
  exports.PathSolver = PathSolver;
17
+ PathSolver.PLACEHOLDER = "/placeholder";
@@ -1,5 +1,5 @@
1
1
  import { Rule, Tree } from "@angular-devkit/schematics";
2
- export declare function isInRootDirectory(host: Tree, extraFiles?: string[]): boolean;
2
+ export declare function doesTreeContainFiles(host: Tree, extraFiles?: string[]): boolean;
3
3
  export declare function mergeSourceRoot<T extends {
4
4
  sourceRoot?: string;
5
5
  path?: string;
@@ -1,24 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isInRootDirectory = isInRootDirectory;
3
+ exports.doesTreeContainFiles = doesTreeContainFiles;
4
4
  exports.mergeSourceRoot = mergeSourceRoot;
5
5
  const core_1 = require("@angular-devkit/core");
6
6
  const defaults_1 = require("../lib/defaults");
7
- function isInRootDirectory(host, extraFiles = []) {
8
- const files = ["typeix-cli.json", "typeix.json"].concat(extraFiles || []);
9
- return files.map(file => host.exists(file)).some(isPresent => isPresent);
7
+ const DEFAULT_ROOT_FILES = ["typeix-cli.json", "typeix.json"];
8
+ function doesTreeContainFiles(host, extraFiles = []) {
9
+ const filesToCheck = [...DEFAULT_ROOT_FILES, ...extraFiles];
10
+ return filesToCheck.some(file => host.exists(file));
10
11
  }
11
12
  function mergeSourceRoot(options) {
13
+ const calculatePath = (sourceRoot, path) => path !== undefined ? (0, core_1.join)((0, core_1.normalize)(sourceRoot), path) : (0, core_1.normalize)(sourceRoot);
12
14
  return (host) => {
13
- const isInRoot = isInRootDirectory(host, ["tsconfig.json", "package.json"]);
15
+ var _a;
16
+ const isInRoot = doesTreeContainFiles(host, ["tsconfig.json", "package.json"]);
14
17
  if (!isInRoot) {
15
18
  return host;
16
19
  }
17
- const defaultSourceRoot = options.sourceRoot !== undefined ? options.sourceRoot : defaults_1.DEFAULT_PATH_NAME;
18
- options.path =
19
- options.path !== undefined
20
- ? (0, core_1.join)((0, core_1.normalize)(defaultSourceRoot), options.path)
21
- : (0, core_1.normalize)(defaultSourceRoot);
20
+ const defaultSourceRoot = (_a = options.sourceRoot) !== null && _a !== void 0 ? _a : defaults_1.DEFAULT_PATH_NAME;
21
+ options.path = calculatePath(defaultSourceRoot, options.path);
22
22
  return host;
23
23
  };
24
24
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@typeix/schematics",
3
3
  "testRunner": "jest-jasmine2",
4
- "version": "8.8.0-rc.1",
4
+ "version": "8.9.1",
5
5
  "files": [
6
6
  "dist/**/*"
7
7
  ],
@@ -22,7 +22,7 @@
22
22
  "devDependencies": {
23
23
  "@commitlint/cli": "~17.8.1",
24
24
  "@commitlint/config-angular": "~17.8.1",
25
- "@typeix/resty": "^8.8.0-rc.1",
25
+ "@typeix/resty": "^8.9.1",
26
26
  "@types/fs-extra": "~11.0.4",
27
27
  "@types/jest": "~29.5.14",
28
28
  "cpx": "~1.5.0",
@@ -50,5 +50,5 @@
50
50
  "bugs": {
51
51
  "url": "https://github.com/typeix/schematics/issues"
52
52
  },
53
- "gitHead": "8d95e8382394eeb45075731f936f44cd1766047c"
53
+ "gitHead": "df6da4cae5e0c269f673c948ce61ad2ae7f37382"
54
54
  }