-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.js
executable file
·141 lines (115 loc) · 3.14 KB
/
cmd.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env node
var path = require('path');
var cheerio = require('cheerio');
var glob = require('glob');
var watch = require('glob-watcher');
var decompress = require('decompress');
var tmp = require('tmp');
tmp.setGracefulCleanup(); // clean up files even if exception occurs
var fs = require('fs-extra');
var argv = require('minimist')(process.argv.slice(2));
var entries = [];
function usage(err) {
var p;
if(err) {
p = console.error;
p("Error:", err);
p("");
} else {
p = console.log;
}
p("Usage: cmd.js dir_to_watch output_dir");
p("");
process.exit((err) ? 1 : 0);
}
function fail(err) {
console.error(err);
process.exit(1);
}
if(argv._.length < 2) {
usage("Missing directory name");
}
function isNotebookFile(filepath) {
var parts = filepath.split('/');
var i;
for(i=0; i < parts.length; i++) {
if(parts[i].match(/^notebook$/i)) {
return true;
}
}
return false;
}
function parseHtml(filePath) {
var data = fs.readFileSync(filePath);
const $ = cheerio.load(data);
var els = $('.editor').children();
var i;
for(i=0; i < els.length; i++) {
entries += $(els[i]).html();
}
}
function writeLog() {
var html = '<!DOCTYPE html>';
html += '<html lang="en">';
html += '<head>';
html += '<title>Real Vegan Cheese - Labnotes</title>';
html += '<link href="../benchling.css" rel="stylesheet"/>';
html += '<link href="../main.css" rel="stylesheet"/>';
html += '</head>';
html += '<body>';
for(i=0; i < entries.length; i++) {
html += entries[i];
}
html += '</body>';
html += '</html>';
fs.writeFileSync(path.join(labnotesDir, 'index.html'), html);
}
var watchDir = argv._[0];
var outDir = argv._[1];
var labnotesDir = path.join(outDir, 'labnotes');
fs.ensureDirSync(labnotesDir);
var inventoryDir = path.join(outDir, 'inventory');
fs.ensureDirSync(inventoryDir);
function handleZip(zipfilePath) {
if(!zipfilePath.match(/\.zip$/i)) return;
tmp.dir({unsafeCleanup: true}, function(err, tmpPath, tmpCleanup) {
if(err) fail(err);
console.log("UNZIPPING:", zipfilePath, tmpPath);
decompress(zipfilePath, tmpPath).then(function(files) {
var i, curFile, curFileSrc, curFileDst;
for(i=0; i < files.length; i++) {
curFile = files[i];
if(isNotebookFile(curFile.path)) {
curFileSrc = path.join(tmpPath, curFile.path)
curFileDst = path.join(labnotesDir, path.basename(curFile.path));
fs.copySync(curFileSrc, curFileDst);
if(curFile.path.match(/\.html?$/i)) {
parseHtml(curFileDst);
}
}
}
tmpCleanup();
writeLog();
}, function(err) {
// tmpCleanup();
console.error(err);
});
});
}
function waitForFile(zipfilePath, lastSize) {
lastSize = lastSize || 0;
var size = fs.statSync(zipfilePath).size;
console.log("WAITING", size);
if(size > lastSize) {
setTimeout(function() {
waitForFile(zipfilePath, size);
}, 3000);
return;
}
handleZip(zipfilePath);
}
var watcher = watch([watchDir]);
// watch specified directory for new zip files
watcher.on('add', function(zipfilePath, stat) {
waitForFile(zipfilePath);
});