1 | let bundler = require("./base/bundler");
|
2 | let path = require("path");
|
3 | let chokidar = require('chokidar');
|
4 | let Path = require("path");
|
5 | let colors = require("colors");
|
6 | let util = require("./base/util");
|
7 | let maker = require("./base/maker/maker");
|
8 |
|
9 | let waiter = {
|
10 | _data: {},
|
11 | _tid: null,
|
12 | _time: 500,
|
13 | _fn: null,
|
14 | _times: 0,
|
15 | add: function (type, path) {
|
16 | if (!this._data[type]) {
|
17 | this._data[type] = [];
|
18 | }
|
19 | if (this._data[type].indexOf(path) === -1) {
|
20 | this._data[type].push(path);
|
21 | }
|
22 | if (this.tid !== null) {
|
23 | clearTimeout(this.tid);
|
24 | }
|
25 | setTimeout(() => {
|
26 | let _has = false;
|
27 | for (let i in this._data) {
|
28 | _has = true;
|
29 | }
|
30 | if (_has) {
|
31 | this._fn && this._fn(this._data, this._times);
|
32 | this._times++;
|
33 | }
|
34 | this._data = {};
|
35 | this._tid = null;
|
36 | }, this._time);
|
37 | return this;
|
38 | },
|
39 | setHandler: function (fn) {
|
40 | this._fn = fn;
|
41 | return this;
|
42 | }
|
43 | };
|
44 |
|
45 | module.exports = {
|
46 | develop(appPath = "", fn) {
|
47 | return new Promise((resolve, reject) => {
|
48 | util.getAppInfo(appPath).then(config => {
|
49 | let basePath = path.resolve(appPath, "./../");
|
50 | bundler(Object.assign({
|
51 | base_path: basePath,
|
52 | develop: true,
|
53 | projectPath: path.resolve(__dirname, "./../../"),
|
54 | complete() {
|
55 | resolve();
|
56 | }
|
57 | }, config)).then(_bundler => {
|
58 | chokidar.watch(path.resolve(basePath, config.source_path), {ignored: /[\/\\]\./}).on('change', function (path) {
|
59 | waiter.add("edit", path);
|
60 | }).on('add', function (path) {
|
61 | waiter.add("add", path);
|
62 | }).on('unlink', function (path) {
|
63 | waiter.add("remove", path);
|
64 | }).on("ready", function () {
|
65 | waiter.setHandler(function (a, times) {
|
66 | if (a.add) {
|
67 | _bundler.addFiles(a.add).then((info) => {
|
68 | fn && fn({
|
69 | type: "add",
|
70 | files: a.add.map(a => a.substring(Path.resolve(basePath, config.source_path).length + 1).replace(/\\/g, "/")),
|
71 | map: info.map,
|
72 | log: info.log
|
73 | });
|
74 | });
|
75 | } else if (a.edit) {
|
76 | _bundler.editFiles(a.edit).then((info) => {
|
77 | fn && fn({
|
78 | type: "edit",
|
79 | files: a.edit.map(a => a.substring(Path.resolve(basePath, config.source_path).length + 1).replace(/\\/g, "/")),
|
80 | map: info.map,
|
81 | log: info.log
|
82 | });
|
83 | });
|
84 | } else if (a.remove) {
|
85 | _bundler.editFiles(a.remove).then((info) => {
|
86 | fn && fn({
|
87 | type: "remove",
|
88 | files: a.remove.map(a => a.substring(Path.resolve(basePath, config.source_path).length + 1).replace(/\\/g, "/")),
|
89 | map: info.map,
|
90 | log: info.log
|
91 | });
|
92 | });
|
93 | }
|
94 | });
|
95 | });
|
96 | });
|
97 | });
|
98 | });
|
99 | },
|
100 | publish(appPath = "") {
|
101 | util.getAppInfo(appPath).then(config => {
|
102 | let basePath = path.resolve(appPath, "./../");
|
103 | return bundler(Object.assign({
|
104 | base_path: basePath,
|
105 | develop: false,
|
106 | projectPath: path.resolve(__dirname, "./../../")
|
107 | }, config)).then(_bundler => {
|
108 | return _bundler.publish();
|
109 | });
|
110 | });
|
111 | }
|
112 | }; |
\ | No newline at end of file |