unplugin-auto-git-log 0.0.2 โ 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -45
- package/dist/api.d.mts +2 -2
- package/dist/api.mjs +2 -2
- package/dist/cli.mjs +1 -1
- package/dist/esbuild.d.mts +0 -1
- package/dist/esbuild.mjs +1 -1
- package/dist/farm.d.mts +0 -1
- package/dist/farm.mjs +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/options-g2PItbEC.d.mts +70 -0
- package/dist/{outputs-CbT7SkvQ.mjs โ outputs-Brlh_kpb.mjs} +34 -79
- package/dist/rolldown.d.mts +0 -1
- package/dist/rolldown.mjs +1 -1
- package/dist/rollup.d.mts +0 -1
- package/dist/rollup.mjs +1 -1
- package/dist/rspack.d.mts +0 -1
- package/dist/rspack.mjs +1 -1
- package/dist/src-C7eWkLP7.mjs +103 -0
- package/dist/vite.d.mts +0 -1
- package/dist/vite.mjs +1 -1
- package/dist/webpack.d.mts +0 -1
- package/dist/webpack.mjs +1 -1
- package/package.json +1 -1
- package/dist/options-B7zpW-6U.d.mts +0 -72
- package/dist/src-Bbcs1xDg.mjs +0 -65
package/README.md
CHANGED
|
@@ -9,11 +9,10 @@ Unplugin for automatically generating Git information (repo, branch, commit, etc
|
|
|
9
9
|
- ๐ฆ Automatically extract Git repository information
|
|
10
10
|
- ๐ฏ Support multiple output formats:
|
|
11
11
|
- JSON file
|
|
12
|
-
- Window global variable
|
|
13
|
-
- Environment variables (.env)
|
|
14
|
-
- TypeScript type definitions
|
|
12
|
+
- Window global variable (via define replacement and HTML injection)
|
|
15
13
|
- ๐ง Works with all major build tools (Vite, Webpack, Rollup, esbuild, Rspack, etc.)
|
|
16
14
|
- โ๏ธ Configurable fields and output options
|
|
15
|
+
- ๐ Compile-time injection for optimal performance
|
|
17
16
|
|
|
18
17
|
## Installation
|
|
19
18
|
|
|
@@ -119,7 +118,7 @@ By default, the plugin will:
|
|
|
119
118
|
- Extract all available Git fields (repo, branch, commit, commitShort, author, authorEmail, commitTime, commitMessage, isDirty)
|
|
120
119
|
- Generate a JSON file at your build output directory (e.g., `dist/git-log.json` for Vite)
|
|
121
120
|
- Automatically detect the output directory from your build tool configuration
|
|
122
|
-
- Run after build
|
|
121
|
+
- Run after build completion
|
|
123
122
|
|
|
124
123
|
You can use the plugin without any configuration:
|
|
125
124
|
|
|
@@ -149,29 +148,16 @@ AutoGitLog({
|
|
|
149
148
|
fileName: 'git-log.json', // Relative to build output directory
|
|
150
149
|
},
|
|
151
150
|
|
|
152
|
-
// Generate window global variable
|
|
151
|
+
// Generate window global variable (default: '__GIT_LOG__')
|
|
152
|
+
// Uses define replacement for compile-time injection
|
|
153
153
|
window: {
|
|
154
|
-
varName: '__GIT_LOG__',
|
|
155
|
-
console: true, // Log Git log to browser console
|
|
156
|
-
},
|
|
157
|
-
|
|
158
|
-
// Generate environment variables file (default: '.env.git')
|
|
159
|
-
env: {
|
|
160
|
-
prefix: '__GIT_',
|
|
161
|
-
file: '.env.git',
|
|
162
|
-
},
|
|
163
|
-
|
|
164
|
-
// Generate TypeScript type definitions (default: 'git-log.d.ts' in output directory)
|
|
165
|
-
types: {
|
|
166
|
-
fileName: 'git-log.d.ts', // Relative to build output directory
|
|
154
|
+
varName: '__GIT_LOG__', // Global variable name
|
|
155
|
+
console: true, // Log Git log to browser console (default: false)
|
|
167
156
|
},
|
|
168
157
|
},
|
|
169
158
|
|
|
170
159
|
// Working directory (optional, defaults to process.cwd())
|
|
171
160
|
// cwd: './custom-path',
|
|
172
|
-
|
|
173
|
-
// Plugin execution timing
|
|
174
|
-
enforce: 'post', // 'pre' | 'post'
|
|
175
161
|
})
|
|
176
162
|
```
|
|
177
163
|
|
|
@@ -204,15 +190,18 @@ AutoGitLog({
|
|
|
204
190
|
|
|
205
191
|
The following Git information can be extracted:
|
|
206
192
|
|
|
207
|
-
- `repo` - Repository URL
|
|
208
|
-
- `branch` - Current branch name
|
|
193
|
+
- `repo` - Repository name (extracted from remote URL or directory name)
|
|
194
|
+
- `branch` - Current branch name (handles detached HEAD state)
|
|
209
195
|
- `commit` - Full commit hash
|
|
210
196
|
- `commitShort` - Short commit hash (7 characters)
|
|
211
197
|
- `author` - Author name
|
|
212
198
|
- `authorEmail` - Author email
|
|
213
|
-
- `commitTime` - Commit timestamp
|
|
214
|
-
- `commitMessage` - Commit message
|
|
199
|
+
- `commitTime` - Commit timestamp (ISO 8601 format)
|
|
200
|
+
- `commitMessage` - Commit message (first line, newlines removed)
|
|
201
|
+
- `tag` - Current tag if HEAD points to a tag
|
|
215
202
|
- `isDirty` - Whether the working directory has uncommitted changes
|
|
203
|
+
- `remoteUrl` - Remote repository URL (e.g., `https://github.com/user/repo.git`)
|
|
204
|
+
- `root` - Git repository root directory path
|
|
216
205
|
|
|
217
206
|
## Output Examples
|
|
218
207
|
|
|
@@ -238,8 +227,8 @@ By default, the JSON file is generated at your build output directory (e.g., `di
|
|
|
238
227
|
|
|
239
228
|
When window output is enabled, the plugin will:
|
|
240
229
|
|
|
241
|
-
1.
|
|
242
|
-
2.
|
|
230
|
+
1. **For Vite**: Automatically inject a `<script>` tag into your HTML `<head>` section
|
|
231
|
+
2. **For other build tools**: Use define replacement to inject the variable at compile time
|
|
243
232
|
3. Optionally log Git log to browser console (with `console: true`)
|
|
244
233
|
|
|
245
234
|
You can then access the Git log anywhere in your code:
|
|
@@ -251,29 +240,23 @@ console.log(window.__GIT_LOG__.branch)
|
|
|
251
240
|
console.log(window.__GIT_LOG__.commit)
|
|
252
241
|
```
|
|
253
242
|
|
|
254
|
-
|
|
243
|
+
**How it works:**
|
|
255
244
|
|
|
256
|
-
|
|
257
|
-
|
|
245
|
+
- **Vite**: The plugin injects a `<script>` tag in the HTML that sets `window.__GIT_LOG__` before your code runs
|
|
246
|
+
- **Other build tools**: Uses define replacement - code references to `window.__GIT_LOG__` are replaced with the actual Git log object at compile time
|
|
247
|
+
|
|
248
|
+
**Example HTML injection (Vite):**
|
|
249
|
+
|
|
250
|
+
```html
|
|
251
|
+
<script>
|
|
258
252
|
if (typeof window !== 'undefined') {
|
|
259
|
-
window.__GIT_LOG__ = {
|
|
253
|
+
window.__GIT_LOG__ = {"repo":"...","branch":"main",...};
|
|
254
|
+
// console.log('[Git Log]', window.__GIT_LOG__); // if console: true
|
|
260
255
|
}
|
|
261
|
-
|
|
256
|
+
</script>
|
|
262
257
|
```
|
|
263
258
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
```bash
|
|
267
|
-
__GIT_REPO=https://github.com/user/repo.git
|
|
268
|
-
__GIT_BRANCH=main
|
|
269
|
-
__GIT_COMMIT=abc123def456...
|
|
270
|
-
__GIT_COMMIT_SHORT=abc123d
|
|
271
|
-
__GIT_AUTHOR=John Doe
|
|
272
|
-
__GIT_AUTHOR_EMAIL=john@example.com
|
|
273
|
-
__GIT_COMMIT_TIME=2025-01-08T12:00:00.000Z
|
|
274
|
-
__GIT_COMMIT_MESSAGE=feat: add new feature
|
|
275
|
-
__GIT_IS_DIRTY=false
|
|
276
|
-
```
|
|
259
|
+
**Note**: For non-Vite build tools, you need to reference `window.__GIT_LOG__` in your code for the define replacement to work.
|
|
277
260
|
|
|
278
261
|
## License
|
|
279
262
|
|
package/dist/api.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
export { type
|
|
1
|
+
import { a as WindowOutputOptions, c as GitField, d as getGitLog, i as OutputOptions, l as GitLog, n as OptionsResolved, o as generateJson, r as JsonOutputOptions, s as generateOutputs, t as Options, u as getAvailableFields } from "./options-g2PItbEC.mjs";
|
|
2
|
+
export { type GitField, type GitLog, type JsonOutputOptions, type Options, type OptionsResolved, type OutputOptions, type WindowOutputOptions, generateJson, generateOutputs, getAvailableFields, getGitLog };
|
package/dist/api.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as getGitLog, n as generateOutputs, r as getAvailableFields, t as generateJson } from "./outputs-Brlh_kpb.mjs";
|
|
2
2
|
|
|
3
|
-
export {
|
|
3
|
+
export { generateJson, generateOutputs, getAvailableFields, getGitLog };
|
package/dist/cli.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { i as getGitLog, n as generateOutputs } from "./outputs-Brlh_kpb.mjs";
|
|
3
3
|
import { readFileSync } from "node:fs";
|
|
4
4
|
import { resolve } from "node:path";
|
|
5
5
|
import process from "node:process";
|
package/dist/esbuild.d.mts
CHANGED
package/dist/esbuild.mjs
CHANGED
package/dist/farm.d.mts
CHANGED
package/dist/farm.mjs
CHANGED
package/dist/index.d.mts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
//#region src/core/git.d.ts
|
|
2
|
+
type GitField = "repo" | "branch" | "commit" | "commitShort" | "author" | "authorEmail" | "commitTime" | "commitMessage" | "tag" | "isDirty" | "remoteUrl" | "root";
|
|
3
|
+
type GitLog = Record<string, string | boolean>;
|
|
4
|
+
/**
|
|
5
|
+
* ่ทๅ Git ๆฅๅฟไฟกๆฏ
|
|
6
|
+
* @param fields ้่ฆๆๅ็ Git ๅญๆฎตๅ่กจ
|
|
7
|
+
* @param cwd ๅทฅไฝ็ฎๅฝ๏ผ้ป่ฎคไธบๅฝๅ็ฎๅฝ
|
|
8
|
+
* @returns Git ๆฅๅฟๅฏน่ฑก๏ผๅ
ๅซๆๆ่ฏทๆฑ็ๅญๆฎต
|
|
9
|
+
*/
|
|
10
|
+
declare function getGitLog(fields?: string[], cwd?: string): GitLog;
|
|
11
|
+
/**
|
|
12
|
+
* ่ทๅๆๆๅฏ็จ็ Git ๅญๆฎต
|
|
13
|
+
*/
|
|
14
|
+
declare function getAvailableFields(): GitField[];
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/core/outputs.d.ts
|
|
17
|
+
interface JsonOutputOptions {
|
|
18
|
+
fileName?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Window ๅ
จๅฑๅ้่พๅบ้้กน
|
|
22
|
+
* ้่ฟ define ๆฟๆข๏ผ็ผ่ฏๆถ๏ผๅ HTML ๆณจๅ
ฅ๏ผVite๏ผๅฎ็ฐ
|
|
23
|
+
*/
|
|
24
|
+
interface WindowOutputOptions {
|
|
25
|
+
/** ๅ
จๅฑๅ้ๅ็งฐ๏ผ้ป่ฎคไธบ '__GIT_LOG__' */
|
|
26
|
+
varName?: string;
|
|
27
|
+
/** ๆฏๅฆๅจๆต่งๅจๆงๅถๅฐๆๅฐ Git ๆฅๅฟ๏ผ้ป่ฎค๏ผfalse */
|
|
28
|
+
console?: boolean;
|
|
29
|
+
}
|
|
30
|
+
interface OutputOptions {
|
|
31
|
+
json?: JsonOutputOptions;
|
|
32
|
+
window?: WindowOutputOptions;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* ็ๆ JSON ๆไปถๅฐ่พๅบ็ฎๅฝ
|
|
36
|
+
* @param gitLog Git ๆฅๅฟๅฏน่ฑก
|
|
37
|
+
* @param options JSON ่พๅบ้้กน
|
|
38
|
+
* @param outputDir ่พๅบ็ฎๅฝ่ทฏๅพ
|
|
39
|
+
*/
|
|
40
|
+
declare function generateJson(gitLog: GitLog, options?: JsonOutputOptions, outputDir?: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* ๆ นๆฎ้
็ฝฎ็ๆๆๆ่พๅบๆไปถ
|
|
43
|
+
* ็ฎๅๆฏๆ JSON ๆไปถ่พๅบ
|
|
44
|
+
* Window ๅ
จๅฑๅ้้่ฟ define ๆฟๆขๅ HTML ๆณจๅ
ฅๅฎ็ฐ๏ผไธ็ๆๆไปถ
|
|
45
|
+
*
|
|
46
|
+
* @param gitLog Git ๆฅๅฟๅฏน่ฑก
|
|
47
|
+
* @param outputOptions ่พๅบ้้กน้
็ฝฎ
|
|
48
|
+
* @param outputDir ่พๅบ็ฎๅฝ่ทฏๅพ
|
|
49
|
+
*/
|
|
50
|
+
declare function generateOutputs(gitLog: GitLog, outputOptions: OutputOptions, outputDir?: string): void;
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/core/options.d.ts
|
|
53
|
+
/**
|
|
54
|
+
* ๆไปถ้
็ฝฎ้้กน
|
|
55
|
+
*/
|
|
56
|
+
interface Options {
|
|
57
|
+
/** ๆฏๅฆๅฏ็จๆไปถ๏ผ้ป่ฎค๏ผtrue */
|
|
58
|
+
enable?: boolean;
|
|
59
|
+
/** ้่ฆๆๅ็ Git ๅญๆฎตๅ่กจ๏ผ้ป่ฎค๏ผๆๆๅฏ็จๅญๆฎต */
|
|
60
|
+
fields?: GitField[] | string[];
|
|
61
|
+
/** ่พๅบ้้กน้
็ฝฎ */
|
|
62
|
+
outputs?: OutputOptions;
|
|
63
|
+
/** ๅทฅไฝ็ฎๅฝ๏ผ้ป่ฎคไธบๅฝๅ็ฎๅฝ */
|
|
64
|
+
cwd?: string;
|
|
65
|
+
}
|
|
66
|
+
type OptionsResolved = Omit<Pick<Options, "enable" | "fields" | "outputs" | "cwd">, "outputs"> & {
|
|
67
|
+
outputs: OutputOptions;
|
|
68
|
+
};
|
|
69
|
+
//#endregion
|
|
70
|
+
export { WindowOutputOptions as a, GitField as c, getGitLog as d, OutputOptions as i, GitLog as l, OptionsResolved as n, generateJson as o, JsonOutputOptions as r, generateOutputs as s, Options as t, getAvailableFields as u };
|
|
@@ -27,7 +27,8 @@ function execGitCommand(command, cwd) {
|
|
|
27
27
|
encoding: "utf8",
|
|
28
28
|
stdio: "pipe"
|
|
29
29
|
}).trim();
|
|
30
|
-
} catch {
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.warn(`[unplugin-auto-git-log] Git command failed: ${command}`, error);
|
|
31
32
|
return "";
|
|
32
33
|
}
|
|
33
34
|
}
|
|
@@ -38,11 +39,17 @@ function getGitRoot(cwd) {
|
|
|
38
39
|
return execGitCommand("git rev-parse --show-toplevel", cwd);
|
|
39
40
|
}
|
|
40
41
|
/**
|
|
41
|
-
* ่ทๅ Git
|
|
42
|
+
* ่ทๅ Git ๆฅๅฟไฟกๆฏ
|
|
43
|
+
* @param fields ้่ฆๆๅ็ Git ๅญๆฎตๅ่กจ
|
|
44
|
+
* @param cwd ๅทฅไฝ็ฎๅฝ๏ผ้ป่ฎคไธบๅฝๅ็ฎๅฝ
|
|
45
|
+
* @returns Git ๆฅๅฟๅฏน่ฑก๏ผๅ
ๅซๆๆ่ฏทๆฑ็ๅญๆฎต
|
|
42
46
|
*/
|
|
43
47
|
function getGitLog(fields = [], cwd) {
|
|
44
48
|
const result = {};
|
|
45
|
-
if (!isGitRepository(cwd))
|
|
49
|
+
if (!isGitRepository(cwd)) {
|
|
50
|
+
console.warn("[unplugin-auto-git-log] Not a Git repository or cannot access .git directory");
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
46
53
|
const gitRoot = getGitRoot(cwd);
|
|
47
54
|
for (const field of fields) switch (field) {
|
|
48
55
|
case "repo": {
|
|
@@ -53,9 +60,15 @@ function getGitLog(fields = [], cwd) {
|
|
|
53
60
|
} else result.repo = gitRoot ? basename(gitRoot) : "";
|
|
54
61
|
break;
|
|
55
62
|
}
|
|
56
|
-
case "branch":
|
|
57
|
-
|
|
63
|
+
case "branch": {
|
|
64
|
+
const branch = execGitCommand("git rev-parse --abbrev-ref HEAD", cwd);
|
|
65
|
+
if (branch === "HEAD") {
|
|
66
|
+
const tag = execGitCommand("git describe --tags --exact-match HEAD 2>/dev/null", cwd);
|
|
67
|
+
if (tag) result.branch = tag;
|
|
68
|
+
else result.branch = execGitCommand("git rev-parse --short HEAD", cwd) || "HEAD";
|
|
69
|
+
} else result.branch = branch || "";
|
|
58
70
|
break;
|
|
71
|
+
}
|
|
59
72
|
case "commit":
|
|
60
73
|
result.commit = execGitCommand("git rev-parse HEAD", cwd) || "";
|
|
61
74
|
break;
|
|
@@ -69,10 +82,10 @@ function getGitLog(fields = [], cwd) {
|
|
|
69
82
|
result.authorEmail = execGitCommand("git log -1 --pretty=format:\"%ae\"", cwd) || "";
|
|
70
83
|
break;
|
|
71
84
|
case "commitTime":
|
|
72
|
-
result.commitTime = execGitCommand("git log -1 --pretty=format:\"%
|
|
85
|
+
result.commitTime = execGitCommand("git log -1 --pretty=format:\"%cI\"", cwd) || "";
|
|
73
86
|
break;
|
|
74
87
|
case "commitMessage":
|
|
75
|
-
result.commitMessage = execGitCommand("git log -1 --pretty=format:\"%s\"", cwd) || "";
|
|
88
|
+
result.commitMessage = execGitCommand("git log -1 --pretty=format:\"%s\"", cwd).replace(/\n/g, " ").trim() || "";
|
|
76
89
|
break;
|
|
77
90
|
case "tag":
|
|
78
91
|
result.tag = execGitCommand("git describe --tags --exact-match HEAD 2>/dev/null", cwd) || "";
|
|
@@ -116,12 +129,17 @@ function getAvailableFields() {
|
|
|
116
129
|
//#region src/core/outputs.ts
|
|
117
130
|
/**
|
|
118
131
|
* ็ๆ JSON ๆไปถๅ
ๅฎน
|
|
132
|
+
* @param gitLog Git ๆฅๅฟๅฏน่ฑก
|
|
133
|
+
* @returns ๆ ผๅผๅ็ JSON ๅญ็ฌฆไธฒ
|
|
119
134
|
*/
|
|
120
135
|
function generateJsonContent(gitLog) {
|
|
121
136
|
return JSON.stringify(gitLog, null, 2);
|
|
122
137
|
}
|
|
123
138
|
/**
|
|
124
|
-
* ็ๆ JSON
|
|
139
|
+
* ็ๆ JSON ๆไปถๅฐ่พๅบ็ฎๅฝ
|
|
140
|
+
* @param gitLog Git ๆฅๅฟๅฏน่ฑก
|
|
141
|
+
* @param options JSON ่พๅบ้้กน
|
|
142
|
+
* @param outputDir ่พๅบ็ฎๅฝ่ทฏๅพ
|
|
125
143
|
*/
|
|
126
144
|
function generateJson(gitLog, options = {}, outputDir) {
|
|
127
145
|
const fileName = options.fileName || "git-log.json";
|
|
@@ -130,80 +148,17 @@ function generateJson(gitLog, options = {}, outputDir) {
|
|
|
130
148
|
writeFileSync(fullPath, generateJsonContent(gitLog), "utf8");
|
|
131
149
|
}
|
|
132
150
|
/**
|
|
133
|
-
*
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
${options.console ? `console.log('[Git Log]', window.${varName});` : ""}
|
|
141
|
-
}
|
|
142
|
-
})();`;
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* ็ๆ window ๅ
จๅฑๅ้ๆไปถ
|
|
146
|
-
*/
|
|
147
|
-
function generateWindowVar(gitLog, options = {}, outputDir) {
|
|
148
|
-
const fileName = `${options.varName || "__GIT_LOG__"}.js`;
|
|
149
|
-
const fullPath = outputDir ? resolve(outputDir, fileName) : fileName;
|
|
150
|
-
mkdirSync(dirname(fullPath), { recursive: true });
|
|
151
|
-
writeFileSync(fullPath, generateWindowVarContent(gitLog, options), "utf8");
|
|
152
|
-
return fileName;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* ็ๆ็ฏๅขๅ้ๆไปถๅ
ๅฎน
|
|
156
|
-
*/
|
|
157
|
-
function generateEnvVarsContent(gitLog, options = {}) {
|
|
158
|
-
const prefix = options.prefix || "__GIT_";
|
|
159
|
-
const lines = [];
|
|
160
|
-
for (const [key, value] of Object.entries(gitLog)) {
|
|
161
|
-
const envKey = `${prefix}${key.toUpperCase()}`;
|
|
162
|
-
const escapedValue = (typeof value === "string" ? value : String(value)).replaceAll("\"", String.raw`\"`);
|
|
163
|
-
lines.push(`${envKey}="${escapedValue}"`);
|
|
164
|
-
}
|
|
165
|
-
return lines.join("\n");
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* ็ๆ็ฏๅขๅ้ๆไปถ
|
|
169
|
-
*/
|
|
170
|
-
function generateEnvVars(gitLog, options = {}, outputDir) {
|
|
171
|
-
const filePath = options.file || ".env.git";
|
|
172
|
-
const fullPath = outputDir ? resolve(outputDir, filePath) : filePath;
|
|
173
|
-
mkdirSync(dirname(fullPath), { recursive: true });
|
|
174
|
-
writeFileSync(fullPath, generateEnvVarsContent(gitLog, options), "utf8");
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* ็ๆ TypeScript ็ฑปๅๅฎไนๆไปถๅ
ๅฎน
|
|
178
|
-
*/
|
|
179
|
-
function generateTypeDefinitionsContent(gitLog) {
|
|
180
|
-
return `export interface GitLog {
|
|
181
|
-
${Object.entries(gitLog).map(([key, value]) => {
|
|
182
|
-
return ` ${key}: ${typeof value === "boolean" ? "boolean" : "string"}`;
|
|
183
|
-
}).join("\n")}
|
|
184
|
-
}
|
|
185
|
-
`;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* ็ๆ TypeScript ็ฑปๅๅฎไนๆไปถ
|
|
189
|
-
*/
|
|
190
|
-
function generateTypeDefinitions(gitLog, options = {}, outputDir) {
|
|
191
|
-
const fileName = options.fileName || "git-log.d.ts";
|
|
192
|
-
const fullPath = outputDir && !isAbsolute(fileName) ? resolve(outputDir, fileName) : fileName;
|
|
193
|
-
mkdirSync(dirname(fullPath), { recursive: true });
|
|
194
|
-
writeFileSync(fullPath, generateTypeDefinitionsContent(gitLog), "utf8");
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* ๆ นๆฎ้
็ฝฎ็ๆๆๆ่พๅบ
|
|
151
|
+
* ๆ นๆฎ้
็ฝฎ็ๆๆๆ่พๅบๆไปถ
|
|
152
|
+
* ็ฎๅๆฏๆ JSON ๆไปถ่พๅบ
|
|
153
|
+
* Window ๅ
จๅฑๅ้้่ฟ define ๆฟๆขๅ HTML ๆณจๅ
ฅๅฎ็ฐ๏ผไธ็ๆๆไปถ
|
|
154
|
+
*
|
|
155
|
+
* @param gitLog Git ๆฅๅฟๅฏน่ฑก
|
|
156
|
+
* @param outputOptions ่พๅบ้้กน้
็ฝฎ
|
|
157
|
+
* @param outputDir ่พๅบ็ฎๅฝ่ทฏๅพ
|
|
198
158
|
*/
|
|
199
159
|
function generateOutputs(gitLog, outputOptions, outputDir) {
|
|
200
|
-
let windowVarFileName;
|
|
201
160
|
if (outputOptions.json) generateJson(gitLog, outputOptions.json, outputDir);
|
|
202
|
-
if (outputOptions.window) windowVarFileName = generateWindowVar(gitLog, outputOptions.window, outputDir);
|
|
203
|
-
if (outputOptions.env) generateEnvVars(gitLog, outputOptions.env, outputDir);
|
|
204
|
-
if (outputOptions.types) generateTypeDefinitions(gitLog, outputOptions.types, outputDir);
|
|
205
|
-
return windowVarFileName;
|
|
206
161
|
}
|
|
207
162
|
|
|
208
163
|
//#endregion
|
|
209
|
-
export {
|
|
164
|
+
export { getGitLog as i, generateOutputs as n, getAvailableFields as r, generateJson as t };
|
package/dist/rolldown.d.mts
CHANGED
package/dist/rolldown.mjs
CHANGED
package/dist/rollup.d.mts
CHANGED
package/dist/rollup.mjs
CHANGED
package/dist/rspack.d.mts
CHANGED
package/dist/rspack.mjs
CHANGED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { i as getGitLog, n as generateOutputs } from "./outputs-Brlh_kpb.mjs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
import { createUnplugin } from "unplugin";
|
|
5
|
+
|
|
6
|
+
//#region src/core/options.ts
|
|
7
|
+
/**
|
|
8
|
+
* ้ป่ฎคๆๅ็ Git ๅญๆฎตๅ่กจ
|
|
9
|
+
*/
|
|
10
|
+
const DEFAULT_FIELDS = [
|
|
11
|
+
"repo",
|
|
12
|
+
"branch",
|
|
13
|
+
"commit",
|
|
14
|
+
"commitShort",
|
|
15
|
+
"author",
|
|
16
|
+
"authorEmail",
|
|
17
|
+
"commitTime",
|
|
18
|
+
"commitMessage",
|
|
19
|
+
"isDirty"
|
|
20
|
+
];
|
|
21
|
+
/**
|
|
22
|
+
* ่งฃๆๅนถ่ง่ๅๆไปถ้้กน
|
|
23
|
+
* @param options ็จๆทๆไพ็้้กน
|
|
24
|
+
* @returns ่งฃๆๅ็้้กนๅฏน่ฑก
|
|
25
|
+
*/
|
|
26
|
+
function resolveOptions(options = {}) {
|
|
27
|
+
return {
|
|
28
|
+
enable: options.enable !== void 0 ? options.enable : true,
|
|
29
|
+
fields: options.fields || DEFAULT_FIELDS,
|
|
30
|
+
outputs: options.outputs || {},
|
|
31
|
+
cwd: options.cwd
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/index.ts
|
|
37
|
+
/**
|
|
38
|
+
* ไปๆๅปบๅทฅๅ
ท้
็ฝฎไธญ่ทๅ่พๅบ็ฎๅฝ
|
|
39
|
+
* ๆฏๆ ViteใWebpackใRollup/Rolldown ็ญๆๅปบๅทฅๅ
ท
|
|
40
|
+
*/
|
|
41
|
+
function getOutputDir(config, cwd) {
|
|
42
|
+
const projectRoot = cwd || process.cwd();
|
|
43
|
+
if ("vite" in config || config.vite) {
|
|
44
|
+
const outDir = (config.vite || config).build?.outDir;
|
|
45
|
+
if (outDir) return resolve(projectRoot, outDir);
|
|
46
|
+
}
|
|
47
|
+
if ("webpack" in config || config.output?.path) {
|
|
48
|
+
const outputPath = config.output?.path;
|
|
49
|
+
if (outputPath) return resolve(projectRoot, outputPath);
|
|
50
|
+
}
|
|
51
|
+
if (config.output?.dir) {
|
|
52
|
+
const outputDir = config.output.dir;
|
|
53
|
+
return resolve(projectRoot, outputDir);
|
|
54
|
+
}
|
|
55
|
+
return resolve(projectRoot, "dist");
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* ๆ นๆฎ้
็ฝฎ็ๆ Git ๆฅๅฟ่พๅบๆไปถ
|
|
59
|
+
* ็ฎๅๆฏๆ JSON ๆไปถ่พๅบ
|
|
60
|
+
*/
|
|
61
|
+
function generateGitLogFiles(options, outputDir) {
|
|
62
|
+
if (!options.enable) return;
|
|
63
|
+
const gitLog = getGitLog(options.fields, options.cwd);
|
|
64
|
+
if (options.outputs && (options.outputs.json || options.outputs.window)) generateOutputs(gitLog, options.outputs, outputDir);
|
|
65
|
+
else generateOutputs(gitLog, { json: {} }, outputDir);
|
|
66
|
+
}
|
|
67
|
+
const AutoGitLog = createUnplugin((rawOptions = {}) => {
|
|
68
|
+
const options = resolveOptions(rawOptions);
|
|
69
|
+
let outputDir;
|
|
70
|
+
let gitLog = null;
|
|
71
|
+
const varName = options.outputs?.window?.varName || "__GIT_LOG__";
|
|
72
|
+
const shouldConsole = options.enable && options.outputs?.window?.console === true;
|
|
73
|
+
const name = "unplugin-auto-git-log";
|
|
74
|
+
if (options.enable && options.outputs?.window) gitLog = getGitLog(options.fields, options.cwd);
|
|
75
|
+
return {
|
|
76
|
+
name,
|
|
77
|
+
enforce: "post",
|
|
78
|
+
define: options.enable && options.outputs?.window && gitLog ? { [`window.${varName}`]: JSON.stringify(gitLog) } : {},
|
|
79
|
+
vite: { transformIndexHtml(html) {
|
|
80
|
+
if (!options.enable || !options.outputs?.window || !gitLog) return html;
|
|
81
|
+
const scriptCode = `<script>
|
|
82
|
+
if (typeof window !== 'undefined') {
|
|
83
|
+
window.${varName} = ${JSON.stringify(gitLog)};${shouldConsole ? `\n console.log('[Git Log]', window.${varName});` : ""}
|
|
84
|
+
}
|
|
85
|
+
<\/script>`;
|
|
86
|
+
return html.replace("</head>", ` ${scriptCode}\n</head>`);
|
|
87
|
+
} },
|
|
88
|
+
configResolved(config) {
|
|
89
|
+
outputDir = getOutputDir(config, options.cwd);
|
|
90
|
+
},
|
|
91
|
+
buildEnd() {
|
|
92
|
+
if (!options.enable) return;
|
|
93
|
+
generateGitLogFiles(options, outputDir || "dist");
|
|
94
|
+
},
|
|
95
|
+
writeBundle() {
|
|
96
|
+
if (!options.enable) return;
|
|
97
|
+
generateGitLogFiles(options, outputDir || "dist");
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
export { AutoGitLog as t };
|
package/dist/vite.d.mts
CHANGED
package/dist/vite.mjs
CHANGED
package/dist/webpack.d.mts
CHANGED
package/dist/webpack.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unplugin-auto-git-log",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"packageManager": "pnpm@10.27.0",
|
|
6
6
|
"description": "Unplugin for automatically generating Git information (repo, branch, commit, etc.) in multiple output formats.",
|
|
7
7
|
"author": "Drswith",
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { FilterPattern } from "unplugin";
|
|
2
|
-
|
|
3
|
-
//#region src/core/git.d.ts
|
|
4
|
-
type GitField = "repo" | "branch" | "commit" | "commitShort" | "author" | "authorEmail" | "commitTime" | "commitMessage" | "tag" | "isDirty" | "remoteUrl" | "root";
|
|
5
|
-
type GitLog = Record<string, string | boolean>;
|
|
6
|
-
/**
|
|
7
|
-
* ่ทๅ Git ๆฅๅฟ
|
|
8
|
-
*/
|
|
9
|
-
declare function getGitLog(fields?: string[], cwd?: string): GitLog;
|
|
10
|
-
/**
|
|
11
|
-
* ่ทๅๆๆๅฏ็จ็ Git ๅญๆฎต
|
|
12
|
-
*/
|
|
13
|
-
declare function getAvailableFields(): GitField[];
|
|
14
|
-
//#endregion
|
|
15
|
-
//#region src/core/outputs.d.ts
|
|
16
|
-
interface JsonOutputOptions {
|
|
17
|
-
fileName?: string;
|
|
18
|
-
}
|
|
19
|
-
interface WindowOutputOptions {
|
|
20
|
-
varName?: string;
|
|
21
|
-
console?: boolean;
|
|
22
|
-
}
|
|
23
|
-
interface EnvOutputOptions {
|
|
24
|
-
prefix?: string;
|
|
25
|
-
file?: string;
|
|
26
|
-
}
|
|
27
|
-
interface TypesOutputOptions {
|
|
28
|
-
fileName?: string;
|
|
29
|
-
}
|
|
30
|
-
interface OutputOptions {
|
|
31
|
-
json?: JsonOutputOptions;
|
|
32
|
-
window?: WindowOutputOptions;
|
|
33
|
-
env?: EnvOutputOptions;
|
|
34
|
-
types?: TypesOutputOptions;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* ็ๆ JSON ๆไปถ
|
|
38
|
-
*/
|
|
39
|
-
declare function generateJson(gitLog: GitLog, options?: JsonOutputOptions, outputDir?: string): void;
|
|
40
|
-
/**
|
|
41
|
-
* ็ๆ window ๅ
จๅฑๅ้ๆไปถ
|
|
42
|
-
*/
|
|
43
|
-
declare function generateWindowVar(gitLog: GitLog, options?: WindowOutputOptions, outputDir?: string): string;
|
|
44
|
-
/**
|
|
45
|
-
* ็ๆ็ฏๅขๅ้ๆไปถ
|
|
46
|
-
*/
|
|
47
|
-
declare function generateEnvVars(gitLog: GitLog, options?: EnvOutputOptions, outputDir?: string): void;
|
|
48
|
-
/**
|
|
49
|
-
* ็ๆ TypeScript ็ฑปๅๅฎไนๆไปถ
|
|
50
|
-
*/
|
|
51
|
-
declare function generateTypeDefinitions(gitLog: GitLog, options?: TypesOutputOptions, outputDir?: string): void;
|
|
52
|
-
/**
|
|
53
|
-
* ๆ นๆฎ้
็ฝฎ็ๆๆๆ่พๅบ
|
|
54
|
-
*/
|
|
55
|
-
declare function generateOutputs(gitLog: GitLog, outputOptions: OutputOptions, outputDir?: string): string | undefined;
|
|
56
|
-
//#endregion
|
|
57
|
-
//#region src/core/options.d.ts
|
|
58
|
-
interface Options {
|
|
59
|
-
include?: FilterPattern;
|
|
60
|
-
exclude?: FilterPattern;
|
|
61
|
-
enforce?: "pre" | "post" | undefined;
|
|
62
|
-
enable?: boolean;
|
|
63
|
-
fields?: GitField[] | string[];
|
|
64
|
-
outputs?: OutputOptions;
|
|
65
|
-
cwd?: string;
|
|
66
|
-
}
|
|
67
|
-
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
|
|
68
|
-
type OptionsResolved = Overwrite<Required<Pick<Options, "include" | "exclude">>, Omit<Pick<Options, "enforce" | "enable" | "fields" | "outputs" | "cwd">, "outputs"> & {
|
|
69
|
-
outputs: OutputOptions;
|
|
70
|
-
}>;
|
|
71
|
-
//#endregion
|
|
72
|
-
export { OutputOptions as a, generateEnvVars as c, generateTypeDefinitions as d, generateWindowVar as f, getGitLog as g, getAvailableFields as h, JsonOutputOptions as i, generateJson as l, GitLog as m, OptionsResolved as n, TypesOutputOptions as o, GitField as p, EnvOutputOptions as r, WindowOutputOptions as s, Options as t, generateOutputs as u };
|
package/dist/src-Bbcs1xDg.mjs
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { r as generateOutputs, s as getGitLog } from "./outputs-CbT7SkvQ.mjs";
|
|
2
|
-
import { createUnplugin } from "unplugin";
|
|
3
|
-
|
|
4
|
-
//#region src/core/options.ts
|
|
5
|
-
const DEFAULT_FIELDS = [
|
|
6
|
-
"repo",
|
|
7
|
-
"branch",
|
|
8
|
-
"commit",
|
|
9
|
-
"commitShort",
|
|
10
|
-
"author",
|
|
11
|
-
"authorEmail",
|
|
12
|
-
"commitTime",
|
|
13
|
-
"commitMessage",
|
|
14
|
-
"isDirty"
|
|
15
|
-
];
|
|
16
|
-
function resolveOptions(options = {}) {
|
|
17
|
-
return {
|
|
18
|
-
include: options.include || [/\.[cm]?[jt]sx?$/],
|
|
19
|
-
exclude: options.exclude || [/node_modules/],
|
|
20
|
-
enforce: "enforce" in options ? options.enforce : "post",
|
|
21
|
-
enable: options.enable !== void 0 ? options.enable : true,
|
|
22
|
-
fields: options.fields || DEFAULT_FIELDS,
|
|
23
|
-
outputs: options.outputs || {},
|
|
24
|
-
cwd: options.cwd
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
//#endregion
|
|
29
|
-
//#region src/index.ts
|
|
30
|
-
const AutoGitLog = createUnplugin((rawOptions = {}) => {
|
|
31
|
-
const options = resolveOptions(rawOptions);
|
|
32
|
-
let outputDir;
|
|
33
|
-
let windowVarFileName;
|
|
34
|
-
return {
|
|
35
|
-
name: "unplugin-auto-git-log",
|
|
36
|
-
enforce: options.enforce,
|
|
37
|
-
configResolved(config) {
|
|
38
|
-
if ("vite" in config) {
|
|
39
|
-
const viteConfig = config;
|
|
40
|
-
outputDir = viteConfig.build?.outDir || viteConfig.vite?.config?.build?.outDir;
|
|
41
|
-
} else if ("webpack" in config) outputDir = config.output?.path;
|
|
42
|
-
else if ("output" in config && typeof config.output === "object") outputDir = config.output?.dir;
|
|
43
|
-
if (!outputDir) outputDir = "dist";
|
|
44
|
-
},
|
|
45
|
-
buildEnd() {
|
|
46
|
-
if (!options.enable) return;
|
|
47
|
-
const gitLog = getGitLog(options.fields, options.cwd);
|
|
48
|
-
if (options.outputs && (options.outputs.json || options.outputs.window || options.outputs.env || options.outputs.types)) windowVarFileName = generateOutputs(gitLog, options.outputs, outputDir);
|
|
49
|
-
else generateOutputs(gitLog, { json: {} }, outputDir);
|
|
50
|
-
},
|
|
51
|
-
vite: { transformIndexHtml(html) {
|
|
52
|
-
if (options.outputs?.window && windowVarFileName) return {
|
|
53
|
-
html,
|
|
54
|
-
tags: [{
|
|
55
|
-
tag: "script",
|
|
56
|
-
attrs: { src: `/${windowVarFileName}` },
|
|
57
|
-
injectTo: "head"
|
|
58
|
-
}]
|
|
59
|
-
};
|
|
60
|
-
} }
|
|
61
|
-
};
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
//#endregion
|
|
65
|
-
export { AutoGitLog as t };
|