-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuglify2.mjs
125 lines (115 loc) · 4.81 KB
/
uglify2.mjs
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
import browserify from "browserify";
import commonShakeify from "common-shakeify";
import esmify from "esmify";
import babelify from "babelify";
import UglifyJS from "uglify-js";
import gitlog from "gitlog";
import fs from "fs";
import ts from "typescript";
const STANDALONE = "./src/trackjoiner/standalone.js";
const TRACKJOINER_TS = "src/trackjoiner/trackjoiner.ts";
const TRACKJOINER = "./src/trackjoiner/trackjoiner.js";
const DEV_BUNDLE = "./dist/js/trackjoiner-bundle.js";
const LEGACY_TRACKJOINER = (process.env.CF_PAGES === "1") ? "./dist/js/trackjoiner-dev.js" : "./dist/js/trackjoiner.js";
const MIN_BUNDLE = (process.env.CF_PAGES === "1") ? "./dist/js/trackjoiner.js" : "./dist/js/trackjoiner-min.js";
const NOMORENEEDED = "n'est plus utilisé vous pouvez le supprimer de vos scripts";
const IGC_FILE = "./dist/js/igc-parser.js";
const GPX_FILE = "./dist/js/gpx-parser.js";
const FIT_FILE = "./dist/js/fit-parser.js";
const IGC_TS = "./src/trackjoiner/igc-parser/index.ts";
const IGC_JS = "./src/trackjoiner/igc-parser/index.js";
const GPX_TS = "./src/trackjoiner/gpx-parser/index.ts";
const GPX_JS = "./src/trackjoiner/gpx-parser/index.js";
function mdSync(dir) {
fs.mkdirSync(dir, { recursive: true }, (err) => {
if (err) throw err;
});
}
function copyCallbackErr(err) {
if (err) throw err;
console.log('File copied');
}
function minify(fin, fout) {
return new Promise((resolve, reject) => {
fs.readFile(fin, 'utf8', (err, data) => {
if (err) {
reject(console.log(err));
return;
}
var result = UglifyJS.minify(data);
if (result.code !== undefined) {
fs.writeFile(fout, result.code, function (err) {
if (err) {
reject(console.log(err));
} else {
resolve(result);
}
});
} else {
console.log(result.error);
reject(result.error);
}
});
})
}
function compileTypeScript(fin, fout) {
return new Promise((resolve, reject) => {
console.log(`compiling ${fin} to ${fout}`);
fs.readFile(fin, 'utf8', function (err, data) {
if (err) {
reject(console.log(err));
}
let result = ts.transpileModule(data, { compilerOptions: { esModuleInterop: true} }).outputText;
fs.writeFile(fout, result, 'utf8', function (err) {
if (err) reject(console.log(err));
else {
resolve(result);
}
});
});
})
}
const commits = gitlog.default({
repo: ".",
number: 1,
fields: ["authorDate"],
});
mdSync('./dist');
mdSync('./dist/css');
mdSync('./dist/js');
fs.copyFile('./public/css/blue.css', './dist/css/blue.css', copyCallbackErr);
fs.copyFile('./public/legacy.html', './dist/legacy.html', copyCallbackErr);
fs.copyFile('./public/legacy2.html', './dist/legacy2.html', copyCallbackErr);
console.log(`compiling ${TRACKJOINER} and create bundle ${DEV_BUNDLE}`);
Promise.all([
compileTypeScript(TRACKJOINER_TS, TRACKJOINER),
compileTypeScript(IGC_TS, IGC_JS),
compileTypeScript(GPX_TS, GPX_JS)
]).then(() => {
console.log("Start bundling");
browserify({ standalone: "Trackjoiner" })
.add(TRACKJOINER)
.plugin(esmify)
.transform(babelify, { presets: ["@babel/preset-env"], extensions: ['.jsx', '.js', '.tsx', '.ts'] })
.bundle()
.pipe(fs.createWriteStream(DEV_BUNDLE, { autoClose: true }))
.on('error', function (error) { console.error(error.toString()); })
.on('close', () => {
let bundle = fs.readFileSync(DEV_BUNDLE, { encoding: 'utf8', flag: 'r' });
let standalone = fs.readFileSync(STANDALONE, { encoding: 'utf8', flag: 'r' });
let trackjoiner = `${bundle}\n${standalone}\nconsole.log("Trackjoiner v:${(new Date(commits[0].authorDate)).toISOString()} ©Ronan Le Meillat, see https://github.com/highcanfly-club/cfdtrackjoiner")`;
fs.writeFileSync(LEGACY_TRACKJOINER, trackjoiner);
console.log(`Minify bundle to ${MIN_BUNDLE}`);
minify(LEGACY_TRACKJOINER, MIN_BUNDLE).then(() => {
console.log("removed temp files");
fs.rmSync(DEV_BUNDLE);
fs.rmSync(IGC_JS);
fs.rmSync(GPX_JS);
fs.rmSync(TRACKJOINER);
console.log("Add messages telling that old scripts are not needed anymore")
fs.writeFileSync(IGC_FILE, `console.log("${IGC_FILE} ${NOMORENEEDED}");`);
fs.writeFileSync(GPX_FILE, `console.log("${GPX_FILE} ${NOMORENEEDED}");`);
fs.writeFileSync(FIT_FILE, `console.log("${FIT_FILE} ${NOMORENEEDED}");`);
});
});
})