-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gruntfile.js
175 lines (141 loc) · 5 KB
/
Gruntfile.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
172
173
174
175
const path = require("path");
const pkg = require("./package.json");
const cp = require("child_process");
const crypto = require("crypto");
const fs = require("fs");
const os = require("os");
const PATH_DIST = path.resolve(process.cwd(), "dist");
const PATH_BUILD = path.resolve(process.cwd(), "build");
process.env = Object.assign({
NODE_ENV: "production"
}, process.env);
module.exports = function (grunt) {
// Project configuration.
grunt.initConfig({
pkg,
uglify: {
// NOTE: if true, this truncate variables&class names
// Are original names neede for production?!
// i dont thinks so, its only usefull in development
options: {
mangle: {
toplevel: true
},
//banner: fs.readFileSync(path.join(process.cwd(), "docs/banner.txt"), "utf8")
},
build: {
files: [{
expand: true,
src: [
//"package.json",
//"package-lock.json",
"**/*.js",
"!plugins/**",
"**/*.gitkeep",
"!Gruntfile.js",
"!node_modules/**",
"!scripts/**",
"!tests/**"
],
dest: PATH_BUILD,
//cwd: process.cwd()
}]
}
}
});
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.registerTask("build", () => {
[
`rm -rf ${path.join(PATH_BUILD, "/*")}`,
`rm -rf ${path.join(PATH_DIST, "/*")}`,
`mkdir -p ${PATH_BUILD}`,
`mkdir ${path.join(PATH_BUILD, "logs")}`,
`mkdir ${path.join(PATH_BUILD, "plugins")}`,
`mkdir ${path.join(PATH_BUILD, "scripts")}`,
`echo "exit 0" > ${path.join(PATH_BUILD, "scripts/post-install.sh")}`,
`chmod +x ${path.join(PATH_BUILD, "scripts/post-install.sh")}`,
`cp ./package*.json ${PATH_BUILD}`,
"grunt uglify",
].forEach((cmd) => {
cp.execSync(cmd, {
env: process.env,
stdio: "inherit"
});
});
});
grunt.registerTask("install", () => {
cp.execSync(`cd ${PATH_BUILD} && npm install --prod-only`, {
env: process.env,
stdio: "inherit"
});
});
grunt.registerTask("compress", () => {
cp.execSync(`cd ${PATH_BUILD} && tar -czvf ${path.join(PATH_DIST, `${pkg.name}-v${pkg.version}.tgz`)} *`, {
env: process.env,
stdio: "inherit"
});
});
grunt.registerTask("build:docker", () => {
let buildArgs = [
`--build-arg version=${pkg.version}`,
`--build-arg buildDate=${Date.now()}`,
].join(" ");
cp.execSync(`docker build . -t openhaus/${pkg.name}:${pkg.version} ${buildArgs}`, {
env: process.env,
stdio: "inherit"
});
// remove build, use taggin. see: #451
/*
cp.execSync(`docker build . -t openhaus/${pkg.name}:latest ${buildArgs}`, {
env: process.env,
stdio: "inherit"
});
*/
cp.execSync(`docker image tag openhaus/${pkg.name}:${pkg.version} openhaus/${pkg.name}:latest`, {
env: process.env,
stdio: "inherit"
});
});
grunt.registerTask("checksum", () => {
let m5f = path.join(PATH_DIST, "./checksums.txt");
fs.rmSync(m5f, { force: true });
let files = fs.readdirSync(PATH_DIST);
let fd = fs.openSync(m5f, "w");
files.forEach((name) => {
let file = path.join(PATH_DIST, name);
let content = fs.readFileSync(file);
let hasher = crypto.createHash("md5");
let hash = hasher.update(content).digest("hex");
fs.writeSync(fd, `${hash}\t${name}${os.EOL}`);
});
fs.closeSync(fd);
});
grunt.registerTask("release", () => {
[
`mkdir -p ${PATH_DIST}`,
"grunt build",
"grunt compress",
"grunt build:docker",
`docker save openhaus/${pkg.name}:latest | gzip > ${path.join(PATH_DIST, `${pkg.name}-v${pkg.version}-docker.tgz`)}`,
"grunt install",
`cd ${PATH_BUILD} && tar -czvf ${path.join(PATH_DIST, `${pkg.name}-v${pkg.version}-bundle.tgz`)} *`,
"grunt checksum"
].forEach((cmd) => {
cp.execSync(cmd, {
env: process.env,
stdio: "inherit"
});
});
});
grunt.registerTask("publish", () => {
[
`docker push openhaus/${pkg.name}:${pkg.version}`,
`docker push openhaus/${pkg.name}:latest`
].forEach((cmd) => {
cp.execSync(cmd, {
env: process.env,
stdio: "inherit"
});
});
});
};