sandhog
Version:
A virtual Open Source project maintainer
127 lines (121 loc) • 4.59 kB
JavaScript
import {
isKnownLicenseName
} from "./chunk-GEXBMSSY.js";
import {
CONSTANT
} from "./chunk-VYUMLP7D.js";
// src/package/find-package/find-package.ts
import path from "crosspath";
import _fs from "fs";
async function findPackage({
root = process.cwd(),
logger,
fs = { existsSync: _fs.existsSync, readFileSync: _fs.readFileSync }
}) {
const packageJsonPath = path.join(root, "package.json");
const nativePackageJsonPath = path.native.normalize(packageJsonPath);
if (fs.existsSync(nativePackageJsonPath)) {
logger.debug(`Found package.json: ${nativePackageJsonPath}`);
return {
root: path.dirname(packageJsonPath),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
pkg: await import(packageJsonPath, { with: { type: "json" } })
};
}
const newRoot = path.join(root, "../");
if (newRoot === root || newRoot === "/" || newRoot === "") {
throw new ReferenceError(`Could not find a package.json. Are you sure you are running sandhog inside an NPM project?`);
}
return await findPackage({
root: newRoot,
logger
});
}
// src/license/find-license/find-license.ts
import path2 from "crosspath";
import _fs2 from "fs";
// src/license/detect-license/detect-license.ts
function detectLicense(text) {
if (text.includes("GNU AGPL")) return "AGPL-3.0";
else if (text.includes("Apache License")) return "APACHE-2.0";
else if (text.includes("The Artistic License 2.0")) return "ARTISTIC-2.0";
else if (text.includes("BSD 2-Clause License")) return "BSD-2-CLAUSE";
else if (text.includes("BSD 3-Clause License")) return "BSD-3-CLAUSE";
else if (text.includes("Attribution 4.0 International")) return "CC-BY-4.0";
else if (text.includes("Attribution-ShareAlike 4.0 International")) return "CC-BY-SA-4.0";
else if (text.includes("Eclipse Public License")) return "EPL-1.0";
else if (text.includes("GNU GENERAL PUBLIC LICENSE") && text.includes("Version 2, June 1991")) return "GPL-2.0";
else if (text.includes("GNU GENERAL PUBLIC LICENSE") && text.includes("Version 3, 29 June 2007")) return "GPL-3.0";
else if (text.includes("GNU LESSER GENERAL PUBLIC LICENSE") && text.includes("Version 3, 29 June 2007")) return "LGPL-3.0";
else if (text.includes("The MIT License (MIT)")) return "MIT";
else if (text.includes("Mozilla Public License Version 2.0")) return "MPL-2.0";
else if (text.includes("zlib License")) return "ZLIB";
else return void 0;
}
// src/license/find-license/find-license.ts
async function findLicense({
logger,
root = process.cwd(),
fs = { existsSync: _fs2.existsSync, readFileSync: _fs2.readFileSync },
pkg
}) {
if (pkg == null) {
pkg = (await findPackage({ root, logger, fs })).pkg;
}
logger.debug(`Trying to locate license inside package.json`);
if (pkg.license != null) {
logger.debug(`Found license: '${pkg.license}' inside package.json`);
if (!isKnownLicenseName(pkg.license)) {
throw new TypeError(`The license found within package.json: '${pkg.license}' is not valid or not supported`);
}
return pkg.license;
}
const license = findLicenseRecursiveStep(root, logger, fs);
if (license == null) {
return void 0;
}
const [text, p] = license;
const nativePath = path2.native.normalize(p);
logger.verbose(`Detecting license for file: ${nativePath}`);
const name = detectLicense(text);
if (name == null || !isKnownLicenseName(name)) {
throw new TypeError(`The license found on path: '${nativePath}' is not valid or not supported`);
}
logger.verbose(`Detected license: '${name}' for file: ${nativePath}`);
return name;
}
function findLicenseRecursiveStep(root, logger, fs) {
const absolutePath = path2.join(root, CONSTANT.licenseFilename);
const nativeAbsolutePath = path2.native.normalize(absolutePath);
logger.debug(`Checking path for license: ${nativeAbsolutePath}`);
if (fs.existsSync(nativeAbsolutePath)) {
logger.debug(`Matched license at path: ${nativeAbsolutePath}`);
return [fs.readFileSync(nativeAbsolutePath).toString(), absolutePath];
}
const newRoot = path2.join(root, "../");
if (newRoot === root || newRoot === "/" || newRoot === "") return void 0;
return findLicenseRecursiveStep(newRoot, logger, fs);
}
// src/section/section-kind.ts
var SECTION_KINDS = [
"toc",
"logo",
"badges",
"description_short",
"description_long",
"features",
"feature_image",
"usage",
"install",
"contributing",
"maintainers",
"faq",
"backers",
"license"
];
export {
findPackage,
findLicense,
SECTION_KINDS
};
//# sourceMappingURL=chunk-4KN5GL56.js.map