@reliverse/typerso 2.2.7

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.
Files changed (3) hide show
  1. package/dist/mod.d.ts +60 -0
  2. package/dist/mod.js +154 -0
  3. package/package.json +24 -0
package/dist/mod.d.ts ADDED
@@ -0,0 +1,60 @@
1
+ import type { PackageJson } from "pkg-types";
2
+ import { defineTSConfig, readPackageJSON, readTSConfig, resolvePackageJSON, writePackageJSON, writeTSConfig } from "pkg-types";
3
+ export type { PackageJson };
4
+ export { readPackageJSON, writePackageJSON, resolvePackageJSON, defineTSConfig, writeTSConfig, readTSConfig, };
5
+ export declare const readPackageJSONSafe: (path: string) => Promise<PackageJson | null>;
6
+ export declare const hasWorkspaces: (packageJson: PackageJson | null | undefined) => boolean;
7
+ export declare const getWorkspacePatterns: (packageJson: PackageJson | null | undefined) => string[];
8
+ export type PackageKind = "library" | "browser-app" | "native-app" | "cli";
9
+ /**
10
+ * Extract package name from scoped package name
11
+ * @param packageName - The full package name (e.g., "@reliverse/dler")
12
+ * @returns The package name without scope (e.g., "dler")
13
+ */
14
+ export declare function extractPackageName(packageName: string): string;
15
+ /**
16
+ * Parse bin argument string into a record of binary definitions
17
+ * @param binArg - The bin argument string (e.g., "dler=dist/cli.js,login=dist/foo/bar/login.js")
18
+ * @returns Record mapping binary names to their file paths
19
+ */
20
+ export declare function parseBinArgument(binArg: string): Record<string, string>;
21
+ /**
22
+ * Transform exports field to point to built files instead of source files
23
+ * This converts source paths (src/*.ts) to built paths (dist/*.js, dist/*.d.ts)
24
+ */
25
+ export declare function transformExportsForBuild(exports: PackageJson["exports"]): PackageJson["exports"];
26
+ /**
27
+ * Add bin field to package.json for CLI packages
28
+ */
29
+ export declare function addBinFieldToPackageJson(pkg: PackageJson, kind: PackageKind | undefined, binDefinitions?: string): void;
30
+ /**
31
+ * Add files field to package.json if missing or empty
32
+ * @param packagePath - Path to the package directory
33
+ * @returns Object indicating success and whether the field was added
34
+ */
35
+ export declare function addFilesFieldIfMissing(packagePath: string): Promise<{
36
+ success: boolean;
37
+ added: boolean;
38
+ error?: string;
39
+ }>;
40
+ export interface PreparePackageJsonOptions {
41
+ /** Package kind (library, cli, browser-app, native-app) */
42
+ kind?: PackageKind;
43
+ /** Binary definitions for CLI packages */
44
+ binDefinitions?: string;
45
+ /** Access level for publishing */
46
+ access?: "public" | "restricted";
47
+ /** Whether to add publishConfig */
48
+ addPublishConfig?: boolean;
49
+ /** Whether to add license field if missing */
50
+ addLicense?: boolean;
51
+ /** Whether to transform exports from src/*.ts to dist/*.js (default: false) */
52
+ transformExports?: boolean;
53
+ }
54
+ /**
55
+ * Prepare package.json for publishing by transforming exports and adding necessary fields
56
+ */
57
+ export declare function preparePackageJsonForPublishing(packagePath: string, options?: PreparePackageJsonOptions): Promise<{
58
+ success: boolean;
59
+ error?: string;
60
+ }>;
package/dist/mod.js ADDED
@@ -0,0 +1,154 @@
1
+ import { resolve } from "node:path";
2
+ import {
3
+ defineTSConfig,
4
+ readPackageJSON,
5
+ readTSConfig,
6
+ resolvePackageJSON,
7
+ writePackageJSON,
8
+ writeTSConfig
9
+ } from "pkg-types";
10
+ export {
11
+ readPackageJSON,
12
+ writePackageJSON,
13
+ resolvePackageJSON,
14
+ defineTSConfig,
15
+ writeTSConfig,
16
+ readTSConfig
17
+ };
18
+ export const readPackageJSONSafe = async (path) => {
19
+ try {
20
+ return await readPackageJSON(path);
21
+ } catch (_error) {
22
+ return null;
23
+ }
24
+ };
25
+ export const hasWorkspaces = (packageJson) => !!(packageJson && typeof packageJson === "object" && "workspaces" in packageJson && packageJson.workspaces && typeof packageJson.workspaces === "object" && "packages" in packageJson.workspaces && Array.isArray(packageJson.workspaces.packages));
26
+ export const getWorkspacePatterns = (packageJson) => {
27
+ if (packageJson && typeof packageJson === "object" && "workspaces" in packageJson && packageJson.workspaces && typeof packageJson.workspaces === "object" && "packages" in packageJson.workspaces && Array.isArray(packageJson.workspaces.packages)) {
28
+ return packageJson.workspaces.packages;
29
+ }
30
+ return [];
31
+ };
32
+ export function extractPackageName(packageName) {
33
+ const parts = packageName.split("/");
34
+ return parts[parts.length - 1] || packageName;
35
+ }
36
+ export function parseBinArgument(binArg) {
37
+ const binMap = {};
38
+ if (!binArg) {
39
+ return binMap;
40
+ }
41
+ const entries = binArg.split(",");
42
+ for (const entry of entries) {
43
+ const [name, path] = entry.split("=");
44
+ if (name && path) {
45
+ binMap[name.trim()] = path.trim();
46
+ }
47
+ }
48
+ return binMap;
49
+ }
50
+ export function transformExportsForBuild(exports) {
51
+ if (typeof exports === "string") {
52
+ return exports.replace(/\.ts$/, ".js").replace(/^\.\/src\//, "./dist/");
53
+ }
54
+ if (Array.isArray(exports)) {
55
+ return exports.map((item) => {
56
+ const result = transformExportsForBuild(item);
57
+ return result;
58
+ });
59
+ }
60
+ if (typeof exports === "object" && exports !== null) {
61
+ const updated = {};
62
+ for (const [key, value] of Object.entries(exports)) {
63
+ if (typeof value === "object" && value !== null) {
64
+ const updatedValue = {};
65
+ for (const [subKey, subValue] of Object.entries(value)) {
66
+ if (typeof subValue === "string") {
67
+ if (subKey === "types") {
68
+ updatedValue[subKey] = subValue.replace(/\.ts$/, ".d.ts").replace(/^\.\/src\//, "./dist/");
69
+ } else {
70
+ updatedValue[subKey] = subValue.replace(/\.ts$/, ".js").replace(/^\.\/src\//, "./dist/");
71
+ }
72
+ } else {
73
+ updatedValue[subKey] = subValue;
74
+ }
75
+ }
76
+ updated[key] = updatedValue;
77
+ } else if (typeof value === "string") {
78
+ updated[key] = value.replace(/\.ts$/, ".js").replace(/^\.\/src\//, "./dist/");
79
+ } else {
80
+ updated[key] = value;
81
+ }
82
+ }
83
+ return updated;
84
+ }
85
+ return exports;
86
+ }
87
+ export function addBinFieldToPackageJson(pkg, kind, binDefinitions) {
88
+ if (kind !== "cli") {
89
+ return;
90
+ }
91
+ const binMap = parseBinArgument(binDefinitions || "");
92
+ if (Object.keys(binMap).length === 0) {
93
+ const packageName = extractPackageName(pkg.name || "cli");
94
+ binMap[packageName] = "dist/cli.js";
95
+ }
96
+ pkg.bin = binMap;
97
+ }
98
+ export async function addFilesFieldIfMissing(packagePath) {
99
+ try {
100
+ const pkg = await readPackageJSON(packagePath);
101
+ if (!pkg) {
102
+ return {
103
+ success: false,
104
+ added: false,
105
+ error: "Could not read package.json"
106
+ };
107
+ }
108
+ if (pkg.files && Array.isArray(pkg.files) && pkg.files.length > 0) {
109
+ return { success: true, added: false };
110
+ }
111
+ const files = ["dist", "package.json"];
112
+ pkg.files = files;
113
+ await writePackageJSON(resolve(packagePath, "package.json"), pkg);
114
+ return { success: true, added: true };
115
+ } catch (error) {
116
+ return {
117
+ success: false,
118
+ added: false,
119
+ error: error instanceof Error ? error.message : String(error)
120
+ };
121
+ }
122
+ }
123
+ export async function preparePackageJsonForPublishing(packagePath, options = {}) {
124
+ try {
125
+ const pkg = await readPackageJSON(packagePath);
126
+ if (!pkg) {
127
+ return { success: false, error: "Could not read package.json" };
128
+ }
129
+ if (options.transformExports && pkg.exports) {
130
+ pkg.exports = transformExportsForBuild(pkg.exports);
131
+ }
132
+ addBinFieldToPackageJson(pkg, options.kind, options.binDefinitions);
133
+ if (!pkg.files || !Array.isArray(pkg.files) || pkg.files.length === 0) {
134
+ pkg.files = ["dist", "package.json"];
135
+ }
136
+ const isPublishable = pkg.private !== true;
137
+ if (options.addPublishConfig !== false) {
138
+ if (!pkg.publishConfig) {
139
+ pkg.publishConfig = {};
140
+ }
141
+ pkg.publishConfig.access = options.access || "public";
142
+ }
143
+ if (isPublishable && options.addLicense !== false && !pkg.license) {
144
+ pkg.license = "MIT";
145
+ }
146
+ await writePackageJSON(resolve(packagePath, "package.json"), pkg);
147
+ return { success: true };
148
+ } catch (error) {
149
+ return {
150
+ success: false,
151
+ error: error instanceof Error ? error.message : String(error)
152
+ };
153
+ }
154
+ }
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@reliverse/typerso",
3
+ "version": "2.2.7",
4
+ "private": false,
5
+ "type": "module",
6
+ "description": "@reliverse/typerso is a collection of utils to work with package.json and tsconfig.json files.",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/mod.d.ts",
10
+ "default": "./dist/mod.js"
11
+ }
12
+ },
13
+ "dependencies": {
14
+ "pkg-types": "^2.3.0"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "package.json"
22
+ ],
23
+ "license": "MIT"
24
+ }