Repository navigation
Expand file tree
/
Copy pathgulpfile.js
More file actions
122 lines (108 loc) · 3.33 KB
/
Copy pathgulpfile.js
File metadata and controls
122 lines (108 loc) · 3.33 KB
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
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
);
gulp.task("build", ["prepare-for-espruino"]);
gulp.task("prepare-for-espruino", ["compile-ts", "content-to-dist"], 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", code => {
cb();
});
});
gulp.task("compile-ts", ["gen-config-ts"], function() {
const tsResult = tsProject.src().pipe(tsProject());
return tsResult.js.pipe(gulp.dest(distDir));
});
gulp.task("content-to-dist", () => {
return gulp.src("src/**/*.js", { base: "src" }).pipe(gulp.dest(distDir));
});
gulp.task("send-to-espruino-console", cb => {
const content = fs.readFileSync(espReadyBundlePath);
fs.writeFile(espConsoleBeingWatchedFilePath, content, err => {
if (err) {
throw err;
}
cb();
});
});
gulp.task("clear-espruino-watch-file", cb => {
fs.writeFile(espConsoleBeingWatchedFilePath, "", err => {
if (err) {
throw err;
}
cb();
});
});
gulp.task("espruino-console", ["clear-espruino-watch-file"], 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", code => {
cb();
});
});
gulp.task("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();
});