UNPKG

2.55 kBJavaScriptView Raw
1///@ts-check
2'use strict';
3var path = require('path');
4
5var gulp = require('gulp');
6var fs = require('fs');
7var colors = require('ansi-colors');
8
9var log = require('../log/logger');
10const color = require('../log/color');
11
12var unlink = require('../lib/unlink');
13const npm = require('../lib/npm-dependency');
14
15var buildNpm = require('../compiler/build-npm');
16// var npmInstall = require('../compiler/npm-install');
17// var copy = require('../compiler/copy');
18// var rollup = require('rollup')
19var watchLog = require('../log/watch');
20// var merge = require('merge-stream');
21
22
23var PACKAGE_JSON = 'package.json';
24/**
25 * @param {object} config
26 */
27exports.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 * @param {object} config
66 */
67exports.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