-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.d.ts
222 lines (158 loc) · 7.42 KB
/
index.d.ts
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
import type {PackageJson, JsonObject} from 'type-fest';
export type Options = {
/**
The indentation to use for new files.
Accepts `'\t'` for tab indentation or a number of spaces.
If the file already exists, the existing indentation will be used.
Default: Auto-detected or `'\t'`
*/
readonly indent?: string | number;
/**
Remove empty `dependencies`, `devDependencies`, `optionalDependencies` and `peerDependencies` objects.
@default true
*/
readonly normalize?: boolean;
};
/**
A JSON object with suggested fields for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file).
*/
type PackageJsonData = PackageJson | JsonObject;
/**
Write a `package.json` file.
Writes atomically and creates directories for you as needed. Sorts dependencies when writing. Preserves the indentation if the file already exists.
To write to a `package.json` file while preserving unchanged fields, see {@link updatePackage}.
@param path - The path to where the `package.json` file should be written or its directory.
@example
```
import path from 'node:path';
import {writePackage} from 'write-package';
await writePackage({foo: true});
console.log('done');
await writePackage(path.join('unicorn', 'package.json'), {foo: true});
console.log('done');
```
*/
export function writePackage(path: string, data: PackageJsonData, options?: Options): Promise<void>;
export function writePackage(data: PackageJsonData, options?: Options): Promise<void>;
/**
Synchronously write a `package.json` file.
Writes atomically and creates directories for you as needed. Sorts dependencies when writing. Preserves the indentation if the file already exists.
To write to a `package.json` file while preserving unchanged fields, see {@link updatePackageSync}.
@param path - The path to where the `package.json` file should be written or its directory.
@example
```
import path from 'node:path';
import {writePackageSync} from 'write-package';
writePackageSync({foo: true});
console.log('done');
writePackageSync(path.join('unicorn', 'package.json'), {foo: true});
console.log('done');
```
*/
export function writePackageSync(path: string, data: PackageJsonData, options?: Options): void;
export function writePackageSync(data: PackageJsonData, options?: Options): void;
/**
Update a `package.json` file.
Writes atomically and creates directories for you as needed. Sorts dependencies when writing. Preserves the indentation if the file already exists.
@param path - The path to where the `package.json` file should be written or its directory.
@example
```
import {updatePackage} from 'write-package';
await updatePackage({foo: true});
//=> { "foo": true }
await updatePackage({foo: false, bar: true});
//=> { "foo": false, "bar": true }
```
*/
export function updatePackage(path: string, data: PackageJsonData, options?: Options): Promise<void>;
export function updatePackage(data: PackageJsonData, options?: Options): Promise<void>;
/**
Update a `package.json` file.
Writes atomically and creates directories for you as needed. Sorts dependencies when writing. Preserves the indentation if the file already exists.
@param path - The path to where the `package.json` file should be written or its directory.
@example
```
import {updatePackageSync} from 'write-package';
updatePackageSync({foo: true});
//=> { "foo": true }
updatePackageSync({foo: false, bar: true});
//=> { "foo": false, "bar": true }
```
*/
export function updatePackageSync(path: string, data: PackageJsonData, options?: Options): void;
export function updatePackageSync(data: PackageJsonData, options?: Options): void;
type DependencyKeys =
| 'dependencies'
| 'devDependencies'
| 'optionalDependencies'
| 'peerDependencies';
type Dependencies = Partial<Record<string, string>> | Pick<PackageJson, DependencyKeys>;
/**
Add dependencies to a `package.json` file.
Sorts dependencies when writing. Preserves indentation, or creates the file if it does not exist.
@param path - The path to where the `package.json` file should be written or its directory.
@example
```
import {writePackage, addPackageDependencies} from 'write-package';
await writePackage({foo: true});
//=> { "foo": true }
await addPackageDependencies({foo: '1.0.0'});
//=> { "foo": true, "dependencies": { "foo": "1.0.0" } }
await addPackageDependencies({dependencies: {foo: '1.0.0'}, devDependencies: {bar: '1.0.0'}});
//=> { "foo": true, "dependencies": { "foo": "1.0.0" }, "devDependencies": { "bar": "1.0.0" } }
```
*/
export function addPackageDependencies(path: string, dependencies: Dependencies, options?: Options): Promise<void>;
export function addPackageDependencies(dependencies: Dependencies, options?: Options): Promise<void>;
/**
Add dependencies to a `package.json` file.
Sorts dependencies when writing. Preserves indentation, or creates the file if it does not exist.
@param path - The path to where the `package.json` file should be written or its directory.
@example
```
import {writePackageSync, addPackageDependenciesSync} from 'write-package';
writePackageSync({foo: true});
//=> { "foo": true }
addPackageDependenciesSync({foo: '1.0.0'});
//=> { "foo": true, "dependencies": { "foo": "1.0.0" } }
addPackageDependenciesSync({dependencies: {foo: '1.0.0'}, devDependencies: {bar: '1.0.0'}});
//=> { "foo": true, "dependencies": { "foo": "1.0.0" }, "devDependencies": { "bar": "1.0.0" } }
```
*/
export function addPackageDependenciesSync(path: string, dependencies: Dependencies, options?: Options): void;
export function addPackageDependenciesSync(dependencies: Dependencies, options?: Options): void;
type DependenciesToRemove = string[] | Partial<Record<DependencyKeys, string[]>>;
/**
Remove dependencies from a `package.json` file.
Sorts dependencies when writing. Preserves indentation. Does not throw if the file does not exist.
@param path - The path to where the `package.json` file should be written or its directory.
@example
```
import {writePackage, removePackageDependencies} from 'write-package';
await writePackage({foo: true, dependencies: {foo: '1.0.0'}, devDependencies: {bar: '1.0.0'}});
//=> { "foo": true, "dependencies": { "foo": "1.0.0" }, "devDependencies": { "bar": "1.0.0" } }
await removePackageDependencies(['foo']);
//=> { "foo": true, "devDependencies": { "bar": "1.0.0" } }
await removePackageDependencies({devDependencies: ['bar']});
//=> { "foo": true }
```
*/
export function removePackageDependencies(path: string, dependencies: DependenciesToRemove, options?: Options): Promise<void>;
export function removePackageDependencies(dependencies: DependenciesToRemove, options?: Options): Promise<void>;
/**
Remove dependencies from a `package.json` file.
Sorts dependencies when writing. Preserves indentation. Does not throw if the file does not exist.
@param path - The path to where the `package.json` file should be written or its directory.
@example
```
import {writePackageSync, removePackageDependenciesSync} from 'write-package';
writePackageSync({foo: true, dependencies: {foo: '1.0.0'}, devDependencies: {bar: '1.0.0'}});
//=> { "foo": true, "dependencies": { "foo": "1.0.0" }, "devDependencies": { "bar": "1.0.0" } }
removePackageDependenciesSync(['foo']);
//=> { "foo": true, "devDependencies": { "bar": "1.0.0" } }
removePackageDependenciesSync({devDependencies: ['bar']});
//=> { "foo": true }
```
*/
export function removePackageDependenciesSync(path: string, dependencies: DependenciesToRemove, options?: Options): void;
export function removePackageDependenciesSync(dependencies: DependenciesToRemove, options?: Options): void;