@softwareventures/maintain-project
Version:
Automatically create and maintain TypeScript projects with standard settings for Software Ventures Limited
54 lines • 2.57 kB
JavaScript
import { basename, extname, isAbsolute, relative, sep } from "path";
import { Legacy } from "@eslint/eslintrc";
import { mapNullableFn } from "@softwareventures/nullable";
import { hasProperty } from "unknown";
import { contains, isArray } from "@softwareventures/array";
import { projectDevDependsOn } from "../project/dev-depends-on.js";
import { readProjectJson } from "../project/read-json.js";
import { toAsyncNullable } from "../result/result.js";
import { readProjectYaml } from "../project/read-yaml.js";
export async function readEslintProject(project) {
const readPackageJsonConfigFile = async (relativePath) => toAsyncNullable(readProjectJson(project, relativePath)).then(mapNullableFn(json => (hasProperty(json, "eslintConfig") ? json.eslintConfig : null)));
const readJsonConfigFile = async (relativePath) => toAsyncNullable(readProjectJson(project, relativePath));
const readYamlConfigFile = async (relativePath) => toAsyncNullable(readProjectYaml(project, relativePath));
const readConfigFile = async (relativePath) => {
switch (extname(relativePath)) {
case ".js":
case ".cjs":
return null; // Unsupported
case ".json":
if (basename(relativePath) === "package.json") {
return readPackageJsonConfigFile(relativePath);
}
else {
return readJsonConfigFile(relativePath);
}
default:
return readYamlConfigFile(relativePath);
}
};
if (!(await projectDevDependsOn(project, "eslint"))) {
return undefined;
}
const dependsOnSoftwareVenturesPreset = projectDevDependsOn(project, "@softwareventures/eslint-config");
const configPath = Legacy.ConfigArrayFactory.getPathToConfigFileInDirectory(project.path);
if (configPath == null) {
return undefined;
}
const relativeConfigPath = relative(project.path, configPath);
if (isAbsolute(relativeConfigPath) || (relativeConfigPath.split(sep)[0] ?? "") === "..") {
return undefined;
}
const config = await readConfigFile(relativeConfigPath);
if ((await dependsOnSoftwareVenturesPreset) &&
config != null &&
hasProperty(config, "extends") &&
((isArray(config.extends) && contains(config.extends, "@softwareventures")) ||
config.extends === "@softwareventures")) {
return { preset: "softwareventures" };
}
else {
return { preset: "other" };
}
}
//# sourceMappingURL=read.js.map