-
Notifications
You must be signed in to change notification settings - Fork 21
/
build.js
executable file
·101 lines (78 loc) · 2.94 KB
/
build.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
#! /usr/bin/env node
var fs = require("fs");
var path = require("path");
const PLACEHOLDER = '@@SHHLACK_PLACEHOLDER@@'
const CURRENT_DIR = __dirname;
const SRC_DIR = path.join(CURRENT_DIR, "src");
const SRC_EXTENSION_DIR = path.join(SRC_DIR, "extension");
const SRC_STANDALONE_DIR = path.join(SRC_DIR, "standalone");
const BUILD_DIR = path.join(CURRENT_DIR, "build");
const RELEASE_DIR = path.join(CURRENT_DIR, "release");
const BUILD_EXTENSION_DIR = path.join(BUILD_DIR, "extension");
const BUILD_STANDALONE_DIR = path.join(BUILD_DIR, "standalone");
var shhlack_patch = fs.readFileSync('src/shhlack.js');
var extension_template = fs.readFileSync('src/templates/extension.tpl').toString();
var standalone_template = fs.readFileSync('src/templates/standalone.tpl').toString();
//////////////////////////////////////////////////////
/////////////// Utils ///////////////////////////////
function makeDirSync(dir) {
try {
fs.mkdirSync(dir)
} catch (exc) {
if (exc.code !== 'EEXIST') {
console.error(exc);
}
}
}
function copyFileSync(source, target) {
var targetFile = target;
//if target is a directory a new file with the same name will be created
if (fs.existsSync(target)) {
if (fs.lstatSync(target).isDirectory()) {
targetFile = path.join(target, path.basename(source));
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
function copyFolderRecursiveSync(source, target) {
console.log("COPY",source, target)
var files = [];
if (!source) {
throw "no Source Name";
}
if (!target) {
throw "no target Name";
}
//check if folder needs to be created or integrated
if (!fs.existsSync(target)) {
fs.mkdirSync(target);
}
var targetFolder = path.join(target, path.basename(source));
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
//copy
if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach(function(file) {
var curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderRecursiveSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
}
////////////////////////////////////////////////////////
copyFolderRecursiveSync(SRC_EXTENSION_DIR, BUILD_DIR);
copyFolderRecursiveSync(SRC_STANDALONE_DIR, BUILD_DIR);
copyFileSync(path.join(__dirname, "package.json"), BUILD_STANDALONE_DIR);
copyFileSync(path.join(__dirname, "README.md"), BUILD_STANDALONE_DIR);
copyFileSync(path.join(__dirname, "README.md"), BUILD_EXTENSION_DIR);
fs.writeFileSync(path.join(BUILD_EXTENSION_DIR, "shhlack.js"), extension_template.replace(PLACEHOLDER, shhlack_patch));
fs.writeFileSync(path.join(BUILD_STANDALONE_DIR, "shhlack.js"), standalone_template.replace(PLACEHOLDER, shhlack_patch));
if (process.argv[2] === '--release'){
copyFolderRecursiveSync(BUILD_EXTENSION_DIR, RELEASE_DIR);
copyFolderRecursiveSync(BUILD_STANDALONE_DIR, RELEASE_DIR);
}