-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_support_zips.js
executable file
·171 lines (141 loc) · 4.34 KB
/
build_support_zips.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const fsPath = require('path');
const JSZip = require('./jscbuilding/vendor/jszip.min.js');
runBuild();
function onFileRead(zip, fileName, fileContents) {
console.log('Processing: ' + fileName);
zip.file(fsPath.join('jscause', fileName), fileContents);
}
function onTemplateFileRead(zip, fileName, fileContents) {
console.log('Processing: ' + fileName);
zip.file(fsPath.join('jscause', fsPath.relative(fsPath.join('jscbuilding', 'site_template'), fileName)), fileContents);
}
function readDir(dirPath, onReadError) {
let fileNames;
try
{
fileNames = fs.readdirSync(dirPath);
}
catch(e)
{
onReadError(e);
}
return fileNames;
}
function readStats(path, onReadError) {
let stats;
try
{
stats = fs.lstatSync(path);
}
catch (e)
{
onReadError(e);
}
return stats;
}
function readFileContents(path, onReadError) {
let fileContents;
try
{
fileContents = fs.readFileSync(path);
}
catch(e)
{
onReadError(e);
}
return fileContents;
}
function readDirectoryContents(dirPath, zip, onfileContents, onReadError) {
const directoryPaths = [ dirPath ];
let allGood = true;
do {
let currentDirectoryPath = directoryPaths.shift();
const fileNames = readDir(currentDirectoryPath, onReadError);
if (!fileNames) {
allGood = false;
return;
}
while (allGood && fileNames.length)
{
const fileName = fileNames.shift();
if (fileName === '.DS_Store') {
continue;
}
const fullPath = fsPath.join(currentDirectoryPath, fileName);
const stats = readStats(fullPath, onReadError);
if (stats) {
if (stats.isDirectory())
{
directoryPaths.unshift(fullPath);
}
else {
const fileContents = readFileContents(fullPath, onReadError);
allGood = typeof(fileContents) !== 'undefined';
if (allGood) {
onfileContents(zip, fullPath, fileContents);
}
}
}
else {
allGood = false;
}
}
}
while (allGood && (directoryPaths.length > 0));
return allGood;
}
function onReadJSCVendorError(err) {
console.error(err)
console.error('ERROR: An error occurred while processing jscvendor.');
}
function onReadJSCTemplatesError(err) {
console.error(err)
console.error('ERROR: An error occurred while processing the site templates.');
}
function runBuild() {
const zip = new JSZip();
const fileContents = readFileContents('./jscause.js', onReadJSCVendorError);
zip.file(fsPath.join('jscause', 'jscause.js'), fileContents);
if (readDirectoryContents('jscvendor', zip, onFileRead, onReadJSCVendorError) &&
readDirectoryContents(fsPath.join('jscbuilding', 'site_template'), zip, onTemplateFileRead, onReadJSCTemplatesError)) {
zip
.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
.pipe(fs.createWriteStream(fsPath.join('dist', 'jscause_standalone.zip')))
.on('finish', function() {
console.log();
console.log('SUCCESS: dist/jscause_standalone.zip created.');
console.log();
runSiteBuild();
})
.on('error', function(err) {
console.error(err);
console.error('ERROR: Could not create standalone zip file. Make sure you have all the required permissions to write to dist/, and that any previous zip files in it are deletable/overwritable.');
});
}
else {
console.error('ERROR: Could not create standalone zip file.');
}
}
function runSiteBuild() {
const zip = new JSZip();
if (readDirectoryContents(fsPath.join('jscbuilding', 'site_template'), zip, onTemplateFileRead, onReadJSCTemplatesError)) {
zip
.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
.pipe(fs.createWriteStream(fsPath.join('dist', 'jscause_site_template.zip')))
.on('finish', function() {
console.log();
console.log('SUCCESS: dist/jscause_site_template.zip created.');
console.log();
})
.on('error', function(err) {
console.error(err);
console.error('ERROR: Could not create site template zip file. Make sure you have all the required permissions to write to dist/, and that any previous zip files in it are deletable/overwritable.');
});
}
else {
console.error('ERROR: Could not create site zip file.');
}
}