screw-up 0.7.1 → 0.9.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.
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ const fs = require("fs");
3
+ const promises = require("fs/promises");
4
+ const path = require("path");
5
+ const flattenObject = (obj, prefix, map) => {
6
+ for (const [key, value] of Object.entries(obj)) {
7
+ if (!value)
8
+ continue;
9
+ const fullKey = prefix ? `${prefix}.${key}` : key;
10
+ if (typeof value === "string") {
11
+ map[fullKey] = value;
12
+ } else if (Array.isArray(value)) {
13
+ map[fullKey] = value.map((v) => String(v)).join(",");
14
+ } else if (typeof value === "object") {
15
+ flattenObject(value, fullKey, map);
16
+ } else {
17
+ map[fullKey] = String(value);
18
+ }
19
+ }
20
+ };
21
+ const readPackageMetadata = async (packagePath) => {
22
+ try {
23
+ const content = await promises.readFile(packagePath, "utf-8");
24
+ const json = JSON.parse(content);
25
+ const map = {};
26
+ flattenObject(json, "", map);
27
+ return map;
28
+ } catch (error) {
29
+ console.warn(`Failed to read package.json from ${packagePath}:`, error);
30
+ return {};
31
+ }
32
+ };
33
+ const findWorkspaceRoot = async (startPath) => {
34
+ let currentPath = startPath;
35
+ while (currentPath !== path.dirname(currentPath)) {
36
+ const packageJsonPath = path.join(currentPath, "package.json");
37
+ if (fs.existsSync(packageJsonPath)) {
38
+ try {
39
+ const content = await promises.readFile(packageJsonPath, "utf-8");
40
+ const packageJson = JSON.parse(content);
41
+ if (packageJson.workspaces || fs.existsSync(path.join(currentPath, "pnpm-workspace.yaml")) || fs.existsSync(path.join(currentPath, "lerna.json"))) {
42
+ return currentPath;
43
+ }
44
+ } catch (error) {
45
+ console.warn(`Failed to parse package.json at ${packageJsonPath}:`, error);
46
+ }
47
+ }
48
+ currentPath = path.dirname(currentPath);
49
+ }
50
+ return void 0;
51
+ };
52
+ const mergePackageMetadata = (parentMetadata, childMetadata) => {
53
+ const merged = {};
54
+ for (const key in parentMetadata) {
55
+ const value = parentMetadata[key];
56
+ if (value !== void 0) {
57
+ merged[key] = value;
58
+ }
59
+ }
60
+ for (const key in childMetadata) {
61
+ const value = childMetadata[key];
62
+ if (value !== void 0) {
63
+ merged[key] = value;
64
+ }
65
+ }
66
+ return merged;
67
+ };
68
+ const resolvePackageMetadataT = async (projectRoot, readPackageMetadataFn, mergePackageMetadataFn) => {
69
+ const workspaceRoot = await findWorkspaceRoot(projectRoot);
70
+ if (!workspaceRoot) {
71
+ const localPackagePath = path.join(projectRoot, "package.json");
72
+ return await readPackageMetadataFn(localPackagePath);
73
+ }
74
+ const projectPackagePath = path.join(projectRoot, "package.json");
75
+ const rootPackagePath = path.join(workspaceRoot, "package.json");
76
+ const metadata = await readPackageMetadataFn(rootPackagePath);
77
+ if (projectPackagePath !== rootPackagePath && fs.existsSync(projectPackagePath)) {
78
+ const projectMetadata = await readPackageMetadataFn(projectPackagePath);
79
+ return mergePackageMetadataFn(metadata, projectMetadata);
80
+ } else {
81
+ return metadata;
82
+ }
83
+ };
84
+ const readRawPackageJson = async (packagePath) => {
85
+ try {
86
+ const content = await promises.readFile(packagePath, "utf-8");
87
+ return JSON.parse(content);
88
+ } catch (error) {
89
+ console.error(`Failed to read package.json from ${packagePath}:`, error);
90
+ throw error;
91
+ }
92
+ };
93
+ const mergeRawPackageJson = (parentPackage, childPackage) => {
94
+ const inheritableFields = [
95
+ "version",
96
+ "description",
97
+ "author",
98
+ "license",
99
+ "repository",
100
+ "keywords",
101
+ "homepage",
102
+ "bugs"
103
+ ];
104
+ const merged = { ...childPackage };
105
+ for (const field of inheritableFields) {
106
+ if (parentPackage[field] && !merged[field]) {
107
+ merged[field] = parentPackage[field];
108
+ }
109
+ }
110
+ return merged;
111
+ };
112
+ const resolvePackageMetadata = (projectRoot) => {
113
+ return resolvePackageMetadataT(projectRoot, readPackageMetadata, mergePackageMetadata);
114
+ };
115
+ const resolveRawPackageJson = (projectRoot) => {
116
+ return resolvePackageMetadataT(projectRoot, readRawPackageJson, mergeRawPackageJson);
117
+ };
118
+ exports.resolvePackageMetadata = resolvePackageMetadata;
119
+ exports.resolveRawPackageJson = resolveRawPackageJson;
@@ -0,0 +1,120 @@
1
+ import { existsSync } from "fs";
2
+ import { readFile } from "fs/promises";
3
+ import { join, dirname } from "path";
4
+ const flattenObject = (obj, prefix, map) => {
5
+ for (const [key, value] of Object.entries(obj)) {
6
+ if (!value)
7
+ continue;
8
+ const fullKey = prefix ? `${prefix}.${key}` : key;
9
+ if (typeof value === "string") {
10
+ map[fullKey] = value;
11
+ } else if (Array.isArray(value)) {
12
+ map[fullKey] = value.map((v) => String(v)).join(",");
13
+ } else if (typeof value === "object") {
14
+ flattenObject(value, fullKey, map);
15
+ } else {
16
+ map[fullKey] = String(value);
17
+ }
18
+ }
19
+ };
20
+ const readPackageMetadata = async (packagePath) => {
21
+ try {
22
+ const content = await readFile(packagePath, "utf-8");
23
+ const json = JSON.parse(content);
24
+ const map = {};
25
+ flattenObject(json, "", map);
26
+ return map;
27
+ } catch (error) {
28
+ console.warn(`Failed to read package.json from ${packagePath}:`, error);
29
+ return {};
30
+ }
31
+ };
32
+ const findWorkspaceRoot = async (startPath) => {
33
+ let currentPath = startPath;
34
+ while (currentPath !== dirname(currentPath)) {
35
+ const packageJsonPath = join(currentPath, "package.json");
36
+ if (existsSync(packageJsonPath)) {
37
+ try {
38
+ const content = await readFile(packageJsonPath, "utf-8");
39
+ const packageJson = JSON.parse(content);
40
+ if (packageJson.workspaces || existsSync(join(currentPath, "pnpm-workspace.yaml")) || existsSync(join(currentPath, "lerna.json"))) {
41
+ return currentPath;
42
+ }
43
+ } catch (error) {
44
+ console.warn(`Failed to parse package.json at ${packageJsonPath}:`, error);
45
+ }
46
+ }
47
+ currentPath = dirname(currentPath);
48
+ }
49
+ return void 0;
50
+ };
51
+ const mergePackageMetadata = (parentMetadata, childMetadata) => {
52
+ const merged = {};
53
+ for (const key in parentMetadata) {
54
+ const value = parentMetadata[key];
55
+ if (value !== void 0) {
56
+ merged[key] = value;
57
+ }
58
+ }
59
+ for (const key in childMetadata) {
60
+ const value = childMetadata[key];
61
+ if (value !== void 0) {
62
+ merged[key] = value;
63
+ }
64
+ }
65
+ return merged;
66
+ };
67
+ const resolvePackageMetadataT = async (projectRoot, readPackageMetadataFn, mergePackageMetadataFn) => {
68
+ const workspaceRoot = await findWorkspaceRoot(projectRoot);
69
+ if (!workspaceRoot) {
70
+ const localPackagePath = join(projectRoot, "package.json");
71
+ return await readPackageMetadataFn(localPackagePath);
72
+ }
73
+ const projectPackagePath = join(projectRoot, "package.json");
74
+ const rootPackagePath = join(workspaceRoot, "package.json");
75
+ const metadata = await readPackageMetadataFn(rootPackagePath);
76
+ if (projectPackagePath !== rootPackagePath && existsSync(projectPackagePath)) {
77
+ const projectMetadata = await readPackageMetadataFn(projectPackagePath);
78
+ return mergePackageMetadataFn(metadata, projectMetadata);
79
+ } else {
80
+ return metadata;
81
+ }
82
+ };
83
+ const readRawPackageJson = async (packagePath) => {
84
+ try {
85
+ const content = await readFile(packagePath, "utf-8");
86
+ return JSON.parse(content);
87
+ } catch (error) {
88
+ console.error(`Failed to read package.json from ${packagePath}:`, error);
89
+ throw error;
90
+ }
91
+ };
92
+ const mergeRawPackageJson = (parentPackage, childPackage) => {
93
+ const inheritableFields = [
94
+ "version",
95
+ "description",
96
+ "author",
97
+ "license",
98
+ "repository",
99
+ "keywords",
100
+ "homepage",
101
+ "bugs"
102
+ ];
103
+ const merged = { ...childPackage };
104
+ for (const field of inheritableFields) {
105
+ if (parentPackage[field] && !merged[field]) {
106
+ merged[field] = parentPackage[field];
107
+ }
108
+ }
109
+ return merged;
110
+ };
111
+ const resolvePackageMetadata = (projectRoot) => {
112
+ return resolvePackageMetadataT(projectRoot, readPackageMetadata, mergePackageMetadata);
113
+ };
114
+ const resolveRawPackageJson = (projectRoot) => {
115
+ return resolvePackageMetadataT(projectRoot, readRawPackageJson, mergeRawPackageJson);
116
+ };
117
+ export {
118
+ resolveRawPackageJson as a,
119
+ resolvePackageMetadata as r
120
+ };
@@ -18,6 +18,20 @@ export declare const findWorkspaceRoot: (startPath: string) => Promise<string |
18
18
  * @returns Merged metadata
