mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-11 04:22:13 +00:00
[now-build-utils][now-static-build][now-cli] Update detectors and undo previous changes (#3452)
* Revert detect-builders and detect-routes changes * Add `projectSettings` and support non-api routes * Start with framework detectors * Update @now/static-build * Update now-dev * Update tests * Use local builders fro testing with now-dev * Add outputDirectory to @now/static builds * Ignore update only for bundeled builders * Revert now-dev builder changes * Revert more changes * Adjust tests further * Update gridsome test * Adjust hugo test * Undo version change * Read file instead of importing * Ignore failed.page.txt * Update packages/now-build-utils/src/detect-builders.ts Co-Authored-By: Steven <steven@ceriously.com> * Simplify outputDirectory * Remove NODE_ENV * Convert tests to typescript * Remove console.log Co-authored-by: Steven <steven@ceriously.com>
This commit is contained in:
81
packages/now-build-utils/src/detect-framework.ts
Normal file
81
packages/now-build-utils/src/detect-framework.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { Framework, FrameworkDetectionItem } from '@now/frameworks';
|
||||
import { DetectorFilesystem } from './detectors/filesystem';
|
||||
|
||||
export interface DetectFrameworkOptions {
|
||||
fs: DetectorFilesystem;
|
||||
frameworkList: Framework[];
|
||||
}
|
||||
|
||||
async function matches(fs: DetectorFilesystem, framework: Framework) {
|
||||
const { detectors } = framework;
|
||||
|
||||
if (!detectors) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { every, some } = detectors;
|
||||
|
||||
if (every !== undefined && !Array.isArray(every)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (some !== undefined && !Array.isArray(some)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const check = async ({ file, matchContent }: FrameworkDetectionItem) => {
|
||||
if (!file) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((await fs.exists(file)) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (matchContent) {
|
||||
const regex = new RegExp(matchContent, 'gm');
|
||||
const content = await fs.readFile(file);
|
||||
|
||||
if (!regex.test(content.toString())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
const result: boolean[] = [];
|
||||
|
||||
if (every) {
|
||||
const everyResult = await Promise.all(every.map(item => check(item)));
|
||||
result.push(...everyResult);
|
||||
}
|
||||
|
||||
if (some) {
|
||||
let someResult = false;
|
||||
|
||||
for (const item of some) {
|
||||
if (await check(item)) {
|
||||
someResult = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result.push(someResult);
|
||||
}
|
||||
|
||||
return result.every(res => res === true);
|
||||
}
|
||||
|
||||
export async function detectFramework({
|
||||
fs,
|
||||
frameworkList,
|
||||
}: DetectFrameworkOptions): Promise<string | null> {
|
||||
for (const framework of frameworkList) {
|
||||
if (await matches(fs, framework)) {
|
||||
return framework.slug;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user