1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.detectFramework = void 0;
|
4 | async function matches(fs, framework) {
|
5 | const { detectors } = framework;
|
6 | if (!detectors) {
|
7 | return false;
|
8 | }
|
9 | const { every, some } = detectors;
|
10 | if (every !== undefined && !Array.isArray(every)) {
|
11 | return false;
|
12 | }
|
13 | if (some !== undefined && !Array.isArray(some)) {
|
14 | return false;
|
15 | }
|
16 | const check = async ({ path, matchContent }) => {
|
17 | if (!path) {
|
18 | return false;
|
19 | }
|
20 | if ((await fs.hasPath(path)) === false) {
|
21 | return false;
|
22 | }
|
23 | if (matchContent) {
|
24 | if ((await fs.isFile(path)) === false) {
|
25 | return false;
|
26 | }
|
27 | const regex = new RegExp(matchContent, 'gm');
|
28 | const content = await fs.readFile(path);
|
29 | if (!regex.test(content.toString())) {
|
30 | return false;
|
31 | }
|
32 | }
|
33 | return true;
|
34 | };
|
35 | const result = [];
|
36 | if (every) {
|
37 | const everyResult = await Promise.all(every.map(item => check(item)));
|
38 | result.push(...everyResult);
|
39 | }
|
40 | if (some) {
|
41 | let someResult = false;
|
42 | for (const item of some) {
|
43 | if (await check(item)) {
|
44 | someResult = true;
|
45 | break;
|
46 | }
|
47 | }
|
48 | result.push(someResult);
|
49 | }
|
50 | return result.every(res => res === true);
|
51 | }
|
52 | async function detectFramework({ fs, frameworkList, }) {
|
53 | for (const framework of frameworkList) {
|
54 | if (await matches(fs, framework)) {
|
55 | return framework.slug;
|
56 | }
|
57 | }
|
58 | return null;
|
59 | }
|
60 | exports.detectFramework = detectFramework;
|