1
1
// This script is used to retrieve data generated by `go run generate.go` at backend folder.
2
2
// It will generate registry data that will be used by this script to feed into wrangler dev folder, mimicking a R2 bucket.
3
3
// This script expects data will be at /tmp/registry.
4
- import { createRequire } from "module" ;
5
- const require = createRequire ( import . meta. url ) ;
6
4
const fs = require ( 'fs' ) ;
7
5
const path = require ( 'path' ) ;
8
6
const { exec } = require ( 'node:child_process' ) ;
9
7
10
- const BATCH_SIZE = 10
11
- const DIR_PATH = '/tmp/registry' ;
8
+ const directoryPath = '/tmp/registry' ;
12
9
13
- const putObject = ( directoryPath ) => ( filename ) => {
14
- return new Promise ( ( resolve ) => {
15
- const realFilePath = path . join ( directoryPath , filename ) ;
10
+ fs . readdir ( directoryPath , { recursive : true } , ( err , files ) => {
11
+ if ( err ) {
12
+ console . error ( 'Error reading directory:' , err ) ;
13
+ return ;
14
+ }
15
+
16
+ files . forEach ( fileName => {
17
+ const realFilePath = path . join ( directoryPath , fileName ) ;
16
18
const r2Name = realFilePath . replace ( directoryPath , "" ) ;
17
19
18
20
const cmd = `npx wrangler r2 object put registry-ui-api${ r2Name } --file ${ realFilePath } --local`
@@ -21,23 +23,6 @@ const putObject = (directoryPath) => (filename) => {
21
23
console . log ( stdout ) ;
22
24
console . error ( stderr ) ;
23
25
} ) ;
24
- resolve ( ) ;
25
- } ) ;
26
- }
27
26
28
- const run = async ( ) => {
29
- fs . readdir ( DIR_PATH , { recursive : true } , async ( err , files ) => {
30
- if ( err ) {
31
- console . error ( 'Error reading directory:' , err ) ;
32
- return ;
33
- }
34
-
35
- let i = 1 ;
36
- while ( files . length ) {
37
- await Promise . all ( files . splice ( 0 , BATCH_SIZE ) . map ( putObject ( DIR_PATH ) ) ) ;
38
- i ++ ;
39
- }
40
27
} ) ;
41
- }
42
-
43
- await run ( )
28
+ } ) ;
0 commit comments