11// This script is used to retrieve data generated by `go run generate.go` at backend folder.
22// It will generate registry data that will be used by this script to feed into wrangler dev folder, mimicking a R2 bucket.
33// This script expects data will be at /tmp/registry.
4- import { createRequire } from "module" ;
5- const require = createRequire ( import . meta. url ) ;
64const fs = require ( 'fs' ) ;
75const path = require ( 'path' ) ;
86const { exec } = require ( 'node:child_process' ) ;
97
10- const BATCH_SIZE = 10
11- const DIR_PATH = '/tmp/registry' ;
8+ const directoryPath = '/tmp/registry' ;
129
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 ) ;
1618 const r2Name = realFilePath . replace ( directoryPath , "" ) ;
1719
1820 const cmd = `npx wrangler r2 object put registry-ui-api${ r2Name } --file ${ realFilePath } --local`
@@ -21,23 +23,6 @@ const putObject = (directoryPath) => (filename) => {
2123 console . log ( stdout ) ;
2224 console . error ( stderr ) ;
2325 } ) ;
24- resolve ( ) ;
25- } ) ;
26- }
2726
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- }
4027 } ) ;
41- }
42-
43- await run ( )
28+ } ) ;
0 commit comments