-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
100 lines (93 loc) · 2.88 KB
/
start.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
92
93
94
95
96
97
98
99
100
const fs = require('fs');
const path = require('path');
const { fdir } = require('fdir');
const logUpdate = require('log-update');
const chalk = require('chalk');
require('log-timestamp')('ARCHIVIST');
// const ftp = '//slcprodftp01/ftp/';
const ftp = '//slcbartender01/PrintToBartender';
const regEx = new RegExp(/archive$/i);
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
console.log('Begining Archive Process');
const crawler = new fdir().withBasePath().withDirs().withMaxDepth(4);
const archives = crawler
.crawl(ftp)
.sync()
.filter((path) => {
return path.match(regEx);
});
console.log(chalk.cyan(`got ${archives.length} archive folders`));
archives.forEach((archive, i) => {
//only act on folders, where the folder containes files
//filter to folders
let files = fs.readdirSync(archive, { withFileTypes: true }).filter((file) => file.isFile());
console.log(chalk.cyan(`found ${files.length} in archive ${i + 1} folder`));
//limit to folders with files
if (files.length == 0) return;
//identify file dates
console.log('Getting File Stats...');
let fileStats = files.map((file, i) => {
logUpdate(`Reading file: ${i}`);
let stat = fs.statSync(path.join(archive, file.name));
let date = {
day: stat.ctime.getDate().toString(),
month: months[stat.ctime.getMonth()],
year: stat.ctime.getFullYear().toString(),
};
return { file, stat, date };
});
console.log('Got File Stats');
console.log('Reducing Dates...');
let dates = fileStats.reduce((dates, file, i, stats) => {
const { date } = file;
prevDate = dates.find((d) => {
return d.day === date.day && d.month === date.month && d.year === date.year;
});
if (prevDate) return dates;
dates.push(date);
return dates;
}, []);
//create file date folders if not exists
console.log('checking for date folders');
dates.forEach((date) => {
let year = path.join(archive, date.year);
let month = path.join(archive, date.year, date.month);
let day = path.join(archive, date.year, date.month, date.day);
if (!fs.existsSync(year)) {
fs.mkdirSync(year);
console.log(chalk.red(`creating folder ${year}`));
}
if (!fs.existsSync(month)) {
fs.mkdirSync(month);
console.log(chalk.yellow(`creating folder ${month}`));
}
if (!fs.existsSync(day)) {
fs.mkdirSync(day);
console.log(chalk.green(`creating folder ${day}`));
}
});
//move each file to the proper archive folder
console.log('Begining File Move...');
fileStats.forEach((fileStat, i) => {
logUpdate(`Moving File: ${i}`);
let { file, date, stat } = fileStat;
fs.renameSync(
path.join(archive, file.name),
path.join(archive, date.year, date.month, date.day, file.name)
);
});
console.log('Finished File Move');
});