1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | const fs = require('fs'),
|
11 | path = require('path'),
|
12 | through2 = require('through2'),
|
13 | PluginError = require('plugin-error'),
|
14 | Statics = require('../config/statics');
|
15 |
|
16 | const PLUGIN_NAME = 'publish';
|
17 |
|
18 |
|
19 | function publish(options) {
|
20 | let statics = new Statics(options);
|
21 |
|
22 | return through2.obj(function(file, enc, next) {
|
23 | if (file.isStream()) {
|
24 | this.emit('error', new PluginError(PLUGIN_NAME, 'Stream content is not supported'));
|
25 | return next(null, file);
|
26 | }
|
27 |
|
28 | if (file.isBuffer()) {
|
29 | let extname = path.extname(file.path);
|
30 | if (statics.isExecute === true && !!statics.hostname && statics.testExt.test(extname)) {
|
31 | try {
|
32 | let content = statics.execture(file) || file.contents;
|
33 | file.contents = new Buffer(content);
|
34 | } catch (err) {
|
35 | this.emit('error', new PluginError(PLUGIN_NAME, err.message));
|
36 | }
|
37 | }
|
38 | }
|
39 | this.push(file);
|
40 | return next();
|
41 | });
|
42 | }
|
43 |
|
44 | module.exports = publish;
|