Skip to content

Commit

Permalink
feat: add --config option
Browse files Browse the repository at this point in the history
  • Loading branch information
MtBlue81 committed Mar 17, 2024
1 parent d7e2fd3 commit 7cc2aa9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
overrideLicense: () => {
console.log("load awesome-config.js");
return "OVERRIDE_LICENSE";
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,19 @@ describe("analyze : override-licenses", () => {
});

expect(console.log).toBeCalledWith(pc.green("✅ All dependencies confirmed"));
expect(console.log).toBeCalledWith("💡 Detected license-manager.config.js");
});

it("apply specified config file", async () => {
await analyze({
...analyzeDefaultOption,
allowLicenses: ["OVERRIDE_LICENSE"],
allowPackages: [],
configFilePath: "awesome-config.js",
});

expect(console.log).toBeCalledWith(pc.green("✅ All dependencies confirmed"));
expect(console.log).toBeCalledWith("💡 Detected awesome-config.js");
expect(console.log).toBeCalledWith("load awesome-config.js");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ describe("extract : override-license-text", () => {
expect(console.log).toBeCalledWith(pc.green(`✅ Extracted to ${expectedOutputPath}`));

expect(memfs.readFileSync(expectedOutputPath).toString("utf-8")).toMatchSnapshot();

expect(console.log).toBeCalledWith("💡 Detected license-manager.config.js");
});
});
9 changes: 7 additions & 2 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ export type AnalyzeArgs = {
packageManager: string;
};

export const analyze = async (cliArgs: AnalyzeArgs) => {
export const analyze = async ({
configFilePath,
...cliArgs
}: AnalyzeArgs & {
configFilePath?: string;
}) => {
console.log("🔎 Analyzing dependencies...");

const config = await loadConfigScript();
const config = await loadConfigScript(configFilePath);
const args = mergeConfig(cliArgs, config);
const unreadLicenseTextPattern = [/.*/];
const dependencies = await aggregate(
Expand Down
4 changes: 4 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ program
.option("-l, --allowLicense <string...>", "allow licenses")
.option("-p, --allowPackage <string...>", "allow packages (allowed without license)")
.option("-m, --packageManager <npm|pnpm>", "package manager used to analyze licenses (default: auto detect)")
.option("-c, --config <path>", "config path. defaults to ./license-manager.config.js")
.action((options) => {
analyze({
workspace: options.workspace || "",
Expand All @@ -24,6 +25,7 @@ program
allowLicenses: options.allowLicense || [],
allowPackages: options.allowPackage || [],
packageManager: options.packageManager || "",
configFilePath: options.config || "",
});
});

Expand All @@ -41,6 +43,7 @@ program
.option("-o, --output <string>", "output file name")
.option("--json", "output in JSON format")
.option("-m, --packageManager <npm|pnpm>", "package manager used to extract licenses (default: auto detect)")
.option("-c, --config <path>", "config path. defaults to ./license-manager.config.js")
.action((options) => {
extract({
workspace: options.workspace || "",
Expand All @@ -51,6 +54,7 @@ program
output: options.output || "",
json: !!options.json,
packageManager: options.packageManager || "",
configFilePath: options.config || "",
});
});

Expand Down
9 changes: 7 additions & 2 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ export type ExtractArgs = {
packageManager: string;
};

export const extract = async (cliArgs: ExtractArgs) => {
export const extract = async ({
configFilePath,
...cliArgs
}: ExtractArgs & {
configFilePath?: string;
}) => {
console.log("🔪 Extract licenses...");

const config = await loadConfigScript();
const config = await loadConfigScript(configFilePath);
const args = mergeConfig(cliArgs, config);
const dependencies = await aggregate(
args.packageManager,
Expand Down
8 changes: 5 additions & 3 deletions src/functions/loadConfigScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import fs from "fs";
import path from "path";
import { Config } from "../types";

export const loadConfigScript = async (relativeConfigPath: string = ""): Promise<Partial<Config>> => {
const configPath = path.join(process.cwd(), relativeConfigPath || "license-manager.config.js");
export const loadConfigScript = async (
relativeConfigPath: string = "license-manager.config.js"
): Promise<Partial<Config>> => {
const configPath = path.join(process.cwd(), relativeConfigPath);

if (!fs.existsSync(configPath)) {
return {};
}

console.log("💡 Detected license-manager.config.js");
console.log(`💡 Detected ${relativeConfigPath}`);

const requirePath = path.relative(__dirname, configPath);
const config = require(requirePath);
Expand Down

0 comments on commit 7cc2aa9

Please sign in to comment.