-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
91 lines (81 loc) · 2.54 KB
/
index.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
88
89
90
91
var fs = require('fs')
var xlsx = require('node-xlsx')
var commandLineArgs = require('command-line-args')
var mkdirp = require('mkdirp')
var async = require('async')
var optionDefinitions = [
{ name: 'excel_filename', alias: 'f', type: String },
{ name: 'input_folder', alias: 'i', type: String },
{ name: 'output_folder', alias: 'o', type: String },
{ name: 'file_ending', alias: 'e', type: String }
]
var options = commandLineArgs(optionDefinitions)
var read_excel = function (excel_filename) {
var workSheets = xlsx.parse(excel_filename)
return workSheets
}
if (!options.excel_filename) {
console.error('No excel_filename')
return
}
var create_folder = function (folder_name, callback) {
mkdirp(folder_name, callback)
}
function copy_file(source, target, cb) {
var cbCalled = false
var rd = fs.createReadStream(source)
rd.on("error", function(err) {
done(err)
})
var wr = fs.createWriteStream(target)
wr.on("error", function(err) {
done(err)
})
wr.on("close", function(ex) {
done()
})
rd.pipe(wr)
function done(err) {
if (!cbCalled) {
cb(err)
cbCalled = true
}
}
}
var move_files = function (options, callback) {
var input_folder = options.input_folder || '.'
var output_folder = options.output_folder || '.'
var excel_filename = options.excel_filename
var file_ending = options.file_ending || 'mp4'
if (!excel_filename) throw new Error('file not found')
var workSheets = read_excel(options.excel_filename)
console.log('excel data: ', JSON.stringify(workSheets))
if (!workSheets || !workSheets.length || !workSheets[0].data) {
throw new Error('problem with file')
}
async.each(workSheets[0].data, function (row, cb) {
var name = row[0]
var date = row[1]
var sign = row[2]
var out_folder = output_folder + '/' + name + '_' + date + '_' + sign
create_folder(out_folder, function (err) {
if (err) return cb(err)
async.waterfall([
function (cb) {
copy_file(input_folder + '/names/' + name + '.' + file_ending, out_folder + '/' + name + '.' + file_ending, cb)
},
function (cb) {
copy_file(input_folder + '/dates/' + date + '.' + file_ending, out_folder + '/' + date + '.' + file_ending, cb)
},
function (cb) {
copy_file(input_folder + '/signs/' + sign + '.' + file_ending, out_folder + '/' + sign + '.' + file_ending, cb)
}
], cb)
})
}, callback)
}
move_files(options, function (err) {
if (err) return console.error('!!!', err)
console.log('Done!')
process.exit(0)
})