Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow disabling remote extend with giget: false #181

Merged
merged 6 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ The final config is merged result of extended options and user options with [unj
Each item in extends is a string that can be either an absolute or relative path to the current config file pointing to a config file for extending or the directory containing the config file.
If it starts with either `github:`, `gitlab:`, `bitbucket:`, or `https:`, c12 automatically clones it.

You can set the `extendOptions` option when loading the config (with `loadConfig`) to customize the extending behavior like ignoring remote sources.

For custom merging strategies, you can directly access each layer with `layers` property.

**Example:**
Expand Down
19 changes: 19 additions & 0 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,25 @@ async function extendConfig<
);
continue;
}

// Skipping remote sources if set up
const skipRemote = options?.extendOptions?.skipRemote;
const isGiGetSource = GIGET_PREFIXES.some((prefix) =>
extendSource.startsWith(prefix),
);
const isNPMSource = NPM_PACKAGE_RE.test(extendSource);
if (skipRemote && (isNPMSource || isGiGetSource)) {
if (skipRemote === true) continue;

const hasAuth = Boolean((sourceOptions as SourceOptions)?.auth);
const skippingGitgetSource =
isGiGetSource && !(hasAuth ? skipRemote.allowAuth : false);
if (skippingGitgetSource) continue;

const skippingNPMSource = isNPMSource && !skipRemote.allowNPM;
if (skippingNPMSource) continue;
}

const _config = await resolveConfig(extendSource, options, sourceOptions);
if (!_config.config) {
// TODO: Use error in next major versions
Expand Down
21 changes: 21 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,27 @@ export interface LoadConfigOptions<
| {
extendKey?: string | string[];
};
/**
* Options for extending configs
*/
extendOptions?: {
/**
* Skip and ignore remote configs when extending. This blocks downloading configs
* from github or npm, only local configs will be used.
*/
skipRemote?:
| boolean
| {
/**
* Allow extending configs with auth tokens.
*/
allowAuth?: boolean;
/**
* Allow extending configs from npm.
*/
allowNPM?: boolean;
};
};
}

export type DefineConfig<
Expand Down
51 changes: 51 additions & 0 deletions test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,57 @@ describe("loader", () => {
`);
});

it("skip remote configs", async () => {
type Config = {
extends?: string[];
defaultValue?: boolean;
npmValue?: boolean;
};
const opts = {
name: "test",
cwd: r("./fixture/new_dir"),
overrides: {
extends: ["virtual", "github:unjs/c12/test/fixture"],
},
resolve: (id: string) => {
if (id === "virtual") {
return { config: { npmValue: true } };
}
},
defaultConfig: {
defaultValue: true,
},
};
const { config: remoteConfig } = await loadConfig<Config>({
...opts,
extendOptions: {
skipRemote: true,
},
});

const { config: npmConfig } = await loadConfig<Config>({
...opts,
extendOptions: {
skipRemote: {
allowAuth: false,
allowNPM: true,
},
},
});

expect(transformPaths(remoteConfig!)).toMatchInlineSnapshot(`
{
"defaultValue": true,
}
`);
expect(transformPaths(npmConfig!)).toMatchInlineSnapshot(`
{
"defaultValue": true,
"npmValue": true,
}
`);
});

it("omit$Keys", async () => {
const { config, layers } = await loadConfig({
name: "test",
Expand Down