-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.ts
More file actions
230 lines (210 loc) · 6.86 KB
/
edit.ts
File metadata and controls
230 lines (210 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* @fileoverview Editable package.json manipulation utilities.
*
* Convenience wrappers around the `EditablePackageJson` class for the
* two common shapes: "I have a plain object, give me an editable" and
* "I have a path, load it as editable (sync or async)."
*
* - `EditablePackageJsonInstance` — public interface for instances
* - `pkgJsonToEditable` — wrap an in-memory PackageJson object
* - `toEditablePackageJson` — async path-based load
* - `toEditablePackageJsonSync` — sync path-based load
*
* The class factory lives in a sibling leaf and is re-exported here
* so existing `packages/edit` importers keep working unchanged:
*
* - `getEditablePackageJsonClass` — `./edit-class`
*
* Lazy `node:fs` / `node:path` / `node:util` loaders use the canonical
* `getNodeFs` / `getNodePath` / `getNodeUtil` helpers from
* `@socketsecurity/lib/node/{fs,path,util}`.
*/
import { JSONStringify } from '../primordials/json'
import { isNodeModules } from '../paths/normalize'
import { resolvePackageJsonDirname } from '../paths/packages'
import { getEditablePackageJsonClass } from './edit-class'
import { normalizePackageJson } from './normalize'
import type {
EditablePackageJsonOptions,
NormalizeOptions,
PackageJson,
SaveOptions,
} from './types'
/**
* EditablePackageJson instance interface extending NPMCliPackageJson functionality.
* Provides enhanced package.json manipulation with Socket-specific features.
* @extends NPMCliPackageJson (from @npmcli/package-json)
*/
export interface EditablePackageJsonInstance {
/**
* The parsed package.json content as a readonly object.
* @readonly
*/
content: Readonly<PackageJson>
/**
* Create a new package.json file at the specified path.
* @param path - The directory path where package.json will be created
*/
create(path: string): this
/**
* Apply automatic fixes to the package.json based on npm standards.
* @param opts - Optional fix configuration
*/
fix(opts?: unknown | undefined): Promise<this>
/**
* Initialize the instance from a content object.
* @param content - The package.json content object
*/
fromContent(content: unknown): this
/**
* Initialize the instance from a JSON string.
* @param json - The package.json content as a JSON string
*/
fromJSON(json: string): this
/**
* Load a package.json file from the specified path.
* @param path - The directory containing the package.json
* @param create - Whether to create the file if it doesn't exist
*/
load(path: string, create?: boolean): Promise<this>
/**
* Normalize the package.json content according to npm standards.
* @param opts - Normalization options
*/
normalize(opts?: NormalizeOptions): Promise<this>
/**
* Prepare the package.json for publishing.
* @param opts - Preparation options
*/
prepare(opts?: unknown): Promise<this>
/**
* Update the package.json content with new values.
* @param content - Partial package.json object with fields to update
* @override from NPMCliPackageJson
*/
update(content: Partial<PackageJson>): this
/**
* Save the package.json file to disk.
* @param options - Save options for formatting and sorting
* @override from NPMCliPackageJson
*/
save(options?: SaveOptions | undefined): Promise<boolean>
/**
* Synchronously save the package.json file to disk.
* @param options - Save options for formatting and sorting
*/
saveSync(options?: SaveOptions | undefined): boolean
/**
* Check if the package.json will be saved based on current changes.
* @param options - Save options to evaluate
*/
willSave(options?: SaveOptions | undefined): boolean
}
/**
* Convert a package.json object to an editable instance.
*
* @example
* ```typescript
* const editable = pkgJsonToEditable({ name: 'my-pkg', version: '1.0.0' })
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function pkgJsonToEditable(
pkgJson: PackageJson,
options?: EditablePackageJsonOptions,
): unknown {
const { normalize, ...normalizeOptions } = {
__proto__: null,
...options,
} as EditablePackageJsonOptions
const EditablePackageJson = getEditablePackageJsonClass()
return new EditablePackageJson().fromContent(
normalize ? normalizePackageJson(pkgJson, normalizeOptions) : pkgJson,
)
}
/**
* Convert package.json to editable instance with file persistence.
*
* @example
* ```typescript
* const editable = await toEditablePackageJson(
* { name: 'my-pkg', version: '1.0.0' },
* { path: '/tmp/my-project' }
* )
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export async function toEditablePackageJson(
pkgJson: PackageJson,
options?: EditablePackageJsonOptions,
): Promise<unknown> {
const { path: filepath, ...pkgJsonToEditableOptions } = {
__proto__: null,
...options,
}
const { normalize, ...normalizeOptions } = pkgJsonToEditableOptions
if (typeof filepath !== 'string') {
return pkgJsonToEditable(pkgJson, pkgJsonToEditableOptions)
}
const EditablePackageJson = getEditablePackageJsonClass()
const pkgJsonPath = resolvePackageJsonDirname(filepath)
return (
await EditablePackageJson.load(pkgJsonPath, { create: true })
).fromJSON(
`${JSONStringify(
normalize
? normalizePackageJson(pkgJson, {
...(isNodeModules(pkgJsonPath) ? {} : { preserve: ['repository'] }),
...normalizeOptions,
})
: pkgJson,
undefined,
2,
)}\n`,
)
}
/**
* Convert package.json to editable instance with file persistence synchronously.
*
* @example
* ```typescript
* const editable = toEditablePackageJsonSync(
* { name: 'my-pkg', version: '1.0.0' },
* { path: '/tmp/my-project' }
* )
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function toEditablePackageJsonSync(
pkgJson: PackageJson,
options?: EditablePackageJsonOptions,
): unknown {
const { path: filepath, ...pkgJsonToEditableOptions } = {
__proto__: null,
...options,
}
const { normalize, ...normalizeOptions } = pkgJsonToEditableOptions
if (typeof filepath !== 'string') {
return pkgJsonToEditable(pkgJson, pkgJsonToEditableOptions)
}
const EditablePackageJson = getEditablePackageJsonClass()
const pkgJsonPath = resolvePackageJsonDirname(filepath)
return new EditablePackageJson().create(pkgJsonPath).fromJSON(
`${JSONStringify(
normalize
? normalizePackageJson(pkgJson, {
...(isNodeModules(pkgJsonPath) ? {} : { preserve: ['repository'] }),
...normalizeOptions,
})
: pkgJson,
undefined,
2,
)}\n`,
)
}
// Re-exports — preserve the historical `packages/edit` surface so
// downstream importers don't have to chase the split. The lazy
// `node:fs` / `node:path` / `node:util` loaders were removed: use the
// canonical `getNodeFs` / `getNodePath` / `getNodeUtil` from
// `@socketsecurity/lib/node/{fs,path,util}` instead.
export { getEditablePackageJsonClass } from './edit-class'