66import crypto from 'crypto' ;
77import fs from 'fs' ;
88import path from 'path' ;
9+ import recursiveFs from 'recursive-fs' ;
910import stream from 'stream' ;
1011
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-
1812const HASH_ALGORITHM = 'sha256' ;
1913
2014export function generatePackageHashFromDirectory (
@@ -36,44 +30,39 @@ export function generatePackageManifestFromDirectory(
3630 directoryPath : string ,
3731 basePath : string ,
3832) : Promise < PackageManifest > {
39- return new Promise < PackageManifest > ( ( resolve , reject ) => {
33+ return new Promise < PackageManifest > ( async ( resolve , reject ) => {
4034 var fileHashesMap = new Map < string , string > ( ) ;
4135
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+ }
5442
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+ }
7766 } ) ;
7867}
7968
0 commit comments