screw-up 0.12.0 → 0.14.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 +21 -7
- 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 +36 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +10 -1
- package/dist/index.js +36 -13
- 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-D9nXAoK9.cjs +20 -0
- package/dist/packageMetadata-D9nXAoK9.cjs.map +1 -0
- package/dist/packageMetadata-Dsxn2dKN.js +20 -0
- package/dist/packageMetadata-Dsxn2dKN.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,13 +46,22 @@ 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.
|
|
47
63
|
* Workspace support: Works with monorepos and automatically inherits metadata from parent packages.
|
|
48
|
-
* Flexible output: Specify exactly which
|
|
49
|
-
* Nested object support: Handles nested objects like `author.name`, `repository.url`.
|
|
50
|
-
* Customizable: Choose which metadata fields to include in your banner.
|
|
64
|
+
* Flexible output: Specify exactly which metadata to include and in what order.
|
|
51
65
|
* TypeScript metadata generation: Can automatically generates TypeScript files with metadata constants for use in your source code.
|
|
52
66
|
* Git metadata extraction: Automatically extracts Git commit hash, tags, branches, and version information from local Git repository.
|
|
53
67
|
* Supported pack/publish CLI interface: When publishing using this feature, the package is generated after applying the above processing to `package.json`.
|
package/dist/analyzer.d.ts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* name: screw-up
|
|
3
|
+
* version: 0.14.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: f1871df4c43aee9ab389a6ab1b2769b32322793b
|
|
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.14.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: f1871df4c43aee9ab389a6ab1b2769b32322793b
|
|
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;AA6DnJ,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.14.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: f1871df4c43aee9ab389a6ab1b2769b32322793b
|
|
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.14.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: f1871df4c43aee9ab389a6ab1b2769b32322793b
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export declare const name = "screw-up";
|
|
12
|
+
export declare const version = "0.14.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 = "f1871df4c43aee9ab389a6ab1b2769b32322793b";
|
|
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.14.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: f1871df4c43aee9ab389a6ab1b2769b32322793b
|
|
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,33 +59,46 @@ 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));
|
|
55
66
|
let banner;
|
|
56
67
|
let metadata;
|
|
57
68
|
let projectRoot;
|
|
69
|
+
const generateMetadataSourceFile = async () => {
|
|
70
|
+
const result = await internal.resolvePackageMetadata(
|
|
71
|
+
projectRoot,
|
|
72
|
+
checkWorkingDirectoryStatus,
|
|
73
|
+
alwaysOverrideVersionFromGit,
|
|
74
|
+
logger
|
|
75
|
+
);
|
|
76
|
+
metadata = result.metadata;
|
|
77
|
+
banner = generateBanner(metadata, outputKeys);
|
|
78
|
+
if (outputMetadataFile) {
|
|
79
|
+
const metadataSourceContent = generateMetadataFile(metadata, outputMetadataKeys);
|
|
80
|
+
const metadataSourcePath = path.join(projectRoot, outputMetadataFilePath);
|
|
81
|
+
try {
|
|
82
|
+
await promises.mkdir(path.dirname(metadataSourcePath), { recursive: true });
|
|
83
|
+
await promises.writeFile(metadataSourcePath, metadataSourceContent);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
logger.warn(`[screw-up]: Failed to write metadata source file: ${metadataSourcePath}: ${error}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
};
|
|
58
89
|
return {
|
|
59
90
|
name: "screw-up",
|
|
60
91
|
apply: "build",
|
|
92
|
+
// Ensure screw-up runs before other plugins (especially vite-plugin-dts)
|
|
93
|
+
enforce: "pre",
|
|
61
94
|
// Configuration resolved phase
|
|
62
95
|
configResolved: async (config) => {
|
|
63
96
|
projectRoot = config.root;
|
|
64
|
-
|
|
65
|
-
banner = generateBanner(metadata, outputKeys);
|
|
97
|
+
await generateMetadataSourceFile();
|
|
66
98
|
},
|
|
67
99
|
// Build start phase
|
|
68
100
|
buildStart: async () => {
|
|
69
|
-
|
|
70
|
-
const metadataSourceContent = generateMetadataFile(metadata, outputMetadataKeys);
|
|
71
|
-
const metadataSourcePath = path.join(projectRoot, outputMetadataFilePath);
|
|
72
|
-
try {
|
|
73
|
-
await promises.mkdir(path.dirname(metadataSourcePath), { recursive: true });
|
|
74
|
-
await promises.writeFile(metadataSourcePath, metadataSourceContent);
|
|
75
|
-
} catch (error) {
|
|
76
|
-
console.warn(`[screw-up]: Failed to write metadata source file: ${metadataSourcePath}:`, error);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
101
|
+
await generateMetadataSourceFile();
|
|
79
102
|
},
|
|
80
103
|
// Generate bundle phase
|
|
81
104
|
generateBundle: (_options, bundle) => {
|
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 metadata = await resolvePackageMetadata(config.root, checkWorkingDirectoryStatus);\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 console.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":["resolvePackageMetadata","join","mkdir","dirname","writ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;AAC5F,gBAAI,OAAO,MAAM,WAAW,UAAU;AACpC,oBAAM,SAAS,mBAAmB,MAAM,QAAQ,SAAS,IAAI;AAAA,YAC/D;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA;AAAA,IAEA,aAAa,OAAMC,aAAW;AAE5B,UAAI,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;;"}
|
|
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 // Generate and write metadata TypeScript file\n const generateMetadataSourceFile = async () => {\n // Resolve package metadata\n const result = await resolvePackageMetadata(\n projectRoot, checkWorkingDirectoryStatus, alwaysOverrideVersionFromGit, logger);\n metadata = result.metadata;\n // Regenerate banner with updated metadata\n banner = generateBanner(metadata, outputKeys);\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\n return {\n name: 'screw-up',\n apply: 'build',\n // Ensure screw-up runs before other plugins (especially vite-plugin-dts)\n enforce: 'pre',\n // Configuration resolved phase\n configResolved: async config => {\n // Save project root\n projectRoot = config.root;\n // Generate metadata TypeScript file early to ensure it's available during TypeScript compilation\n await generateMetadataSourceFile();\n },\n // Build start phase\n buildStart: async () => {\n // Re-resolve package metadata to capture any changes since configResolved\n // Update metadata TypeScript file with latest data\n await generateMetadataSourceFile();\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;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,OAAO,MAAM,MAAM,CAAC,EAAE,KAAK,IAAI;AAAA,EACnE,OAAO;AAEL,WAAO,SAAS,OAAO;AAAA,EACzB;AACF;AAOA,MAAM,cAAc,CAAC,QAAwB;AAE3C,SAAO,IAAI,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,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.14.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: f1871df4c43aee9ab389a6ab1b2769b32322793b
|
|
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.14.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: f1871df4c43aee9ab389a6ab1b2769b32322793b
|
|
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 { r as resolvePackageMetadata, c as createConsoleLogger } 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,33 +58,46 @@ 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));
|
|
54
65
|
let banner;
|
|
55
66
|
let metadata;
|
|
56
67
|
let projectRoot;
|
|
68
|
+
const generateMetadataSourceFile = async () => {
|
|
69
|
+
const result = await resolvePackageMetadata(
|
|
70
|
+
projectRoot,
|
|
71
|
+
checkWorkingDirectoryStatus,
|
|
72
|
+
alwaysOverrideVersionFromGit,
|
|
73
|
+
logger
|
|
74
|
+
);
|
|
75
|
+
metadata = result.metadata;
|
|
76
|
+
banner = generateBanner(metadata, outputKeys);
|
|
77
|
+
if (outputMetadataFile) {
|
|
78
|
+
const metadataSourceContent = generateMetadataFile(metadata, outputMetadataKeys);
|
|
79
|
+
const metadataSourcePath = join(projectRoot, outputMetadataFilePath);
|
|
80
|
+
try {
|
|
81
|
+
await mkdir(dirname(metadataSourcePath), { recursive: true });
|
|
82
|
+
await writeFile(metadataSourcePath, metadataSourceContent);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
logger.warn(`[screw-up]: Failed to write metadata source file: ${metadataSourcePath}: ${error}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
57
88
|
return {
|
|
58
89
|
name: "screw-up",
|
|
59
90
|
apply: "build",
|
|
91
|
+
// Ensure screw-up runs before other plugins (especially vite-plugin-dts)
|
|
92
|
+
enforce: "pre",
|
|
60
93
|
// Configuration resolved phase
|
|
61
94
|
configResolved: async (config) => {
|
|
62
95
|
projectRoot = config.root;
|
|
63
|
-
|
|
64
|
-
banner = generateBanner(metadata, outputKeys);
|
|
96
|
+
await generateMetadataSourceFile();
|
|
65
97
|
},
|
|
66
98
|
// Build start phase
|
|
67
99
|
buildStart: async () => {
|
|
68
|
-
|
|
69
|
-
const metadataSourceContent = generateMetadataFile(metadata, outputMetadataKeys);
|
|
70
|
-
const metadataSourcePath = join(projectRoot, outputMetadataFilePath);
|
|
71
|
-
try {
|
|
72
|
-
await mkdir(dirname(metadataSourcePath), { recursive: true });
|
|
73
|
-
await writeFile(metadataSourcePath, metadataSourceContent);
|
|
74
|
-
} catch (error) {
|
|
75
|
-
console.warn(`[screw-up]: Failed to write metadata source file: ${metadataSourcePath}:`, error);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
100
|
+
await generateMetadataSourceFile();
|
|
78
101
|
},
|
|
79
102
|
// Generate bundle phase
|
|
80
103
|
generateBundle: (_options, bundle) => {
|
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 metadata = await resolvePackageMetadata(config.root, checkWorkingDirectoryStatus);\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 console.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,OAAO,MAAM,MAAM,CAAC,EAAE,KAAK,IAAI;AAAA,EACnE,OAAO;AAEL,WAAO,SAAS,OAAO;AAAA,EACzB;AACF;AAOA,MAAM,cAAc,CAAC,QAAwB;AAE3C,SAAO,IAAI,QAAQ,kBAAkB,GAAG,EAAE,QAAQ,SAAS,KAAK;AAClE;AAQA,MAAM,uBAAuB,CAAC,UAA2B,eAAiC;AACxF,QAAM,QAAkB,CAAA;AAExB,QAAM,KAAK,mDAAmD;AAC9D,QAAM,KAAK,yBAAyB;AACpC,QAAM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AAK;AAAA,QAChG;AAAA,MACF;AAAA,IACF;AAAA;AAAA,IAEA,gBAAgB,CAAC,UAAU,WAAW;AAEpC,UAAI,sBAAsB;AACxB,mBAAW,YAAY,QAAQ;AAC7B,gBAAM,QAAQ,OAAO,QAAQ;AAC7B,cAAI,MAAM,SAAS,SAAS;AAC1B,kBAAM,OAAO,mBAAmB,MAAM,MAAM,MAAM;AAAA,UACpD,WAAW,MAAM,SAAS,WAAW,kBAAkB,KAAK,CAAA,WAAU,OAAO,KAAK,QAAQ,CAAC,GAAG;AAC5F,gBAAI,OAAO,MAAM,WAAW,UAAU;AACpC,oBAAM,SAAS,mBAAmB,MAAM,QAAQ,SAAS,IAAI;AAAA,YAC/D;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA;AAAA,IAEA,aAAa,OAAMA,aAAW;AAE5B,UAAI,CAAC,wBAAwB,CAACA,SAAQ,IAAK;AAE3C,UAAI;AAEF,cAAM,QAAQ,MAAM,QAAQA,SAAQ,KAAK,EAAE,WAAW,MAAM;AAG5D,mBAAW,QAAQ,OAAO;AACxB,gBAAM,WAAW,KAAKA,SAAQ,KAAK,IAAI;AAGvC,cAAI,kBAAkB,KAAK,CAAA,WAAU,OAAO,KAAK,IAAI,CAAC,GAAG;AACvD,gBAAI;AAEF,oBAAM,UAAU,MAAM,SAAS,UAAU,OAAO;AAEhD,kBAAI,CAAC,QAAQ,SAAS,MAAM,GAAG;AAC7B,sBAAM,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;"}
|
|
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 // Generate and write metadata TypeScript file\n const generateMetadataSourceFile = async () => {\n // Resolve package metadata\n const result = await resolvePackageMetadata(\n projectRoot, checkWorkingDirectoryStatus, alwaysOverrideVersionFromGit, logger);\n metadata = result.metadata;\n // Regenerate banner with updated metadata\n banner = generateBanner(metadata, outputKeys);\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\n return {\n name: 'screw-up',\n apply: 'build',\n // Ensure screw-up runs before other plugins (especially vite-plugin-dts)\n enforce: 'pre',\n // Configuration resolved phase\n configResolved: async config => {\n // Save project root\n projectRoot = config.root;\n // Generate metadata TypeScript file early to ensure it's available during TypeScript compilation\n await generateMetadataSourceFile();\n },\n // Build start phase\n buildStart: async () => {\n // Re-resolve package metadata to capture any changes since configResolved\n // Update metadata TypeScript file with latest data\n await generateMetadataSourceFile();\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;AAGh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