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

gulpfile.common.js « gulp - github.com/thsmi/sieve.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7294bbc90606f6e873e22199d6073b8a35a5ba15 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * 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>
 */

const { src, dest } = require('gulp');
const logger = require('gulplog');

const { readdir, unlink, rmdir, readFile, writeFile } = require('fs').promises;
const { createWriteStream, existsSync } = require('fs');

const path = require('path');
const yazl = require('yazl');

const JSON_INDENTATION = 2;

const BASE_DIR_BOOTSTRAP = "./node_modules/bootstrap/dist";
const BASE_DIR_CODEMIRROR = "./node_modules/codemirror";

const BASE_DIR_COMMON = "./src/common";
const BASE_DIR_BUILD = "./build";

const BASE_DIR_LIBSIEVE = path.join(BASE_DIR_COMMON, "libSieve");
const BASE_DIR_MANAGESIEVEUI = path.join(BASE_DIR_COMMON, "managesieve.ui");

const INDEX_MAJOR = 0;
const INDEX_MINOR = 1;
const INDEX_PATCH = 2;

/**
 * Delete all files from the given path.
 *
 * @param  {string} dir
 *   the base path which should be cleared.
 */
async function deleteRecursive(dir) {

  if (!existsSync(dir))
    return;

  const items = await readdir(dir, { withFileTypes: true });

  for (const item of items) {
    const curPath = path.join(dir, item.name);

    if (!item.isDirectory()) {
      await unlink(curPath);
      continue;
    }

    await deleteRecursive(curPath);
  }

  await rmdir(dir);
}

/**
 * Clean the build environment including all build and packaging artifacts.
 */
async function clean() {
  await deleteRecursive(BASE_DIR_BUILD);
}

/**
 * Copies the codemirror sources into the build directory.
 *
 * @param {string} destination
 *   where to place the codemirror sources
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 */
function packageCodeMirror(destination) {

  return src([
    BASE_DIR_CODEMIRROR + "/addon/edit/**",
    BASE_DIR_CODEMIRROR + "/addon/search/**",
    BASE_DIR_CODEMIRROR + "/lib/**",
    BASE_DIR_CODEMIRROR + "/mode/sieve/**",
    BASE_DIR_CODEMIRROR + "/theme/eclipse.css",
    BASE_DIR_CODEMIRROR + "/LICENSE",
    BASE_DIR_CODEMIRROR + "/package.json"
  ], { base: BASE_DIR_CODEMIRROR }).pipe(
    dest(destination));
}

/**
 * Copies the bootstrap sources into the build directory.
 *
 * @param {string} destination
 *   where to place the bootstrap sources
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 **/
function packageBootstrap(destination) {

  return src([
    BASE_DIR_BOOTSTRAP + "/css/*.min.css",
    BASE_DIR_BOOTSTRAP + "/js/*.bundle.min.js"
  ], { base: BASE_DIR_BOOTSTRAP }).pipe(
    dest(destination));
}

/**
 * An src clone which reasonable default values which avoid code duplication
 *
 * @param {string} dir
 *   the source directory which contains the files
 * @param {string|string[]} [files]
 *   the globs to be applied to the directory. If omitted it will select
 *   everything except a "doc" folder.
 * @returns {Stream}
 *   a vinyl file stream.
 */
function src2(dir, files) {

  if (!files)
    files = [`./**`, `!./doc/**`];

  if (!Array.isArray(files))
    files = [files];

  return src(
    files, { base: dir, root: dir, cwd:dir, passthrough: true });
}

/**
 * Packages the common libSieve files
 *
 * @param {string} destination
 *   where to place the common libSieve files
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 */
function packageLibSieve(destination) {
  return src([
    BASE_DIR_LIBSIEVE + "/**",
    "!" + BASE_DIR_LIBSIEVE + "/**/rfc*.txt",
    "!" + BASE_DIR_LIBSIEVE + "/**/tests/",
    "!" + BASE_DIR_LIBSIEVE + "/**/tests/**"
  ], { base: BASE_DIR_COMMON }).pipe(dest(destination));
}

/**
 * Packages the common managesieve.ui files
 *
 * @param {string} destination
 *   where to place the common managesieve.ui files
 *
 * @returns {Stream}
 *   a stream to be consumed by gulp
 */
function packageManageSieveUi(destination) {
  return src([
    BASE_DIR_MANAGESIEVEUI + "/**"
  ], { base: BASE_DIR_COMMON }).pipe(dest(destination));
}

/**
 * Extracts the version from the package.json file
 *
 * @param {string} [file]
 *   the path to the package json file.
 * @returns {int[]}
 *   the version as a triple of integer
 */
async function getPackageVersion(file) {

  if ((typeof (file) === "undefined") || file === null)
    file = "./package.json";

  let version = JSON.parse(await readFile(file, 'utf8')).version;

  version = version.split(".");

  while (version.length < 3)
    version.push(0);

  return version;
}

/**
 * Updates the version in a package json file.
 *
 * @param {string} version
 *   the new version string
 * @param {string} [file]
 *   the path to the npm package json file.
 */
