scripting-cli 1.0.8 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,147 +0,0 @@
1
- import http from 'node:http'
2
- import path from 'node:path'
3
- import { readFile, } from 'node:fs/promises'
4
- import chalk from 'chalk'
5
- import express from 'express'
6
- import minimist from 'minimist'
7
- import qrcode from 'qrcode-terminal'
8
-
9
- import { Server, Socket } from 'socket.io'
10
-
11
- import * as utils from './utils'
12
-
13
- import Webpack from "webpack"
14
- import getConfig from './config/webpack.config'
15
-
16
- export function startDevServer() {
17
- const args = minimist(process.argv.slice(2))
18
-
19
- const ipAddress = utils.getIpAddress()
20
- const port = args['port'] || 8000
21
-
22
- const app = express()
23
- const server = http.createServer(app)
24
- const io = new Server(server)
25
-
26
-
27
- let isStarted = false
28
- let connectedSockets: Socket[] = []
29
-
30
- const webpackConfig = getConfig()
31
-
32
- webpackConfig.mode = 'development'
33
-
34
- const compiler = Webpack(webpackConfig)
35
-
36
- console.log(
37
- 'Start running webpack compiler in watch mode...'
38
- )
39
-
40
- compiler.watch({}, (err, stats) => {
41
- utils.clearConsole()
42
- if (err) {
43
- console.log(
44
- chalk.red(' ✖️ webpack compile error: \n') + err
45
- )
46
- } else {
47
- console.log(stats?.toString({
48
- chunks: false,
49
- colors: true,
50
- }))
51
-
52
- if (stats?.toJson().errorsCount === 0) {
53
- onCompileDone()
54
- }
55
- }
56
- })
57
-
58
- async function onCompileDone() {
59
- if (isStarted) {
60
- printDevServerAddress()
61
-
62
- console.log(
63
- chalk.gray(` ~ Sent update event to ${connectedSockets.length} client(s)`)
64
- )
65
-
66
- const scriptJson = await readFile(
67
- path.resolve(
68
- process.cwd(),
69
- 'build/script.json'
70
- ),
71
- 'utf-8'
72
- )
73
-
74
- for (const socket of connectedSockets) {
75
- socket.emit('update', scriptJson)
76
- }
77
- } else {
78
- start()
79
- }
80
- }
81
-
82
- function printDevServerAddress() {
83
- console.log(`Server running at ${chalk.bold(
84
- chalk.green(
85
- `http://${ipAddress}:${port}`
86
- ),
87
- )}`)
88
- console.log(`Use the Scripting app to scan the QR code below:`)
89
- qrcode.generate(`http://${ipAddress}:${port}`, { small: true })
90
- }
91
-
92
- function start() {
93
- if (isStarted) {
94
- return
95
- }
96
-
97
- app.all('*', function (req, res, next) {
98
- res.header('Access-Control-Allow-Origin', '*')
99
- res.header('Access-Control-Allow-Headers', '*')
100
- res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, OPTIONS, PUT, DELETE')
101
- next()
102
- })
103
-
104
- app.use(
105
- '/',
106
- express.static(
107
- path.resolve(process.cwd(), 'build')
108
- )
109
- )
110
-
111
- //有新的客户端连接时触发
112
- io.on('connection', function (socket) {
113
- console.log(
114
- chalk.green(
115
- ` + A client(${socket.id}) connected.`
116
- )
117
- )
118
- connectedSockets.push(socket)
119
-
120
- socket.on('error', function (err) {
121
- console.log(
122
- chalk.red(
123
- ' ✖️ Server Error: ', err
124
- )
125
- )
126
- })
127
-
128
- socket.on('disconnect', function () {
129
- var index = connectedSockets.indexOf(socket)
130
- if (index > -1) {
131
- connectedSockets.splice(index, 1)
132
- console.log(
133
- chalk.gray(
134
- ` - A client(${socket.id}) disconnected.`
135
- )
136
- )
137
- }
138
- })
139
- })
140
-
141
- server.listen(port)
142
- printDevServerAddress()
143
- isStarted = true
144
- }
145
-
146
- }
147
-
package/src/utils.ts DELETED
@@ -1,30 +0,0 @@
1
-
2
- import os from 'node:os'
3
-
4
- export function getIpAddress() {
5
- const interfaces = os.networkInterfaces()
6
-
7
- for (const devName in interfaces) {
8
- const iface = interfaces[devName]
9
-
10
- if (iface == null) {
11
- continue
12
- }
13
-
14
- for (let i = 0; i < iface.length; i++) {
15
- const alias = iface[i]
16
- if (alias.family === 'IPv4'
17
- && alias.address !== '127.0.0.1'
18
- && !alias.internal
19
- ) {
20
- return alias.address
21
- }
22
- }
23
- }
24
- }
25
-
26
- export function clearConsole() {
27
- process.stdout.write(
28
- process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H'
29
- )
30
- }