-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
207 lines (183 loc) · 5.02 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const {
copy,
copySync,
removeSync,
copyFileSync,
unlinkSync,
exists,
existsSync,
mkdir,
mkdirSync,
readFile,
readFileSync,
writeFile,
writeFileSync,
} = require("fs-extra");
const { execSync } = require("child_process");
const replace = require("replace-in-file");
const util = require("util");
const rimRaf = util.promisify(require("rimraf"));
const pkg = require("./package.json");
const { version } = pkg;
const root = process.cwd();
(async function exec() {
// Cleanup
removeSync(`${root}/dist`);
removeSync(`${root}/packages`);
// Make the dist Dir
mkdirSync(`${root}/dist`);
mkdirSync(`${root}/src`);
mkdirSync(`${root}/packages`);
// Download a fresh copy of the js
execSync(
"node_modules/.bin/gist-fetch DavidKuennen minimal-analytics-snippet.js --out src/minimal-analytics-snippet.js"
);
await copy("./src", "./package");
if (!(await exists("./dist"))) {
await mkdir("./dist");
}
// Copy the folers
copySync(`${root}/mod_perfectgridga`, `${root}/dist/mod_perfectgridga`);
copySync(`${root}/plg_perfectgridga`, `${root}/dist/plg_perfectgridga`);
// Run the rollup
execSync("node_modules/.bin/rollup -c rollup.config.js");
if (existsSync("dist/script.js")) {
scriptContent = readFileSync("dist/script.js", {
encoding: "utf8",
});
scriptContent = scriptContent.replace(/\r?\n|\r/g, "");
scriptContent = `<<<JS
${scriptContent}
JS`;
}
// Get date
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
const currentMonth = currentDate.getMonth() + 1;
// Do some replacing
const optionsXml = {
files: [
`${root}/dist/mod_perfectgridga/*.xml`,
`${root}/dist/plg_perfectgridga/*.xml`,
],
from: [/{{copyright}}/g, /{{date}}/g, /{{version}}/g],
to: [
`(C) ${currentYear} Perfectgrid.io, All Rights Reserved.`,
`${currentMonth}/${currentYear}`,
`${pkg.version}`,
],
};
const optionsPhp = {
files: [
`${root}/dist/mod_perfectgridga/*.php`,
`${root}/dist/plg_perfectgridga/*.php`,
],
from: [/\<\?php/g],
to: [
`<?php
/**
* @copyright Copyright (C) ${currentYear} Dimitrios Grammatikogiannis. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
`,
],
};
const optionsJs = {
files: [
`${root}/dist/mod_perfectgridga/*.php`,
`${root}/dist/plg_perfectgridga/*.php`,
],
from: ['"###script-goes-here-###"'],
to: [`${scriptContent}`],
};
replace.sync(optionsXml);
replace.sync(optionsPhp);
replace.sync(optionsJs);
// Remove the source of the script
removeSync(`${root}/src`);
// remove the update files, they belong to the server
copyFileSync(
`${root}/dist/plg_perfectgridga/update_plg.xml`,
`${root}/docs/update_plg.xml`
);
copyFileSync(
`${root}/dist/mod_perfectgridga/update_mod.xml`,
`${root}/docs/update_mod.xml`
);
unlinkSync(`${root}/dist/plg_perfectgridga/update_plg.xml`);
unlinkSync(`${root}/dist/mod_perfectgridga/update_mod.xml`);
// Package it
const zip = new (require("adm-zip"))();
zip.addLocalFolder(`${root}/dist/mod_perfectgridga`, false);
zip.writeZip(`packages/mod_perfectgridga_v${version}.zip`);
zip.addLocalFolder(`${root}/dist/plg_perfectgridga`, false);
zip.writeZip(`packages/plg_perfectgridga_v${version}.zip`);
await rimRaf("./docs/pkgs");
await copy("./packages", "./docs/pkgs");
// Update the version, docs
if (existsSync(`${root}/docs/quickstart.md`)) {
unlinkSync(`${root}/docs/quickstart.md`);
}
const quickStartContent = readFileSync(`${root}/docs/quickstart.txt`, {
encoding: "utf8",
});
const qS = `
- [Module](/pkgs/mod_perfectgridga_v${pkg.version}.zip ':ignore')
- [Plugin](/pkgs/plg_perfectgridga_v${pkg.version}.zip ':ignore')
`;
writeFileSync(
`${root}/docs/quickstart.md`,
quickStartContent
.replace("```downloads```", qS)
.replace("```version```", `${pkg.version}`),
{ encoding: "utf8" }
);
const updPlg = {
files: [`${root}/docs/update_plg.xml`],
from: [
"{{name}}",
"{{version}}",
"{{name}}",
"{{filename}}",
"{{codeName}}",
"{{type}}",
"{{folder}}",
"{{client}}",
],
to: [
`PerfectGrid Google Analytics Lightweight Plugin`,
pkg.version,
`PerfectGrid Google Analytics Lightweight Plugin`,
`plg_perfectgridga_v${pkg.version}.zip`,
"perfectgridga",
"plugin",
"system",
"0",
],
};
const updMod = {
files: [`${root}/docs/update_mod.xml`],
from: [
"{{name}}",
"{{version}}",
"{{name}}",
"{{filename}}",
"{{codeName}}",
"{{type}}",
"{{folder}}",
"{{client}}",
],
to: [
`PerfectGrid Google Analytics Lightweight Module`,
pkg.version,
`PerfectGrid Google Analytics Lightweight Module`,
`mod_perfectgridga_v${pkg.version}.zip`,
"perfectgridga",
"module",
"",
"0",
],
};
replace.sync(updPlg);
replace.sync(updMod);
})();