-
Notifications
You must be signed in to change notification settings - Fork 5
/
gulpfile.js
executable file
·229 lines (201 loc) · 7.17 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* gulpfile.js
*
* Typo3 Gulp SCSS Autocompiler to CSS + CSS Minifier + CSS Sourcemaps + JS Minifier
*
* @author Sebastian Pieczona
* @company ioCron
* @copyright 2018 Sebastian Pieczona
* @license MIT License (see https://github.com/iocron/typo3-gulp-scss/blob/master/LICENSE)
* @link https://github.com/iocron/typo3-gulp-scss
*/
'use strict';
// NPM PACKAGES
let gulp = require('gulp'),
sass = require('gulp-sass'),
rename = require("gulp-rename"),
minify = require("gulp-babel-minify"),
sourcemaps = require('gulp-sourcemaps'),
commandExists = require('command-exists'),
download = require("gulp-download-stream"),
giftp = require('giftp');
// NATIVE NODE MODULES
let fs = require("fs"),
path = require("path");
// GENERIC VARIABLES
const exec = require('child_process').exec,
themePath = './Resources/Public/',
themeName = path.basename(__dirname),
scssPath = themePath + 'Scss',
jsPath = themePath + 'JavaScript',
cssPath = themePath + 'Css',
imgPath = themePath + 'Images',
repoUrl = 'https://github.com/iocron/typo3-gulp-scss',
repoUrlRaw = 'https://raw.githubusercontent.com/iocron/typo3-gulp-scss/master/';
// DEFAULT HELPER TASK
gulp.task('default', function(cb){
exec('gulp --tasks-simple', function (err, stdout, stderr) {
console.log("\nAll gulp parameters:\n(Usage Example: \"gulp build\")\n-------------------");
console.log(stdout);
console.log(stderr);
cb(err);
});
});
// INSTALLER - IDE Plugins for Atom
gulp.task('install:onsave-atom', function(done){
commandExists('apm').then(function(command){
fs.stat('.on-save.json', function(err, stat){
if(err == null){
// .. file exists already
} else if(err.code == 'ENOENT'){
download(repoUrlRaw + '.on-save.json').pipe(gulp.dest("./"));
} else {
console.log(err, stat);
done(err);
}
});
exec('apm install on-save --compatible', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
console.log("Please restart your IDE so the Plugin can be correctly initialized.");
done(err);
});
done();
}).catch(function(){
console.log("The Command \"apm\" (from Atom) was not found or is not accessible on your System.\nSkipped Implementation of the Auto-Save Functionality..");
});
});
// INSTALLER - IDE Plugins for ATOM AND VSCODE
gulp.task('install:onsave', gulp.parallel('install:onsave-atom'));
// SETUP SYMLINKS (create generic theme symlinks)
gulp.task('setup:symlinks', function(done) {
fs.realpath('../../../fileadmin/', function(err, resolvedPath) {
if(err) { console.log(err); }
console.log("symlink from: ", resolvedPath);
fs.realpath(imgPath, function(errRealPath1, resolvedImgPath) {
if(errRealPath1) { console.log(errRealPath1); }
console.log("symlink to: ", resolvedImgPath);
const targetDir = resolvedPath + '/' + themeName;
if (!fs.existsSync(targetDir)) {
console.log("create dir: " + targetDir);
fs.mkdir(targetDir, { recursive: false }, (errDir) => {
if (errDir) throw errDir;
});
}
fs.symlink(resolvedImgPath, targetDir + '/themeResources', 'dir', function(errRealPath2) {
if(errRealPath2) { console.log(errRealPath2); }
});
});
});
done();
});
gulp.task('setup:symlinks_ext', function(done) {
function createSymlink(sourcePath, targetPath) {
if(fs.existsSync(sourcePath) && !fs.existsSync(targetPath)) {
console.log("Symlink from: " + sourcePath + " to " + targetPath);
try {
var checkFile = fs.lstatSync(sourcePath).isDirectory() ? 'dir' : 'file';
fs.symlink(sourcePath, targetPath, checkFile, function(err) {
if(err) { console.log(err); }
});
} catch(e) {
// Handle error
console.log("ERROR:");
console.log(e);
if(e.code == 'ENOENT') {
//no such file or directory
console.log("No such file or directory found: " + sourcePath);
}
}
console.log(checkFile);
} else {
console.log("ERROR: " + sourcePath + " - resolvedPath doesn't exist or targetPath: " + targetPath + " exists");
}
}
createSymlink(fs.realpathSync('Configuration/Typo3/AdditionalConfiguration.php'), '../../AdditionalConfiguration.php');
createSymlink(fs.realpathSync('Configuration/Typo3/sites'), '../../sites');
done();
});
// SETUP - FULL SETUP
gulp.task('setup', gulp.series('install:onsave', 'setup:symlinks', 'setup:symlinks_ext'));
gulp.task('setup:theme', gulp.series('setup:symlinks', 'setup:symlinks_ext'));
// UPGRADE / SELF-UPDATER
gulp.task('upgrade', function(done){
download([
repoUrlRaw + 'package.json',
repoUrlRaw + 'package-lock.json',
repoUrlRaw + 'gulpfile.js'
])
.pipe(gulp.dest("./"));
exec('npm update', function(err, stdout, stderr){
if(err == null){
console.log("npm update.. Dependencies are up to date.");
} else {
console.log("npm update.. ", err, stdout, stderr);
}
});
done();
});
// SCSS / SASS COMPILER & MINIFIER
gulp.task('sass:uncompressed', function(done){
gulp.src(scssPath + '/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass.sync().on('error', sass.logError))
.pipe(sourcemaps.write("./sourcemaps/"))
.pipe(gulp.dest(cssPath));
done();
});
gulp.task('sass:compressed', function(done){
gulp.src(scssPath + '/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass.sync({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(rename({ suffix:".min" }))
.pipe(sourcemaps.write("./sourcemaps/"))
.pipe(gulp.dest(cssPath));
done();
});
gulp.task('sass:watch', function(done){
let watcher = gulp.watch(scssPath + '/**/*.scss');
watcher.on('change', gulp.series('sass:uncompressed', 'sass:compressed'));
});
// JAVASCRIPT COMPRESSION
gulp.task('js:compressed', function(done){
gulp.src([jsPath + '/**/*.js', '!'+ jsPath +'/**/*.min.js'])
.pipe(sourcemaps.init())
.pipe(minify({
builtIns: false,
evaluate: false,
mangle: false
}))
.pipe(rename({ suffix:".min" }))
.pipe(sourcemaps.write("./sourcemaps/"))
.pipe(gulp.dest(jsPath));
done();
});
gulp.task('js:watch', function(done){
let watcher = gulp.watch([jsPath + '/**/*.js', '!'+ jsPath +'/**/*.min.js']);
watcher.on('change', gulp.series('js:compressed'));
});
// GIT FTP DEPLOYER
gulp.task('gitftp', function(done){
fs.stat('giftp.json', function(err, stat){ // Check if giftp.json exists
if(err == null){
giftp.run();
done();
} else if(err.code == 'ENOENT'){
download(repoUrlRaw + '.giftp.json')
.pipe(rename("giftp.json"))
.pipe(gulp.dest("./"))
.on("end", function(){
console.log("\x1b[31m", "WARNING: Please configure giftp.json");
done();
});
} else {
console.log(err, stat);
done(err);
}
});
});
// GLOBAL WATCHER / BUILD COMMANDS
gulp.task('build', gulp.parallel('sass:uncompressed', 'sass:compressed', 'js:compressed'));
gulp.task('watch', gulp.parallel('build', 'sass:watch', 'js:watch'));