scripting-cli 1.0.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.
- package/.vscode/settings.json +6 -0
- package/dist/build.js +32 -0
- package/dist/config/AppJsonPlugin.js +72 -0
- package/dist/config/webpack.config.js +79 -0
- package/dist/createApp.js +178 -0
- package/dist/index.js +23 -0
- package/dist/startDevServer.js +113 -0
- package/dist/utils.js +29 -0
- package/package.json +41 -0
- package/public/scripting.d.ts +3478 -0
- package/src/build.ts +31 -0
- package/src/config/AppJsonPlugin.ts +96 -0
- package/src/config/webpack.config.ts +80 -0
- package/src/createApp.ts +206 -0
- package/src/index.ts +20 -0
- package/src/startDevServer.ts +136 -0
- package/src/utils.ts +30 -0
- package/tsconfig.json +98 -0
package/dist/build.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.buildApp = void 0;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const webpack_1 = __importDefault(require("webpack"));
|
|
9
|
+
const webpack_config_1 = __importDefault(require("./config/webpack.config"));
|
|
10
|
+
function buildApp() {
|
|
11
|
+
const compiler = (0, webpack_1.default)(webpack_config_1.default);
|
|
12
|
+
console.log('Start running build...\n'
|
|
13
|
+
// +
|
|
14
|
+
// chalk.green(
|
|
15
|
+
// Object.keys(webpackConfig.entry).map(e => ` - ${e}`).join('\n')
|
|
16
|
+
// )
|
|
17
|
+
// +
|
|
18
|
+
// '\n'
|
|
19
|
+
);
|
|
20
|
+
compiler.run((err, stats) => {
|
|
21
|
+
if (err) {
|
|
22
|
+
console.log(chalk_1.default.red('Webpack compile error: \n') + err);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
console.log(stats === null || stats === void 0 ? void 0 : stats.toString({
|
|
26
|
+
chunks: false,
|
|
27
|
+
colors: false,
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
exports.buildApp = buildApp;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class AppJsonPlugin {
|
|
4
|
+
apply(compiler) {
|
|
5
|
+
const pluginName = 'AppJsonPlugin';
|
|
6
|
+
// webpack 模块实例,可以通过 compiler 对象访问,
|
|
7
|
+
// 这样确保使用的是模块的正确版本
|
|
8
|
+
// (不要直接 require/import webpack)
|
|
9
|
+
const { webpack } = compiler;
|
|
10
|
+
// Compilation 对象提供了对一些有用常量的访问。
|
|
11
|
+
const { Compilation } = webpack;
|
|
12
|
+
// RawSource 是其中一种 “源码”("sources") 类型,
|
|
13
|
+
// 用来在 compilation 中表示资源的源码
|
|
14
|
+
const { RawSource } = webpack.sources;
|
|
15
|
+
// 绑定到 “thisCompilation” 钩子,
|
|
16
|
+
// 以便进一步绑定到 compilation 过程更早期的阶段
|
|
17
|
+
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
|
|
18
|
+
// 绑定到资源处理流水线(assets processing pipeline)
|
|
19
|
+
compilation.hooks.processAssets.tap({
|
|
20
|
+
name: pluginName,
|
|
21
|
+
// 用某个靠后的资源处理阶段,
|
|
22
|
+
// 确保所有资源已被插件添加到 compilation
|
|
23
|
+
stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE,
|
|
24
|
+
}, (assets) => {
|
|
25
|
+
const appMap = {};
|
|
26
|
+
Object.keys(assets).forEach(path => {
|
|
27
|
+
const [appName, ...others] = path.split('/');
|
|
28
|
+
const filePath = others[others.length - 1];
|
|
29
|
+
// console.log(appName, filePath)
|
|
30
|
+
// if (appMap[appName] == null) {
|
|
31
|
+
// appMap[appName] = {
|
|
32
|
+
// appName,
|
|
33
|
+
// // assets: [],
|
|
34
|
+
// }
|
|
35
|
+
// }
|
|
36
|
+
if (filePath.match(/\.js$/i)) {
|
|
37
|
+
appMap[appName] = {
|
|
38
|
+
appName,
|
|
39
|
+
index: filePath,
|
|
40
|
+
hash: filePath.replace('.js', '')
|
|
41
|
+
};
|
|
42
|
+
// appMap[appName].index = filePath
|
|
43
|
+
// appMap[appName].hash = filePath.replace('.js', '')
|
|
44
|
+
}
|
|
45
|
+
// else if (filePath.match(/\.(png|jpe?g|gif|svg)$/i)) {
|
|
46
|
+
// appMap[appName].assets.push(path.replace(appName + '/', ''))
|
|
47
|
+
// }
|
|
48
|
+
});
|
|
49
|
+
Object.entries(appMap).forEach(([appName, content]) => {
|
|
50
|
+
compilation.emitAsset(appName + '/app.json', new RawSource(JSON.stringify(content, null, 2)));
|
|
51
|
+
});
|
|
52
|
+
// "assets" 是一个包含 compilation 中所有资源(assets)的对象。
|
|
53
|
+
// 该对象的键是资源的路径,
|
|
54
|
+
// 值是文件的源码
|
|
55
|
+
// 遍历所有资源,
|
|
56
|
+
// 生成 Markdown 文件的内容
|
|
57
|
+
// const content =
|
|
58
|
+
// '# In this build:\n\n' +
|
|
59
|
+
// Object.keys(assets)
|
|
60
|
+
// .map((filename) => `- ${filename}`)
|
|
61
|
+
// .join('\n');
|
|
62
|
+
// // 向 compilation 添加新的资源,
|
|
63
|
+
// // 这样 webpack 就会自动生成并输出到 output 目录
|
|
64
|
+
// compilation.emitAsset(
|
|
65
|
+
// this.options.outputFile,
|
|
66
|
+
// new RawSource(content)
|
|
67
|
+
// );
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.default = AppJsonPlugin;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const path_1 = __importDefault(require("path"));
|
|
7
|
+
const terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin"));
|
|
8
|
+
const AppJsonPlugin_1 = __importDefault(require("./AppJsonPlugin"));
|
|
9
|
+
const baseDir = path_1.default.resolve(__dirname, '..');
|
|
10
|
+
const srcDir = path_1.default.resolve(baseDir, 'src');
|
|
11
|
+
const buildDir = path_1.default.resolve(baseDir, 'build');
|
|
12
|
+
const config = {
|
|
13
|
+
mode: 'production',
|
|
14
|
+
entry: path_1.default.join(srcDir, 'index.tsx'),
|
|
15
|
+
optimization: {
|
|
16
|
+
moduleIds: 'deterministic',
|
|
17
|
+
chunkIds: 'named',
|
|
18
|
+
minimizer: [
|
|
19
|
+
new terser_webpack_plugin_1.default({
|
|
20
|
+
extractComments: false,
|
|
21
|
+
terserOptions: {
|
|
22
|
+
format: {
|
|
23
|
+
comments: false,
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
}),
|
|
27
|
+
],
|
|
28
|
+
},
|
|
29
|
+
module: {
|
|
30
|
+
rules: [
|
|
31
|
+
{
|
|
32
|
+
test: /\.tsx?$/,
|
|
33
|
+
use: 'ts-loader',
|
|
34
|
+
exclude: /node_modules/,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
test: /\.(png|jpe?g|gif|svg)$/i,
|
|
38
|
+
loader: 'file-loader',
|
|
39
|
+
options: {
|
|
40
|
+
name: '[path][contenthash:8].[ext]',
|
|
41
|
+
outputPath: (url, resourcePath, context) => {
|
|
42
|
+
// if (url.startsWith('src/app/')) {
|
|
43
|
+
// return url.replace('src/app/', '')
|
|
44
|
+
// }
|
|
45
|
+
return url.replace('src/', '');
|
|
46
|
+
// return url.replace(/src\/([^/]+)\//, '')
|
|
47
|
+
},
|
|
48
|
+
publicPath: (url, resourcePath, context) => {
|
|
49
|
+
// console.log([url, resourcePath])
|
|
50
|
+
// return url.replace(/.*src\/app\/(\w+\/)?/, '')
|
|
51
|
+
// if (url.startsWith('src/app/')) {
|
|
52
|
+
// return url.replace('src/app/', '')
|
|
53
|
+
// }
|
|
54
|
+
return url.replace('src/', '');
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
resolve: {
|
|
61
|
+
extensions: ['.tsx', '.ts', '.js'],
|
|
62
|
+
modules: ['node_modules', srcDir,],
|
|
63
|
+
alias: {
|
|
64
|
+
'@': srcDir,
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
output: {
|
|
68
|
+
// filename: 'index.js',
|
|
69
|
+
// filename: '[name]/index.js',
|
|
70
|
+
filename: '[name]/[contenthash:8].js',
|
|
71
|
+
path: buildDir,
|
|
72
|
+
clean: true,
|
|
73
|
+
},
|
|
74
|
+
plugins: [
|
|
75
|
+
new AppJsonPlugin_1.default(),
|
|
76
|
+
],
|
|
77
|
+
devtool: false,
|
|
78
|
+
};
|
|
79
|
+
exports.default = config;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.createApp = void 0;
|
|
16
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
17
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
18
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
19
|
+
const promises_1 = require("node:fs/promises");
|
|
20
|
+
console.log(__dirname);
|
|
21
|
+
function getAppDir(appName) {
|
|
22
|
+
return node_path_1.default.join(__dirname, appName);
|
|
23
|
+
}
|
|
24
|
+
function createAppDir(appName) {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
const dir = node_path_1.default.join(getAppDir(appName), 'src');
|
|
27
|
+
if (node_fs_1.default.existsSync(dir)) {
|
|
28
|
+
throw new Error(`Directory "./${appName}" already exists.`);
|
|
29
|
+
}
|
|
30
|
+
yield (0, promises_1.mkdir)(dir, { recursive: true });
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function createEntryFile(appName) {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
console.log(` > Creating ${appName}/src/index.tsx ...`);
|
|
36
|
+
const filePath = node_path_1.default.join(getAppDir(appName), '/src/index.tsx');
|
|
37
|
+
yield (0, promises_1.writeFile)(filePath, `
|
|
38
|
+
const { runApp, createElement, RouterProvider, Router, } = Scripting
|
|
39
|
+
const { Center, Text, CupertinoPageScaffold, CupertinoNavigationBar, } = Scripting.Widgets
|
|
40
|
+
|
|
41
|
+
function IndexPage() {
|
|
42
|
+
return (
|
|
43
|
+
<CupertinoPageScaffold
|
|
44
|
+
navigationBar={
|
|
45
|
+
<CupertinoNavigationBar
|
|
46
|
+
middle={
|
|
47
|
+
<Text>${appName}</Text>
|
|
48
|
+
}
|
|
49
|
+
/>
|
|
50
|
+
}
|
|
51
|
+
>
|
|
52
|
+
<Center>
|
|
53
|
+
<Text>Welcome to Scripting!</Text>
|
|
54
|
+
</Center>
|
|
55
|
+
</CupertinoPageScaffold>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const router = new Router([
|
|
60
|
+
{
|
|
61
|
+
path: '/',
|
|
62
|
+
element: <IndexPage />,
|
|
63
|
+
},
|
|
64
|
+
])
|
|
65
|
+
|
|
66
|
+
runApp(
|
|
67
|
+
<RouterProvider router={router} />
|
|
68
|
+
)
|
|
69
|
+
`, 'utf-8');
|
|
70
|
+
console.log(chalk_1.default.green(` ✔️ Created ${appName}/src/index.tsx`));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function createPackageJson(appName) {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
console.log(` > Creating ${appName}/package.json ...`);
|
|
76
|
+
const filePath = node_path_1.default.join(getAppDir(appName), 'package.json');
|
|
77
|
+
yield (0, promises_1.writeFile)(filePath, `{
|
|
78
|
+
"name": "${appName}",
|
|
79
|
+
"version": "0.0.1",
|
|
80
|
+
"private": true,
|
|
81
|
+
"scripts": {
|
|
82
|
+
"dev": "npx scripting-cli dev",
|
|
83
|
+
"build": "npx scripting-cli build"
|
|
84
|
+
}
|
|
85
|
+
}`, 'utf-8');
|
|
86
|
+
console.log(chalk_1.default.green(` ✔️ Created ${appName}/package.json`));
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function createReadme(appName) {
|
|
90
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
91
|
+
console.log(` > Creating ${appName}/README.md ...`);
|
|
92
|
+
const filePath = node_path_1.default.join(getAppDir(appName), 'README.md');
|
|
93
|
+
yield (0, promises_1.writeFile)(filePath, `# ${appName} - A Scripting application
|
|
94
|
+
|
|
95
|
+
## Quick Start
|
|
96
|
+
|
|
97
|
+
### Installation
|
|
98
|
+
|
|
99
|
+
\`npm install\`
|
|
100
|
+
|
|
101
|
+
### Start dev server
|
|
102
|
+
|
|
103
|
+
\`npm run dev\`
|
|
104
|
+
|
|
105
|
+
### Build App
|
|
106
|
+
|
|
107
|
+
\`npm run build\`
|
|
108
|
+
`, 'utf-8');
|
|
109
|
+
console.log(chalk_1.default.green(` ✔️ Created ${appName}/README.md`));
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
function createTSConfig(appName) {
|
|
113
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
114
|
+
console.log(` > Creating ${appName}/tsconfig.json ...`);
|
|
115
|
+
const filePath = node_path_1.default.join(getAppDir(appName), 'tsconfig.json');
|
|
116
|
+
yield (0, promises_1.writeFile)(filePath, `{
|
|
117
|
+
"compilerOptions": {
|
|
118
|
+
"target": "es5",
|
|
119
|
+
"lib": [
|
|
120
|
+
"esnext",
|
|
121
|
+
],
|
|
122
|
+
"allowJs": false,
|
|
123
|
+
"skipLibCheck": true,
|
|
124
|
+
"esModuleInterop": true,
|
|
125
|
+
"allowSyntheticDefaultImports": true,
|
|
126
|
+
"strict": true,
|
|
127
|
+
"forceConsistentCasingInFileNames": true,
|
|
128
|
+
"noFallthroughCasesInSwitch": true,
|
|
129
|
+
"module": "esnext",
|
|
130
|
+
"moduleResolution": "node",
|
|
131
|
+
"resolveJsonModule": true,
|
|
132
|
+
"isolatedModules": true,
|
|
133
|
+
"jsx": "react",
|
|
134
|
+
"jsxFactory": "createElement",
|
|
135
|
+
"baseUrl": "src",
|
|
136
|
+
"paths": {
|
|
137
|
+
"@/*": [
|
|
138
|
+
"src/*"
|
|
139
|
+
]
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
"include": [
|
|
143
|
+
"src"
|
|
144
|
+
],
|
|
145
|
+
}`, 'utf-8');
|
|
146
|
+
console.log(chalk_1.default.green(` ✔️ Created ${appName}/tsconfig.json`));
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
function createScriptingDeclaration(appName) {
|
|
150
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
151
|
+
console.log(` > Creating ${appName}/src/scripting.d.ts ...`);
|
|
152
|
+
const srcPath = node_path_1.default.join(__dirname, '../public/scripting.d.ts');
|
|
153
|
+
const destPath = node_path_1.default.join(getAppDir(appName), 'src/scripting.d.ts');
|
|
154
|
+
yield (0, promises_1.copyFile)(srcPath, destPath);
|
|
155
|
+
console.log(chalk_1.default.green(` ✔️ Created ${appName}/src/scripting.d.ts`));
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
function createApp(appName) {
|
|
159
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
160
|
+
if (!/^[a-zA-Z]+[a-zA-Z0-9-_]+$/.test(appName)) {
|
|
161
|
+
console.log(chalk_1.default.red(`✖️ Invalid Scripting application name "${appName}"`));
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
yield createAppDir(appName);
|
|
166
|
+
yield createPackageJson(appName);
|
|
167
|
+
yield createTSConfig(appName);
|
|
168
|
+
yield createReadme(appName);
|
|
169
|
+
yield createEntryFile(appName);
|
|
170
|
+
yield createScriptingDeclaration(appName);
|
|
171
|
+
// exec('npm i')
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
console.log(chalk_1.default.red(`✖️ Create Scripting application failed: ${e.message}`));
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
exports.createApp = createApp;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const minimist_1 = __importDefault(require("minimist"));
|
|
7
|
+
const startDevServer_1 = require("./startDevServer");
|
|
8
|
+
const build_1 = require("./build");
|
|
9
|
+
const createApp_1 = require("./createApp");
|
|
10
|
+
function run() {
|
|
11
|
+
const args = (0, minimist_1.default)(process.argv.slice(2));
|
|
12
|
+
console.log(args);
|
|
13
|
+
if (args['create'] != null) {
|
|
14
|
+
(0, createApp_1.createApp)(args['create']);
|
|
15
|
+
}
|
|
16
|
+
else if (args._.includes('dev')) {
|
|
17
|
+
(0, startDevServer_1.startDevServer)();
|
|
18
|
+
}
|
|
19
|
+
else if (args._.includes('build')) {
|
|
20
|
+
(0, build_1.buildApp)();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
run();
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.startDevServer = void 0;
|
|
30
|
+
const node_http_1 = __importDefault(require("node:http"));
|
|
31
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
32
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
33
|
+
const express_1 = __importDefault(require("express"));
|
|
34
|
+
const minimist_1 = __importDefault(require("minimist"));
|
|
35
|
+
const socket_io_1 = require("socket.io");
|
|
36
|
+
const utils = __importStar(require("./utils"));
|
|
37
|
+
const webpack_1 = __importDefault(require("webpack"));
|
|
38
|
+
const webpack_config_1 = __importDefault(require("./config/webpack.config"));
|
|
39
|
+
function startDevServer() {
|
|
40
|
+
const args = (0, minimist_1.default)(process.argv.slice(2));
|
|
41
|
+
const ipAddress = utils.getIpAddress();
|
|
42
|
+
const port = args['port'] || 8000;
|
|
43
|
+
const app = (0, express_1.default)();
|
|
44
|
+
const server = node_http_1.default.createServer(app);
|
|
45
|
+
const io = new socket_io_1.Server(server);
|
|
46
|
+
let isStarted = false;
|
|
47
|
+
let connectedSockets = [];
|
|
48
|
+
webpack_config_1.default.mode = 'development';
|
|
49
|
+
const compiler = (0, webpack_1.default)(webpack_config_1.default);
|
|
50
|
+
console.log('Start running webpack compiler in watch mode...');
|
|
51
|
+
compiler.watch({}, (err, stats) => {
|
|
52
|
+
utils.clearConsole();
|
|
53
|
+
if (err) {
|
|
54
|
+
console.log(chalk_1.default.red('[DevServer] webpack compile error: \n') + err);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
console.log(stats === null || stats === void 0 ? void 0 : stats.toString({
|
|
58
|
+
chunks: false,
|
|
59
|
+
colors: true,
|
|
60
|
+
}));
|
|
61
|
+
if ((stats === null || stats === void 0 ? void 0 : stats.toJson().errorsCount) === 0) {
|
|
62
|
+
onCompileDone();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
function onCompileDone() {
|
|
67
|
+
if (isStarted) {
|
|
68
|
+
printDevServerAddress();
|
|
69
|
+
console.log(chalk_1.default.gray(`[DevServer] Sent update event to ${connectedSockets.length} client(s)`));
|
|
70
|
+
connectedSockets.forEach(socket => {
|
|
71
|
+
socket.emit('update');
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
start();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function printDevServerAddress() {
|
|
79
|
+
console.log(`Server running at ${chalk_1.default.bold(chalk_1.default.green(`http://${ipAddress}:${port}`))}`);
|
|
80
|
+
}
|
|
81
|
+
function start() {
|
|
82
|
+
if (isStarted) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
app.all('*', function (req, res, next) {
|
|
86
|
+
res.header('Access-Control-Allow-Origin', '*');
|
|
87
|
+
res.header('Access-Control-Allow-Headers', '*');
|
|
88
|
+
// res.header('Content-Type', 'application/jsoncharset=utf-8')
|
|
89
|
+
res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, OPTIONS, PUT, DELETE');
|
|
90
|
+
next();
|
|
91
|
+
});
|
|
92
|
+
app.use('/', express_1.default.static(node_path_1.default.resolve(__dirname, '../build')));
|
|
93
|
+
//有新的客户端连接时触发
|
|
94
|
+
io.on('connection', function (socket) {
|
|
95
|
+
console.log(chalk_1.default.green('[DevServer] A client connected.'));
|
|
96
|
+
connectedSockets.push(socket);
|
|
97
|
+
socket.on('error', function (err) {
|
|
98
|
+
console.log(chalk_1.default.red('[DevServer] Error: ', err));
|
|
99
|
+
});
|
|
100
|
+
socket.on('disconnect', function () {
|
|
101
|
+
var index = connectedSockets.indexOf(socket);
|
|
102
|
+
if (index > -1) {
|
|
103
|
+
connectedSockets.splice(index, 1);
|
|
104
|
+
console.log(chalk_1.default.gray('[DevServer] A client disconnected.'));
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
server.listen(port);
|
|
109
|
+
printDevServerAddress();
|
|
110
|
+
isStarted = true;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.startDevServer = startDevServer;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.clearConsole = exports.getIpAddress = void 0;
|
|
7
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
8
|
+
function getIpAddress() {
|
|
9
|
+
const interfaces = node_os_1.default.networkInterfaces();
|
|
10
|
+
for (const devName in interfaces) {
|
|
11
|
+
const iface = interfaces[devName];
|
|
12
|
+
if (iface == null) {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
for (let i = 0; i < iface.length; i++) {
|
|
16
|
+
const alias = iface[i];
|
|
17
|
+
if (alias.family === 'IPv4'
|
|
18
|
+
&& alias.address !== '127.0.0.1'
|
|
19
|
+
&& !alias.internal) {
|
|
20
|
+
return alias.address;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.getIpAddress = getIpAddress;
|
|
26
|
+
function clearConsole() {
|
|
27
|
+
process.stdout.write(process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H');
|
|
28
|
+
}
|
|
29
|
+
exports.clearConsole = clearConsole;
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "scripting-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "This plugin include all the commands for developing scripting-js apps.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"scripting-cli": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"dev": "tsc -w"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/thomfang/scripting-cli.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"scripting",
|
|
19
|
+
"javascript",
|
|
20
|
+
"typescript"
|
|
21
|
+
],
|
|
22
|
+
"author": "tilfon9017@gmail.com",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/thomfang/scripting-cli/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/thomfang/scripting-cli#readme",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/express": "^4.17.21",
|
|
30
|
+
"@types/minimist": "^1.2.5",
|
|
31
|
+
"chalk": "^4.1.2",
|
|
32
|
+
"express": "^4.18.2",
|
|
33
|
+
"file-loader": "^6.2.0",
|
|
34
|
+
"minimist": "^1.2.8",
|
|
35
|
+
"socket.io": "^4.7.5",
|
|
36
|
+
"ts-loader": "^9.4.4",
|
|
37
|
+
"typescript": "^5.1.6",
|
|
38
|
+
"webpack": "^5.88.2",
|
|
39
|
+
"webpack-cli": "^5.1.4"
|
|
40
|
+
}
|
|
41
|
+
}
|