From 7ac987ea06b6713291a89c67fa4397adfd2a16ca Mon Sep 17 00:00:00 2001 From: zkochan Date: Sun, 5 Feb 2017 15:43:10 +0200 Subject: [PATCH] feat: save the original shasum of each package in the store Rel #585 --- package.json | 1 + src/fs/dirsum.ts | 10 ++++++++++ src/install/fetch.ts | 15 +++++++++++++++ typings/local.d.ts | 5 +++++ 4 files changed, 31 insertions(+) create mode 100644 src/fs/dirsum.ts diff --git a/package.json b/package.json index 48501fac49..e42234a0dc 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "common-tags": "1.4.0", "cross-spawn": "^5.0.0", "delocalize-dependencies": "0.1.0", + "dirsum": "0.1.1", "execa": "0.6.0", "exists-file": "3.0.0", "find-up": "2.1.0", diff --git a/src/fs/dirsum.ts b/src/fs/dirsum.ts new file mode 100644 index 0000000000..f5090c0600 --- /dev/null +++ b/src/fs/dirsum.ts @@ -0,0 +1,10 @@ +import dirsum = require('dirsum') + +export default function (dirpath: string): Promise { + return new Promise((resolve, reject) => { + dirsum.digest(dirpath, 'sha1', (err: Error, hashes: {hash: string}) => { + if (err) return reject(err) + resolve(hashes.hash) + }) + }) +} diff --git a/src/install/fetch.ts b/src/install/fetch.ts index 7da810c672..ac06935da8 100644 --- a/src/install/fetch.ts +++ b/src/install/fetch.ts @@ -16,6 +16,7 @@ import {InstallContext} from '../api/install' import fetchResolution from './fetchResolution' import logStatus from '../logging/logInstallStatus' import {PackageMeta} from '../resolve/utils/loadPackageMeta' +import dirsum from '../fs/dirsum' export type FetchOptions = { keypath?: string[], @@ -198,7 +199,21 @@ function fetchToStoreCached (opts: FetchToStoreOptions): Promise { // fs.rename(oldPath, newPath) is an atomic operation, so we do it at the // end await fs.rename(targetStage, target) + + createShasum(target) } const pkg = await requireJson(path.join(target, 'package.json')) }) } + +async function createShasum(dirPath: string) { + try { + const shasum = await dirsum(dirPath) + await fs.writeFile(`${dirPath}_shasum`, shasum, 'utf8') + } catch (err) { + logger.error({ + message: `Failed to calculate shasum for ${dirPath}`, + err, + }) + } +} diff --git a/typings/local.d.ts b/typings/local.d.ts index c0ce228983..87f3be8af0 100644 --- a/typings/local.d.ts +++ b/typings/local.d.ts @@ -232,3 +232,8 @@ declare module 'p-limit' { const anything: any; export = anything; } + +declare module 'dirsum' { + const anything: any; + export = anything; +}