fix: install pkg when restore data

This commit is contained in:
Innei
2022-05-26 10:41:05 +08:00
parent 127d909609
commit e3a6d4b18a
2 changed files with 29 additions and 4 deletions

View File

@@ -18,7 +18,7 @@ import { BACKUP_DIR, DATA_DIR } from '~/constants/path.constant'
import { CacheService } from '~/processors/cache/cache.service'
import { EventManagerService } from '~/processors/helper/helper.event.service'
import { getMediumDateTime } from '~/utils'
import { getFolderSize } from '~/utils/system.util'
import { getFolderSize, installPKG } from '~/utils/system.util'
import { ConfigsService } from '../configs/configs.service'
@@ -188,7 +188,9 @@ export class BackupService {
}
// 还原 backup_data
const backupDataDirFilenames = await readdir(join(dirPath, 'backup_data'))
const backupDataDir = join(dirPath, 'backup_data')
const backupDataDirFilenames = await readdir(backupDataDir)
await Promise.all(
backupDataDirFilenames.map(async (filename) => {
@@ -201,11 +203,28 @@ export class BackupService {
}),
)
try {
const packageJson = await readFile(join(backupDataDir, 'package.json'), {
encoding: 'utf-8',
})
const pkg = JSON.parse(packageJson)
if (pkg.dependencies) {
await Promise.all(
Object.entries(pkg.dependencies).map(([name, version]) => {
this.logger.log(`--> 安装依赖 ${name}@${version}`)
return installPKG(`${name}@${version}`, DATA_DIR).catch((er) => {
this.logger.error(`--> 依赖安装失败:${er.message}`)
})
}),
)
}
} catch (er) {}
await Promise.all([
this.cacheService.cleanAllRedisKey(),
this.cacheService.cleanCatch(),
])
await rm(join(dirPath, 'backup_data'), { force: true, recursive: true })
await rm(backupDataDir, { force: true, recursive: true })
}
async rollbackTo(dirname: string) {

View File

@@ -77,7 +77,13 @@ export const installPKG = async (name: string, cwd: string) => {
}
}
if (!manager) {
throw new Error('No package manager found')
// fallback to npm
const npmVersion = await nothrow($`npm -v`)
if (npmVersion.exitCode === 0) {
manager = 'npm'
} else {
throw new Error('No package manager found')
}
}
cd(cwd)
await $`${manager} ${INSTALL_COMMANDS[manager]} ${name}`