|
| 1 | +import { resolve } from 'node:path' |
| 2 | + |
| 3 | +import { |
| 4 | + existsSync, |
| 5 | + outputFile, |
| 6 | + readFile, |
| 7 | +} from 'fs-extra' |
| 8 | +import readYaml from 'read-yaml-file' |
| 9 | +import writeYaml from 'write-yaml-file' |
| 10 | + |
| 11 | +import { version } from '../../../package.json' |
| 12 | +import { |
| 13 | + LOCK_FILE, |
| 14 | + STORE_PACKAGES_DIR, |
| 15 | + STORE_PATH, |
| 16 | +} from './constants' |
| 17 | +import { computeCacheKey } from './utils' |
| 18 | +import { debug } from '@/common/log' |
| 19 | + |
| 20 | +interface Options { |
| 21 | + storePath?: string |
| 22 | +} |
| 23 | + |
| 24 | +interface ResolvedLockFileOptions { |
| 25 | + /** |
| 26 | + * @description Virtual store path |
| 27 | + * @default <homedir>/<.vsit-store> |
| 28 | + */ |
| 29 | + storePath: string |
| 30 | + /** |
| 31 | + * @description Virtual store lock file path |
| 32 | + * @default <homedir>/<.vsit-store>/vsit-lock.yaml |
| 33 | + */ |
| 34 | + lockFilePath: string |
| 35 | +} |
| 36 | + |
| 37 | +export interface Package { |
| 38 | + /** |
| 39 | + * @description Encoded url |
| 40 | + */ |
| 41 | + id: string |
| 42 | + /** |
| 43 | + * @description Remote package url |
| 44 | + */ |
| 45 | + url: string |
| 46 | + /** |
| 47 | + * @description Dependent packages |
| 48 | + * @todo not used currently, in the future, we will outdated the persist cache, and concurrent download the packages based on deps |
| 49 | + */ |
| 50 | + deps?: string[] |
| 51 | +} |
| 52 | + |
| 53 | +interface LockFileYaml { |
| 54 | + version: string |
| 55 | + packages?: Record<string, Package> |
| 56 | +} |
| 57 | + |
| 58 | +export class LockFile { |
| 59 | + options: ResolvedLockFileOptions |
| 60 | + lockFile: LockFileYaml = { version } |
| 61 | + constructor(options: Options) { |
| 62 | + this.options = this.resolveOptions(options) |
| 63 | + debug.store('resolved lock-file options %o', this.options) |
| 64 | + } |
| 65 | + |
| 66 | + resolveOptions(options: Options): ResolvedLockFileOptions { |
| 67 | + const resolvedStorePath = options.storePath ?? STORE_PATH |
| 68 | + return { |
| 69 | + storePath: resolvedStorePath, |
| 70 | + lockFilePath: resolve(resolvedStorePath, LOCK_FILE), |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + async init() { |
| 75 | + if (!existsSync(this.options.lockFilePath)) { |
| 76 | + await writeYaml(this.options.lockFilePath, { version }) |
| 77 | + } |
| 78 | + await this.read() |
| 79 | + } |
| 80 | + |
| 81 | + async read(): Promise<LockFileYaml> { |
| 82 | + this.lockFile = await readYaml(this.options.lockFilePath) |
| 83 | + return this.lockFile as LockFileYaml |
| 84 | + } |
| 85 | + |
| 86 | + async write(data: LockFileYaml): Promise<void> { |
| 87 | + await writeYaml(this.options.lockFilePath, data) |
| 88 | + } |
| 89 | + |
| 90 | + async save() { |
| 91 | + await writeYaml(this.options.lockFilePath, this.lockFile) |
| 92 | + } |
| 93 | + |
| 94 | + async writePackage(url: string, deps: string[] = []) { |
| 95 | + const id = computeCacheKey(url) |
| 96 | + this.lockFile.packages = { |
| 97 | + ...this.lockFile.packages, |
| 98 | + [id]: { |
| 99 | + id, |
| 100 | + url, |
| 101 | + deps, |
| 102 | + }, |
| 103 | + } |
| 104 | + await this.save() |
| 105 | + } |
| 106 | + |
| 107 | + async writePackages(packages: Record<string, Package> = {}) { |
| 108 | + this.lockFile.packages = { |
| 109 | + ...this.lockFile.packages, |
| 110 | + ...packages, |
| 111 | + } |
| 112 | + await this.save() |
| 113 | + } |
| 114 | + |
| 115 | + static async create(options: Options) { |
| 116 | + const instance = new LockFile(options) |
| 117 | + await instance.init() |
| 118 | + return instance |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +interface ResolvedCacheOptions { |
| 123 | + storePath: string |
| 124 | + packagesPath: string |
| 125 | +} |
| 126 | + |
| 127 | +export class PersistCache { |
| 128 | + options: ResolvedCacheOptions |
| 129 | + lockFile?: LockFile |
| 130 | + constructor(options: Options = { storePath: STORE_PATH }) { |
| 131 | + this.options = this.resolveOptions(options) |
| 132 | + this.lockFile = undefined |
| 133 | + } |
| 134 | + |
| 135 | + resolveOptions(options: Options): ResolvedCacheOptions { |
| 136 | + const resolvedStorePath = options.storePath ?? STORE_PATH |
| 137 | + return { |
| 138 | + storePath: resolvedStorePath, |
| 139 | + packagesPath: resolve(resolvedStorePath, STORE_PACKAGES_DIR), |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + static async create(options: Options = { storePath: STORE_PATH }) { |
| 144 | + const instance = new PersistCache(options) |
| 145 | + instance.lockFile = await LockFile.create(options) |
| 146 | + return instance |
| 147 | + } |
| 148 | + |
| 149 | + async writePackages(packages: Record<string, Package> = {}) { |
| 150 | + this.lockFile?.writePackages(packages) |
| 151 | + } |
| 152 | + |
| 153 | + async getCache(url: string) { |
| 154 | + const id = computeCacheKey(url) |
| 155 | + const path = resolve(this.options.packagesPath, id) |
| 156 | + if (existsSync(path)) { |
| 157 | + const content = (await readFile(path)).toString('utf-8') |
| 158 | + return content |
| 159 | + } |
| 160 | + return '' |
| 161 | + } |
| 162 | + |
| 163 | + async saveCache(url: string, content: string) { |
| 164 | + const id = computeCacheKey(url) |
| 165 | + const path = resolve(this.options.packagesPath, id) |
| 166 | + await this.lockFile?.writePackage(url) |
| 167 | + await outputFile(path, content) |
| 168 | + } |
| 169 | +} |
0 commit comments