-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·87 lines (67 loc) · 2.49 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env node
const fs = require('fs'),
path = require('path'),
{ promisify } = require('util')
yargs = require('yargs'),
colors = require('colors')
const mkdirAsync = promisify(fs.mkdir)
const ShardingWatcher = require('./index.js')
const fixedLength = (string, length) => string.length > length ? `${string.slice(0, -3)}...` : string.padEnd(length),
fileArg = file => path.isAbsolute(file) ? file : path.resolve(process.cwd(), file)
let {source, destination, isolate, verbose} = yargs
.alias('source', 's')
.describe('source', 'A path pointing to the sys42.js file to shard.')
.coerce('source', fileArg)
.demandOption('source')
.alias('destination', 'd')
.describe('destination', 'A path pointing to the directory in which to place the sharded files.')
.coerce('destination', fileArg)
.demandOption('source')
.alias('isolate', 'i')
.describe('isolate', 'Whether or not to create a directory in the destination directory to place the sharded files in.')
.boolean('isolate')
.alias('verbose', 'v')
.describe('verbose', 'Print everything the sharder is doing to the console.')
.boolean('verbose')
.argv
const log = verbose ? console.log : () => {}
;(async () => {
if(isolate) {
destination = path.join(destination, 'sys42')
if(verbose) console.log('Creating directory to place files in...'.dim)
await mkdirAsync(destination)
}
let progress
const log = value => {
message = fixedLength(progress.title, 24).bold
+ ` ${parseInt((progress.current / progress.total) * 100).toString()}% `.padEnd(5).yellow
+ ` [ ${fixedLength(value, 24)} ]`
console.log(message)
progress.current++
}
const newOperation = title => value => {
progress = {
total: value,
current: 0,
title
}
}
const shards = await new Promise((resolve, reject) =>
new ShardingWatcher(source, destination, verbose)
.on('error', console.error)
.on('ready', () => verbose && console.log(`Sharding ${source}...`))
.on('log', (name, value) => ({
'startShardSearching': newOperation('Finding shards...'),
'shardFound': log,
'startDirCreation': newOperation('Creating directories...'),
'dirCreation': log,
'startFileCreation': newOperation('Creating files...'),
'fileCreation': log
})[name](value))
.on('close', resolve)
)
if(verbose) {
console.log(`Created ${shards.size} shards!`.dim)
console.log('Done!'.green)
}
})()