|
1 |
| -import { resolve, relative, dirname } from 'path' |
2 |
| -import { createReadStream, promises as fsAsync } from 'fs' |
3 |
| -import { createHash } from 'crypto' |
4 |
| - |
5 |
| -import { compareString } from '@dr-js/core/module/common/compare.js' |
6 |
| -import { createAsyncLane, extendAutoSelectLane } from '@dr-js/core/module/common/module/AsyncLane.js' |
| 1 | +import { dirname } from 'path' |
| 2 | +import { promises as fsAsync } from 'fs' |
7 | 3 |
|
8 | 4 | import { calcHash } from '@dr-js/core/module/node/data/Buffer.js'
|
9 |
| -import { setupStreamPipe, readableStreamToBufferAsync } from '@dr-js/core/module/node/data/Stream.js' |
10 |
| -import { PATH_TYPE, getPathStat, getPathTypeFromStat } from '@dr-js/core/module/node/fs/Path.js' |
11 |
| -import { getDirInfoTree, walkDirInfoTreeAsync, createDirectory } from '@dr-js/core/module/node/fs/Directory.js' |
| 5 | +import { createDirectory } from '@dr-js/core/module/node/fs/Directory.js' |
| 6 | +import { describeChecksumOfPathList } from '@dr-js/core/module/node/fs/Checksum.js' |
12 | 7 |
|
13 | 8 | import { packTime, parseTime, loadStat, saveStat } from './function.js'
|
14 | 9 |
|
15 |
| -const getChecksumInfoOfFile = async ( |
16 |
| - absolutePath, // absolute path of file |
17 |
| - fromPath // path will relative to this in output file |
18 |
| -) => [ // [ 'relative/path', 'hash-bash64' ] |
19 |
| - relative(fromPath, absolutePath), |
20 |
| - (await readableStreamToBufferAsync(setupStreamPipe( |
21 |
| - createReadStream(absolutePath), |
22 |
| - createHash('sha1') |
23 |
| - ))).toString('base64') |
24 |
| -] |
25 |
| - |
26 |
| -const getChecksumInfoListOfPath = async ( |
27 |
| - path = '.', // relative or absolute path |
28 |
| - fromPath = process.cwd(), // path will relative to this in output file |
29 |
| - checksumInfoList = [] |
30 |
| -) => { |
31 |
| - path = resolve(fromPath, path) // to absolute path |
32 |
| - const collector = async (filePath) => { checksumInfoList.push(await getChecksumInfoOfFile(filePath, fromPath)) } |
33 |
| - const pathType = getPathTypeFromStat(await getPathStat(path)) |
34 |
| - switch (pathType) { |
35 |
| - case PATH_TYPE.File: |
36 |
| - await collector(path) |
37 |
| - break |
38 |
| - case PATH_TYPE.Directory: { |
39 |
| - const { getTailPromise, pushAuto } = extendAutoSelectLane(createAsyncLane({ laneSize: 4 })) // NOTE: too much or too lane will actually be slower |
40 |
| - await walkDirInfoTreeAsync(await getDirInfoTree(path), async (dirInfo) => { |
41 |
| - dirInfo.type === PATH_TYPE.File && pushAuto(() => collector(dirInfo.path)) |
42 |
| - }) |
43 |
| - await getTailPromise() |
44 |
| - break |
45 |
| - } |
46 |
| - default: |
47 |
| - throw new Error(`invalid pathType: ${pathType} for ${path}`) |
48 |
| - } |
49 |
| - return checksumInfoList |
50 |
| -} |
51 |
| - |
52 |
| -const getChecksumInfoListOfPathList = async ( |
53 |
| - pathList = [], // relative or absolute path |
54 |
| - fromPath = process.cwd(), // path will relative to this in output file |
55 |
| - checksumInfoList = [] |
56 |
| -) => { |
57 |
| - for (const path of pathList) await getChecksumInfoListOfPath(path, fromPath, checksumInfoList) |
58 |
| - return checksumInfoList |
59 |
| -} |
60 |
| - |
61 |
| -const describeChecksumInfoList = (checksumInfoList) => checksumInfoList |
62 |
| - .sort(([ a ], [ b ]) => compareString(a, b))// sort by relativePath so the order is stable |
63 |
| - .map(([ relativePath, hashBase64 ]) => `${hashBase64} ${relativePath}`) // hash first seems better |
64 |
| - .join('\n') |
65 |
| - |
66 |
| -const describeChecksumOfPathList = async ({ |
67 |
| - pathList = [], // relative or absolute path |
68 |
| - fromPath = process.cwd() // path will relative to this in output file |
69 |
| -}) => describeChecksumInfoList(await getChecksumInfoListOfPathList(pathList, fromPath)) |
70 |
| - |
71 | 10 | __DEV__ && console.log({
|
72 | 11 | SAMPLE_CHECKSUM_CONFIG: {
|
73 | 12 | pathStatFile: '/absolute/path/to/stat-file', // path of stat file, used to help detect checksum change
|
@@ -130,13 +69,14 @@ const checksumDetectChange = async (config, isSkipSave = false) => { // set isSk
|
130 | 69 | }
|
131 | 70 |
|
132 | 71 | export {
|
133 |
| - getChecksumInfoOfFile, |
134 |
| - getChecksumInfoListOfPath, |
135 |
| - getChecksumInfoListOfPathList, |
136 |
| - |
137 |
| - describeChecksumInfoList, |
138 |
| - describeChecksumOfPathList, |
139 |
| - |
140 | 72 | loadStatFile, saveStatFile,
|
141 | 73 | checksumUpdate, checksumDetectChange
|
142 | 74 | }
|
| 75 | + |
| 76 | +export { |
| 77 | + getChecksumInfoOfFile, // TODO: DEPRECATE |
| 78 | + getChecksumInfoListOfPath, // TODO: DEPRECATE |
| 79 | + getChecksumInfoListOfPathList, // TODO: DEPRECATE |
| 80 | + describeChecksumInfoList, // TODO: DEPRECATE |
| 81 | + describeChecksumOfPathList // TODO: DEPRECATE |
| 82 | +} from '@dr-js/core/module/node/fs/Checksum.js' |
0 commit comments