screw-up 0.12.0 → 0.13.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/LICENSE +21 -0
- package/README.md +20 -4
- package/dist/analyzer.d.ts +13 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/cli-internal.d.ts +30 -7
- package/dist/cli-internal.d.ts.map +1 -1
- package/dist/cli.d.ts +12 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/generated/packageMetadata.d.ts +18 -0
- package/dist/generated/packageMetadata.d.ts.map +1 -0
- package/dist/index.cjs +20 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +10 -1
- package/dist/index.js +20 -3
- package/dist/index.js.map +1 -1
- package/dist/{internal-Di0s8LQa.cjs → internal-BHSe5LIZ.cjs} +349 -322
- package/dist/internal-BHSe5LIZ.cjs.map +1 -0
- package/dist/{internal-BaMzTKS2.js → internal-BgCvktPU.js} +351 -324
- package/dist/internal-BgCvktPU.js.map +1 -0
- package/dist/internal.d.ts +64 -14
- package/dist/internal.d.ts.map +1 -1
- package/dist/main.cjs +1166 -0
- package/dist/main.cjs.map +1 -0
- package/dist/main.d.ts +13 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +1165 -0
- package/dist/main.js.map +1 -0
- package/dist/packageMetadata-DLfWvbn3.cjs +20 -0
- package/dist/packageMetadata-DLfWvbn3.cjs.map +1 -0
- package/dist/packageMetadata-Dm-0ihYZ.js +20 -0
- package/dist/packageMetadata-Dm-0ihYZ.js.map +1 -0
- package/dist/types.d.ts +15 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/vite-plugin.d.ts +10 -1
- package/dist/vite-plugin.d.ts.map +1 -1
- package/images/screw-up-120.png +0 -0
- package/package.json +13 -14
- package/README_pack.md +0 -63
- package/dist/cli.cjs +0 -765
- package/dist/cli.cjs.map +0 -1
- package/dist/cli.js +0 -764
- package/dist/cli.js.map +0 -1
- package/dist/internal-BaMzTKS2.js.map +0 -1
- package/dist/internal-Di0s8LQa.cjs.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Kouji Matsui (@kekyo@mi.kekyo.net)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# screw-up
|
|
2
2
|
|
|
3
|
-
Simply package metadata inserter for
|
|
3
|
+
Simply package metadata inserter for NPM.
|
|
4
|
+
|
|
5
|
+

