-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
135 lines (117 loc) · 3.38 KB
/
Copy pathgulpfile.js
File metadata and controls
135 lines (117 loc) · 3.38 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
123
124
125
126
127
128
129
130
131
132
133
134
135
/*npm packages required to run gulp.*/
const gulp = require("gulp");
//for cleaning the dist folder.
const del = require("del");
//for merging environment config files.
const merge = require("gulp-merge-json");
//to read and write files
const fs = require("fs");
//for typescript
var ts = require("gulp-typescript");
var tsProject = ts.createProject("tsconfig.json");
//for minifying js
const uglify = require("gulp-uglify");
var cssMinify = require("gulp-css-minify");
//Reading the command-line argument called config. If it is not present, the default value is DEV.
const argv = require("yargs").argv;
const config = argv.config == undefined ? "DEV" : argv.config;
//removes the existing dist and makes room for the new build.
function clean() {
return del(["./dist/"]);
}
//moves files from src to dist
function copyLocales() {
return gulp
.src(["_locales/**/*.*"], { base: "." })
.pipe(gulp.dest("./dist/"));
}
function copyAllFiles() {
copyLocales();
return gulp
.src([
"src/**/*.*",
"!src/config.json",
"!src/config.PROD.json",
"!src/manifest*.json",
"!src/typings.d.ts",
"!src/css/*.*",
"!src/**/*.ts",
])
.pipe(gulp.dest("./dist/"));
}
//Transforms the config.json files, merging the source one with the environment one.
function transformConfig(cb) {
return transformJson(cb, "config");
}
//Transforms the manifest.json file.
function transformManifest(cb) {
return transformJson(cb, "manifest");
}
function transformJson(cb, fileName) {
if (config == "DEV") {
return gulp.src(`src/${fileName}.json`).pipe(gulp.dest("./dist/"));
}
return gulp
.src([`src/${fileName}.json`, `src/${fileName}.${config}.json`])
.pipe(
merge({
fileName: `${fileName}.json`,
})
)
.pipe(gulp.dest("./dist"));
}
//Writes the transformed config.json file to the scripts folder to allow the Chrome Extension’s .js files access the config values.
function writeConfigJsFile(cb) {
var content = fs.readFileSync("./dist/config.json", "utf8").trim();
content = `// File generated by gulpfile.js.\n// DO NOT CHANGE IT DIRECTLY.\n// Edit config.*.json files instead.\nconst config = ${content};\nexport default config;`;
fs.mkdirSync("./dist/scripts", { recursive: true }, (err) => {
if (err) throw err;
});
fs.writeFileSync("./dist/scripts/config.js", content);
fs.mkdirSync("./src/scripts", { recursive: true }, (err) => {
if (err) throw err;
});
fs.writeFileSync("./src/scripts/config.js", content);
del(["./dist/config.json"]);
return cb();
}
function compileTypescript() {
return tsProject
.src()
.pipe(tsProject())
.js.pipe(uglify())
.pipe(gulp.dest("dist/"));
}
function compileCSS() {
return gulp
.src("./src/css/**/*.css")
.pipe(cssMinify())
.pipe(gulp.dest("./dist/css/"));
}
//Watches for any change in the src folder and automatically repeat the previous steps.
function watch(cb) {
if (argv.watch == undefined) return cb();
return gulp.watch(
["src/**/*.*", "!src/scripts/config.js"],
gulp.series(
clean,
compileCSS,
compileTypescript,
copyAllFiles,
transformConfig,
writeConfigJsFile,
transformManifest
)
);
}
// order in which the functions are run
exports.default = gulp.series(
clean,
compileCSS,
compileTypescript,
copyAllFiles,
transformConfig,
writeConfigJsFile,
transformManifest,
watch
);