6
6
import crypto from 'crypto' ;
7
7
import fs from 'fs' ;
8
8
import path from 'path' ;
9
+ import recursiveFs from 'recursive-fs' ;
9
10
import stream from 'stream' ;
10
11
11
- // Do not throw an exception if either of these modules are missing, as they may not be needed by the
12
- // consumer of this file.
13
- // - recursiveFs: Only required for hashing of directories
14
- try {
15
- var recursiveFs = require ( 'recursive-fs' ) ;
16
- } catch ( e ) { }
17
-
18
12
const HASH_ALGORITHM = 'sha256' ;
19
13
20
14
export function generatePackageHashFromDirectory (
@@ -36,44 +30,39 @@ export function generatePackageManifestFromDirectory(
36
30
directoryPath : string ,
37
31
basePath : string ,
38
32
) : Promise < PackageManifest > {
39
- return new Promise < PackageManifest > ( ( resolve , reject ) => {
33
+ return new Promise < PackageManifest > ( async ( resolve , reject ) => {
40
34
var fileHashesMap = new Map < string , string > ( ) ;
41
35
42
- recursiveFs . readdirr (
43
- directoryPath ,
44
- ( error ?: any , directories ?: string [ ] , files ?: string [ ] ) : void => {
45
- if ( error ) {
46
- reject ( error ) ;
47
- return ;
48
- }
49
-
50
- if ( ! files || files . length === 0 ) {
51
- reject ( "Error: Can't sign the release because no files were found." ) ;
52
- return ;
53
- }
36
+ try {
37
+ const { files } = await recursiveFs . read ( directoryPath ) ;
38
+ if ( ! files || files . length === 0 ) {
39
+ reject ( "Error: Can't sign the release because no files were found." ) ;
40
+ return ;
41
+ }
54
42
55
- // Hash the files sequentially, because streaming them in parallel is not necessarily faster
56
- var generateManifestPromise : Promise < void > = files . reduce (
57
- ( soFar : Promise < void > , filePath : string ) => {
58
- return soFar . then ( ( ) => {
59
- var relativePath : string = PackageManifest . normalizePath (
60
- path . relative ( basePath , filePath ) ,
61
- ) ;
62
- if ( ! PackageManifest . isIgnored ( relativePath ) ) {
63
- return hashFile ( filePath ) . then ( ( hash : string ) => {
64
- fileHashesMap . set ( relativePath , hash ) ;
65
- } ) ;
66
- }
67
- } ) ;
68
- } ,
69
- Promise . resolve ( null as void ) ,
70
- ) ;
71
-
72
- generateManifestPromise . then ( ( ) => {
73
- resolve ( new PackageManifest ( fileHashesMap ) ) ;
74
- } , reject ) ;
75
- } ,
76
- ) ;
43
+ // Hash the files sequentially, because streaming them in parallel is not necessarily faster
44
+ var generateManifestPromise : Promise < void > = files . reduce (
45
+ ( soFar : Promise < void > , filePath : string ) => {
46
+ return soFar . then ( ( ) => {
47
+ var relativePath : string = PackageManifest . normalizePath (
48
+ path . relative ( basePath , filePath ) ,
49
+ ) ;
50
+ if ( ! PackageManifest . isIgnored ( relativePath ) ) {
51
+ return hashFile ( filePath ) . then ( ( hash : string ) => {
52
+ fileHashesMap . set ( relativePath , hash ) ;
53
+ } ) ;
54
+ }
55
+ } ) ;
56
+ } ,
57
+ Promise . resolve ( null as void ) ,
58
+ ) ;
59
+
60
+ generateManifestPromise . then ( ( ) => {
61
+ resolve ( new PackageManifest ( fileHashesMap ) ) ;
62
+ } , reject ) ;
63
+ } catch ( error ) {
64
+ reject ( error ) ;
65
+ }
77
66
} ) ;
78
67
}
79
68
0 commit comments