19
19
  */
20
20
  export declare const mergePackageMetadata: (parentMetadata: PackageMetadata, childMetadata: PackageMetadata) => PackageMetadata;
21
+ /**
22
+ * Read and parse package.json file without flattening
23
+ * @param packagePath - Path to package.json
24
+ * @returns Promise resolving to raw package object
25
+ */
26
+ export declare const readRawPackageJson: (packagePath: string) => Promise<any>;
27
+ /**
28
+ * Merge raw package.json objects with inheritance (child overrides parent)
29
+ * Only inherits package metadata fields, not project-specific configurations
30
+ * @param parentPackage - Parent package object
31
+ * @param childPackage - Child package object
32
+ * @returns Merged package object with only metadata fields
33
+ */
34
+ export declare const mergeRawPackageJson: (parentPackage: any, childPackage: any) => any;
21
35
  /**
22
36
  * Resolve package metadata for current project with workspace inheritance
23
37
  * @param projectRoot - Current project root
@@ -25,17 +39,9 @@ export declare const mergePackageMetadata: (parentMetadata: PackageMetadata, chi
25
39
  */
26
40
  export declare const resolvePackageMetadata: (projectRoot: string) => Promise<PackageMetadata>;
27
41
  /**
28
- * Generate banner string from package.json metadata
29
- * @param metadata - Package metadata
30
- * @param outputKeys - Array of keys to output in specified order
31
- * @returns Banner string
32
- */
33
- export declare const generateBanner: (metadata: PackageMetadata, outputKeys: string[]) => string;
34
- /**
35
- * Insert banner header at appropriate position considering shebang
36
- * @param content - The content to insert banner into
37
- * @param banner - The banner header to insert
38
- * @returns Content with banner header inserted
42
+ * Resolve raw package.json for current project with workspace inheritance
43
+ * @param projectRoot - Current project root
44
+ * @returns Promise resolving to resolved raw package.json object
39
45
  */
