Files
vercel/packages/now-build-utils/src/detect-framework.ts
Andy 96dbc6d348 [now-static-build][frameworks][examples] Fixes examples and adjust frameworks (#3584)
* [examples] Fix ionic-react example

* [examples] Fix vue example

* [examples] Fix mithril example

* [examples] Fix riot example

* Fix readmes

* [now-static-build] Add Zola

* Add tests

* [now-build-utils][frameworks] Adjust detect framework

* Move zola back

* Undo Hugo detection changes

* [examples] Fix Vue logo path

* [now-static-build] Use package.json script if defined instead of framework command

* [now-static-build] Add buildCommand everywhere

* Remove devCommand from frameworks.ts

* Fix type

* Change output directory

* [now-static-build] Remove minNodeRange

* Remove devCommand
2020-01-16 00:12:55 +01:00

86 lines
1.7 KiB
TypeScript

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 ({ path, matchContent }: FrameworkDetectionItem) => {
if (!path) {
return false;
}
if ((await fs.hasPath(path)) === false) {
return false;
}
if (matchContent) {
if ((await fs.isFile(path)) === false) {
return false;
}
const regex = new RegExp(matchContent, 'gm');
const content = await fs.readFile(path);
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;
}