@pnpm/meta-updater
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -5,11 +5,11 @@ import { ProjectManifest } from '@pnpm/types'; | ||
}): Promise<void>; | ||
declare type UpdateFunc = (obj: object, dir: string, manifest: ProjectManifest) => object | Promise<object>; | ||
declare type UpdateFunc = (obj: object, dir: string, manifest: ProjectManifest) => object | Promise<object | null> | null; | ||
declare type UpdateError = { | ||
expected: Object; | ||
actual: Object; | ||
expected: Object | null; | ||
actual: Object | null; | ||
path: string; | ||
}; | ||
export declare function performUpdates(workspaceDir: string, update: Record<string, UpdateFunc>, opts: { | ||
export declare function performUpdates(workspaceDir: string, update: Record<string, UpdateFunc>, opts?: { | ||
test?: boolean; | ||
}): Promise<null | UpdateError>; | ||
export {}; |
import findWorkspaceDir from '@pnpm/find-workspace-dir'; | ||
import findWorkspacePackages from '@pnpm/find-workspace-packages'; | ||
import { findWorkspacePackagesNoCheck } from '@pnpm/find-workspace-packages'; | ||
import fs from 'fs'; | ||
import loadJsonFile from 'load-json-file'; | ||
import path from 'path'; | ||
import exists from 'path-exists'; | ||
import R from 'ramda'; | ||
@@ -23,3 +23,3 @@ import writeJsonFile from 'write-json-file'; | ||
export async function performUpdates(workspaceDir, update, opts) { | ||
let pkgs = await findWorkspacePackages['default'](workspaceDir, { engineStrict: false }); | ||
let pkgs = await findWorkspacePackagesNoCheck(workspaceDir); | ||
for (const { dir, manifest, writeProjectManifest } of pkgs) { | ||
@@ -34,3 +34,3 @@ for (const [p, updateFn] of Object.entries(update)) { | ||
continue; | ||
if (!opts.test) { | ||
if (!(opts === null || opts === void 0 ? void 0 : opts.test)) { | ||
await writeProjectManifest(updatedManifest); | ||
@@ -47,6 +47,3 @@ continue; | ||
continue; | ||
if (!await exists(fp)) { | ||
continue; | ||
} | ||
const obj = await loadJsonFile(fp); | ||
const obj = await readJsonFile(fp); | ||
const updatedObj = await updateFn(R.clone(obj), dir, clonedManifest); | ||
@@ -56,11 +53,14 @@ const needsUpdate = !R.equals(obj, updatedObj); | ||
continue; | ||
if (!opts.test) { | ||
await writeJsonFile(fp, updatedObj, { detectIndent: true }); | ||
if (opts === null || opts === void 0 ? void 0 : opts.test) { | ||
return { | ||
expected: obj, | ||
actual: updatedObj, | ||
path: fp, | ||
}; | ||
} | ||
if (updatedObj == null) { | ||
await fs.promises.unlink(fp); | ||
continue; | ||
} | ||
return { | ||
expected: obj, | ||
actual: updatedObj, | ||
path: fp, | ||
}; | ||
await writeJsonFile(fp, updatedObj, { detectIndent: true }); | ||
} | ||
@@ -70,2 +70,13 @@ } | ||
} | ||
async function readJsonFile(p) { | ||
try { | ||
return await loadJsonFile(p); | ||
} | ||
catch (err) { | ||
if (err.code === 'ENOENT') { | ||
return null; | ||
} | ||
throw err; | ||
} | ||
} | ||
function printJsonDiff(actual, expected) { | ||
@@ -72,0 +83,0 @@ printDiff(JSON.stringify(actual, null, 2), JSON.stringify(expected, null, 2)); |
{ | ||
"name": "@pnpm/meta-updater", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Keeps meta files up-to-date in a monorepo", | ||
@@ -29,3 +29,3 @@ "main": "lib/index.js", | ||
"@pnpm/find-workspace-dir": "^3.0.1", | ||
"@pnpm/find-workspace-packages": "^3.0.8", | ||
"@pnpm/find-workspace-packages": "^3.1.0", | ||
"@pnpm/logger": "^4.0.0", | ||
@@ -35,3 +35,2 @@ "@pnpm/types": "^7.3.0", | ||
"meow": "^10.0.1", | ||
"path-exists": "^4.0.0", | ||
"print-diff": "^1.0.0", | ||
@@ -38,0 +37,0 @@ "ramda": "^0.27.1", |
@@ -50,4 +50,10 @@ # @pnpm/meta-updater | ||
## API | ||
### Updater Function: `(config | null, dir, manifest) => Promise<config | null>` | ||
The updater function recieves the config object or null (if the config file does not exist). The updater function returns the config object that should be saved. If the updater function returns null, the config should be removed. | ||
## License | ||
[MIT](./LICENSE) © [Zoltan Kochan](https://www.kochan.io/) |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
11486
9
118
59
1
- Removedpath-exists@^4.0.0