screw-up 0.7.1 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -1
- package/dist/index.cjs +39 -2
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +40 -3
- package/dist/internal.d.ts +7 -0
- package/dist/internal.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,6 +48,7 @@ To insert banner header each bundled source files (`dist/index.js` and etc.):
|
|
|
48
48
|
* Flexible output: Specify exactly which keys to include and in what order.
|
|
49
49
|
* Nested object support: Handles nested objects like `author.name`, `repository.url`.
|
|
50
50
|
* Customizable: Choose which metadata fields to include in your banner.
|
|
51
|
+
* TypeScript metadata generation: Automatically generates TypeScript files with metadata constants for use in your source code.
|
|
51
52
|
|
|
52
53
|
## Installation
|
|
53
54
|
|
|
@@ -155,6 +156,66 @@ Results in:
|
|
|
155
156
|
*/
|
|
156
157
|
```
|
|
157
158
|
|
|
159
|
+
### TypeScript Metadata Generation
|
|
160
|
+
|
|
161
|
+
The plugin can generate TypeScript files containing metadata constants that you can import and use in your source code:
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
import { defineConfig } from 'vite';
|
|
165
|
+
import screwUp from 'screw-up';
|
|
166
|
+
|
|
167
|
+
export default defineConfig({
|
|
168
|
+
plugins: [
|
|
169
|
+
screwUp({
|
|
170
|
+
outputMetadataFile: true, // Enable metadata file generation
|
|
171
|
+
outputMetadataFilePath: 'src/generated/packageMetadata.ts', // Custom path (optional)
|
|
172
|
+
outputMetadataKeys: ['name', 'version', 'description', 'author', 'license'] // Keys to include
|
|
173
|
+
})
|
|
174
|
+
],
|
|
175
|
+
build: {
|
|
176
|
+
lib: {
|
|
177
|
+
entry: 'src/index.ts',
|
|
178
|
+
name: 'MyLibrary',
|
|
179
|
+
fileName: 'index'
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
This generates `src/generated/packageMetadata.ts` with sanitized TypeScript constants:
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
// This file is auto-generated by screw-up plugin
|
|
189
|
+
// Do not edit manually
|
|
190
|
+
|
|
191
|
+
export const name = "my-awesome-library";
|
|
192
|
+
export const version = "2.1.0";
|
|
193
|
+
export const description = "An awesome TypeScript library";
|
|
194
|
+
export const author = "Jane Developer <jane@example.com>";
|
|
195
|
+
export const license = "Apache-2.0";
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
You can then import and use these constants in your source code:
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
import { name, version } from './generated/packageMetadata.js';
|
|
202
|
+
|
|
203
|
+
console.log(`${name} v${version}`);
|
|
204
|
+
// Output: my-awesome-library v2.1.0
|
|
205
|
+
|
|
206
|
+
export function getLibraryInfo() {
|
|
207
|
+
return { name, version };
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### Key Sanitization
|
|
212
|
+
|
|
213
|
+
Keys with special characters are automatically sanitized to valid TypeScript identifiers:
|
|
214
|
+
|
|
215
|
+
- `repository.url` → `repository_url`
|
|
216
|
+
- `custom-key` → `custom_key`
|
|
217
|
+
- `123invalid` → `_123invalid`
|
|
218
|
+
|
|
158
219
|
----
|
|
159
220
|
|
|
160
221
|
## Advanced Usage
|
|
@@ -236,4 +297,4 @@ The plugin automatically detects and supports:
|
|
|
236
297
|
|
|
237
298
|
## License
|
|
238
299
|
|
|
239
|
-
Under MIT
|
|
300
|
+
Under MIT
|
package/dist/index.cjs
CHANGED
|
@@ -100,20 +100,57 @@ const insertBannerHeader = (content, banner) => {
|
|
|
100
100
|
return banner + "\n" + content;
|
|
101
101
|
}
|
|
102
102
|
};
|
|
103
|
+
const sanitizeKey = (key) => {
|
|
104
|
+
return key.replace(/[^a-zA-Z0-9_]/g, "_").replace(/^(\d)/, "_$1");
|
|
105
|
+
};
|
|
106
|
+
const generateMetadataFile = (metadata, outputKeys) => {
|
|
107
|
+
const lines = [];
|
|
108
|
+
lines.push("// This file is auto-generated by screw-up plugin");
|
|
109
|
+
lines.push("// Do not edit manually");
|
|
110
|
+
lines.push("");
|
|
111
|
+
for (const key of outputKeys) {
|
|
112
|
+
const value = metadata[key];
|
|
113
|
+
if (value) {
|
|
114
|
+
const sanitizedKey = sanitizeKey(key);
|
|
115
|
+
const escapedValue = JSON.stringify(value);
|
|
116
|
+
lines.push(`export const ${sanitizedKey} = ${escapedValue};`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
lines.push("");
|
|
120
|
+
return lines.join("\n");
|
|
121
|
+
};
|
|
103
122
|
const screwUp = (options = {}) => {
|
|
104
123
|
const {
|
|
105
124
|
outputKeys = ["name", "version", "description", "author", "license", "repository.url"],
|
|
106
|
-
assetFilters = ["\\.d\\.ts$"]
|
|
125
|
+
assetFilters = ["\\.d\\.ts$"],
|
|
126
|
+
outputMetadataFile = false,
|
|
127
|
+
outputMetadataFilePath = "src/generated/packageMetadata.ts",
|
|
128
|
+
outputMetadataKeys = ["name", "version", "description", "author", "license", "repository.url"]
|
|
107
129
|
} = options;
|
|
108
130
|
const assetFiltersRegex = assetFilters.map((filter) => new RegExp(filter));
|
|
109
131
|
let banner;
|
|
132
|
+
let metadata;
|
|
133
|
+
let projectRoot;
|
|
110
134
|
return {
|
|
111
135
|
name: "screw-up",
|
|
112
136
|
apply: "build",
|
|
113
137
|
async configResolved(config) {
|
|
114
|
-
|
|
138
|
+
projectRoot = config.root;
|
|
139
|
+
metadata = await resolvePackageMetadata(config.root);
|
|
115
140
|
banner = generateBanner(metadata, outputKeys);
|
|
116
141
|
},
|
|
142
|
+
async buildStart() {
|
|
143
|
+
if (outputMetadataFile) {
|
|
144
|
+
const metadataContent = generateMetadataFile(metadata, outputMetadataKeys);
|
|
145
|
+
const metadataPath = path.join(projectRoot, outputMetadataFilePath);
|
|
146
|
+
try {
|
|
147
|
+
await promises.mkdir(path.dirname(metadataPath), { recursive: true });
|
|
148
|
+
await promises.writeFile(metadataPath, metadataContent);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
console.warn(`Failed to write metadata file to ${metadataPath}:`, error);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
},
|
|
117
154
|
generateBundle(_options, bundle) {
|
|
118
155
|
for (const fileName in bundle) {
|
|
119
156
|
const chunk = bundle[fileName];
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,21 @@ export interface ScrewUpOptions {
|
|
|
14
14
|
* @default ['\.d\.ts$']
|
|
15
15
|
*/
|
|
16
16
|
assetFilters?: string[];
|
|
17
|
+
/**
|
|
18
|
+
* Enable TypeScript metadata file generation
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
outputMetadataFile?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Output path for TypeScript metadata file
|
|
24
|
+
* @default 'src/generated/packageMetadata.ts'
|
|
25
|
+
*/
|
|
26
|
+
outputMetadataFilePath?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Array of keys to output in metadata file in the specified order
|
|
29
|
+
* @default ['name', 'version', 'description', 'author', 'license', 'repository.url']
|
|
30
|
+
*/
|
|
31
|
+
outputMetadataKeys?: string[];
|
|
17
32
|
}
|
|
18
33
|
/**
|
|
19
34
|
* Vite plugin that adds banner to the bundled code
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAKnC;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAKnC;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;OAGG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,EAAE,CAAC;CAC/B;AAED;;;;GAIG;AACH,QAAA,MAAM,OAAO,GAAI,UAAS,cAAmB,KAAG,MAiF/C,CAAC;AAEF,eAAe,OAAO,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readFile, readdir, writeFile } from "fs/promises";
|
|
1
|
+
import { readFile, readdir, writeFile, mkdir } from "fs/promises";
|
|
2
2
|
import { join, dirname } from "path";
|
|
3
3
|
import { existsSync } from "fs";
|
|
4
4
|
const flattenObject = (obj, prefix, map) => {
|
|
@@ -99,20 +99,57 @@ const insertBannerHeader = (content, banner) => {
|
|
|
99
99
|
return banner + "\n" + content;
|
|
100
100
|
}
|
|
101
101
|
};
|
|
102
|
+
const sanitizeKey = (key) => {
|
|
103
|
+
return key.replace(/[^a-zA-Z0-9_]/g, "_").replace(/^(\d)/, "_$1");
|
|
104
|
+
};
|
|
105
|
+
const generateMetadataFile = (metadata, outputKeys) => {
|
|
106
|
+
const lines = [];
|
|
107
|
+
lines.push("// This file is auto-generated by screw-up plugin");
|
|
108
|
+
lines.push("// Do not edit manually");
|
|
109
|
+
lines.push("");
|
|
110
|
+
for (const key of outputKeys) {
|
|
111
|
+
const value = metadata[key];
|
|
112
|
+
if (value) {
|
|
113
|
+
const sanitizedKey = sanitizeKey(key);
|
|
114
|
+
const escapedValue = JSON.stringify(value);
|
|
115
|
+
lines.push(`export const ${sanitizedKey} = ${escapedValue};`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
lines.push("");
|
|
119
|
+
return lines.join("\n");
|
|
120
|
+
};
|
|
102
121
|
const screwUp = (options = {}) => {
|
|
103
122
|
const {
|
|
104
123
|
outputKeys = ["name", "version", "description", "author", "license", "repository.url"],
|
|
105
|
-
assetFilters = ["\\.d\\.ts$"]
|
|
124
|
+
assetFilters = ["\\.d\\.ts$"],
|
|
125
|
+
outputMetadataFile = false,
|
|
126
|
+
outputMetadataFilePath = "src/generated/packageMetadata.ts",
|
|
127
|
+
outputMetadataKeys = ["name", "version", "description", "author", "license", "repository.url"]
|
|
106
128
|
} = options;
|
|
107
129
|
const assetFiltersRegex = assetFilters.map((filter) => new RegExp(filter));
|
|
108
130
|
let banner;
|
|
131
|
+
let metadata;
|
|
132
|
+
let projectRoot;
|
|
109
133
|
return {
|
|
110
134
|
name: "screw-up",
|
|
111
135
|
apply: "build",
|
|
112
136
|
async configResolved(config) {
|
|
113
|
-
|
|
137
|
+
projectRoot = config.root;
|
|
138
|
+
metadata = await resolvePackageMetadata(config.root);
|
|
114
139
|
banner = generateBanner(metadata, outputKeys);
|
|
115
140
|
},
|
|
141
|
+
async buildStart() {
|
|
142
|
+
if (outputMetadataFile) {
|
|
143
|
+
const metadataContent = generateMetadataFile(metadata, outputMetadataKeys);
|
|
144
|
+
const metadataPath = join(projectRoot, outputMetadataFilePath);
|
|
145
|
+
try {
|
|
146
|
+
await mkdir(dirname(metadataPath), { recursive: true });
|
|
147
|
+
await writeFile(metadataPath, metadataContent);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
console.warn(`Failed to write metadata file to ${metadataPath}:`, error);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
},
|
|
116
153
|
generateBundle(_options, bundle) {
|
|
117
154
|
for (const fileName in bundle) {
|
|
118
155
|
const chunk = bundle[fileName];
|
package/dist/internal.d.ts
CHANGED
|
@@ -38,4 +38,11 @@ export declare const generateBanner: (metadata: PackageMetadata, outputKeys: str
|
|
|
38
38
|
* @returns Content with banner header inserted
|
|
39
39
|
*/
|
|
40
40
|
export declare const insertBannerHeader: (content: string, banner: string) => string;
|
|
41
|
+
/**
|
|
42
|
+
* Generate TypeScript metadata file content from package metadata
|
|
43
|
+
* @param metadata - Package metadata
|
|
44
|
+
* @param outputKeys - Array of keys to output
|
|
45
|
+
* @returns TypeScript file content
|
|
46
|
+
*/
|
|
47
|
+
export declare const generateMetadataFile: (metadata: PackageMetadata, outputKeys: string[]) => string;
|
|
41
48
|
//# sourceMappingURL=internal.d.ts.map
|
package/dist/internal.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AASA,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AA2BrD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,GAAU,aAAa,MAAM,KAAG,OAAO,CAAC,eAAe,CAWtF,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CA0BrF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,GAC/B,gBAAgB,eAAe,EAC/B,eAAe,eAAe,KAC7B,eAoBF,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,GAAU,aAAa,MAAM,KAAG,OAAO,CAAC,eAAe,CAsBzF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GAAI,UAAU,eAAe,EAAE,YAAY,MAAM,EAAE,KAAG,MAWhF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,SAAS,MAAM,EAAE,QAAQ,MAAM,KAAG,MAWpE,CAAC"}
|
|
1
|
+
{"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AASA,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AA2BrD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,GAAU,aAAa,MAAM,KAAG,OAAO,CAAC,eAAe,CAWtF,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAU,WAAW,MAAM,KAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CA0BrF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,GAC/B,gBAAgB,eAAe,EAC/B,eAAe,eAAe,KAC7B,eAoBF,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,GAAU,aAAa,MAAM,KAAG,OAAO,CAAC,eAAe,CAsBzF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GAAI,UAAU,eAAe,EAAE,YAAY,MAAM,EAAE,KAAG,MAWhF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,SAAS,MAAM,EAAE,QAAQ,MAAM,KAAG,MAWpE,CAAC;AAYF;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,GAAI,UAAU,eAAe,EAAE,YAAY,MAAM,EAAE,KAAG,MAmBtF,CAAC"}
|