Skip to content

Commit

Permalink
feat: allow setting giget clone options (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Oct 19, 2023
1 parent a8b73c2 commit c28b776
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ Custom [unjs/jiti](https://github.com/unjs/jiti) instance used to import configu

Custom [unjs/jiti](https://github.com/unjs/jiti) options to import configuration files.

### `giget`

Options passed to [unjs/giget](https://github.com/unjs/giget) when extending layer from git source.

### `envName`

Environment name used for [environment specific configuration](#environment-specific-configuration).
Expand Down Expand Up @@ -205,6 +209,39 @@ Layers:
]
```

## Extending Config Layer from Remote Sources

You can also extend configuration from remote sources such as npm or github.

In the repo, there should be a `config.ts` (or `config.{name}.ts`) file to be considered as a valid config layer.

**Example:** Extend from a github repository

```js
// config.ts
export default {
extends: "gh:repo/owner",
};
```

**Example:** Extend from a github repository with branch and subpath

```js
// config.ts
export default {
extends: "gh:repo/owner/theme#dev",
};
```

**Example:** Extend with custom configuration ([giget](https://github.com/unjs/giget) options)

```js
// config.ts
export default {
extends: ["gh:repo/owner", { giget: { auth: process.env.GITHUB_TOKEN } }],
};
```

## Environment-specific configuration

Users can define environment-specific configuration using these config keys:
Expand Down
6 changes: 5 additions & 1 deletion src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ async function resolveConfig<
if (existsSync(cloneDir)) {
await rm(cloneDir, { recursive: true });
}
const cloned = await downloadTemplate(source, { dir: cloneDir });
const cloned = await downloadTemplate(source, {
dir: cloneDir,
...options.giget,
...sourceOptions.giget,
});
source = cloned.dir;
}

Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { JITI } from "jiti";
import type { JITIOptions } from "jiti/dist/types";
import type { DownloadTemplateOptions } from "giget";
import type { DotenvOptions } from "./dotenv";

export interface ConfigLayerMeta {
Expand Down Expand Up @@ -30,6 +31,7 @@ export interface SourceOptions<
MT extends ConfigLayerMeta = ConfigLayerMeta,
> {
meta?: MT;
giget?: DownloadTemplateOptions;
overrides?: T;
[key: string]: any;
}
Expand Down Expand Up @@ -88,6 +90,8 @@ export interface LoadConfigOptions<
jiti?: JITI;
jitiOptions?: JITIOptions;

giget?: DownloadTemplateOptions;

extend?:
| false
| {
Expand Down
4 changes: 2 additions & 2 deletions test/fixture/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default {
theme: "./theme",
extends: [
["c12-npm-test", { userMeta: 123 }],
["gh:unjs/c12/test/fixture/_github#main", { userMeta: 123 }],
["c12-npm-test"],
["gh:unjs/c12/test/fixture/_github#main", { giget: {} }],
],
$test: {
extends: ["./config.dev"],
Expand Down
11 changes: 3 additions & 8 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,11 @@ describe("c12", () => {
"./config.dev",
[
"c12-npm-test",
{
"userMeta": 123,
},
],
[
"gh:unjs/c12/test/fixture/_github#main",
{
"userMeta": 123,
"giget": {},
},
],
],
Expand Down Expand Up @@ -193,9 +190,7 @@ describe("c12", () => {
"cwd": "<path>/fixture/node_modules/c12-npm-test",
"meta": {},
"source": "<path>/fixture/node_modules/c12-npm-test/config.ts",
"sourceOptions": {
"userMeta": 123,
},
"sourceOptions": {},
},
{
"config": {
Expand All @@ -206,7 +201,7 @@ describe("c12", () => {
"meta": {},
"source": "config",
"sourceOptions": {
"userMeta": 123,
"giget": {},
},
},
{
Expand Down

0 comments on commit c28b776

Please sign in to comment.