@@ -5,7 +5,7 @@ import { get_client } from './shared'
55/**
66 * Seeds the data recorded in SQL files into the database.
77 */
8- async function seed ( ) {
8+ function seed ( ) {
99 console . info ( '\n--- Seed CatDat database ---' )
1010 const db = get_client ( )
1111 const data_folder = path . join ( process . cwd ( ) , 'databases' , 'catdat' , 'data' )
@@ -22,26 +22,25 @@ async function seed() {
2222 const process_files = db . transaction ( ( ) => {
2323 for ( const folder of subfolders ) {
2424 const folder_path = path . join ( data_folder , folder )
25+
2526 const files = fs
26- . readdirSync ( folder_path , { withFileTypes : true } )
27- . filter ( ( f ) => f . isFile ( ) && f . name . endsWith ( '.sql' ) )
28- . map ( ( f ) => path . join ( folder_path , f . name ) )
27+ . readdirSync ( folder_path , 'utf8' )
28+ . filter ( ( file ) => file . endsWith ( '.sql' ) )
2929 . sort ( )
3030
31- for ( const file of files ) {
32- const base = path . basename ( file )
33- const is_valid = base ?. match ( / ^ [ A - Z a - z 0 - 9 _ . , \- ( ) ] + $ / )
34- if ( ! is_valid ) {
35- throw new Error ( `Invalid file name: ${ base } ` )
36- }
31+ const invalid_file = files . find (
32+ ( file ) => ! file . match ( / ^ [ A - Z a - z 0 - 9 _ . , \- ( ) ] + $ / ) ,
33+ )
34+ if ( invalid_file ) throw new Error ( `Invalid file name: ${ invalid_file } ` )
3735
38- console . info ( `Seed: ${ base } ` )
36+ for ( const file of files ) {
37+ console . info ( `Seed: ${ file } ` )
3938
4039 try {
41- const sql = fs . readFileSync ( file , 'utf8' )
40+ const sql = fs . readFileSync ( path . join ( folder_path , file ) , 'utf8' )
4241 db . exec ( sql )
4342 } catch ( err ) {
44- throw new Error ( `Seed failed in ${ base } : ${ String ( err ) } ` )
43+ throw new Error ( `Seed failed in ${ file } : ${ String ( err ) } ` )
4544 }
4645 }
4746 }
@@ -55,4 +54,4 @@ async function seed() {
5554 }
5655}
5756
58- await seed ( )
57+ seed ( )
0 commit comments