async function setPackageVersion(version, file) {

  if ((typeof (file) === "undefined") || file === null)
    file = "./package.json";

  version = version.join(".");

  logger.info(`Updating ${file} to ${version}`);

  const data = JSON.parse(await readFile(file, 'utf8'));
  data.version = version;

  await writeFile(file, JSON.stringify(data, null, JSON_INDENTATION), 'utf-8');
}

// We can only use major, minor and patch. Everything else
// clashes with mozilla's naming semantic.

/**
 * Bumps the package.json version info to the next major version.
 * The minor and patch level is reset to zero
 */
async function bumpMajorVersion() {

  const pkgVersion = await getPackageVersion('./package.json');

  logger.info("Major bump from " + pkgVersion.join(".") + " ...");

  pkgVersion[INDEX_MAJOR] = parseInt(pkgVersion[INDEX_MAJOR], 10) + 1;
  pkgVersion[INDEX_MINOR] = 0;
  pkgVersion[INDEX_PATCH] = 0;

  logger.info("... to " + pkgVersion.join("."));

  await setPackageVersion(pkgVersion, './package.json');
}

/**
 * Bumps the package.json version info to the next minor version.
 * The major version remains untouched but the patch level is reset to zero
 */
async function bumpMinorVersion() {

  const pkgVersion = await getPackageVersion('./package.json');

  logger.info("Minor bump from " + pkgVersion.join("."));

  pkgVersion[INDEX_MINOR] = parseInt(pkgVersion[INDEX_MINOR], 10) + 1;
  pkgVersion[INDEX_PATCH] = 0;

  logger.info("... to " + pkgVersion.join("."));

  await setPackageVersion(pkgVersion, './package.json');
}

/**
 * Pumps the package.json version info to the next patch level.
 * Neither the major nor the minor version will be changed.
 */
async function bumpPatchVersion() {

  const pkgVersion = await getPackageVersion('./package.json');

  logger.info("Patch bump from " + pkgVersion.join("."));

  pkgVersion[INDEX_PATCH] = parseInt(pkgVersion[INDEX_PATCH], 10) + 1;

  logger.info("... to " + pkgVersion.join("."));

  await setPackageVersion(pkgVersion, './package.json');
}

/**
 * Compresses the given file or directory recursively.
 *
 * You can set special file permissions via the options.
 * See the parent compress method for more details.
 *
 * The path's of zipped files are stored relative to an
 * root directory. By default the root directory is set to
 * the source directory. But you can override it by setting
 * "options.root".
 *
 * @param {ZipFile} zip
 *   the yazl object
 * @param {string} dir
 *   the directory or file which should be compressed.
 * @param {object} options
 *   extended instructions for compressing.
 */
async function compressDirectory(zip, dir, options) {

  if (typeof (options) === "undefined" || options === null)
    options = {};

  if (!options.root)
    options.root = dir;

  const dirs = await readdir(dir, { withFileTypes: true });

  for (const item of dirs) {

    const realPath = path.join(dir, item.name);
    const metaPath = path.relative(options.root, realPath);

    if (item.isDirectory()) {
      zip.addEmptyDirectory(metaPath);
      await compressDirectory(zip, realPath, options);
      continue;
    }

    let fileOptions = null;
    if (options.permissions) {
      if (options.permissions[metaPath])
        fileOptions = { mode: options.permissions[metaPath] };

      if (options.permissions["*"]) {
        fileOptions = { mode: options.permissions["*"] };
      }
    }

    zip.addFile(realPath, metaPath, fileOptions);
  }
}

/**
 * Stores and compresses all data from the source directory
 * into the destination file
 *
 * You can change the default permissions for all files by setting
 * the option "permissions[*]" to the desired permission.
 *
 * To change the permission for a single file just specify the
 * meta file name instead of the asterisk.
 *
 * @param {string} source
 *   the source directory or file.
 * @param {string} destination
 *   the destination file. In case it exists it will be overwritten.
 * @param {object} options
 *   extended instructions for compressing.
 */
async function compress(source, destination, options) {

  if (existsSync(destination)) {
    logger.info(`Deleting ${path.basename(destination)}`);
    await unlink(destination);
  }

  logger.info(`Collecting files ${source}`);

  const zip = new yazl.ZipFile();

  await compressDirectory(zip, source, options);

  logger.info(`Compressing files ${path.basename(destination)}`);

  await new Promise((resolve) => {
    zip.outputStream
      .pipe(createWriteStream(destination))
      .on("close", () => { resolve(); });

    zip.end();
  });
}

exports["clean"] = clean;
exports["compress"] = compress;

exports["packageCodeMirror"] = packageCodeMirror;
exports["packageBootstrap"] = packageBootstrap;

exports["packageLibSieve"] = packageLibSieve;
exports["packageManageSieveUi"] = packageManageSieveUi;

exports["getPackageVersion"] = getPackageVersion;
exports["setPackageVersion"] = setPackageVersion;

exports["bumpMajorVersion"] = bumpMajorVersion;
exports["bumpMinorVersion"] = bumpMinorVersion;
exports["bumpPatchVersion"] = bumpPatchVersion;

exports["BASE_DIR_BUILD"] = BASE_DIR_BUILD;
exports["BASE_DIR_COMMON"] = BASE_DIR_COMMON;

exports["src2"] = src2;