aboutsummaryrefslogtreecommitdiffstats
path: root/gulpfile.js
blob: 26ad0c1f86870a5dce91a272d420c13f30fc424c (plain) (blame)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
var watchify = require('watchify')
var browserify = require('browserify')
var gulp = require('gulp')
var source = require('vinyl-source-stream')
var buffer = require('vinyl-buffer')
var gutil = require('gulp-util')
var watch = require('gulp-watch')
var sourcemaps = require('gulp-sourcemaps')
var jsoneditor = require('gulp-json-editor')
var zip = require('gulp-zip')
var assign = require('lodash.assign')
var livereload = require('gulp-livereload')
var brfs = require('gulp-brfs')
var del = require('del')
var eslint = require('gulp-eslint')
var fs = require('fs')
var path = require('path')
var manifest = require('./app/manifest.json')
var gulpif = require('gulp-if')
var replace = require('gulp-replace')
var disclaimer = fs.readFileSync(path.join(__dirname, 'USER_AGREEMENT.md')).toString()
var crypto = require('crypto')
var hash = crypto.createHash('sha256')

hash.update(disclaimer)
var tosHash = hash.digest('hex')

var disableLiveReload = gutil.env.disableLiveReload
var debug = gutil.env.debug

// browser reload

gulp.task('dev:reload', function() {
  livereload.listen({
    port: 35729,
    // basePath: './dist/firefox/'
  })
})


// copy static

gulp.task('copy:locales', copyTask({
  source: './app/_locales/',
  destinations: [
    './dist/firefox/_locales',
    './dist/chrome/_locales',
    './dist/edge/_locales',
  ]
}))
gulp.task('copy:images', copyTask({
  source: './app/images/',
  destinations: [
    './dist/firefox/images',
    './dist/chrome/images',
    './dist/edge/images',
  ],
}))
gulp.task('copy:fonts', copyTask({
  source: './app/fonts/',
  destinations: [
    './dist/firefox/fonts',
    './dist/chrome/fonts',
    './dist/edge/fonts',
  ],
}))
gulp.task('copy:reload', copyTask({
  source: './app/scripts/',
  destinations: [
    './dist/firefox/scripts',
    './dist/chrome/scripts',
    './dist/edge/scripts',
  ],
  pattern: '/chromereload.js',
}))
gulp.task('copy:root', copyTask({
  source: './app/',
  destinations: [
    './dist/firefox',
    './dist/chrome',
    './dist/edge',
  ],
  pattern: '/*',
}))

gulp.task('manifest:chrome', function() {
  return gulp.src('./dist/chrome/manifest.json')
  .pipe(jsoneditor(function(json) {
    delete json.applications
    return json
  }))
  .pipe(gulp.dest('./dist/chrome', { overwrite: true }))
})

gulp.task('manifest:production', function() {
  return gulp.src([
    './dist/firefox/manifest.json',
    './dist/chrome/manifest.json',
    './dist/edge/manifest.json',
  ],{base: './dist/'})
  .pipe(gulpif(disableLiveReload,jsoneditor(function(json) {
    json.background.scripts = ["scripts/background.js"]
    return json
  })))
  .pipe(gulp.dest('./dist/', { overwrite: true }))
})

const staticFiles = [
  'locales',
  'images',
  'fonts',
  'root'
]

var copyStrings = staticFiles.map(staticFile => `copy:${staticFile}`)

if (!disableLiveReload) {
  copyStrings.push('copy:reload')
}

gulp.task('copy', gulp.series(gulp.parallel(...copyStrings), 'manifest:production', 'manifest:chrome'))
gulp.task('copy:watch', function(){
  gulp.watch(['./app/{_locales,images}/*', './app/scripts/chromereload.js', './app/*.{html,json}'], gulp.series('copy'))
})

// lint js

gulp.task('lint', function () {
  // Ignoring node_modules, dist/firefox, and docs folders:
  return gulp.src(['app/**/*.js', 'ui/**/*.js', '!node_modules/**', '!dist/firefox/**', '!docs/**', '!app/scripts/chromereload.js'])
    .pipe(eslint(fs.readFileSync(path.join(__dirname, '.eslintrc'))))
    // 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())
});