40
- export declare const insertBannerHeader: (content: string, banner: string) => string;
46
+ export declare const resolveRawPackageJson: (projectRoot: string) => Promise<any>;
41
47
  //# sourceMappingURL=internal.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AASA,MAAM,MAAM,e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H,eAAO,MAAM,mBAAmB,GAAI,eAAe,GAAG,EAAE,cAAc,GAAG,KAAG,GAwB3E,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,GAAI,aAAa,MAAM,KAAG,OAAO,CAAC,eAAe,CAEnF,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,GAAI,aAAa,MAAM,KAAG,OAAO,CAAC,GAAG,CAEtE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screw-up",
3
- "version": "0.7.1",
3
+ "version": "0.9.1",
4
4
  "description": "Simply package metadata inserter on Vite plugin",
5
5
  "keywords": [
6
6
  "vite",
@@ -27,20 +27,28 @@
27
27
  "types": "./dist/index.d.ts"
28
28
  }
29
29
  },
30
+ "bin": {
31
+ "screw-up": "./dist/cli.js"
32
+ },
30
33
  "files": [
31
34
  "dist"
32
35
  ],
33
36
  "scripts": {
34
- "build": "rv --npm . && tsc --noEmit && vite build && tsc --emitDeclarationOnly --outDir dist",
35
- "test": "rv --npm . && tsc --noEmit && vitest run"
37
+ "build": "rv --npm . && npm i && vite build",
38
+ "test": "rv --npm . && npm i && npm run build && vitest run"
36
39
  },
37
40
  "peerDependencies": {
41
+ "glob": ">=11.0.0",
42
+ "tar-stream": ">=3.0.0",
38
43
  "vite": ">=5.0.0"
39
44
  },
40
45
  "devDependencies": {
41
- "@types/node": "^20.0.0",
42
- "typescript": "^5.0.0",
43
- "vite-plugin-dts": "^3.0.0",
44
- "vitest": "^1.0.0"
46
+ "@types/node": ">=20.0.0",
47
+ "@types/tar-stream": "^3.1.4",
48
+ "dayjs": "^1.11.13",
49
+ "tar": ">=7.4.0",
50
+ "typescript": ">=5.0.0",
51
+ "vite-plugin-dts": ">=3.0.0",
52
+ "vitest": ">=1.0.0"
45
53
  }
46
54
  }