-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-archive
More file actions
executable file
·84 lines (67 loc) · 2.19 KB
/
build-archive
File metadata and controls
executable file
·84 lines (67 loc) · 2.19 KB
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const processCommandLine = () => {
const usage = () => {
console.log('usage: build-archive <path to snapshot directory>');
process.exit(1);
};
const error = msg => {
console.error(msg);
process.exit(1);
};
const args = {
snapshotPath: process.argv[2],
outputFile: 'archive.js'
};
if (!args.snapshotPath) {
return usage();
}
try {
fs.statSync(args.snapshotPath);
} catch (err) {
return error('snapshot directory not found');
}
return args;
};
const processSnapshotFile = (snapshotPath, file) => {
const snapshot = JSON.parse(fs.readFileSync(path.join(snapshotPath, file)));
const parts = file.match(/-(\d{2})(\d{2})(\d{2})\.json$/);
return {
grid: snapshot.grid.cells,
time: [parts[1], parts[2]].join(':')
};
};
const getTeamsFromSnapshotFile = (snapshotPath, file) => {
const snapshot = JSON.parse(fs.readFileSync(path.join(snapshotPath, file)));
return snapshot.teams.map(team => {
return {
name: team.name,
role: team.role,
colour: team.colour,
gravatar: team.gravatar
};
});
};
const createArchive = args => {
const isSnapshotFile = name => name.match(/-\d{6}\.json$/);
const snapshotFileNames = fs.readdirSync(args.snapshotPath).filter(isSnapshotFile);
const teams = getTeamsFromSnapshotFile(args.snapshotPath, snapshotFileNames[snapshotFileNames.length - 1]);
const snapshots = snapshotFileNames.map(processSnapshotFile.bind(null, args.snapshotPath));
return {
teamCount: teams.length,
snapshotCount: snapshots.length,
teams: JSON.stringify(teams),
snapshots: JSON.stringify(snapshots)
};
};
const writeArchiveFile = (filename, archive) => {
const data = `var teams = ${archive.teams}; var snapshots = ${archive.snapshots};`;
fs.writeFileSync(`${__dirname}/${filename}`, data);
return data.length;
};
const args = processCommandLine();
const archive = createArchive(args);
const written = writeArchiveFile(args.outputFile, archive);
console.log(`file: ${args.outputFile}, size: ${Math.round(written/1024)} KiB`);
console.log(`snapshots: ${archive.snapshotCount}, teams: ${archive.teamCount}`);