1
- const fs = require ( 'fs-extra' )
2
- const axios = require ( 'axios' )
3
- const inquirer = require ( 'inquirer' )
1
+ import fs from 'fs-extra'
2
+ import axios from 'axios'
3
+ import inquirer from 'inquirer'
4
4
5
- const logger = require ( './logger' )
5
+ import * as logger from './logger'
6
6
7
- exports . download = async function download ( apiPath , dest ) {
7
+ export async function download ( apiPath : string , dest : string ) : Promise < void > {
8
8
logger . Assets . info ( 'DOWNLOAD' , apiPath )
9
- const { data} = await axios . get ( apiPath , { responseType : 'stream' } )
10
- . catch ( err => logger . Assets . error ( 'DOWNLOAD' , apiPath , err . toJSON ( ) ) )
11
- data . pipe ( fs . createWriteStream ( dest ) )
9
+ const { data} = await axios . get < fs . ReadStream > ( apiPath , { responseType : 'stream' } )
10
+ . catch ( err => {
11
+ logger . Assets . error ( 'DOWNLOAD' , apiPath , err . toJSON ( ) )
12
+ return { data : null }
13
+ } )
14
+ data ?. pipe ( fs . createWriteStream ( dest ) )
12
15
}
13
16
14
- exports . writeJSON = function writeJSON ( dest , data ) {
17
+ export function writeJSON ( dest : string , data : never ) : void {
15
18
fs . writeFile ( dest , JSON . stringify ( data ) , ( err ) => {
16
19
if ( err ) {
17
20
logger . WriteJSON . error ( 'CREATE' , dest , err )
@@ -21,11 +24,11 @@ exports.writeJSON = function writeJSON (dest, data) {
21
24
} )
22
25
}
23
26
24
- exports . mergeStats = function mergeStats ( data ) {
25
- let merged = { }
27
+ export function mergeStats ( data : McPlayerStatsJson ) : McPlayerStatsJson {
28
+ const merged : McPlayerStatsJson = { }
26
29
if ( Object . prototype . hasOwnProperty . call ( data , 'stats' ) ) {
27
- for ( let key in data . stats ) {
28
- for ( let s in data . stats [ key ] ) {
30
+ for ( const key in data . stats ) {
31
+ for ( const s in data . stats [ key ] ) {
29
32
merged [ key + '/' + s ] = data . stats [ key ] [ s ]
30
33
}
31
34
}
@@ -34,18 +37,18 @@ exports.mergeStats = function mergeStats (data) {
34
37
return data
35
38
}
36
39
37
- exports . defaultSkin = function defaultSkin ( uuid ) {
40
+ export function defaultSkin ( uuid : LongUuid ) : string {
38
41
// great thanks to Minecrell for research into Minecraft and Java's UUID hashing!
39
42
// https://git.io/xJpV
40
43
// MC uses `uuid.hashCode() & 1` for alex
41
44
// that can be compacted to counting the LSBs of every 4th byte in the UUID
42
45
// an odd sum means alex, an even sum means steve
43
46
// XOR-ing all the LSBs gives us 1 for alex and 0 for steve
44
- const isEven = ( c ) => {
47
+ const isEven = ( c : string ) => {
45
48
if ( c >= '0' && c <= '9' ) {
46
- return ( c & 1 ) === 0
49
+ return ( Number ( c ) & 1 ) === 0
47
50
} else if ( c >= 'a' && c <= 'f' ) {
48
- return ( c & 1 ) === 1
51
+ return ( Number ( c ) & 1 ) === 1
49
52
}
50
53
console . log ( 'Invalid digit' , c )
51
54
return null
@@ -55,10 +58,10 @@ exports.defaultSkin = function defaultSkin (uuid) {
55
58
return lsbsEven ? 'Alex' : 'Steve'
56
59
}
57
60
58
- exports . confirm = async function confirm ( message , _default = true ) {
61
+ export async function confirm ( message : string , _default = true ) : Promise < boolean > {
59
62
const prompt = inquirer . createPromptModule ( )
60
63
try {
61
- const res = await prompt ( {
64
+ const res = await prompt < { confirm : boolean } > ( {
62
65
type : 'confirm' ,
63
66
name : 'confirm' ,
64
67
default : _default ,
@@ -71,6 +74,6 @@ exports.confirm = async function confirm (message, _default = true) {
71
74
}
72
75
}
73
76
74
- exports . delay = function delay ( ms ) {
77
+ export function delay ( ms : number ) : Promise < void > {
75
78
return new Promise ( resolve => setTimeout ( resolve , ms ) )
76
79
}
0 commit comments