obsidian-dev-utils
Version:
This is the collection of useful functions that you can use for your Obsidian plugin development
150 lines (149 loc) • 6.89 kB
text/typescript
/**
* @packageDocumentation
*
* Contains utility functions for NPM package.json.
*/
import type { PackageJson, Promisable } from 'type-fest';
/**
* Options for {@link editPackageJson}.
*/
export interface EditPackageJsonOptions {
/**
* A current working folder where `package.json` is located.
*/
cwd?: string;
/**
* If true, skips editing if the file does not exist.
*/
shouldSkipIfMissing?: boolean;
}
/**
* A type of the `package.json` file.
*/
export type { PackageJson };
/**
* A type of the `package-lock.json` file.
*/
export interface PackageLockJson extends Partial<PackageJson> {
/**
* Packages in the `package-lock.json` file.
*/
packages?: Record<string, PackageJson>;
}
/**
* Reads, edits, and writes back the `package-lock.json` file using the provided edit function.
*
* @param editFn - The function to edit the parsed `PackageJson` object.
* @param options - Additional options for editing.
* @returns A {@link Promise} that resolves when the file has been edited and written.
*/
export declare function editNpmShrinkWrapJson(editFn: (packageLockJson: PackageLockJson) => Promisable<void>, options?: EditPackageJsonOptions): Promise<void>;
/**
* Reads, edits, and writes back the `package.json` file using the provided edit function.
*
* @param editFn - The function to edit the parsed `PackageJson` object.
* @param options - Additional options for editing.
* @returns A {@link Promise} that resolves when the file has been edited and written.
*/
export declare function editPackageJson(editFn: (packageJson: PackageJson) => Promisable<void>, options?: EditPackageJsonOptions): Promise<void>;
/**
* Reads, edits, and writes back the `package.json` file using the provided edit function.
*
* @param editFn - The function to edit the parsed `PackageJson` object.
* @param options - Additional options for editing.
*/
export declare function editPackageJsonSync(editFn: (packageJson: PackageJson) => void, options?: EditPackageJsonOptions): void;
/**
* Reads, edits, and writes back the `package-lock.json` file using the provided edit function.
*
* @param editFn - The function to edit the parsed `PackageJson` object.
* @param options - Additional options for editing.
* @returns A {@link Promise} that resolves when the file has been edited and written.
*/
export declare function editPackageLockJson(editFn: (packageLockJson: PackageLockJson) => Promisable<void>, options?: EditPackageJsonOptions): Promise<void>;
/**
* Reads, edits, and writes back the `package-lock.json` file using the provided edit function.
*
* @param editFn - The function to edit the parsed `PackageLockJson` object.
* @param options - Additional options for editing.
*/
export declare function editPackageLockJsonSync(editFn: (packageLockJson: PackageLockJson) => void, options?: EditPackageJsonOptions): void;
/**
* Resolves the path to the `npm-shrinkwrap.json` file in the specified folder or in the root if no folder is specified.
*
* @param cwd - The current working folder where `npm-shrinkwrap.json` is located.
* @returns The resolved path to the `npm-shrinkwrap.json` file.
*/
export declare function getNpmShrinkWrapJsonPath(cwd?: string): string;
/**
* Resolves the path to the `package.json` file in the specified folder or in the root if no folder is specified.
*
* @param cwd - The current working folder where `package.json` is located.
* @returns The resolved path to the `package.json` file.
*/
export declare function getPackageJsonPath(cwd?: string): string;
/**
* Resolves the path to the `package-lock.json` file in the specified folder or in the root if no folder is specified.
*
* @param cwd - The current working folder where `package-lock.json` is located.
* @returns The resolved path to the `package-lock.json` file.
*/
export declare function getPackageLockJsonPath(cwd?: string): string;
/**
* Reads the `package.json` file from the specified folder or from the root if no folder is specified.
*
* @param cwd - The current working folder where `package.json` is located.
* @returns A {@link Promise} that resolves with the parsed `PackageJson` object.
*/
export declare function readPackageJson(cwd?: string): Promise<PackageJson>;
/**
* Reads the `package.json` file from the specified folder or from the root if no folder is specified.
*
* @param cwd - The current working folder where `package.json` is located.
* @returns The parsed `PackageJson` object.
*/
export declare function readPackageJsonSync(cwd?: string): PackageJson;
/**
* Reads the `package-lock.json` file from the specified folder or from the root if no folder is specified.
*
* @param cwd - The current working folder where `package-lock.json` is located.
* @returns A {@link Promise} that resolves with the parsed `PackageJson` object.
*/
export declare function readPackageLockJson(cwd?: string): Promise<PackageLockJson>;
/**
* Reads the `package-lock.json` file from the specified folder or from the root if no folder is specified.
*
* @param cwd - The current working folder where `package-lock.json` is located.
* @returns The parsed `PackageLockJson` object.
*/
export declare function readPackageLockJsonSync(cwd?: string): PackageLockJson;
/**
* Writes the provided `PackageJson` object to the `package.json` file in the specified folder or in the root.
*
* @param packageJson - The `PackageJson` object to write.
* @param cwd - The current working folder where `package.json` is located.
* @returns A {@link Promise} that resolves when the file has been written.
*/
export declare function writePackageJson(packageJson: PackageJson, cwd?: string): Promise<void>;
/**
* Writes the provided `PackageJson` object to the `package.json` file in the specified folder or in the root.
*
* @param packageJson - The `PackageJson` object to write.
* @param cwd - The current working folder where `package.json` is located.
*/
export declare function writePackageJsonSync(packageJson: PackageJson, cwd?: string): void;
/**
* Writes the provided `PackageJson` object to the `package-lock.json` file in the specified folder or in the root.
*
* @param packageLockJson - The `PackageLockJson` object to write.
* @param cwd - The current working folder where `package-lock.json` is located.
* @returns A {@link Promise} that resolves when the file has been written.
*/
export declare function writePackageLockJson(packageLockJson: PackageLockJson, cwd?: string): Promise<void>;
/**
* Writes the provided `PackageLockJson` object to the `package-lock.json` file in the specified folder or in the root.
*
* @param packageLockJson - The `PackageLockJson` object to write.
* @param cwd - The current working folder where `package-lock.json` is located.
*/
export declare function writePackageLockJsonSync(packageLockJson: PackageLockJson, cwd?: string): void;