|
|
4
6
|
|
|
5
7
|
[](https://www.repostatus.org/#wip)
|
|
6
8
|
[](https://opensource.org/licenses/MIT)
|
|
@@ -9,9 +11,12 @@ Simply package metadata inserter for Vite plugins.
|
|
|
9
11
|
|
|
10
12
|
## What is this?
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
Looking for a simple solution to apply versions to TypeScript projects and NPM packages?
|
|
15
|
+
`screw-up` could be the tool you need.
|
|
16
|
+
|
|
17
|
+
It is a Vite plugin that automatically inserts banner comments containing package metadata (name, version, description, author, license, etc.) into bundled files, and a CLI tool that applies them to NPM packages.
|
|
13
18
|
|
|
14
|
-
|
|
19
|
+
The Vite plugin automatically reads metadata from `package.json`:
|
|
15
20
|
|
|
16
21
|
```json
|
|
17
22
|
{
|
|
@@ -41,6 +46,17 @@ To insert banner header each bundled source files (`dist/index.js` and etc.):
|
|
|
41
46
|
// Your bundled code here...
|
|
42
47
|
```
|
|
43
48
|
|
|
49
|
+
You may have noticed the line `git.commit.hash:`. That's right, if your project is managed by Git (it is, right?), you can also insert commit IDs, branch information, and tag information.
|
|
50
|
+
Most importantly, if a version is applied to a Git tag, you can automatically reflect that version tag in the `version` field of `package.json`. In other words, you can manage version numbers using only Git tags!
|
|
51
|
+
|
|
52
|
+
Instead of using `npm pack`, you can use the CLI tool `screw-up` to generate packages, which will automatically apply the collected metadata to the NPM package's `package.json`:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# Generate a package using `screw-up` command
|
|
56
|
+
$ screw-up pack
|
|
57
|
+
my-awesome-library-2.1.0.tgz
|
|
58
|
+
```
|
|
59
|
+
|
|
44
60
|
## Key Features
|
|
45
61
|
|
|
46
62
|
* Automatic metadata extraction: Reads metadata from `package.json` automatically.
|
package/dist/analyzer.d.ts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* name: screw-up
|
|
3
|
+
* version: 0.13.0
|
|
4
|
+
* description: Simply package metadata inserter on Vite plugin
|
|
5
|
+
* author: Kouji Matsui (@kekyo@mi.kekyo.net)
|
|
6
|
+
* license: MIT
|
|
7
|
+
* repository.url: https://github.com/kekyo/screw-up.git
|
|
8
|
+
* git.commit.hash: 9d03898db6924cec97e10bf6a1208b2c52ee87c0
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { Logger } from './internal.js';
|
|
1
12
|
/**
|
|
2
13
|
* Get default Git metadata from local repository.
|
|
3
14
|
* @param repositoryPath - Local Git repository directory
|
|
4
15
|
* @param checkWorkingDirectoryStatus - Check working directory status to increase version
|
|
16
|
+
* @param logger - Logger instance
|
|
5
17
|
* @returns The metadata object with git metadata
|
|
6
18
|
*/
|
|
7
|
-
export declare const getGitMetadata: (repositoryPath: string, checkWorkingDirectoryStatus: boolean) => Promise<any>;
|
|
19
|
+
export declare const getGitMetadata: (repositoryPath: string, checkWorkingDirectoryStatus: boolean, logger: Logger) => Promise<any>;
|
|
8
20
|
//# sourceMappingURL=analyzer.d.ts.map
|
package/dist/analyzer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analyzer.d.ts","sourceRoot":"","sources":["../src/analyzer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"analyzer.d.ts","sourceRoot":"","sources":["../src/analyzer.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAqfvC;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,GACzB,gBAAgB,MAAM,EAAE,6BAA6B,OAAO,EAAE,QAAQ,MAAM,iBA8D7E,CAAC"}
|
package/dist/cli-internal.d.ts
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* name: screw-up
|
|
3
|
+
* version: 0.13.0
|
|
4
|
+
* description: Simply package metadata inserter on Vite plugin
|
|
5
|
+
* author: Kouji Matsui (@kekyo@mi.kekyo.net)
|
|
6
|
+
* license: MIT
|
|
7
|
+
* repository.url: https://github.com/kekyo/screw-up.git
|
|
8
|
+
* git.commit.hash: 9d03898db6924cec97e10bf6a1208b2c52ee87c0
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { Logger } from './internal.js';
|
|
12
|
+
export interface PackedResult {
|
|
13
|
+
readonly packageFileName: string;
|
|
14
|
+
readonly metadata: any;
|
|
15
|
+
}
|
|
1
16
|
/**
|
|
2
|
-
* Pack assets
|
|
17
|
+
* Pack assets using npm pack delegation method
|
|
3
18
|
* @param targetDir - Target directory to pack
|
|
4
19
|
* @param outputDir - Output directory to write the tarball
|
|
5
20
|
* @param checkWorkingDirectoryStatus - Check working directory status
|
|
@@ -7,9 +22,10 @@
|
|
|
7
22
|
* @param readmeReplacementPath - Optional path to replacement README file
|
|
8
23
|
* @param replacePeerDepsWildcards - Replace "*" in peerDependencies with actual versions
|
|
9
24
|
* @param peerDepsVersionPrefix - Version prefix for replaced peerDependencies
|
|
25
|
+
* @param alwaysOverrideVersionFromGit - Always override version from Git (default: true)
|
|
10
26
|
* @returns Package metadata (package.json) or undefined if failed
|
|
11
27
|
*/
|
|
12
|
-
export declare const packAssets: (targetDir: string, outputDir: string, checkWorkingDirectoryStatus: boolean, inheritableFields: Set<string>, readmeReplacementPath: string | undefined, replacePeerDepsWildcards
|
|
28
|
+
export declare const packAssets: (targetDir: string, outputDir: string, checkWorkingDirectoryStatus: boolean, alwaysOverrideVersionFromGit: boolean, inheritableFields: Set<string>, readmeReplacementPath: string | undefined, replacePeerDepsWildcards: boolean, peerDepsVersionPrefix: string, logger: Logger) => Promise<PackedResult | undefined>;
|
|
13
29
|
/**
|
|
14
30
|
* Get computed package.json object
|
|
15
31
|
* @param targetDir - Target directory to resolve package metadata
|
|
@@ -17,11 +33,18 @@ export declare const packAssets: (targetDir: string, outputDir: string, checkWor
|
|
|
17
33
|
* @param inheritableFields - Package metadata fields that should be inherited from parent
|
|
18
34
|
* @returns Computed package.json object or undefined if failed
|
|
19
35
|
*/
|
|
20
|
-
export declare const getComputedPackageJsonObject: (targetDir: string, checkWorkingDirectoryStatus: boolean, inheritableFields: Set<string
|
|
36
|
+
export declare const getComputedPackageJsonObject: (targetDir: string, checkWorkingDirectoryStatus: boolean, alwaysOverrideVersionFromGit: boolean, inheritableFields: Set<string>, logger: Logger) => Promise<any>;
|
|
21
37
|
export interface ParsedArgs {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
38
|
+
readonly argv: string[];
|
|
39
|
+
readonly command?: string;
|
|
40
|
+
readonly positional: string[];
|
|
41
|
+
readonly options: Record<string, string | boolean>;
|
|
25
42
|
}
|
|
26
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Parse command line arguments
|
|
45
|
+
* @param args - Command line arguments
|
|
46
|
+
* @param argOptionMap - Map of command options to their argument options
|
|
47
|
+
* @returns Parsed arguments
|
|
48
|
+
*/
|
|
49
|
+
export declare const parseArgs: (args: string[], argOptionMap: Map<string, Set<string>>) => ParsedArgs;
|
|
27
50
|
//# sourceMappingURL=cli-internal.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli-internal.d.ts","sourceRoot":"","sources":["../src/cli-internal.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cli-internal.d.ts","sourceRoot":"","sources":["../src/cli-internal.ts"],"names":[],"mappings":"AAWA,OAAO,EAA8G,MAAM,EAAE,MAAM,eAAe,CAAC;AA4DnJ,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;CACxB;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,UAAU,GACrB,WAAW,MAAM,EACjB,WAAW,MAAM,EACjB,6BAA6B,OAAO,EACpC,8BAA8B,OAAO,EACrC,mBAAmB,GAAG,CAAC,MAAM,CAAC,EAC9B,uBAAuB,MAAM,GAAG,SAAS,EACzC,0BAA0B,OAAO,EACjC,uBAAuB,MAAM,EAC7B,QAAQ,MAAM,KAAI,OAAO,CAAC,YAAY,GAAG,SAAS,CAwGnD,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,4BAA4B,GACvC,WAAW,MAAM,EACjB,6BAA6B,OAAO,EACpC,8BAA8B,OAAO,EACrC,mBAAmB,GAAG,CAAC,MAAM,CAAC,EAC9B,QAAQ,MAAM,KAAI,OAAO,CAAC,GAAG,CAa9B,CAAC;AAIF,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC;IACxB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC;IAC9B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC;CACpD;AAED;;;;;GAKG;AACH,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,EAAE,EAAE,cAAc,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,KAAG,UAyClF,CAAC"}
|
package/dist/cli.d.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/*!
|
|
2
|
+
* name: screw-up
|
|
3
|
+
* version: 0.13.0
|
|
4
|
+
* description: Simply package metadata inserter on Vite plugin
|
|
5
|
+
* author: Kouji Matsui (@kekyo@mi.kekyo.net)
|
|
6
|
+
* license: MIT
|
|
7
|
+
* repository.url: https://github.com/kekyo/screw-up.git
|
|
8
|
+
* git.commit.hash: 9d03898db6924cec97e10bf6a1208b2c52ee87c0
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { Logger } from './internal.js';
|
|
12
|
+
export declare const cliMain: (args: string[], logger: Logger) => Promise<number>;
|
|
3
13
|
//# sourceMappingURL=cli.d.ts.map
|
package/dist/cli.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AA6WvC,eAAO,MAAM,OAAO,GAAU,MAAM,MAAM,EAAE,EAAE,QAAQ,MAAM,KAAG,OAAO,CAAC,MAAM,CAyB5E,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* name: screw-up
|
|
3
|
+
* version: 0.13.0
|
|
4
|
+
* description: Simply package metadata inserter on Vite plugin
|
|
5
|
+
* author: Kouji Matsui (@kekyo@mi.kekyo.net)
|
|
6
|
+
* license: MIT
|
|
7
|
+
* repository.url: https://github.com/kekyo/screw-up.git
|
|
8
|
+
* git.commit.hash: 9d03898db6924cec97e10bf6a1208b2c52ee87c0
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export declare const name = "screw-up";
|
|
12
|
+
export declare const version = "0.13.0";
|
|
13
|
+
export declare const description = "Simply package metadata inserter on Vite plugin";
|
|
14
|
+
export declare const author = "Kouji Matsui (@kekyo@mi.kekyo.net)";
|
|
15
|
+
export declare const license = "MIT";
|
|
16
|
+
export declare const repository_url = "https://github.com/kekyo/screw-up.git";
|
|
17
|
+
export declare const git_commit_hash = "9d03898db6924cec97e10bf6a1208b2c52ee87c0";
|
|
18
|
+
//# sourceMappingURL=packageMetadata.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"packageMetadata.d.ts","sourceRoot":"","sources":["../../src/generated/packageMetadata.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,IAAI,aAAa,CAAC;AAC/B,eAAO,MAAM,OAAO,WAAW,CAAC;AAChC,eAAO,MAAM,WAAW,oDAAoD,CAAC;AAC7E,eAAO,MAAM,MAAM,uCAAuC,CAAC;AAC3D,eAAO,MAAM,OAAO,QAAQ,CAAC;AAC7B,eAAO,MAAM,cAAc,0CAA0C,CAAC;AACtE,eAAO,MAAM,eAAe,6CAA6C,CAAC"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* name: screw-up
|
|
3
|
+
* version: 0.13.0
|
|
4
|
+
* description: Simply package metadata inserter on Vite plugin
|
|
5
|
+
* author: Kouji Matsui (@kekyo@mi.kekyo.net)
|
|
6
|
+
* license: MIT
|
|
7
|
+
* repository.url: https://github.com/kekyo/screw-up.git
|
|
8
|
+
* git.commit.hash: 9d03898db6924cec97e10bf6a1208b2c52ee87c0
|
|
9
|
+
*/
|
|
1
10
|
"use strict";
|
|
2
11
|
const promises = require("fs/promises");
|
|
3
12
|
const path = require("path");
|
|
4
|
-
const internal = require("./internal-
|
|
13
|
+
const internal = require("./internal-BHSe5LIZ.cjs");
|
|
5
14
|
const generateBanner = (metadata, outputKeys) => {
|
|
6
15
|
const parts = [];
|
|
7
16
|
for (const key of outputKeys) {
|
|
@@ -42,6 +51,7 @@ const generateMetadataFile = (metadata, outputKeys) => {
|
|
|
42
51
|
return lines.join("\n");
|
|
43
52
|
};
|
|
44
53
|
const screwUp = (options = {}) => {
|
|
54
|
+
const logger = internal.createConsoleLogger();
|
|
45
55
|
const {
|
|
46
56
|
outputKeys = ["name", "version", "description", "author", "license", "repository.url", "git.commit.hash"],
|
|
47
57
|
assetFilters = ["\\.d\\.ts$"],
|
|
@@ -49,6 +59,7 @@ const screwUp = (options = {}) => {
|
|
|
49
59
|
outputMetadataFilePath = "src/generated/packageMetadata.ts",
|
|
50
60
|
outputMetadataKeys = ["name", "version", "description", "author", "license", "repository.url", "git.commit.hash"],
|
|
51
61
|
checkWorkingDirectoryStatus = true,
|
|
62
|
+
alwaysOverrideVersionFromGit = true,
|
|
52
63
|
insertMetadataBanner = true
|
|
53
64
|
} = options;
|
|
54
65
|
const assetFiltersRegex = assetFilters.map((filter) => new RegExp(filter));
|
|
@@ -61,7 +72,13 @@ const screwUp = (options = {}) => {
|
|
|
61
72
|
// Configuration resolved phase
|
|
62
73
|
configResolved: async (config) => {
|
|
63
74
|
projectRoot = config.root;
|
|
64
|
-
|
|
75
|
+
const result = await internal.resolvePackageMetadata(
|
|
76
|
+
projectRoot,
|
|
77
|
+
checkWorkingDirectoryStatus,
|
|
78
|
+
alwaysOverrideVersionFromGit,
|
|
79
|
+
logger
|
|
80
|
+
);
|
|
81
|
+
metadata = result.metadata;
|
|
65
82
|
banner = generateBanner(metadata, outputKeys);
|
|
66
83
|
},
|
|
67
84
|
// Build start phase
|
|
@@ -73,7 +90,7 @@ const screwUp = (options = {}) => {
|
|
|
73
90
|
await promises.mkdir(path.dirname(metadataSourcePath), { recursive: true });
|
|
74
91
|
await promises.writeFile(metadataSourcePath, metadataSourceContent);
|
|
75
92
|
} catch (error) {
|
|
76
|
-
|
|
93
|
+
logger.warn(`[screw-up]: Failed to write metadata source file: ${metadataSourcePath}: ${error}`);
|
|
77
94
|
}
|
|
78
95
|
}
|
|
79
96
|
},
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/vite-plugin.ts"],"sourcesContent":["// screw-up - Easy package metadata inserter on Vite plugin\n// Copyright (c) Kouji Matsui (@kekyo@mi.kekyo.net)\n// Under MIT.\n// https://github.com/kekyo/screw-up/\n\nimport type { Plugin } from 'vite';\nimport { readFile, writeFile, readdir, mkdir } from 'fs/promises';\nimport { join, dirname } from 'path';\nimport { resolvePackageMetadata } from './internal.js';\nimport { ScrewUpOptions, PackageMetadata } from './types.js';\n\n/**\n * Generate banner string from package.json metadata\n * @param metadata - Package metadata\n * @param outputKeys - Array of keys to output in specified order\n * @returns Banner string\n */\nexport const generateBanner = (metadata: PackageMetadata, outputKeys: string[]): string => {\n const parts: string[] = [];\n \n for (const key of outputKeys) {\n const value = metadata[key];\n if (value) {\n parts.push(`${key}: ${value}`);\n }\n }\n \n return parts.length > 0 ? `/*!\\n * ${parts.join('\\n * ')}\\n */` : '';\n};\n\n/**\n * Insert banner header at appropriate position considering shebang\n * @param content - The content to insert banner into\n * @param banner - The banner header to insert\n * @returns Content with banner header inserted\n */\nconst insertBannerHeader = (content: string, banner: string): string => {\n const lines = content.split('\\n');\n \n // Check if first line is shebang\n if (lines.length > 0 && lines[0].startsWith('#!')) {\n // Insert banner after shebang line\n return lines[0] + '\\n' + banner + '\\n' + lines.slice(1).join('\\n');\n } else {\n // Insert banner at the beginning\n return banner + '\\n' + content;\n }\n};\n\n/**\n * Convert string key to valid TypeScript identifier\n * @param key - The key to convert\n * @returns Valid TypeScript identifier\n */\nconst sanitizeKey = (key: string): string => {\n // Replace dots and other invalid characters with underscores\n return key.replace(/[^a-zA-Z0-9_]/g, '_').replace(/^(\\d)/, '_$1');\n};\n\n/**\n * Generate TypeScript metadata file content from package metadata\n * @param metadata - Package metadata\n * @param outputKeys - Array of keys to output\n * @returns TypeScript file content\n */\nconst generateMetadataFile = (metadata: PackageMetadata, outputKeys: string[]): string => {\n const lines: string[] = [];\n \n lines.push('// This file is auto-generated by screw-up plugin');\n lines.push('// Do not edit manually');\n lines.push('');\n \n for (const key of outputKeys) {\n const value = metadata[key];\n if (value) {\n const sanitizedKey = sanitizeKey(key);\n const escapedValue = JSON.stringify(value);\n lines.push(`export const ${sanitizedKey} = ${escapedValue};`);\n }\n }\n \n lines.push('');\n \n return lines.join('\\n');\n};\n\n/////////////////////////////////////////////////////////////////////////////////////////\n\n/**\n * Vite plugin that adds banner to the bundled code\n * @param options - Plugin options\n * @returns Vite plugin\n */\nexport const screwUp = (options: ScrewUpOptions = {}): Plugin => {\n const {\n outputKeys = ['name', 'version', 'description', 'author', 'license', 'repository.url', 'git.commit.hash'],\n assetFilters = ['\\\\.d\\\\.ts$'],\n outputMetadataFile = false,\n outputMetadataFilePath = 'src/generated/packageMetadata.ts',\n outputMetadataKeys = ['name', 'version', 'description', 'author', 'license', 'repository.url', 'git.commit.hash'],\n checkWorkingDirectoryStatus = true,\n insertMetadataBanner = true} = options;\n\n const assetFiltersRegex = assetFilters.map(filter => new RegExp(filter));\n let banner: string;\n let metadata: any;\n let projectRoot: string;\n\n return {\n name: 'screw-up',\n apply: 'build',\n // Configuration resolved phase\n configResolved: async config => {\n // Save project root\n projectRoot = config.root;\n // Resolve package metadata\n
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/vite-plugin.ts"],"sourcesContent":["// screw-up - Easy package metadata inserter on Vite plugin\n// Copyright (c) Kouji Matsui (@kekyo@mi.kekyo.net)\n// Under MIT.\n// https://github.com/kekyo/screw-up/\n\nimport type { Plugin } from 'vite';\nimport { readFile, writeFile, readdir, mkdir } from 'fs/promises';\nimport { join, dirname } from 'path';\nimport { resolvePackageMetadata, createConsoleLogger } from './internal.js';\nimport { ScrewUpOptions, PackageMetadata } from './types.js';\n\n/**\n * Generate banner string from package.json metadata\n * @param metadata - Package metadata\n * @param outputKeys - Array of keys to output in specified order\n * @returns Banner string\n */\nexport const generateBanner = (metadata: PackageMetadata, outputKeys: string[]): string => {\n const parts: string[] = [];\n \n for (const key of outputKeys) {\n const value = metadata[key];\n if (value) {\n parts.push(`${key}: ${value}`);\n }\n }\n \n return parts.length > 0 ? `/*!\\n * ${parts.join('\\n * ')}\\n */` : '';\n};\n\n/**\n * Insert banner header at appropriate position considering shebang\n * @param content - The content to insert banner into\n * @param banner - The banner header to insert\n * @returns Content with banner header inserted\n */\nconst insertBannerHeader = (content: string, banner: string): string => {\n const lines = content.split('\\n');\n \n // Check if first line is shebang\n if (lines.length > 0 && lines[0].startsWith('#!')) {\n // Insert banner after shebang line\n return lines[0] + '\\n' + banner + '\\n' + lines.slice(1).join('\\n');\n } else {\n // Insert banner at the beginning\n return banner + '\\n' + content;\n }\n};\n\n/**\n * Convert string key to valid TypeScript identifier\n * @param key - The key to convert\n * @returns Valid TypeScript identifier\n */\nconst sanitizeKey = (key: string): string => {\n // Replace dots and other invalid characters with underscores\n return key.replace(/[^a-zA-Z0-9_]/g, '_').replace(/^(\\d)/, '_$1');\n};\n\n/**\n * Generate TypeScript metadata file content from package metadata\n * @param metadata - Package metadata\n * @param outputKeys - Array of keys to output\n * @returns TypeScript file content\n */\nconst generateMetadataFile = (metadata: PackageMetadata, outputKeys: string[]): string => {\n const lines: string[] = [];\n \n lines.push('// This file is auto-generated by screw-up plugin');\n lines.push('// Do not edit manually');\n lines.push('');\n \n for (const key of outputKeys) {\n const value = metadata[key];\n if (value) {\n const sanitizedKey = sanitizeKey(key);\n const escapedValue = JSON.stringify(value);\n lines.push(`export const ${sanitizedKey} = ${escapedValue};`);\n }\n }\n \n lines.push('');\n \n return lines.join('\\n');\n};\n\n/////////////////////////////////////////////////////////////////////////////////////////\n\n/**\n * Vite plugin that adds banner to the bundled code\n * @param options - Plugin options\n * @returns Vite plugin\n */\nexport const screwUp = (options: ScrewUpOptions = {}): Plugin => {\n const logger = createConsoleLogger();\n const {\n outputKeys = ['name', 'version', 'description', 'author', 'license', 'repository.url', 'git.commit.hash'],\n assetFilters = ['\\\\.d\\\\.ts$'],\n outputMetadataFile = false,\n outputMetadataFilePath = 'src/generated/packageMetadata.ts',\n outputMetadataKeys = ['name', 'version', 'description', 'author', 'license', 'repository.url', 'git.commit.hash'],\n checkWorkingDirectoryStatus = true,\n alwaysOverrideVersionFromGit = true,\n insertMetadataBanner = true} = options;\n\n const assetFiltersRegex = assetFilters.map(filter => new RegExp(filter));\n let banner: string;\n let metadata: any;\n let projectRoot: string;\n\n return {\n name: 'screw-up',\n apply: 'build',\n // Configuration resolved phase\n configResolved: async config => {\n // Save project root\n projectRoot = config.root;\n // Resolve package metadata\n const result = await resolvePackageMetadata(\n projectRoot, checkWorkingDirectoryStatus, alwaysOverrideVersionFromGit, logger);\n metadata = result.metadata;\n // Generate banner\n banner = generateBanner(metadata, outputKeys);\n },\n // Build start phase\n buildStart: async () => {\n // Generate metadata TypeScript file\n if (outputMetadataFile) {\n const metadataSourceContent = generateMetadataFile(metadata, outputMetadataKeys);\n const metadataSourcePath = join(projectRoot, outputMetadataFilePath);\n \n try {\n // Ensure directory exists\n await mkdir(dirname(metadataSourcePath), { recursive: true });\n // Write metadata source file\n await writeFile(metadataSourcePath, metadataSourceContent);\n } catch (error) {\n logger.warn(`[screw-up]: Failed to write metadata source file: ${metadataSourcePath}: ${error}`);\n }\n }\n },\n // Generate bundle phase\n generateBundle: (_options, bundle) => {\n // Add banner to each output file if enabled\n if (insertMetadataBanner) {\n for (const fileName in bundle) {\n const chunk = bundle[fileName];\n if (chunk.type === 'chunk') {\n chunk.code = insertBannerHeader(chunk.code, banner);\n } else if (chunk.type === 'asset' && assetFiltersRegex.some(filter => filter.test(fileName))) {\n if (typeof chunk.source === 'string') {\n chunk.source = insertBannerHeader(chunk.source, banner + '\\n'); // insert more blank line\n }\n }\n }\n }\n },\n // Write bundle phase\n writeBundle: async options => {\n // Handle files written by other plugins (like vite-plugin-dts) if banner insertion is enabled\n if (!insertMetadataBanner || !options.dir) return;\n\n try {\n // Read all files in the output directory\n const files = await readdir(options.dir, { recursive: true });\n\n // Iterate over all files\n for (const file of files) {\n const filePath = join(options.dir, file);\n\n // Check if the file is target asset file\n if (assetFiltersRegex.some(filter => filter.test(file))) {\n try {\n // Read the asset file\n const content = await readFile(filePath, 'utf-8');\n // Append banner to the asset file if it doesn't already contain it\n if (!content.includes(banner)) {\n await writeFile(filePath, insertBannerHeader(content, banner + '\\n'));\n }\n } catch (error) {\n // Skip files that can't be read/written\n }\n }\n }\n } catch (error) {\n // Skip files that can't be read/written\n }\n }\n };\n};\n"],"names":["createConsoleLogger","resolvePackageMetadata","join","mkdir","dirname","writeFile","options","readdir","readFile"],"mappings":";;;;AAiBO,MAAM,iBAAiB,CAAC,UAA2B,eAAiC;AACzF,QAAM,QAAkB,CAAA;AAExB,aAAW,OAAO,YAAY;AAC5B,UAAM,QAAQ,SAAS,GAAG;AAC1B,QAAI,OAAO;AACT,YAAM,KAAK,GAAG,GAAG,KAAK,KAAK,EAAE;AAAA,IAC/B;AAAA,EACF;AAEA,SAAO,MAAM,SAAS,IAAI;AAAA,KAAW,MAAM,KAAK,OAAO,CAAC;AA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AAI,CAAC,wBAAwB,CAACA,SAAQ,IAAK;AAE3C,UAAI;AAEF,cAAM,QAAQ,MAAMC,iBAAQD,SAAQ,KAAK,EAAE,WAAW,MAAM;AAG5D,mBAAW,QAAQ,OAAO;AACxB,gBAAM,WAAWJ,KAAAA,KAAKI,SAAQ,KAAK,IAAI;AAGvC,cAAI,kBAAkB,KAAK,CAAA,WAAU,OAAO,KAAK,IAAI,CAAC,GAAG;AACvD,gBAAI;AAEF,oBAAM,UAAU,MAAME,kBAAS,UAAU,OAAO;AAEhD,kBAAI,CAAC,QAAQ,SAAS,MAAM,GAAG;AAC7B,sBAAMH,SAAAA,UAAU,UAAU,mBAAmB,SAAS,SAAS,IAAI,CAAC;AAAA,cACtE;AAAA,YACF,SAAS,OAAO;AAAA,YAEhB;AAAA,UACF;AAAA,QACF;AAAA,MACF,SAAS,OAAO;AAAA,MAEhB;AAAA,IACF;AAAA,EAAA;AAEJ;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
/*!
|
|
2
|
+
* name: screw-up
|
|
3
|
+
* version: 0.13.0
|
|
4
|
+
* description: Simply package metadata inserter on Vite plugin
|
|
5
|
+
* author: Kouji Matsui (@kekyo@mi.kekyo.net)
|
|
6
|
+
* license: MIT
|
|
7
|
+
* repository.url: https://github.com/kekyo/screw-up.git
|
|
8
|
+
* git.commit.hash: 9d03898db6924cec97e10bf6a1208b2c52ee87c0
|
|
9
|
+
*/
|
|
2
10
|
|
|
11
|
+
import { screwUp } from './vite-plugin.js';
|
|
3
12
|
export * from './types.js';
|
|
4
13
|
export default screwUp;
|
|
5
14
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* name: screw-up
|
|
3
|
+
* version: 0.13.0
|
|
4
|
+
* description: Simply package metadata inserter on Vite plugin
|
|
5
|
+
* author: Kouji Matsui (@kekyo@mi.kekyo.net)
|
|
6
|
+
* license: MIT
|
|
7
|
+
* repository.url: https://github.com/kekyo/screw-up.git
|
|
8
|
+
* git.commit.hash: 9d03898db6924cec97e10bf6a1208b2c52ee87c0
|
|
9
|
+
*/
|
|
1
10
|
import { readdir, readFile, writeFile, mkdir } from "fs/promises";
|
|
2
11
|
import { join, dirname } from "path";
|
|
3
|
-
import { r as resolvePackageMetadata } from "./internal-
|
|
12
|
+
import { c as createConsoleLogger, r as resolvePackageMetadata } from "./internal-BgCvktPU.js";
|
|
4
13
|
const generateBanner = (metadata, outputKeys) => {
|
|
5
14
|
const parts = [];
|
|
6
15
|
for (const key of outputKeys) {
|
|
@@ -41,6 +50,7 @@ const generateMetadataFile = (metadata, outputKeys) => {
|
|
|
41
50
|
return lines.join("\n");
|
|
42
51
|
};
|
|
43
52
|
const screwUp = (options = {}) => {
|
|
53
|
+
const logger = createConsoleLogger();
|
|
44
54
|
const {
|
|
45
55
|
outputKeys = ["name", "version", "description", "author", "license", "repository.url", "git.commit.hash"],
|
|
46
56
|
assetFilters = ["\\.d\\.ts$"],
|
|
@@ -48,6 +58,7 @@ const screwUp = (options = {}) => {
|
|
|
48
58
|
outputMetadataFilePath = "src/generated/packageMetadata.ts",
|
|
49
59
|
outputMetadataKeys = ["name", "version", "description", "author", "license", "repository.url", "git.commit.hash"],
|
|
50
60
|
checkWorkingDirectoryStatus = true,
|
|
61
|
+
alwaysOverrideVersionFromGit = true,
|
|
51
62
|
insertMetadataBanner = true
|
|
52
63
|
} = options;
|
|
53
64
|
const assetFiltersRegex = assetFilters.map((filter) => new RegExp(filter));
|
|
@@ -60,7 +71,13 @@ const screwUp = (options = {}) => {
|
|
|
60
71
|
// Configuration resolved phase
|
|
61
72
|
configResolved: async (config) => {
|
|
62
73
|
projectRoot = config.root;
|
|
63
|
-
|
|
74
|
+
const result = await resolvePackageMetadata(
|
|
75
|
+
projectRoot,
|
|
76
|
+
checkWorkingDirectoryStatus,
|
|
77
|
+
alwaysOverrideVersionFromGit,
|
|
78
|
+
logger
|
|
79
|
+
);
|
|
80
|
+
metadata = result.metadata;
|
|
64
81
|
banner = generateBanner(metadata, outputKeys);
|
|
65
82
|
},
|
|
66
83
|
// Build start phase
|
|
@@ -72,7 +89,7 @@ const screwUp = (options = {}) => {
|
|
|
72
89
|
await mkdir(dirname(metadataSourcePath), { recursive: true });
|
|
73
90
|
await writeFile(metadataSourcePath, metadataSourceContent);
|
|
74
91
|
} catch (error) {
|
|
75
|
-
|
|
92
|
+
logger.warn(`[screw-up]: Failed to write metadata source file: ${metadataSourcePath}: ${error}`);
|
|
76
93
|
}
|
|
77
94
|
}
|
|
78
95
|
},
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/vite-plugin.ts"],"sourcesContent":["// screw-up - Easy package metadata inserter on Vite plugin\n// Copyright (c) Kouji Matsui (@kekyo@mi.kekyo.net)\n// Under MIT.\n// https://github.com/kekyo/screw-up/\n\nimport type { Plugin } from 'vite';\nimport { readFile, writeFile, readdir, mkdir } from 'fs/promises';\nimport { join, dirname } from 'path';\nimport { resolvePackageMetadata } from './internal.js';\nimport { ScrewUpOptions, PackageMetadata } from './types.js';\n\n/**\n * Generate banner string from package.json metadata\n * @param metadata - Package metadata\n * @param outputKeys - Array of keys to output in specified order\n * @returns Banner string\n */\nexport const generateBanner = (metadata: PackageMetadata, outputKeys: string[]): string => {\n const parts: string[] = [];\n \n for (const key of outputKeys) {\n const value = metadata[key];\n if (value) {\n parts.push(`${key}: ${value}`);\n }\n }\n \n return parts.length > 0 ? `/*!\\n * ${parts.join('\\n * ')}\\n */` : '';\n};\n\n/**\n * Insert banner header at appropriate position considering shebang\n * @param content - The content to insert banner into\n * @param banner - The banner header to insert\n * @returns Content with banner header inserted\n */\nconst insertBannerHeader = (content: string, banner: string): string => {\n const lines = content.split('\\n');\n \n // Check if first line is shebang\n if (lines.length > 0 && lines[0].startsWith('#!')) {\n // Insert banner after shebang line\n return lines[0] + '\\n' + banner + '\\n' + lines.slice(1).join('\\n');\n } else {\n // Insert banner at the beginning\n return banner + '\\n' + content;\n }\n};\n\n/**\n * Convert string key to valid TypeScript identifier\n * @param key - The key to convert\n * @returns Valid TypeScript identifier\n */\nconst sanitizeKey = (key: string): string => {\n // Replace dots and other invalid characters with underscores\n return key.replace(/[^a-zA-Z0-9_]/g, '_').replace(/^(\\d)/, '_$1');\n};\n\n/**\n * Generate TypeScript metadata file content from package metadata\n * @param metadata - Package metadata\n * @param outputKeys - Array of keys to output\n * @returns TypeScript file content\n */\nconst generateMetadataFile = (metadata: PackageMetadata, outputKeys: string[]): string => {\n const lines: string[] = [];\n \n lines.push('// This file is auto-generated by screw-up plugin');\n lines.push('// Do not edit manually');\n lines.push('');\n \n for (const key of outputKeys) {\n const value = metadata[key];\n if (value) {\n const sanitizedKey = sanitizeKey(key);\n const escapedValue = JSON.stringify(value);\n lines.push(`export const ${sanitizedKey} = ${escapedValue};`);\n }\n }\n \n lines.push('');\n \n return lines.join('\\n');\n};\n\n/////////////////////////////////////////////////////////////////////////////////////////\n\n/**\n * Vite plugin that adds banner to the bundled code\n * @param options - Plugin options\n * @returns Vite plugin\n */\nexport const screwUp = (options: ScrewUpOptions = {}): Plugin => {\n const {\n outputKeys = ['name', 'version', 'description', 'author', 'license', 'repository.url', 'git.commit.hash'],\n assetFilters = ['\\\\.d\\\\.ts$'],\n outputMetadataFile = false,\n outputMetadataFilePath = 'src/generated/packageMetadata.ts',\n outputMetadataKeys = ['name', 'version', 'description', 'author', 'license', 'repository.url', 'git.commit.hash'],\n checkWorkingDirectoryStatus = true,\n insertMetadataBanner = true} = options;\n\n const assetFiltersRegex = assetFilters.map(filter => new RegExp(filter));\n let banner: string;\n let metadata: any;\n let projectRoot: string;\n\n return {\n name: 'screw-up',\n apply: 'build',\n // Configuration resolved phase\n configResolved: async config => {\n // Save project root\n projectRoot = config.root;\n // Resolve package metadata\n
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/vite-plugin.ts"],"sourcesContent":["// screw-up - Easy package metadata inserter on Vite plugin\n// Copyright (c) Kouji Matsui (@kekyo@mi.kekyo.net)\n// Under MIT.\n// https://github.com/kekyo/screw-up/\n\nimport type { Plugin } from 'vite';\nimport { readFile, writeFile, readdir, mkdir } from 'fs/promises';\nimport { join, dirname } from 'path';\nimport { resolvePackageMetadata, createConsoleLogger } from './internal.js';\nimport { ScrewUpOptions, PackageMetadata } from './types.js';\n\n/**\n * Generate banner string from package.json metadata\n * @param metadata - Package metadata\n * @param outputKeys - Array of keys to output in specified order\n * @returns Banner string\n */\nexport const generateBanner = (metadata: PackageMetadata, outputKeys: string[]): string => {\n const parts: string[] = [];\n \n for (const key of outputKeys) {\n const value = metadata[key];\n if (value) {\n parts.push(`${key}: ${value}`);\n }\n }\n \n return parts.length > 0 ? `/*!\\n * ${parts.join('\\n * ')}\\n */` : '';\n};\n\n/**\n * Insert banner header at appropriate position considering shebang\n * @param content - The content to insert banner into\n * @param banner - The banner header to insert\n * @returns Content with banner header inserted\n */\nconst insertBannerHeader = (content: string, banner: string): string => {\n const lines = content.split('\\n');\n \n // Check if first line is shebang\n if (lines.length > 0 && lines[0].startsWith('#!')) {\n // Insert banner after shebang line\n return lines[0] + '\\n' + banner + '\\n' + lines.slice(1).join('\\n');\n } else {\n // Insert banner at the beginning\n return banner + '\\n' + content;\n }\n};\n\n/**\n * Convert string key to valid TypeScript identifier\n * @param key - The key to convert\n * @returns Valid TypeScript identifier\n */\nconst sanitizeKey = (key: string): string => {\n // Replace dots and other invalid characters with underscores\n return key.replace(/[^a-zA-Z0-9_]/g, '_').replace(/^(\\d)/, '_$1');\n};\n\n/**\n * Generate TypeScript metadata file content from package metadata\n * @param metadata - Package metadata\n * @param outputKeys - Array of keys to output\n * @returns TypeScript file content\n */\nconst generateMetadataFile = (metadata: PackageMetadata, outputKeys: string[]): string => {\n const lines: string[] = [];\n \n lines.push('// This file is auto-generated by screw-up plugin');\n lines.push('// Do not edit manually');\n lines.push('');\n \n for (const key of outputKeys) {\n const value = metadata[key];\n if (value) {\n const sanitizedKey = sanitizeKey(key);\n const escapedValue = JSON.stringify(value);\n lines.push(`export const ${sanitizedKey} = ${escapedValue};`);\n }\n }\n \n lines.push('');\n \n return lines.join('\\n');\n};\n\n/////////////////////////////////////////////////////////////////////////////////////////\n\n/**\n * Vite plugin that adds banner to the bundled code\n * @param options - Plugin options\n * @returns Vite plugin\n */\nexport const screwUp = (options: ScrewUpOptions = {}): Plugin => {\n const logger = createConsoleLogger();\n const {\n outputKeys = ['name', 'version', 'description', 'author', 'license', 'repository.url', 'git.commit.hash'],\n assetFilters = ['\\\\.d\\\\.ts$'],\n outputMetadataFile = false,\n outputMetadataFilePath = 'src/generated/packageMetadata.ts',\n outputMetadataKeys = ['name', 'version', 'description', 'author', 'license', 'repository.url', 'git.commit.hash'],\n checkWorkingDirectoryStatus = true,\n alwaysOverrideVersionFromGit = true,\n insertMetadataBanner = true} = options;\n\n const assetFiltersRegex = assetFilters.map(filter => new RegExp(filter));\n let banner: string;\n let metadata: any;\n let projectRoot: string;\n\n return {\n name: 'screw-up',\n apply: 'build',\n // Configuration resolved phase\n configResolved: async config => {\n // Save project root\n projectRoot = config.root;\n // Resolve package metadata\n const result = await resolvePackageMetadata(\n projectRoot, checkWorkingDirectoryStatus, alwaysOverrideVersionFromGit, logger);\n metadata = result.metadata;\n // Generate banner\n banner = generateBanner(metadata, outputKeys);\n },\n // Build start phase\n buildStart: async () => {\n // Generate metadata TypeScript file\n if (outputMetadataFile) {\n const metadataSourceContent = generateMetadataFile(metadata, outputMetadataKeys);\n const metadataSourcePath = join(projectRoot, outputMetadataFilePath);\n \n try {\n // Ensure directory exists\n await mkdir(dirname(metadataSourcePath), { recursive: true });\n // Write metadata source file\n await writeFile(metadataSourcePath, metadataSourceContent);\n } catch (error) {\n logger.warn(`[screw-up]: Failed to write metadata source file: ${metadataSourcePath}: ${error}`);\n }\n }\n },\n // Generate bundle phase\n generateBundle: (_options, bundle) => {\n // Add banner to each output file if enabled\n if (insertMetadataBanner) {\n for (const fileName in bundle) {\n const chunk = bundle[fileName];\n if (chunk.type === 'chunk') {\n chunk.code = insertBannerHeader(chunk.code, banner);\n } else if (chunk.type === 'asset' && assetFiltersRegex.some(filter => filter.test(fileName))) {\n if (typeof chunk.source === 'string') {\n chunk.source = insertBannerHeader(chunk.source, banner + '\\n'); // insert more blank line\n }\n }\n }\n }\n },\n // Write bundle phase\n writeBundle: async options => {\n // Handle files written by other plugins (like vite-plugin-dts) if banner insertion is enabled\n if (!insertMetadataBanner || !options.dir) return;\n\n try {\n // Read all files in the output directory\n const files = await readdir(options.dir, { recursive: true });\n\n // Iterate over all files\n for (const file of files) {\n const filePath = join(options.dir, file);\n\n // Check if the file is target asset file\n if (assetFiltersRegex.some(filter => filter.test(file))) {\n try {\n // Read the asset file\n const content = await readFile(filePath, 'utf-8');\n // Append banner to the asset file if it doesn't already contain it\n if (!content.includes(banner)) {\n await writeFile(filePath, insertBannerHeader(content, banner + '\\n'));\n }\n } catch (error) {\n // Skip files that can't be read/written\n }\n }\n }\n } catch (error) {\n // Skip files that can't be read/written\n }\n }\n };\n};\n"],"names":["options"],"mappings":";;;AAiBO,MAAM,iBAAiB,CAAC,UAA2B,eAAiC;AACzF,QAAM,QAAkB,CAAA;AAExB,aAAW,OAAO,YAAY;AAC5B,UAAM,QAAQ,SAAS,GAAG;AAC1B,QAAI,OAAO;AACT,YAAM,KAAK,GAAG,GAAG,KAAK,KAAK,EAAE;AAAA,IAC/B;AAAA,EACF;AAEA,SAAO,MAAM,SAAS,IAAI;AAAA,KAAW,MAAM,KAAK,OAAO,CAAC;AAAA,OAAU;AACpE;AAQA,MAAM,qBAAqB,CAAC,SAAiB,WAA2B;AACtE,QAAM,QAAQ,QAAQ,MAAM,IAAI;AAGhC,MAAI,MAAM,SAAS,KAAK,MAAM,CAAC,EAAE,WAAW,IAAI,GAAG;AAEjD,WAAO,MAAM,CAAC,IAAI,OAAO,SAAS,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