This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
185 lines (154 loc) · 4.5 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
const gulp = require('gulp')
const browserify = require('browserify')
const source = require('vinyl-source-stream')
const buffer = require('vinyl-buffer')
const sourcemaps = require('gulp-sourcemaps')
const uglify = require('gulp-terser')
const sass = require('gulp-sass')
const prefix = require('gulp-autoprefixer')
const clean = require('gulp-clean-css')
const concat = require('gulp-concat')
const argv = require('minimist')(process.argv.slice(2), {
default: { publish: true }
})
const log = require('fancy-log')
const PluginError = require('plugin-error')
const path = require('path')
const { exec } = require('child_process')
const paths = {
content: './src/content.json',
entries: ['src/js/index.js'],
src: {
static: ['src/**/*', '!src/css/**/*.css', '!src/sass/**/*.sass', '!src/js/**/*.js'],
styles: ['src/css/**/*.css', 'src/sass/**/*.sass'],
scripts: ['src/js/**/*.js']
},
dist: {
static: 'dist/',
styles: 'dist/css/',
scripts: 'dist/js/'
}
}
gulp.task('static', function () {
return gulp.src(paths.src.static)
.pipe(gulp.dest(paths.dist.static))
})
gulp.task('styles', function () {
return gulp.src(paths.src.styles)
.pipe(sourcemaps.init())
.pipe(sass({
includePaths: [
'node_modules'
]
}).on('error', sass.logError))
.pipe(prefix())
.pipe(clean())
.pipe(concat('all.css'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.dist.styles))
})
gulp.task('scripts', function () {
return browserify({
basedir: '.',
debug: true,
entries: paths.entries,
cache: {},
packageCache: {},
insertGlobals: true
})
.bundle()
.pipe(source('all.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.dist.scripts))
})
gulp.task('build', gulp.parallel('scripts', 'styles', 'static'))
gulp.task('watch', function () {
gulp.watch(paths.src.scripts, gulp.series('scripts'))
gulp.watch(paths.src.styles, gulp.series('styles'))
gulp.watch(paths.src.static, gulp.series('static'))
})
gulp.task('deploy', gulp.series('build', function (done) {
const silent = argv.silent
const zeronet = argv.zeronet
const privkey = argv.privkey ? argv.privkey : ''
const doPublish = argv.publish
if (!zeronet) {
throw new PluginError({
plugin: 'deploy',
message: 'Empty path for ZeroNet installation'
})
}
const content = require(paths.content)
const address = content.address
const from = [path.join(paths.dist.static, '**/*')]
const to = path.join(zeronet, 'data', address)
log('Downloading site')
const download = exec('python3 zeronet.py siteDownload ' + address, { cwd: zeronet })
if (!silent) {
download.stdout.pipe(process.stdout)
download.stderr.pipe(process.stderr)
}
download.stderr.on('data', function (data) {
if (data.indexOf('Error') >= 0) {
throw new PluginError({
plugin: 'deploy',
message: 'Error while downloading'
})
}
})
download.on('exit', function (code, signal) {
log('Downloading done')
gulp.src(from)
.pipe(gulp.dest(to))
log('Signing site')
const sign = exec('python3 zeronet.py siteSign ' + address + ' ' + privkey, { cwd: zeronet })
if (!silent) {
sign.stdout.pipe(process.stdout)
sign.stderr.pipe(process.stderr)
}
sign.stderr.on('data', function (data) {
if (data.indexOf('SignError') >= 0) {
throw new PluginError({
plugin: 'deploy',
message: 'Private key invalid'
})
}
if (data.indexOf('Error') >= 0) {
throw new PluginError({
plugin: 'deploy',
message: 'Error while signing'
})
}
})
sign.on('exit', function (code, signal) {
log('Signing done')
if (!doPublish) {
log('Skipping publishing')
done()
return
}
log('Publishing site')
const publish = exec('python3 zeronet.py sitePublish ' + address, { cwd: zeronet })
if (!silent) {
publish.stdout.pipe(process.stdout)
publish.stderr.pipe(process.stderr)
}
publish.stderr.on('data', function (data) {
if (data.indexOf('Error') >= 0) {
throw new PluginError({
plugin: 'deploy',
message: 'Error while publishing'
})
}
})
publish.on('exit', function (code, signal) {
log('Publishing done')
done()
})
})
})
}))
gulp.task('default', gulp.series('build'))