/*
gulp.task('default', ['lint'], function () {
    // This will only run if the lint task is successful...
});
*/

// build js

const jsFiles = [
  'inpage',
  'contentscript',
  'background',
  'popup',
]

var jsDevStrings = jsFiles.map(jsFile => `dev:js:${jsFile}`)
var jsBuildStrings = jsFiles.map(jsFile => `build:js:${jsFile}`)

jsFiles.forEach((jsFile) => {
  gulp.task(`dev:js:${jsFile}`, bundleTask({ watch: true, filename: `${jsFile}.js` }))
  gulp.task(`build:js:${jsFile}`, bundleTask({ watch: false, filename: `${jsFile}.js` }))
})

gulp.task('dev:js', gulp.parallel(...jsDevStrings))

gulp.task('build:js',  gulp.parallel(...jsBuildStrings))


// clean dist


gulp.task('clean', function clean() {
  return del(['./dist/*'])
})

// zip tasks for distribution
gulp.task('zip:chrome', () => {
  return gulp.src('dist/chrome/**')
  .pipe(zip(`metamask-chrome-${manifest.version}.zip`))
  .pipe(gulp.dest('builds'));
})
gulp.task('zip:firefox', () => {
  return gulp.src('dist/firefox/**')
  .pipe(zip(`metamask-firefox-${manifest.version}.zip`))
  .pipe(gulp.dest('builds'));
})
gulp.task('zip:edge', () => {
  return gulp.src('dist/edge/**')
  .pipe(zip(`metamask-edge-${manifest.version}.zip`))
  .pipe(gulp.dest('builds'));
})
gulp.task('zip', gulp.parallel('zip:chrome', 'zip:firefox', 'zip:edge'))

// high level tasks

gulp.task('dev', gulp.series('dev:js', 'copy', gulp.parallel('copy:watch', 'dev:reload')))

gulp.task('build', gulp.series('clean', gulp.parallel('build:js', 'copy')))
gulp.task('dist', gulp.series('build', 'zip'))

// task generators

function copyTask(opts){
  var source = opts.source
  var destination = opts.destination
  var destinations = opts.destinations || [ destination ]
  var pattern = opts.pattern || '/**/*'

  return performCopy

  function performCopy(){
    let stream = gulp.src(source + pattern, { base: source })
    destinations.forEach(function(destination) {
      stream = stream.pipe(gulp.dest(destination))
    })
    stream.pipe(gulpif(!disableLiveReload,livereload()))

    return stream
  }
}

function bundleTask(opts) {
  var browserifyOpts = assign({}, watchify.args, {
    entries: ['./app/scripts/'+opts.filename],
    debug: true,
    plugin: 'browserify-derequire',
  })

  var bundler = browserify(browserifyOpts)
  bundler.transform('brfs')
  if (opts.watch) {
    bundler = watchify(bundler)
    bundler.on('update', performBundle) // on any dep update, runs the bundler
  }

  bundler.on('log', gutil.log) // output build logs to terminal

  return performBundle

  function performBundle(){
    return (

      bundler.bundle()
      // log errors if they happen
      .on('error', gutil.log.bind(gutil, 'Browserify Error'))
      .pipe(source(opts.filename))
      .pipe(brfs())
      .pipe(replace('GULP_TOS_HASH', tosHash))
      .pipe(replace('\'GULP_METAMASK_DEBUG\'', debug))
      // optional, remove if you don't need to buffer file contents
      .pipe(buffer())
      // optional, remove if you dont want sourcemaps
      .pipe(sourcemaps.init({loadMaps: true})) // loads map from browserify file
      // Add transformation tasks to the pipeline here.
      .pipe(sourcemaps.write('./')) // writes .map file
      .pipe(gulp.dest('./dist/firefox/scripts'))
      .pipe(gulp.dest('./dist/chrome/scripts'))
      .pipe(gulp.dest('./dist/edge/scripts'))
      .pipe(gulpif(!disableLiveReload,livereload()))

    )
  }
}