mirror of
https://github.com/pnpm/pnpm.git
synced 2026-03-29 12:31:52 -04:00
refactor: moving the store saving functions out of context
This commit is contained in:
@@ -7,16 +7,14 @@ import writeJson from '../fs/writeJson'
|
||||
import expandTilde from '../fs/expandTilde'
|
||||
import {StrictPnpmOptions} from '../types'
|
||||
import initLogger from '../logger'
|
||||
import storeJsonController, {StoreJsonCtrl} from '../fs/storeJsonController'
|
||||
import {read as readStoreJson} from '../fs/storeJsonController'
|
||||
import mkdirp from '../fs/mkdirp'
|
||||
import {Package} from '../types'
|
||||
import {StoreJson} from '../fs/storeJsonController'
|
||||
import pnpmPkgJson from '../pnpmPkgJson'
|
||||
import normalizePath = require('normalize-path')
|
||||
|
||||
export type PnpmContext = {
|
||||
pkg?: Package,
|
||||
storeJsonCtrl: StoreJsonCtrl,
|
||||
store: string,
|
||||
root: string,
|
||||
storeJson: StoreJson
|
||||
@@ -26,8 +24,7 @@ export default async function (opts: StrictPnpmOptions): Promise<PnpmContext> {
|
||||
const pkg = await (opts.global ? readGlobalPkg(opts.globalPath) : readPkgUp({ cwd: opts.cwd }))
|
||||
const root = normalizePath(pkg.path ? path.dirname(pkg.path) : opts.cwd)
|
||||
const store = resolveStorePath(opts.storePath, root)
|
||||
const storeJsonCtrl = storeJsonController(store)
|
||||
const storeJson = storeJsonCtrl.read()
|
||||
const storeJson = readStoreJson(store)
|
||||
if (storeJson) {
|
||||
failIfNotCompatible(storeJson.pnpm)
|
||||
}
|
||||
@@ -35,12 +32,7 @@ export default async function (opts: StrictPnpmOptions): Promise<PnpmContext> {
|
||||
pkg: pkg.pkg,
|
||||
root,
|
||||
store,
|
||||
storeJson: storeJson || {
|
||||
pnpm: pnpmPkgJson.version,
|
||||
dependents: {},
|
||||
dependencies: {}
|
||||
},
|
||||
storeJsonCtrl
|
||||
storeJson
|
||||
}
|
||||
|
||||
if (!opts.quiet) initLogger(opts.logger)
|
||||
|
||||
@@ -21,6 +21,7 @@ import {Got} from '../network/got'
|
||||
import pnpmPkgJson from '../pnpmPkgJson'
|
||||
import lock from './lock'
|
||||
import {StoreJson} from '../fs/storeJsonController'
|
||||
import {save as saveStoreJson} from '../fs/storeJsonController'
|
||||
|
||||
export type PackageInstallationResult = {
|
||||
path: string,
|
||||
@@ -100,7 +101,7 @@ async function installInContext (installType: string, packagesToInstall: Depende
|
||||
}
|
||||
}
|
||||
|
||||
ctx.storeJsonCtrl.save(Object.assign(ctx.storeJson, {
|
||||
saveStoreJson(ctx.store, Object.assign(ctx.storeJson, {
|
||||
pnpm: pnpmPkgJson.version
|
||||
}))
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import requireJson from '../fs/requireJson'
|
||||
import {PnpmOptions, StrictPnpmOptions, Package} from '../types'
|
||||
import {StoreJson} from '../fs/storeJsonController'
|
||||
import lock from './lock'
|
||||
import {save as saveStoreJson} from '../fs/storeJsonController'
|
||||
|
||||
export default async function uninstallCmd (pkgsToUninstall: string[], maybeOpts?: PnpmOptions) {
|
||||
const opts = extendOptions(maybeOpts)
|
||||
@@ -47,7 +48,7 @@ export async function uninstallInContext (pkgsToUninstall: string[], pkg: Packag
|
||||
}
|
||||
await Promise.all(uninstalledPkgs.map(pkgId => removePkgFromStore(pkgId, ctx.store)))
|
||||
|
||||
ctx.storeJsonCtrl.save(ctx.storeJson)
|
||||
saveStoreJson(ctx.store, ctx.storeJson)
|
||||
await Promise.all(pkgsToUninstall.map(dep => rimraf(path.join(ctx.root, 'node_modules', dep))))
|
||||
|
||||
const saveType = getSaveType(opts)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import path = require('path')
|
||||
import fs = require('fs')
|
||||
import pnpmPkgJson from '../pnpmPkgJson'
|
||||
|
||||
export type StoreDependents = {
|
||||
[name: string]: string[]
|
||||
@@ -17,24 +18,20 @@ export type StoreJson = {
|
||||
dependencies: StoreDependencies
|
||||
}
|
||||
|
||||
export type StoreJsonCtrl = {
|
||||
read(): StoreJson,
|
||||
save(storeJson: StoreJson): void
|
||||
}
|
||||
|
||||
export default function storeJsonController (storePath: string): StoreJsonCtrl {
|
||||
export function read (storePath: string) {
|
||||
const storeJsonPath = path.join(storePath, 'store.json')
|
||||
|
||||
return {
|
||||
read () {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(storeJsonPath, 'utf8'))
|
||||
} catch (err) {
|
||||
return null
|
||||
}
|
||||
},
|
||||
save (storeJson: StoreJson) {
|
||||
fs.writeFileSync(storeJsonPath, JSON.stringify(storeJson, null, 2), 'utf8')
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(storeJsonPath, 'utf8'))
|
||||
} catch (err) {
|
||||
return {
|
||||
pnpm: pnpmPkgJson.version,
|
||||
dependents: {},
|
||||
dependencies: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function save (storePath: string, storeJson: StoreJson) {
|
||||
const storeJsonPath = path.join(storePath, 'store.json')
|
||||
fs.writeFileSync(storeJsonPath, JSON.stringify(storeJson, null, 2), 'utf8')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user