-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
gulpfile.js
174 lines (156 loc) · 5.22 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
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
var gulp = require('gulp');
var replace = require('gulp-replace');
var del = require('del');
var execSh = require('exec-sh');
var rename = require('gulp-rename');
var changed = require('gulp-changed');
var argv = require('yargs').argv;
var directoryExists = require('directory-exists');
var eslint = require('gulp-eslint');
/**
* Cleans out the dist folders of previously built or pulled code
*/
gulp.task('clean', function (done) {
del(['dist/build/**/*', 'dist/build/**/.*', 'dist/pull/**/*', 'dist/pull/**/.*']).then(paths => {
console.log('Deleted files and folders:\n', paths.join('\n'));
done();
});
});
/**
* Copys source code into the dist/build folder Comments out code which is
* required for the code base to run locally in Node and would error in GAS
*/
gulp.task('build', function (done) {
var stream = gulp
.src(["src/**/*", "src/**/.*"])
// .pipe(replace("module.exports","//module.exports"))
.pipe(replace(/\/\/ Node required code block/g, "/* Node required code block"))
.pipe(replace(/\/\/*.?End of Node required code block/g, "// End of Node required code block*/"))
.pipe(gulp.dest("dist/build"));
stream.on('end', function () {
done();
});
});
/**
* Reads an environemt parameter, Selects the correct enviroment config and drops it into the dist/build folder.
* If no environment set the default is "production"
*/
gulp.task('set-environment-config', function (done) {
var isProduction = true;
isProduction = (argv.test !== undefined) ? false : true;
var environment = isProduction ? 'production' : 'test';
console.log("Using configuration for '" + environment + "' environment");
var configDirectory = "config/" + environment;
directoryExists(configDirectory, (error, directoryFound) => {
if (!directoryFound) {
throw "Config directory not found (" + configDirectory + ") - was environement set correctly";
done(false);
} else {
var stream = gulp
.src([configDirectory + "/**/*"])
.pipe(gulp.dest("dist/build"));
stream.on('end', function () {
done();
});
}
});
});
/**
* Forcing the environment to be "test".
* @param done
* @returns TRUE
*/
gulp.task('use-test-environment', function (done) {
argv.test = true;
done();
return true;
});
/**
* Uses Clasp to push the code in dist/build upto GAS
* https://github.com/google/clasp
*/
gulp.task('clasp-push', function (done) {
process.chdir('dist/build');
execSh('clasp push', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
process.chdir('/');
done(err);
});
});
/**
* Uses Clasp to pull down the code from GAS to the dist/pull folder
* https://github.com/google/clasp
*/
gulp.task('clasp-pull', function (done) {
var stream = gulp
.src(["src/.clasp.json"])
.pipe(gulp.dest("dist/pull"));
stream.on('end', function () {
process.chdir('dist/pull');
execSh('clasp pull', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
process.chdir('../../');
done(err);
});
});
})
/**
* Undoes the clasp conversion of JS and GS file extensions Also uncomments code
* which is required for the code base to run locally in Node
*/
gulp.task('un-google', function (done) {
var stream = gulp
.src(["dist/pull/**/*.js"])
.pipe(rename(function (path) {
path.extname = ".gs";
}))
// .pipe(replace("//module.exports","module.exports"))
.pipe(replace(/\/\* Node required code block/g, "// Node required code block"))
.pipe(replace(/\/\/*.?End of Node required code block\*\//g, "// End of Node required code block"))
.pipe(gulp.dest("dist/pull"));
stream.on('end', function () {
del(["dist/pull/**/*.js"]);
done();
});
})
/**
* Copys the changed code in dist/pull which has been downloaded from GAS into
* the src folder
*/
gulp.task('copy-changed-pulled-code', function (done) {
var stream = gulp
.src(["dist/pull/**/*.html", "dist/pull/**/*.gs", "dist/pull/**/*.json", "dist/pull/**/.*","!dist/pull/environmentConfiguration.gs"])
.pipe(changed('src'))
.pipe(gulp.dest('src'));
stream.on('end', function () {
done();
});
});
gulp.task('lint', (done) => {
return gulp.src("src/*.gs")
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe(eslint())
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(eslint.failAfterError());
});
/**
* Deploys code from source upto GAS
* use --production or --test parameter to specify production or test environment
*/
gulp.task('deploy', gulp.series('clean', 'build', 'set-environment-config', 'clasp-push'));
/**
* Deploys code from source upto GAS
* forces deployment environment to use test config
*/
gulp.task('deploy-test', gulp.series('use-test-environment', 'deploy'));
/**
* Pulls GAS source code into dist/pull and copys changed files into src
*/
gulp.task('pull', gulp.series('clean', 'clasp-pull', 'un-google', 'copy-changed-pulled-code'));