1 |
|
2 | 'use strict';
|
3 | var path = require('path');
|
4 |
|
5 | var gulp = require('gulp');
|
6 | var fs = require('fs');
|
7 | var colors = require('ansi-colors');
|
8 |
|
9 | var log = require('../log/logger');
|
10 | const color = require('../log/color');
|
11 |
|
12 | var unlink = require('../lib/unlink');
|
13 | const npm = require('../lib/npm-dependency');
|
14 |
|
15 | var buildNpm = require('../compiler/build-npm');
|
16 |
|
17 |
|
18 |
|
19 | var watchLog = require('../log/watch');
|
20 |
|
21 |
|
22 |
|
23 | var PACKAGE_JSON = 'package.json';
|
24 |
|
25 |
|
26 |
|
27 | exports.build = function (config) {
|
28 | return function (cb) {
|
29 | fs.exists(PACKAGE_JSON, function (is_json_exists) {
|
30 | if (!is_json_exists) {
|
31 | cb && cb();
|
32 | } else {
|
33 | try {
|
34 | const dependencies = npm.getDependencies(process.cwd());
|
35 | if (dependencies && Object.keys(dependencies).length > 0) {
|
36 | fs.exists('node_modules', function (is_modules_exists) {
|
37 | if (!is_modules_exists) {
|
38 | log.error(
|
39 | color('npm:'),
|
40 | colors.yellowBright('node_modules/ doesn\'t exist! please run `' + colors.bgRedBright('npm i') + '`'),
|
41 | );
|
42 | cb && cb(new Error("node_modules/ doesn't exist!"));
|
43 |
|
44 | } else {
|
45 | return buildNpm(process.cwd(), config.dist, Object.keys(dependencies))(cb);
|
46 | }
|
47 | });
|
48 | } else {
|
49 | log.info(
|
50 | color('npm:'),
|
51 | colors.gray('No `dependency` was found. Skips!')
|
52 | );
|
53 | cb && cb();
|
54 | }
|
55 | } catch (error) {
|
56 | log.error(error);
|
57 | cb && cb(error);
|
58 | }
|
59 | }
|
60 | })
|
61 | };
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | exports.watch = function (config) {
|
68 | return function (cb) {
|
69 | watchLog('npm', PACKAGE_JSON);
|
70 | gulp.watch(PACKAGE_JSON, {})
|
71 | .on('change', function () { exports.build(config)(); })
|
72 | .on('add', function () { exports.build(config)(); })
|
73 | .on('unlink', unlink("miniprogram_npm", config.dist));
|
74 | cb && cb();
|
75 | }
|
76 | } |
\ | No newline at end of file |