1
1
import fs from 'fs/promises' ;
2
2
import path from 'path' ;
3
+ import { availableParallelism } from 'os' ;
4
+ import readline from 'readline' ;
3
5
4
6
import sharp from 'sharp' ;
5
7
import ora from 'ora' ;
6
- import promptSync from 'prompt-sync' ;
7
8
import dotenv from 'dotenv' ;
8
9
import { DateTime } from 'luxon' ;
9
10
10
11
import WorkerPromise , { activeWorkerCount , workerEvents } from './worker-promise.mjs' ;
11
12
12
13
dotenv . config ( ) ;
13
- const prompt = promptSync ( ) ;
14
+ const prompt = ( textPrompt , defaultValue ) => {
15
+ return new Promise ( ( resolve ) => {
16
+ const input = readline . createInterface ( {
17
+ input : process . stdin ,
18
+ output : process . stdout
19
+ } ) ;
20
+ input . question ( textPrompt , response => {
21
+ input . close ( ) ;
22
+ if ( response === '' ) {
23
+ response = undefined ;
24
+ }
25
+ resolve ( response ?? String ( defaultValue ) ) ;
26
+ } ) ;
27
+ } ) ;
28
+ } ;
14
29
15
30
let imagePath = process . env . IMAGE_PATH || process . argv [ 2 ] ;
16
31
if ( ! imagePath ) {
@@ -19,7 +34,7 @@ if (!imagePath) {
19
34
}
20
35
const minTileSize = process . env . MIN_TILE_SIZE || 100 ;
21
36
const maxTileSize = process . env . MAX_TILE_SIZE || 400 ;
22
- const threadLimit = isNaN ( process . env . THREAD_LIMIT ) ? 8 : parseInt ( process . env . THREAD_LIMIT ) ;
37
+ const threadLimit = isNaN ( process . env . THREAD_LIMIT ) ? availableParallelism ( ) : parseInt ( process . env . THREAD_LIMIT ) ;
23
38
const testOutput = Boolean ( process . env . TEST_OUTPUT || false ) ;
24
39
25
40
async function getTileSettings ( ) {
@@ -45,7 +60,7 @@ async function getTileSettings() {
45
60
for ( let i = 0 ; i < files . length ; i ++ ) {
46
61
console . log ( `${ i + 1 } . ${ files [ i ] } ` ) ;
47
62
}
48
- const index = prompt ( `[1-${ files . length } ]: ` ) ;
63
+ const index = await prompt ( `[1-${ files . length } ]: ` ) ;
49
64
if ( isNaN ( index ) || index < 1 || index > files . length ) {
50
65
return Promise . reject ( new Error ( `${ index } is an invalid input image selection` ) ) ;
51
66
}
@@ -58,7 +73,7 @@ async function getTileSettings() {
58
73
tileSettings . imagePath . lastIndexOf ( path . sep ) + 1 ,
59
74
tileSettings . imagePath . lastIndexOf ( '.' )
60
75
) ;
61
- const newMapName = prompt ( `Output folder (${ tileSettings . mapName } ): ` , tileSettings . mapName ) ;
76
+ const newMapName = await prompt ( `Output folder (${ tileSettings . mapName } ): ` , tileSettings . mapName ) ;
62
77
tileSettings . mapName = newMapName . replace ( ' ' , '_' ) ;
63
78
64
79
let inputImage = sharp ( tileSettings . imagePath , {
@@ -69,8 +84,9 @@ async function getTileSettings() {
69
84
let metadata = await inputImage . metadata ( ) ;
70
85
console . log ( `Image size: ${ metadata . width } x${ metadata . height } ` ) ;
71
86
72
- tileSettings . rotation = prompt ( 'Rotate image 90, 180, or 270 degrees (0): ' , 0 ) . trim ( ) ;
87
+ tileSettings . rotation = await prompt ( 'Rotate image 90, 180, or 270 degrees (0): ' , 0 ) ;
73
88
if ( tileSettings . rotation ) {
89
+ tileSettings . rotation = tileSettings . rotation . trim ( ) ;
74
90
if ( ! [ '0' , '90' , '180' , '270' ] . includes ( tileSettings . rotation ) ) {
75
91
return Promise . reject ( new Error ( `${ tileSettings . rotation } is not a valid rotation` ) ) ;
76
92
}
@@ -152,7 +168,7 @@ async function getTileSettings() {
152
168
} px)`
153
169
) ;
154
170
}
155
- const newTileSize = prompt ( `Tile size (${ tileSize } ): ` , tileSize ) ;
171
+ const newTileSize = await prompt ( `Tile size (${ tileSize } ): ` , tileSize ) ;
156
172
if ( isNaN ( newTileSize ) ) {
157
173
return Promise . reject ( new Error ( `${ newTileSize } is not a valid tile size` ) ) ;
158
174
}
@@ -382,8 +398,8 @@ async function createTiles(options) {
382
398
if ( tileSettings . length > 0 ) {
383
399
console . log ( `Queued tiles: ${ tileSettings . map ( setting => setting . mapName ) . join ( ', ' ) } ` ) ;
384
400
}
385
- const again = prompt ( `Do you want to queue another tileset? (n): ` , 'n' ) . trim ( ) . toLocaleLowerCase ( ) ;
386
- if ( ! again . startsWith ( 'y' ) ) {
401
+ const again = ( await prompt ( `Do you want to queue another tileset? (n): ` , 'n' ) ) ? .trim ( ) . toLocaleLowerCase ( ) ;
402
+ if ( ! again ? .startsWith ( 'y' ) ) {
387
403
break ;
388
404
}
389
405
}
0 commit comments