-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
120 lines (98 loc) · 3.79 KB
/
gulpfile.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
const gulp = require('gulp');
const ts = require('gulp-typescript');
const tsProject = ts.createProject('tsconfig.json');
const fs = require("fs");
const { fork } = require('child_process');
const path = require("path");
const yaml = require("js-yaml");
const _ = require("lodash");
const envConfig = yaml.load(fs.readFileSync("env-config.yaml"));
const distDir = "./dist";
const srcDir = "./src";
const espReadyBundleFileName = "bundle.js";
const espReadyBundlePath = path.join(distDir, espReadyBundleFileName);
const appFileName = "app.js";
const appFilePath = path.join(distDir, appFileName);
const appConfigTsFileName = "app-config.ts";
const appConfigFileName = "app-config.yaml";
const userAppConfigFileName = "app-config.user.yaml";
const espConsoleBeingWatchedFileName = "esp-console-input.js";
const espConsoleBeingWatchedFilePath = path.join(distDir, espConsoleBeingWatchedFileName);
function prepare_for_espruino(cb) {
if (!fs.existsSync(appFilePath)) {
cb("main app file does not exit " + appFilePath);
return;
}
let appContent = fs.readFileSync(appFilePath).toString();
appContent = appContent.replace('Object.defineProperty(exports, "__esModule", { value: true });', "");
fs.writeFileSync(appFilePath, appContent);
const buildproc = fork(
require.resolve("espruino/bin/espruino-cli"),
["--board", envConfig.board, appFileName, "-o", espReadyBundleFileName],
{ cwd: distDir });
buildproc.on('close', () => {
cb();
});
}
function compile_ts() {
const tsResult = tsProject.src().pipe(tsProject());
return tsResult.js.pipe(gulp.dest(distDir));
}
function content_to_dist() {
return gulp
.src("src/**/*.js", { base: 'src' })
.pipe(gulp.dest(distDir));
}
function send_to_espruino_console(cb) {
const content = fs.readFileSync(espReadyBundlePath);
fs.writeFile(
espConsoleBeingWatchedFilePath,
content,
(err) => {
if (err) { throw err; }
cb();
});
}
function clear_espruino_watch_file(cb) {
fs.writeFile(
espConsoleBeingWatchedFilePath,
"",
(err) => {
if (err) { throw err; }
cb();
});
}
function espruino_console(cb) {
const buildproc = fork(
require.resolve("espruino/bin/espruino-cli"),
["--board", envConfig.board, "-b", envConfig.port_speed, "--port", envConfig.port, "-w", espConsoleBeingWatchedFileName],
{ cwd: distDir });
buildproc.on('close', () => {
cb();
});
}
function gen_config_ts(cb) {
if (!fs.existsSync(userAppConfigFileName)) {
const content = fs.readFileSync(appConfigFileName)
.toString()
.split("\n")
.map(x => `# ${x}`)
.join("\n");
fs.writeFileSync(userAppConfigFileName, content, { encoding: "utf-8" });
}
const appConfig = yaml.load(fs.readFileSync(appConfigFileName));
const userAppConfig = yaml.load(fs.readFileSync(userAppConfigFileName));
const mergedAppConfig = _.assign(appConfig, userAppConfig);
const jsonString = JSON.stringify(mergedAppConfig);
const resultConfigTsContent = `export default ${jsonString};`;
fs.writeFileSync(path.join(srcDir, appConfigTsFileName), resultConfigTsContent);
cb();
}
gulp.task("gen-config-ts", gen_config_ts);
gulp.task("compile-ts", gulp.series("gen-config-ts", compile_ts));
gulp.task("content-to-dist", content_to_dist);
gulp.task("prepare-for-espruino", gulp.series('compile-ts', 'content-to-dist', prepare_for_espruino));
gulp.task("build", gulp.series("prepare-for-espruino"));
gulp.task("send-to-espurino-console", send_to_espruino_console);
gulp.task("clear-espurino-watch-file", clear_espruino_watch_file);
gulp.task("espruino-console", gulp.series("clear-espurino-watch-file", espruino_console));