1 | const {
|
2 | api
|
3 | } = require('../index')
|
4 | const ip = require('ip').address()
|
5 | const exit = require('exit')
|
6 | const path = require('path')
|
7 | const uuid = require('uuid')
|
8 | const detect = require('detect-port')
|
9 | const pkg = require('../package.json')
|
10 |
|
11 | module.exports = {
|
12 | name: 'debug',
|
13 | description: 'Debug weex bundle',
|
14 | alias: 'd',
|
15 | run: async ({
|
16 | logger,
|
17 | parameters,
|
18 | compile,
|
19 | meta
|
20 | }) => {
|
21 | const options = parameters.options
|
22 | const source = parameters.first
|
23 | const analyzer = options.__analyzer
|
24 |
|
25 | const showHelp = async () => {
|
26 | let params = {
|
27 | commandend: 'Debug Weex page, also can compile some \`.vue\` page',
|
28 | commands: [
|
29 | {
|
30 | heading: ['Usage', 'Description']
|
31 | },
|
32 | {
|
33 | key: 'debug',
|
34 | type: '',
|
35 | description: 'Just open the devtool server.'
|
36 | },
|
37 | {
|
38 | key: 'debug',
|
39 | type: '[source] --<options>',
|
40 | description: 'Compile source then open devtool server.'
|
41 | }
|
42 | ],
|
43 | options: {
|
44 | 'Base': [
|
45 | {
|
46 | key: '-p, --port',
|
47 | description: 'set default extname for compiler',
|
48 | default: '8080'
|
49 | },
|
50 | {
|
51 | key: '--host',
|
52 | description: 'specify host adress',
|
53 | },
|
54 | {
|
55 | key: '--channelid',
|
56 | description: 'specify debug channel id'
|
57 | },
|
58 | {
|
59 | key: '--manul',
|
60 | default: 'false',
|
61 | description: 'control open browser or not'
|
62 | },
|
63 | {
|
64 | key: '--remote-debug-port',
|
65 | description: 'specify remote debug port for headless chromium',
|
66 | default: '9222'
|
67 | },
|
68 | ],
|
69 | 'Miscellaneous:': [
|
70 | {
|
71 | key:'-v, --version',
|
72 | description: 'Output the version number'
|
73 | },
|
74 | {
|
75 | key:'-h, --help',
|
76 | description: 'Show help'
|
77 | }
|
78 | ]
|
79 | }
|
80 | }
|
81 | meta.generateHelp(params)
|
82 | }
|
83 |
|
84 | const transformOptions = async (options) => {
|
85 | let defaultPort = await detect(8089)
|
86 | return {
|
87 | ip: options.host,
|
88 | port: options.port || options.p || defaultPort,
|
89 | channelId: options.channelid || uuid(),
|
90 | manual: options.manual,
|
91 | remoteDebugPort: options.remoteDebugPort
|
92 | }
|
93 | }
|
94 |
|
95 | let devtoolOptions = await transformOptions(options)
|
96 | let shouldReload = false
|
97 | if (options.help || options.h) {
|
98 | await showHelp()
|
99 | } else if (options.v || options.version) {
|
100 | logger.log(pkg.version)
|
101 | } else {
|
102 | if (source) {
|
103 | await compile(
|
104 | source,
|
105 | path.join(__dirname, '../frontend/public/weex'), {
|
106 | watch: true,
|
107 | filename: '[name].js',
|
108 | web: false,
|
109 | config: options.config || options.c
|
110 | },
|
111 | async (error, output, json) => {
|
112 | let bundles = []
|
113 | if (error) {
|
114 | await analyzer('compile', Array.isArray(error)?error.join('\n'):error)
|
115 | }
|
116 | else {
|
117 | bundles = json.assets.map(asset => {
|
118 | let entry
|
119 | let date = new Date()
|
120 | const formateTime = (value) => {
|
121 | return value < 10 ? '0' + value : value
|
122 | }
|
123 | if (/\./.test(source)) {
|
124 | entry = path.resolve(source)
|
125 | } else {
|
126 | entry = path.resolve(source, asset.name.replace('.js', '.vue'))
|
127 | }
|
128 | return {
|
129 | updateTime: `${date.getFullYear()}-${date.getMonth()+1}-${date.getDay()} ${formateTime(date.getHours())}:${formateTime(date.getMinutes())}:${formateTime(date.getSeconds())}`,
|
130 | output: `http://${ip}:${devtoolOptions.port}/weex/${asset.name}?bundleType=vue`,
|
131 | size: (asset.size / 1024).toFixed(0),
|
132 | time: json.time,
|
133 | entry: entry
|
134 | }
|
135 | })
|
136 | }
|
137 | if (!shouldReload) {
|
138 | shouldReload = true
|
139 | await api.startDevtoolServer(bundles, devtoolOptions)
|
140 | } else {
|
141 | api.reload()
|
142 | }
|
143 | }
|
144 | )
|
145 | } else {
|
146 | await api.startDevtoolServer([], devtoolOptions)
|
147 | }
|
148 | }
|
149 | }
|
150 | } |
\ | No newline at end of file |