Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gulpfile.wx.mjs « gulp - github.com/thsmi/sieve.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68dc0cb65f77f374d75dd0be9713a3144b494b04 (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * The content of this file is licensed. You may obtain a copy of
 * the license at https://github.com/thsmi/sieve/ or request it via
 * email from the author.
 *
 * Do not remove or change this comment.
 *
 * The initial author of the code is:
 *   Thomas Schmid <schmid-thomas@gmx.net>
 */

import gulp from 'gulp';

import common from "./gulpfile.common.mjs";

import path from 'path';
import { Stream } from 'stream';

const BUILD_DIR_WX = path.join(common.BASE_DIR_BUILD, "wx");
const BUILD_DIR_WX_LIBS = path.join(BUILD_DIR_WX, '/libs');

const BASE_DIR_WX = "./src/wx/";



/**
 * A gulp helper to rename mjs modules into js.
 * This is needed due to a bug in thunderbird.
 *
 * It means renaming the files as well as all of their imports.
 */
class TransposeMjsToJs extends Stream.Transform {

  /**
   * Create a new instance
   */
  constructor() {
    super({ readableObjectMode: true, writableObjectMode: true });
  }

  /**
   * Implements the stream's transform method which does the actual
   * work. It renames all mjs files and adjusts the imports.
   *
   * @param {File} file
   *   the vinyl file object
   * @param {*} enc
   *   the encoding
   * @param {Function} cb
   *   the callback which is called when processing is completed.
   */
  _transform(file, enc, cb) {
    // HTML, js and mjs files can reference other imports
    if ((file.extname !== ".js") && (file.extname !== ".mjs") && file.extname !== ".html") {
      cb(null, file);
      return;
    }

    if (!file.isBuffer()) {
      cb(null, file);
      return;
    }

    // Rename mjs to js
    if (file.extname === ".mjs")
      file.extname = ".js";

    // Update their import sections.
    if (file.extname === ".js") {
      let content = file.contents.toString();
      content = content.replace(
        /(import\s*({[\s\w,]*}\s*from\s*)?)['"]([\w./-]*)\.mjs['"]/gm,
        '$1"$3.js"');
      file.contents = Buffer.from(content);
    }

    if (file.extname === ".html") {
      let content = file.contents.toString();

      content = content.replace(
        /(<script\s*type="module"\s*src=['"])([\w./-]*)\.mjs(['"]\s*>)/gm,
        '$1$2.js$3');

      file.contents = Buffer.from(content);
    }

    cb(null, file);
  }
}

/**
 * Copies the license file into the build directory.
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 */
function packageLicense() {
  return gulp.src([
    "./LICENSE.md"
  ]).pipe(gulp.dest(BUILD_DIR_WX));
}


/**
 * Copies the codemirror sources into the build directory.
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 */
function packageCodeMirror() {
  return common.packageCodeMirror(
    `${BUILD_DIR_WX}/libs/CodeMirror`);
}

/**
 * Copies the bootstrap sources into the build directory.
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 **/
function packageBootstrap() {
  return common.packageBootstrap(
    `${BUILD_DIR_WX}/libs/bootstrap`);
}

/**
 * Copies the source files into the app/ directory...
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 */
function packageSrc() {
  const options = {
    files : [
      "./**",
      "!./libs/libManageSieve/**",
      "!./api/**"
    ],
    transpose : new TransposeMjsToJs()
  };

  return common.pack(
    BASE_DIR_WX, BUILD_DIR_WX, options);
}

/**
 * Copies the application's icons into the lib folder.
 * We use it internally for windows decoration.
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 */
function packageIcons() {

  return gulp.src([
    path.join(common.BASE_DIR_COMMON, "icons") + "/**"
  ], { base: common.BASE_DIR_COMMON }).pipe(gulp.dest(BUILD_DIR_WX_LIBS));
}

/**
 * Copies the all libManageSieve files into the app's lib folder.
 * It mixes the common files with the app specific.
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 */
function packageLibManageSieve() {

  const BASE_LIB_DIR_WX = path.join(BASE_DIR_WX, "libs", "libManageSieve");
  const BASE_LIB_DIR_COMMON = path.join(common.BASE_DIR_COMMON, "libManageSieve");

  return common.pack(
    [BASE_LIB_DIR_WX, BASE_LIB_DIR_COMMON],
    path.join(BUILD_DIR_WX_LIBS, "libManageSieve"),
    { transpose: new TransposeMjsToJs() }
  );
}

/**
 * Packages the webextension api experiments.
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 */
function packageExperiments() {

  return common.pack(
    path.join(BASE_DIR_WX, "api"),
    path.join(BUILD_DIR_WX, "api"));
}

/**
 * Copies the common libSieve files into the app's lib folder
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 */
function packageLibSieve() {
  return common.packageLibSieve(
    BUILD_DIR_WX_LIBS,
    new TransposeMjsToJs());
}


/**
 * Copies the common managesieve.ui files into the app's lib folder
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 */
function packageManageSieveUi() {
  return common.packageManageSieveUi(BUILD_DIR_WX_LIBS, new TransposeMjsToJs());
}


/**
 * Watches for changed source files and copies them into the build directory.
 */
function watchSrc() {

  gulp.watch(
    ['./src/**/*.js',
      './src/**/*.mjs',
      './src/**/*.html',
      './src/**/*.css',
      './src/**/*.json'],
    gulp.parallel(
      packageSrc,
      packageManageSieveUi,
      packageLibSieve,
      packageLibManageSieve)
  );
}

/**
 * Updates the WebExtension's version.
 * It reads the information from the npm package and updates the install.rdf as well as the manifest.json
 */
async function updateVersion() {

  const pkgVersion = await common.getPackageVersion();
  await common.setPackageVersion(pkgVersion, './src/wx/manifest.json');
}

/**
 * Zips the build directory and creates a XPI inside the release folder.
 */
async function packageXpi() {

  const version = (await common.getPackageVersion()).join(".");

  const destination = path.resolve(common.BASE_DIR_BUILD, `sieve-${version}.xpi`);
  const source = path.resolve(`./${BUILD_DIR_WX}/`);

  await common.compress(source, destination);
}

export default {
  watch: watchSrc,
  updateVersion,
  packageCodeMirror,
  packageBootstrap,
  packageLicense,
  packageSrc,

  packageWx: gulp.series(
    gulp.parallel(
      packageCodeMirror,
      packageBootstrap,
      packageLicense,
      packageIcons,
      packageLibManageSieve,
      packageLibSieve,
      packageManageSieveUi,
      packageExperiments
    ),
    packageSrc
  ),

  packageXpi,

  BASE_DIR_WX
};