@ng-atomic/schematics 4.3.0 → 4.3.2
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/collection.json +1 -1
- package/package.json +1 -1
- package/src/gpt3/distance.d.ts +4 -0
- package/src/gpt3/distance.js +64 -0
- package/src/gpt3/distance.js.map +1 -0
- package/src/gpt3/distance.spec.ts +45 -0
- package/src/gpt3/helpers.d.ts +0 -2
- package/src/gpt3/helpers.js +3 -56
- package/src/gpt3/helpers.js.map +1 -1
- package/src/gpt3/helpers.spec.ts +11 -17
- package/src/gpt3/index.d.ts +6 -4
- package/src/gpt3/index.js +19 -77
- package/src/gpt3/index.js.map +1 -1
- package/src/gpt3/index.spec.ts +11 -12
- package/src/gpt3/prompter.d.ts +22 -0
- package/src/gpt3/prompter.js +79 -0
- package/src/gpt3/prompter.js.map +1 -0
- package/src/gpt3/prompter.spec.ts +28 -0
- package/src/gpt3/schema.json +14 -5
- package/src/gpt3/schematics-x.d.ts +10 -0
- package/src/gpt3/schematics-x.js +75 -0
- package/src/gpt3/schematics-x.js.map +1 -0
- package/src/gpt3/schematics-x.spec.ts +42 -0
package/collection.json
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function standarization(str1: string, str2: string): [string, string];
|
|
2
|
+
export declare function calculateDistance(str1: string, str2: string): number;
|
|
3
|
+
export declare function convertByWords(str: string, words: string[]): string;
|
|
4
|
+
export declare function parseFilePath(filePath: string, characters?: string[]): string[];
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseFilePath = exports.convertByWords = exports.calculateDistance = exports.standarization = void 0;
|
|
4
|
+
function standarization(str1, str2) {
|
|
5
|
+
const wordSet = new Set([str1, str2].map(path => parseFilePath(path)).flat());
|
|
6
|
+
wordSet.delete('.');
|
|
7
|
+
wordSet.delete('/');
|
|
8
|
+
return [convertByWords(str1, [...wordSet]), convertByWords(str2, [...wordSet])];
|
|
9
|
+
}
|
|
10
|
+
exports.standarization = standarization;
|
|
11
|
+
function calculateDistance(str1, str2) {
|
|
12
|
+
const [s1, s2] = standarization(str1, str2);
|
|
13
|
+
return levenshteinDistance(s1, s2) + depthDistance(str1, str2);
|
|
14
|
+
}
|
|
15
|
+
exports.calculateDistance = calculateDistance;
|
|
16
|
+
const LANG_MAP = `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=[]{};:'",.<>/?|\\~\` `.split('');
|
|
17
|
+
function convertByWords(str, words) {
|
|
18
|
+
return parseFilePath(str).map(c => ~words.indexOf(c) ? LANG_MAP[words.indexOf(c)] : c).join('');
|
|
19
|
+
}
|
|
20
|
+
exports.convertByWords = convertByWords;
|
|
21
|
+
function parseFilePath(filePath, characters = ['.', '/']) {
|
|
22
|
+
const result = [];
|
|
23
|
+
let word = '';
|
|
24
|
+
for (let i = 0; i < filePath.length; i++) {
|
|
25
|
+
const char = filePath[i];
|
|
26
|
+
if (characters.includes(char)) {
|
|
27
|
+
if (word.length)
|
|
28
|
+
result.push(word);
|
|
29
|
+
result.push(char);
|
|
30
|
+
word = '';
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
word += char;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
result.push(word);
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
exports.parseFilePath = parseFilePath;
|
|
40
|
+
function depthDistance(str1, str2) {
|
|
41
|
+
const depth1 = str1.split('/').length;
|
|
42
|
+
const depth2 = str2.split('/').length;
|
|
43
|
+
return Math.abs(depth1 - depth2);
|
|
44
|
+
}
|
|
45
|
+
function levenshteinDistance(str1, str2) {
|
|
46
|
+
const cost = (a, b) => {
|
|
47
|
+
return a === b ? 0 : 1;
|
|
48
|
+
};
|
|
49
|
+
const d = [];
|
|
50
|
+
for (let i = 0; i <= str1.length; i++) {
|
|
51
|
+
d[i] = [];
|
|
52
|
+
d[i][0] = i;
|
|
53
|
+
}
|
|
54
|
+
for (let j = 0; j <= str2.length; j++) {
|
|
55
|
+
d[0][j] = j;
|
|
56
|
+
}
|
|
57
|
+
for (let i = 1; i <= str1.length; i++) {
|
|
58
|
+
for (let j = 1; j <= str2.length; j++) {
|
|
59
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost(str1[i - 1], str2[j - 1]));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return d[str1.length][str2.length];
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=distance.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"distance.js","sourceRoot":"","sources":["../../../../../libs/schematics/src/gpt3/distance.ts"],"names":[],"mappings":";;;AAAA,SAAgB,cAAc,CAAC,IAAY,EAAE,IAAY;IACvD,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;IAC9E,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACpB,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACpB,OAAO,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,EAAE,cAAc,CAAC,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAClF,CAAC;AALD,wCAKC;AAED,SAAgB,iBAAiB,CAAC,IAAY,EAAE,IAAY;IAC1D,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,cAAc,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAC5C,OAAO,mBAAmB,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,aAAa,CAAC,IA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C,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CACjD,CAAC;SACH;KACF;IAED,OAAO,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { calculateDistance, convertByWords, parseFilePath, standarization } from './distance';
|
|
2
|
+
|
|
3
|
+
describe('standarization', () => {
|
|
4
|
+
it('', () => {
|
|
5
|
+
expect(standarization(
|
|
6
|
+
'/libs/example/src/lib/domain/models/first-model.ts',
|
|
7
|
+
'/libs/example/src/lib/domain/models/second-model.ts',
|
|
8
|
+
)).toEqual([
|
|
9
|
+
'/0/1/2/3/4/5/6.7',
|
|
10
|
+
'/0/1/2/3/4/5/8.7',
|
|
11
|
+
])
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe('calculateDistance()', () => {
|
|
16
|
+
it('', () => {
|
|
17
|
+
expect(calculateDistance(
|
|
18
|
+
'/libs/example/src/lib/domain/models/first-model.ts',
|
|
19
|
+
'/libs/example/src/lib/domain/models/second-model.ts',
|
|
20
|
+
)).toEqual(1)
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('', () => {
|
|
24
|
+
expect(calculateDistance(
|
|
25
|
+
'/libs/example/src/lib/domain/models/first-model.component.ts',
|
|
26
|
+
'/libs/example/src/lib/domain/models/second-model.module.ts',
|
|
27
|
+
)).toEqual(2)
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('parseFilePath', () => {
|
|
32
|
+
it('should parse file path', () => {
|
|
33
|
+
expect(parseFilePath('/projects/app/src/app/_shared/components/example/example.component.ts')).toEqual([
|
|
34
|
+
'/', 'projects', '/', 'app', '/', 'src', '/', 'app', '/', '_shared', '/',
|
|
35
|
+
'components', '/', 'example', '/', 'example', '.', 'component', '.', 'ts'
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('convertByWords', () => {
|
|
41
|
+
xit('should convert from a string to a number', () => {
|
|
42
|
+
expect(convertByWords('this/is/test/path', ['this', 'is', 'test', 'path'])).toBe('0/1/2/3');
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
package/src/gpt3/helpers.d.ts
CHANGED
package/src/gpt3/helpers.js
CHANGED
|
@@ -1,64 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getEstimateSimilarFilePaths =
|
|
4
|
-
|
|
5
|
-
const result = [];
|
|
6
|
-
let word = '';
|
|
7
|
-
for (let i = 0; i < filePath.length; i++) {
|
|
8
|
-
const char = filePath[i];
|
|
9
|
-
if (characters.includes(char)) {
|
|
10
|
-
if (word.length)
|
|
11
|
-
result.push(word);
|
|
12
|
-
result.push(char);
|
|
13
|
-
word = '';
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
word += char;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
result.push(word);
|
|
20
|
-
return result;
|
|
21
|
-
}
|
|
22
|
-
exports.parseFilePath = parseFilePath;
|
|
23
|
-
const convertByWords = (str, words) => {
|
|
24
|
-
return parseFilePath(str).map(c => ~words.indexOf(c) ? words.indexOf(c) : c).join('');
|
|
25
|
-
};
|
|
26
|
-
exports.convertByWords = convertByWords;
|
|
3
|
+
exports.getEstimateSimilarFilePaths = void 0;
|
|
4
|
+
const distance_1 = require("./distance");
|
|
27
5
|
function getEstimateSimilarFilePaths(path, files) {
|
|
28
|
-
const
|
|
29
|
-
wordSet.delete('.');
|
|
30
|
-
wordSet.delete('/');
|
|
31
|
-
const words = Array.from(wordSet);
|
|
32
|
-
const results = files.map(file => {
|
|
33
|
-
const distance = levenshteinDistance((0, exports.convertByWords)(path, words), (0, exports.convertByWords)(file, words)) + depthDistance(path, file);
|
|
34
|
-
return [distance, file];
|
|
35
|
-
});
|
|
6
|
+
const results = files.map(file => [(0, distance_1.calculateDistance)(path, file), file]);
|
|
36
7
|
const min = Math.min(...results.map(([distance]) => distance));
|
|
37
8
|
return results.filter(([distance]) => distance === min).map(([_, file]) => file);
|
|
38
9
|
}
|
|
39
10
|
exports.getEstimateSimilarFilePaths = getEstimateSimilarFilePaths;
|
|
40
|
-
function depthDistance(str1, str2) {
|
|
41
|
-
const depth1 = str1.split('/').length;
|
|
42
|
-
const depth2 = str2.split('/').length;
|
|
43
|
-
return Math.abs(depth1 - depth2);
|
|
44
|
-
}
|
|
45
|
-
function levenshteinDistance(str1, str2) {
|
|
46
|
-
const cost = (a, b) => {
|
|
47
|
-
return a === b ? 0 : 1;
|
|
48
|
-
};
|
|
49
|
-
const d = [];
|
|
50
|
-
for (let i = 0; i <= str1.length; i++) {
|
|
51
|
-
d[i] = [];
|
|
52
|
-
d[i][0] = i;
|
|
53
|
-
}
|
|
54
|
-
for (let j = 0; j <= str2.length; j++) {
|
|
55
|
-
d[0][j] = j;
|
|
56
|
-
}
|
|
57
|
-
for (let i = 1; i <= str1.length; i++) {
|
|
58
|
-
for (let j = 1; j <= str2.length; j++) {
|
|
59
|
-
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost(str1[i - 1], str2[j - 1]));
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return d[str1.length][str2.length];
|
|
63
|
-
}
|
|
64
11
|
//# sourceMappingURL=helpers.js.map
|
package/src/gpt3/helpers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../../../libs/schematics/src/gpt3/helpers.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../../../libs/schematics/src/gpt3/helpers.ts"],"names":[],"mappings":";;;AAAA,yCAA+C;AAE/C,SAAgB,2BAA2B,CAAC,IAAY,EAAE,KAAe;IACvE,MAAM,OAAO,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,IAAA,4BAAiB,EAAC,IAAI,EAAE,IAAI,CAAC,EAAE,IAAI,CAAqB,CAAC,CAAC;IAC7F,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC/D,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,QAAQ,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC;AACnF,CAAC;AAJD,kEAIC"}
|
package/src/gpt3/helpers.spec.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getEstimateSimilarFilePaths
|
|
1
|
+
import { getEstimateSimilarFilePaths } from './helpers';
|
|
2
2
|
|
|
3
3
|
describe('getEstimateSimilarFilePaths', () => {
|
|
4
4
|
const FILES = [
|
|
@@ -18,8 +18,18 @@ describe('getEstimateSimilarFilePaths', () => {
|
|
|
18
18
|
'/projects/app/src/app/_shared/components/test/test.component.ts',
|
|
19
19
|
'/projects/app/src/app/_shared/components/test/test.stories.ts',
|
|
20
20
|
'/projects/app/src/app/_shared/components/test/index.ts',
|
|
21
|
+
'/libs/nm-common/src/lib/domain/models/invoice.ts',
|
|
22
|
+
'/libs/nm-common/src/lib/domain/models/contract.ts',
|
|
21
23
|
];
|
|
22
24
|
|
|
25
|
+
it('', () => {
|
|
26
|
+
const result = getEstimateSimilarFilePaths('/libs/nm-common/src/lib/domain/models/customer.ts', FILES);
|
|
27
|
+
expect(result).toEqual([
|
|
28
|
+
'/libs/nm-common/src/lib/domain/models/invoice.ts',
|
|
29
|
+
'/libs/nm-common/src/lib/domain/models/contract.ts',
|
|
30
|
+
]);
|
|
31
|
+
});
|
|
32
|
+
|
|
23
33
|
it('', () => {
|
|
24
34
|
const file = '/projects/app/src/app/_shared/components/expected/expected.component.ts';
|
|
25
35
|
const files = getEstimateSimilarFilePaths(file, FILES);
|
|
@@ -38,19 +48,3 @@ describe('getEstimateSimilarFilePaths', () => {
|
|
|
38
48
|
]);
|
|
39
49
|
});
|
|
40
50
|
});
|
|
41
|
-
|
|
42
|
-
describe('parseFilePath', () => {
|
|
43
|
-
it('should parse file path', () => {
|
|
44
|
-
expect(parseFilePath('/projects/app/src/app/_shared/components/example/example.component.ts')).toEqual([
|
|
45
|
-
'/', 'projects', '/', 'app', '/', 'src', '/', 'app', '/', '_shared', '/',
|
|
46
|
-
'components', '/', 'example', '/', 'example', '.', 'component', '.', 'ts'
|
|
47
|
-
]);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
describe('convertByWords', () => {
|
|
52
|
-
xit('should convert from a string to a number', () => {
|
|
53
|
-
expect(convertByWords('this/is/test/path', ['this', 'is', 'test', 'path'])).toBe('0/1/2/3');
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
|
package/src/gpt3/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { Rule,
|
|
1
|
+
import { Rule, Tree } from '@angular-devkit/schematics';
|
|
2
2
|
import { Schema } from '../atomic-component/schema';
|
|
3
|
+
export declare function getFilePaths(tree: Tree, path?: string): string[];
|
|
4
|
+
export declare function resolvePath(tree: any, options: {
|
|
5
|
+
project?: string;
|
|
6
|
+
path?: string;
|
|
7
|
+
}): Promise<string>;
|
|
3
8
|
export declare const gpt3: (options: Schema) => Rule;
|
|
4
|
-
export declare function completeToJson(text: string): string[];
|
|
5
|
-
export declare function parseJsonCodeBlock(text: string): string[];
|
|
6
|
-
export declare function parseCodeBlocks(text: string): FileEntry[];
|
package/src/gpt3/index.js
CHANGED
|
@@ -1,91 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.gpt3 = exports.resolvePath = exports.getFilePaths = void 0;
|
|
4
4
|
const schematics_1 = require("@angular-devkit/schematics");
|
|
5
5
|
const workspace_1 = require("@schematics/angular/utility/workspace");
|
|
6
|
-
const openai_1 = require("openai");
|
|
7
6
|
const path_1 = require("path");
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
const schematics_x_1 = require("./schematics-x");
|
|
8
|
+
function getFilePaths(tree, path = '/') {
|
|
9
|
+
return getFiles(tree.getDir(path)).map(p => (0, path_1.join)(path, p));
|
|
10
|
+
}
|
|
11
|
+
exports.getFilePaths = getFilePaths;
|
|
12
|
+
async function resolvePath(tree, options) {
|
|
13
|
+
const defaultPath = await (0, workspace_1.createDefaultPath)(tree, options.project);
|
|
14
|
+
return (0, path_1.join)(defaultPath, options?.path ?? '');
|
|
15
|
+
}
|
|
16
|
+
exports.resolvePath = resolvePath;
|
|
11
17
|
const gpt3 = (options) => async (tree) => {
|
|
12
|
-
options.path =
|
|
13
|
-
const files =
|
|
18
|
+
options.path = await resolvePath(tree, options);
|
|
19
|
+
const files = getFilePaths(tree, options.path);
|
|
14
20
|
const path = (0, path_1.join)(tree.root.path, options.path, options.name);
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
const schematicsX = new schematics_x_1.SchematicsX();
|
|
22
|
+
const entries = await schematicsX.generate(files.map(file => tree.get(file)), path);
|
|
23
|
+
for (const entry of entries) {
|
|
24
|
+
if (!tree.exists(entry.path)) {
|
|
25
|
+
tree.create(entry.path, entry.content);
|
|
26
|
+
}
|
|
20
27
|
}
|
|
21
28
|
return (0, schematics_1.chain)([]);
|
|
22
29
|
};
|
|
23
30
|
exports.gpt3 = gpt3;
|
|
24
|
-
async function getPredicatedFiles(files, dirPath) {
|
|
25
|
-
const fileStart = dirPath.split('/').slice(-1)[0];
|
|
26
|
-
const prompt = makeDirectoryPrompt(files, (0, path_1.join)(dirPath, fileStart));
|
|
27
|
-
const res = await openai.createCompletion({
|
|
28
|
-
model: 'code-davinci-002',
|
|
29
|
-
prompt,
|
|
30
|
-
temperature: 0,
|
|
31
|
-
max_tokens: 512,
|
|
32
|
-
stop: '\n\`\`\`',
|
|
33
|
-
});
|
|
34
|
-
const results = completeToJson(`${prompt}${res.data.choices?.[0].text}`);
|
|
35
|
-
return results.filter(result => result.startsWith(dirPath));
|
|
36
|
-
}
|
|
37
|
-
async function getPredicatedFileEntry(path, fileEntries = []) {
|
|
38
|
-
const prompt = makeFilePrompt(fileEntries, path);
|
|
39
|
-
const res = await openai.createCompletion({
|
|
40
|
-
model: 'code-davinci-002',
|
|
41
|
-
prompt,
|
|
42
|
-
temperature: 0,
|
|
43
|
-
max_tokens: 256,
|
|
44
|
-
stop: '\n\`\`\`',
|
|
45
|
-
});
|
|
46
|
-
const entries = parseCodeBlocks(`${prompt}${res.data.choices?.[0].text}`);
|
|
47
|
-
return entries.find(entry => entry.path === path);
|
|
48
|
-
}
|
|
49
|
-
function makeDirectoryPrompt(files, name) {
|
|
50
|
-
return `\n\`\`\`tree.json\n[\n${files.map(file => `\t\"${file}\",`).join('\n')}\n\t\"${name}`;
|
|
51
|
-
}
|
|
52
|
-
function makeFilePrompt(files, path) {
|
|
53
|
-
return `${makeFileCodeBlocks(files)}\n\n\`\`\`${path}\n`;
|
|
54
|
-
}
|
|
55
|
-
function makeFileCodeBlocks(files) {
|
|
56
|
-
return files.map(file => makeFileCodeBlock(file.path, file.content.toString())).join('\n\n');
|
|
57
|
-
}
|
|
58
|
-
function makeFileCodeBlock(name, content) {
|
|
59
|
-
return `\`\`\`${name}\n${content}\n\`\`\``;
|
|
60
|
-
}
|
|
61
|
-
function completeToJson(text) {
|
|
62
|
-
while (text.length) {
|
|
63
|
-
let suffixes = ['"]\n```', ']\n```', '\n```', '```', '``', '`', ''];
|
|
64
|
-
for (const suffix of suffixes) {
|
|
65
|
-
try {
|
|
66
|
-
return parseJsonCodeBlock(text + suffix);
|
|
67
|
-
}
|
|
68
|
-
catch { }
|
|
69
|
-
}
|
|
70
|
-
text = text.slice(0, -1);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
exports.completeToJson = completeToJson;
|
|
74
|
-
function parseJsonCodeBlock(text) {
|
|
75
|
-
const code = getCodeBlocks(text);
|
|
76
|
-
return JSON.parse(code);
|
|
77
|
-
}
|
|
78
|
-
exports.parseJsonCodeBlock = parseJsonCodeBlock;
|
|
79
|
-
function getCodeBlocks(text) {
|
|
80
|
-
return text.match(/\`\`\`tree\.json\n([\s\S]*)\`\`\`/)?.[1];
|
|
81
|
-
}
|
|
82
|
-
function parseCodeBlocks(text) {
|
|
83
|
-
return text.split('```').filter((_, i) => i % 2).map(code => {
|
|
84
|
-
const [path, ...lines] = code.split('\n');
|
|
85
|
-
return { path, content: Buffer.from(lines.join('\n')) };
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
exports.parseCodeBlocks = parseCodeBlocks;
|
|
89
31
|
function getFiles(dir) {
|
|
90
32
|
const files = [];
|
|
91
33
|
walkDir(dir, (path, entry) => entry.subfiles.forEach(file => files.push(`${path}/${file}`)));
|
package/src/gpt3/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/schematics/src/gpt3/index.ts"],"names":[],"mappings":";;;AAAA,2DAAoF;AAEpF,qEAA0E;AAC1E
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/schematics/src/gpt3/index.ts"],"names":[],"mappings":";;;AAAA,2DAAoF;AAEpF,qEAA0E;AAC1E,+BAA4B;AAC5B,iDAA6C;AAG7C,SAAgB,YAAY,CAAC,IAAU,EAAE,OAA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AAO,CAAC,CAAC;SACxC;KACF;IAEF,OAAO,IAAA,kBAAK,EAAC,EAAE,CAAC,CAAC;AAClB,CAAC,CAAC;AAfW,QAAA,IAAI,QAef;AAEF,SAAS,QAAQ,CAAC,GAAa;IAC7B,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;IAC7F,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,OAAO,CAAC,GAAa,EAAE,QAAiD,EAAE,MAAM,GAAG,GAAG;IAC7F,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QACzB,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC5B,QAAQ,CAAC,IAAA,WAAI,EAAC,MAAM,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;QACpC,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAA,WAAI,EAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IAC/C,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/src/gpt3/index.spec.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { createWorkspace } from '../_testing';
|
|
4
|
-
import { parseJsonCodeBlock, parseCodeBlocks } from './index';
|
|
5
4
|
|
|
6
5
|
jest.setTimeout(300 * 1000);
|
|
7
6
|
|
|
@@ -11,7 +10,7 @@ describe('Gpt Schematics', () => {
|
|
|
11
10
|
const runner = new SchematicTestRunner('@ng-atomic/schematics', COLLECTION_PATH);
|
|
12
11
|
let tree: UnitTestTree;
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
xdescribe('Angular Workspace', () => {
|
|
15
14
|
beforeEach(async () => {
|
|
16
15
|
tree = await createWorkspace(runner, tree);
|
|
17
16
|
tree = await runner.runSchematicAsync('atomic-component', {
|
|
@@ -80,11 +79,11 @@ const TEST = `
|
|
|
80
79
|
// });
|
|
81
80
|
// });
|
|
82
81
|
|
|
83
|
-
describe('parseJsonCodeBlock', () => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
});
|
|
82
|
+
// describe('parseJsonCodeBlock', () => {
|
|
83
|
+
// it('should parse json code block', () => {
|
|
84
|
+
// expect(parseJsonCodeBlock(TEST)).toBeTruthy();
|
|
85
|
+
// });
|
|
86
|
+
// });
|
|
88
87
|
|
|
89
88
|
|
|
90
89
|
const CODE_BLOCKS = `
|
|
@@ -149,8 +148,8 @@ export class ExpectedComponent implements OnInit {
|
|
|
149
148
|
\`\`\`
|
|
150
149
|
`;
|
|
151
150
|
|
|
152
|
-
describe('parseCodeBlocks', () => {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
});
|
|
151
|
+
// describe('parseCodeBlocks', () => {
|
|
152
|
+
// it('should parse code blocks', () => {
|
|
153
|
+
// expect(parseCodeBlocks(CODE_BLOCKS)).toBeTruthy();
|
|
154
|
+
// });
|
|
155
|
+
// });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { FileEntry } from "@angular-devkit/schematics";
|
|
2
|
+
export interface Options {
|
|
3
|
+
model?: 'text-curie-001' | 'code-davinci-002' | 'code-cushman-001';
|
|
4
|
+
}
|
|
5
|
+
export declare class OpenAiPrompter {
|
|
6
|
+
private _prompt;
|
|
7
|
+
private config;
|
|
8
|
+
constructor(_prompt: string);
|
|
9
|
+
private openai;
|
|
10
|
+
private stop;
|
|
11
|
+
get prompt(): string;
|
|
12
|
+
autoWrite(options?: Options): Promise<void>;
|
|
13
|
+
autoWriteUntilEnd(options?: Options, maxRepeat?: number): Promise<void>;
|
|
14
|
+
write(text: string): Promise<void>;
|
|
15
|
+
isEnd(): boolean;
|
|
16
|
+
getFileEntries(): FileEntry[];
|
|
17
|
+
getFileEntry(path: string): FileEntry | null;
|
|
18
|
+
}
|
|
19
|
+
export declare class JsonPrompter extends OpenAiPrompter {
|
|
20
|
+
parseJson(prompt: string): string[];
|
|
21
|
+
getJsonFuzzy(): string[];
|
|
22
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsonPrompter = exports.OpenAiPrompter = void 0;
|
|
4
|
+
const openai_1 = require("openai");
|
|
5
|
+
class OpenAiPrompter {
|
|
6
|
+
constructor(_prompt) {
|
|
7
|
+
this._prompt = _prompt;
|
|
8
|
+
this.config = new openai_1.Configuration({ apiKey: process.env['OPEN_AI_TOKEN'] });
|
|
9
|
+
this.openai = new openai_1.OpenAIApi(this.config);
|
|
10
|
+
this.stop = '\n\`\`\`';
|
|
11
|
+
}
|
|
12
|
+
get prompt() {
|
|
13
|
+
return this._prompt;
|
|
14
|
+
}
|
|
15
|
+
async autoWrite(options) {
|
|
16
|
+
try {
|
|
17
|
+
const res = await this.openai.createCompletion({
|
|
18
|
+
model: options?.model ?? 'code-cushman-001',
|
|
19
|
+
prompt: this._prompt,
|
|
20
|
+
temperature: 0,
|
|
21
|
+
max_tokens: 512,
|
|
22
|
+
stop: this.stop,
|
|
23
|
+
});
|
|
24
|
+
this._prompt += res.data.choices?.[0].text;
|
|
25
|
+
this._prompt += res.data.choices?.[0].finish_reason === 'stop' ? this.stop : '';
|
|
26
|
+
}
|
|
27
|
+
catch (res) {
|
|
28
|
+
console.error(res.response.data.error);
|
|
29
|
+
throw new Error('OpenAI API Error');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async autoWriteUntilEnd(options, maxRepeat = 3) {
|
|
33
|
+
for (let i = 0; i < maxRepeat; i++) {
|
|
34
|
+
if (this.isEnd())
|
|
35
|
+
return;
|
|
36
|
+
await this.autoWrite();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async write(text) {
|
|
40
|
+
this._prompt += text;
|
|
41
|
+
}
|
|
42
|
+
isEnd() {
|
|
43
|
+
return this._prompt.endsWith(this.stop);
|
|
44
|
+
}
|
|
45
|
+
getFileEntries() {
|
|
46
|
+
return this._prompt.split('```').filter((_, i) => i % 2).map(code => {
|
|
47
|
+
const [path, ...lines] = code.split('\n');
|
|
48
|
+
return { path, content: Buffer.from(lines.join('\n').slice(0, -1)) };
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
getFileEntry(path) {
|
|
52
|
+
const entries = this.getFileEntries();
|
|
53
|
+
return entries.find(file => file.path === path) ?? null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.OpenAiPrompter = OpenAiPrompter;
|
|
57
|
+
class JsonPrompter extends OpenAiPrompter {
|
|
58
|
+
parseJson(prompt) {
|
|
59
|
+
return JSON.parse(prompt.match(/\`\`\`tree\.json\n([\s\S]*)\`\`\`/)?.[1]);
|
|
60
|
+
}
|
|
61
|
+
getJsonFuzzy() {
|
|
62
|
+
let text = this.prompt;
|
|
63
|
+
while (text.length) {
|
|
64
|
+
let suffixes = ['"]\n```', ']\n```', '\n```', '```', '``', '`', ''];
|
|
65
|
+
for (const suffix of suffixes) {
|
|
66
|
+
try {
|
|
67
|
+
return this.parseJson(text + suffix);
|
|
68
|
+
}
|
|
69
|
+
catch { }
|
|
70
|
+
}
|
|
71
|
+
text = text.slice(0, -1);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.JsonPrompter = JsonPrompter;
|
|
76
|
+
// const results = completeToJson(`${prompt}${res.data.choices?.[0].text}`);
|
|
77
|
+
// return [...new Set(results)].filter(result => result.startsWith(dirPath));
|
|
78
|
+
// }
|
|
79
|
+
//# sourceMappingURL=prompter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9C,SAAS,CAAC,MAAc;QACtB,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,mCAAmC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5E,CAAC;IAED,YAAY;QACV,IAAI,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC;QACvB,OAAM,IAAI,CAAC,MAAM,EAAE;YACjB,IAAI,QAAQ,GAAG,CAAC,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;YAEpE,KAAK,MAAM,MAAM,IAAI,QAAQ,EAAE;gBAC7B,IAAI;oBAAE,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,GAAG,MAAM,CAAC,CAAC;iBAAE;gBAAC,MAAM,GAAG;aACxD;YAED,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;SAC1B;IACH,CAAC;CAEF;AAnBD,oCAmBC;AAED,8EAA8E;AAC9E,+EAA+E;AAC/E,IAAI"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { OpenAiPrompter } from './prompter';
|
|
2
|
+
|
|
3
|
+
jest.setTimeout(3000 * 1000);
|
|
4
|
+
|
|
5
|
+
const PROMPT_JSON = `
|
|
6
|
+
\`tree.json\` is 15 lines.
|
|
7
|
+
|
|
8
|
+
\`\`\`tree.json
|
|
9
|
+
[
|
|
10
|
+
"/root/app.json",
|
|
11
|
+
`.slice(1);
|
|
12
|
+
|
|
13
|
+
describe('OpenAiPrompter', () => {
|
|
14
|
+
let prompter: OpenAiPrompter;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
prompter = new OpenAiPrompter('');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
xit('', async () => {
|
|
21
|
+
prompter.write(PROMPT_JSON);
|
|
22
|
+
await prompter.autoWriteUntilEnd();
|
|
23
|
+
const fileEntry = prompter.getFileEntry('tree.json');
|
|
24
|
+
const body = fileEntry.content.toString();
|
|
25
|
+
expect(JSON.parse(body)).toBeTruthy();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
});
|
package/src/gpt3/schema.json
CHANGED
|
@@ -4,18 +4,27 @@
|
|
|
4
4
|
"title": "Angular Atomic Schematics GPT3",
|
|
5
5
|
"type": "object",
|
|
6
6
|
"properties": {
|
|
7
|
+
"path": {
|
|
8
|
+
"type": "string",
|
|
9
|
+
"format": "path",
|
|
10
|
+
"description": "The path at which to create the component file, relative to the current workspace. Default is a folder with the same name as the component in the project root.",
|
|
11
|
+
"visible": false
|
|
12
|
+
},
|
|
7
13
|
"project": {
|
|
8
14
|
"type": "string",
|
|
9
|
-
"description": "
|
|
15
|
+
"description": "The name of the project.",
|
|
10
16
|
"$default": {
|
|
11
17
|
"$source": "projectName"
|
|
12
18
|
}
|
|
13
19
|
},
|
|
14
|
-
"
|
|
15
|
-
"description": "Setup components directory",
|
|
20
|
+
"name": {
|
|
16
21
|
"type": "string",
|
|
17
|
-
"
|
|
18
|
-
"
|
|
22
|
+
"description": "The name of the anything.",
|
|
23
|
+
"$default": {
|
|
24
|
+
"$source": "argv",
|
|
25
|
+
"index": 0
|
|
26
|
+
},
|
|
27
|
+
"x-prompt": "What name would you like to generate?"
|
|
19
28
|
}
|
|
20
29
|
},
|
|
21
30
|
"required": []
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FileEntry } from "@angular-devkit/schematics";
|
|
2
|
+
export declare class SchematicsX {
|
|
3
|
+
generate(files: FileEntry[], path: string): Promise<FileEntry[]>;
|
|
4
|
+
generateFileEntry(path: string, files?: FileEntry[]): Promise<FileEntry>;
|
|
5
|
+
_generateFileEntry(path: string, fileEntries: FileEntry[]): Promise<FileEntry>;
|
|
6
|
+
getGenerateFilePaths(filePaths: string[], generatePath: string): Promise<string[]>;
|
|
7
|
+
predicateGenerateFilePaths(filePaths: string[], generatePath: string): Promise<string[]>;
|
|
8
|
+
private _predicateGenerateFilePaths;
|
|
9
|
+
}
|
|
10
|
+
export declare function getBasePath(paths: string[]): string;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBasePath = exports.SchematicsX = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const helpers_1 = require("./helpers");
|
|
6
|
+
const prompter_1 = require("./prompter");
|
|
7
|
+
class SchematicsX {
|
|
8
|
+
async generate(files, path) {
|
|
9
|
+
const generateFilePaths = await this.getGenerateFilePaths(files.map(file => file.path), path);
|
|
10
|
+
console.log('Estimated! => ', generateFilePaths, '\n');
|
|
11
|
+
return Promise.all(generateFilePaths.map(filePath => {
|
|
12
|
+
return this.generateFileEntry(filePath, files);
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
15
|
+
async generateFileEntry(path, files = []) {
|
|
16
|
+
const similarFilePaths = (0, helpers_1.getEstimateSimilarFilePaths)(path, files.map(file => file.path));
|
|
17
|
+
const fileEntries = files.filter(file => similarFilePaths.includes(file.path));
|
|
18
|
+
console.log(`Estimating content of '${path}' by`, similarFilePaths, '...\n');
|
|
19
|
+
return this._generateFileEntry(path, fileEntries);
|
|
20
|
+
}
|
|
21
|
+
async _generateFileEntry(path, fileEntries) {
|
|
22
|
+
const prompter = new prompter_1.OpenAiPrompter(makeFilePrompt(fileEntries, path));
|
|
23
|
+
await prompter.autoWriteUntilEnd();
|
|
24
|
+
return prompter.getFileEntry(path);
|
|
25
|
+
}
|
|
26
|
+
async getGenerateFilePaths(filePaths, generatePath) {
|
|
27
|
+
if ((0, path_1.parse)(generatePath).ext.length) {
|
|
28
|
+
return [generatePath];
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
console.log('Estimating the paths of files to be generated...\n');
|
|
32
|
+
return this.predicateGenerateFilePaths(filePaths, generatePath);
|
|
33
|
+
}
|
|
34
|
+
;
|
|
35
|
+
}
|
|
36
|
+
async predicateGenerateFilePaths(filePaths, generatePath) {
|
|
37
|
+
const baseDir = getBasePath(filePaths);
|
|
38
|
+
const _filePaths = filePaths.map(path => path.replace(baseDir, ''));
|
|
39
|
+
const _generatePath = generatePath.replace(baseDir, '');
|
|
40
|
+
const paths = await this._predicateGenerateFilePaths(_filePaths, _generatePath);
|
|
41
|
+
return paths.map(path => (0, path_1.join)(baseDir, path));
|
|
42
|
+
}
|
|
43
|
+
async _predicateGenerateFilePaths(filePaths, generatePath) {
|
|
44
|
+
const parentDir = (0, path_1.resolve)(generatePath, '..');
|
|
45
|
+
const prompt = makeDirectoryPrompt(filePaths.filter(path => path.startsWith(parentDir)), generatePath);
|
|
46
|
+
const prompter = new prompter_1.JsonPrompter(prompt);
|
|
47
|
+
await prompter.autoWrite();
|
|
48
|
+
const paths = prompter.getJsonFuzzy();
|
|
49
|
+
return [...new Set(paths)].filter(path => path.startsWith(generatePath));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.SchematicsX = SchematicsX;
|
|
53
|
+
function getBasePath(paths) {
|
|
54
|
+
const basePath = paths.reduce((acc, path) => {
|
|
55
|
+
const pathParts = path.split('/');
|
|
56
|
+
const accParts = acc.split('/');
|
|
57
|
+
const parts = pathParts.filter((part, i) => part === accParts[i]);
|
|
58
|
+
return parts.join('/');
|
|
59
|
+
}, paths[0]);
|
|
60
|
+
return basePath;
|
|
61
|
+
}
|
|
62
|
+
exports.getBasePath = getBasePath;
|
|
63
|
+
function makeDirectoryPrompt(files, name) {
|
|
64
|
+
return `\`tree.json\` has 30 lines.\n\n\`\`\`tree.json\n[\n${files.map(file => `\t\"${file}\",`).join('\n')}\n\t\"${name}`;
|
|
65
|
+
}
|
|
66
|
+
function makeFilePrompt(files, path) {
|
|
67
|
+
return `${makeFileCodeBlocks(files)}\n\n\`\`\`${path}\n`;
|
|
68
|
+
}
|
|
69
|
+
function makeFileCodeBlocks(files) {
|
|
70
|
+
return files.map(file => makeFileCodeBlock(file.path, file.content.toString())).join('\n\n');
|
|
71
|
+
}
|
|
72
|
+
function makeFileCodeBlock(name, content) {
|
|
73
|
+
return `\`\`\`${name}\n${content}\n\`\`\``;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=schematics-x.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schematics-x.js","sourceRoot":"","sources":["../../../../../libs/schematics/src/gpt3/schematics-x.ts"],"names":[],"mappings":";;;AACA,+BAA4C;AAC5C,uCAAwD;AACxD,yCAA0D;AAG1D,MAAa,WAAW;IAEtB,KAAK,CAAC,QAAQ,CAAC,KAAkB,EAAE,IAAY;QAC7C,MAAM,iBAAiB,GAAG,MAAM,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC;QAC9F,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,iBAAiB,EAAE,IAAI,CAAC,CAAC;QAEvD,OAAO,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE;YAClD,OAAO,IAAI,CAAC,iBAAiB,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;QACjD,CAAC,CAAC,CAAC,CAAC;IACN,CAAC;IAED,KAAK,CAAC,iBAAiB,CAAC,IAAY,EAAE,QAAqB,EAAE;QAC3D,MAAM,gBAAgB,GAAG,IAAA,qCAA2B,EAAC,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACzF,MAAM,WAAW,GAAG,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,gBAAgB,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QAC/E,OAAO,CAAC,GAAG,CAAC,0BAA0B,IAAI,MAAM,EAAE,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CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,IAAI,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,IAAI,EAAE,CAAC;AAC7H,CAAC;AAED,SAAS,cAAc,CAAC,KAAkB,EAAE,IAAY;IACtD,OAAO,GAAG,kBAAkB,CAAC,KAAK,CAAC,aAAa,IAAI,IAAI,CAAC;AAC3D,CAAC;AAED,SAAS,kBAAkB,CAAC,KAAkB;IAC5C,OAAO,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,iBAAiB,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAC/F,CAAC;AAED,SAAS,iBAAiB,CAAC,IAAY,EAAE,OAAe;IACtD,OAAO,SAAS,IAAI,KAAK,OAAO,UAAU,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { FileEntry } from '@angular-devkit/schematics';
|
|
2
|
+
import { SchematicsX } from './schematics-x';
|
|
3
|
+
|
|
4
|
+
const FILE_ENTRIES = [
|
|
5
|
+
{
|
|
6
|
+
path: '/projects/app/src/app/_shared/components/test/test.module.ts',
|
|
7
|
+
content: Buffer.from(`test`),
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
path: '/projects/app/src/app/_shared/components/example/example.module.ts',
|
|
11
|
+
content: Buffer.from(`example`),
|
|
12
|
+
},
|
|
13
|
+
] as FileEntry[];
|
|
14
|
+
|
|
15
|
+
describe('SchematicsX', () => {
|
|
16
|
+
let schematicsX: SchematicsX;
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
schematicsX = new SchematicsX();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('generateFileEntry', () => {
|
|
23
|
+
it('should create', async () => {
|
|
24
|
+
const PATH = '/projects/app/src/app/_shared/components/expected/expected.module.ts';
|
|
25
|
+
const entry = await schematicsX.generateFileEntry(PATH, FILE_ENTRIES);
|
|
26
|
+
expect(entry.content.toString()).toEqual(`expected`);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('predicateGenerateFilePaths', () => {
|
|
31
|
+
it('should be succeeded', async () => {
|
|
32
|
+
const PATH = '/projects/app/src/app/_shared/components/expected';
|
|
33
|
+
const context = FILE_ENTRIES.map(entry => entry.path);
|
|
34
|
+
const paths = await schematicsX.predicateGenerateFilePaths(context, PATH);
|
|
35
|
+
expect(paths).toEqual([
|
|
36
|
+
'/projects/app/src/app/_shared/components/expected/expected.module.ts',
|
|
37
|
+
]);
|
|
38
|
+
});
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
});
|