1
1
const fetch = require ( "node-fetch" ) ;
2
+ const path = require ( "path" ) ;
2
3
const basePath = process . cwd ( ) ;
3
4
const fs = require ( "fs" ) ;
4
5
const readDir = `${ basePath } /build/json` ; // change this directory if you are uploading generic images first in order to do a reveal.
@@ -8,46 +9,48 @@ const TIMEOUT = 1000; // Milliseconds. Extend this if needed to wait for each up
8
9
9
10
const allMetadata = [ ] ;
10
11
12
+ if ( ! fs . existsSync ( path . join ( `${ basePath } /build` , "/ipfsMetas" ) ) ) {
13
+ fs . mkdirSync ( path . join ( `${ basePath } /build` , "ipfsMetas" ) ) ;
14
+ }
15
+
11
16
async function main ( ) {
12
- const ipfsMetasFile = `${ basePath } /build/json/_ipfsMetas.json` ;
13
- fs . access ( ipfsMetasFile , fs . F_OK , async ( err ) => {
14
- if ( err ) {
15
- fs . writeFileSync ( ipfsMetasFile , "[]" )
16
- }
17
-
18
- const ipfsMetas = JSON . parse ( fs . readFileSync ( ipfsMetasFile ) ) ;
19
- const files = fs . readdirSync ( readDir ) ;
20
- files . sort ( function ( a , b ) {
21
- return a . split ( "." ) [ 0 ] - b . split ( "." ) [ 0 ] ;
22
- } ) ;
23
- for ( const file of files ) {
24
- if ( file !== "_metadata.json" && file !== "_ipfsMetas.json" ) {
25
- let jsonFile = fs . readFileSync ( `${ readDir } /${ file } ` ) ;
26
- let metaData = JSON . parse ( jsonFile ) ;
27
-
28
- const uploadedMeta = ipfsMetas . find ( item => {
29
- return item . custom_fields . edition === metaData . custom_fields . edition ;
30
- } )
31
-
32
- if ( ! uploadedMeta ) {
33
- try {
34
- const response = await fetchWithRetry ( jsonFile ) ;
35
- allMetadata . push ( response ) ;
36
- console . log ( `${ response . name } metadata uploaded!` ) ;
37
- } catch ( err ) {
38
- console . log ( `Catch: ${ err } ` )
39
- }
17
+ const files = fs . readdirSync ( readDir ) ;
18
+ files . sort ( function ( a , b ) {
19
+ return a . split ( "." ) [ 0 ] - b . split ( "." ) [ 0 ] ;
20
+ } ) ;
21
+ for ( const file of files ) {
22
+ if ( file !== "_metadata.json" && file !== "_ipfsMetas.json" ) {
23
+ let jsonFile = fs . readFileSync ( `${ readDir } /${ file } ` ) ;
24
+ let metaData = JSON . parse ( jsonFile ) ;
25
+ const uploadedMeta = `${ basePath } /build/ipfsMetas/${ metaData . custom_fields . edition } .json` ;
26
+
27
+ try {
28
+ fs . accessSync ( uploadedMeta ) ;
29
+ const uploadedMetaFile = fs . readFileSync ( uploadedMeta )
30
+ if ( uploadedMetaFile . length > 0 ) {
31
+ const ipfsMeta = JSON . parse ( uploadedMetaFile )
32
+ if ( ipfsMeta . response !== "OK" ) throw 'metadata not uploaded'
33
+ allMetadata . push ( ipfsMeta ) ;
34
+ console . log ( `${ metaData . name } metadata already uploaded` ) ;
40
35
} else {
41
- allMetadata . push ( uploadedMeta ) ;
42
- console . log ( `${ uploadedMeta . name } metadata already uploaded` ) ;
36
+ throw 'metadata not uploaded'
37
+ }
38
+ } catch ( err ) {
39
+ try {
40
+ const response = await fetchWithRetry ( jsonFile ) ;
41
+ allMetadata . push ( response ) ;
42
+ writeResponseMetaData ( response )
43
+ console . log ( `${ response . name } metadata uploaded!` ) ;
44
+ } catch ( err ) {
45
+ console . log ( `Catch: ${ err } ` )
43
46
}
44
47
}
45
48
}
46
49
fs . writeFileSync (
47
- `${ basePath } /build/json /_ipfsMetas.json` ,
50
+ `${ basePath } /build/ipfsMetas /_ipfsMetas.json` ,
48
51
JSON . stringify ( allMetadata , null , 2 )
49
52
) ;
50
- } )
53
+ }
51
54
}
52
55
53
56
main ( ) ;
@@ -103,3 +106,7 @@ async function fetchWithRetry(file) {
103
106
return fetch_retry ( file ) ;
104
107
} ) ;
105
108
}
109
+
110
+ const writeResponseMetaData = ( _data ) => {
111
+ fs . writeFileSync ( `${ basePath } /build/ipfsMetas/${ _data . custom_fields . edition } .json` , JSON . stringify ( _data , null , 2 ) ) ;
112
+ } ;
0 commit comments