scripting-cli 1.0.7 → 1.0.9
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/README.md +91 -0
- package/README_zh.md +91 -0
- package/dist/index.js +384 -16
- package/package.json +27 -22
- package/src/index.ts +451 -15
- package/tsconfig.json +16 -95
- package/.vscode/settings.json +0 -6
- package/dist/build.js +0 -98
- package/dist/config/AppJsonPlugin.js +0 -103
- package/dist/config/webpack.config.js +0 -103
- package/dist/createScript.js +0 -210
- package/dist/startDevServer.js +0 -129
- package/dist/utils.js +0 -29
- package/public/assets.d.ts +0 -40
- package/public/global.d.ts +0 -256
- package/public/logo.svg +0 -4
- package/public/scripting.d.ts +0 -6685
- package/src/build.ts +0 -110
- package/src/config/AppJsonPlugin.ts +0 -139
- package/src/config/webpack.config.ts +0 -115
- package/src/createScript.ts +0 -257
- package/src/startDevServer.ts +0 -147
- package/src/utils.ts +0 -30
package/dist/build.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
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.buildApp = void 0;
|
|
30
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
31
|
-
const webpack_1 = __importDefault(require("webpack"));
|
|
32
|
-
const cross_zip_1 = require("cross-zip");
|
|
33
|
-
const qrcode_terminal_1 = __importDefault(require("qrcode-terminal"));
|
|
34
|
-
const node_path_1 = __importDefault(require("node:path"));
|
|
35
|
-
const node_http_1 = __importDefault(require("node:http"));
|
|
36
|
-
const express_1 = __importDefault(require("express"));
|
|
37
|
-
const webpack_config_1 = __importDefault(require("./config/webpack.config"));
|
|
38
|
-
const utils = __importStar(require("./utils"));
|
|
39
|
-
const minimist_1 = __importDefault(require("minimist"));
|
|
40
|
-
function buildApp() {
|
|
41
|
-
const webpackConfig = (0, webpack_config_1.default)();
|
|
42
|
-
const appName = Object.keys(webpackConfig.entry)[0];
|
|
43
|
-
const appFile = `${appName}.zip`;
|
|
44
|
-
const outputDir = node_path_1.default.resolve(process.cwd(), appName);
|
|
45
|
-
webpackConfig.output.path = outputDir;
|
|
46
|
-
const compiler = (0, webpack_1.default)(webpackConfig);
|
|
47
|
-
console.log('Start running build...\n'
|
|
48
|
-
// +
|
|
49
|
-
// chalk.green(
|
|
50
|
-
// Object.keys(webpackConfig.entry).map(e => ` - ${e}`).join('\n')
|
|
51
|
-
// )
|
|
52
|
-
// +
|
|
53
|
-
// '\n'
|
|
54
|
-
);
|
|
55
|
-
compiler.run((err, stats) => {
|
|
56
|
-
if (err) {
|
|
57
|
-
console.log(chalk_1.default.red('Webpack compile error: \n') + err);
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
console.log(stats === null || stats === void 0 ? void 0 : stats.toString({
|
|
61
|
-
chunks: false,
|
|
62
|
-
colors: false,
|
|
63
|
-
}));
|
|
64
|
-
createAppFile();
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
function createAppFile() {
|
|
68
|
-
console.log(` ~ Creating ${appFile}....`);
|
|
69
|
-
(0, cross_zip_1.zip)(outputDir, node_path_1.default.resolve(outputDir, appFile), (error) => {
|
|
70
|
-
if (error != null) {
|
|
71
|
-
console.log(chalk_1.default.red('Error zipping app files: \n') + error);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
console.log(chalk_1.default.green(` ✔️ ${appFile} created.`));
|
|
75
|
-
startServer();
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
function startServer() {
|
|
80
|
-
const app = (0, express_1.default)();
|
|
81
|
-
const server = node_http_1.default.createServer(app);
|
|
82
|
-
const args = (0, minimist_1.default)(process.argv.slice(2));
|
|
83
|
-
const ipAddress = utils.getIpAddress();
|
|
84
|
-
const port = args['port'] || 8000;
|
|
85
|
-
app.all('*', function (req, res, next) {
|
|
86
|
-
res.header('Access-Control-Allow-Origin', '*');
|
|
87
|
-
res.header('Access-Control-Allow-Headers', '*');
|
|
88
|
-
res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, OPTIONS, PUT, DELETE');
|
|
89
|
-
next();
|
|
90
|
-
});
|
|
91
|
-
app.use('/', express_1.default.static(outputDir));
|
|
92
|
-
server.listen(port);
|
|
93
|
-
console.log(`Server running at ${chalk_1.default.bold(chalk_1.default.green(`http://${ipAddress}:${port}`))}`);
|
|
94
|
-
console.log(`Use the Scripting app to scan the QR code below to install:`);
|
|
95
|
-
qrcode_terminal_1.default.generate(`http://${ipAddress}:${port}`, { small: true });
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
exports.buildApp = buildApp;
|
|
@@ -1,103 +0,0 @@
|
|
|
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 scriptJson = {
|
|
26
|
-
name: '',
|
|
27
|
-
index: '',
|
|
28
|
-
hash: '',
|
|
29
|
-
assets: Object.keys(assets),
|
|
30
|
-
};
|
|
31
|
-
const indexJs = scriptJson.assets.find(filePath => filePath.match(/\.js$/i) != null);
|
|
32
|
-
if (indexJs != null) {
|
|
33
|
-
const [appName, ...others] = indexJs.split('/');
|
|
34
|
-
const filePath = others[others.length - 1];
|
|
35
|
-
scriptJson.name = appName;
|
|
36
|
-
scriptJson.index = filePath;
|
|
37
|
-
scriptJson.hash = filePath.replace('.js', '');
|
|
38
|
-
const indexJsSource = assets[indexJs];
|
|
39
|
-
scriptJson.assets = [
|
|
40
|
-
...scriptJson.assets.filter(e => e !== indexJs),
|
|
41
|
-
filePath,
|
|
42
|
-
];
|
|
43
|
-
compilation.deleteAsset(indexJs);
|
|
44
|
-
compilation.emitAsset(filePath, indexJsSource);
|
|
45
|
-
}
|
|
46
|
-
compilation.emitAsset('script.json', new RawSource(JSON.stringify(scriptJson, null, 2)));
|
|
47
|
-
// const appMap: {
|
|
48
|
-
// [appName: string]: {
|
|
49
|
-
// appName: string
|
|
50
|
-
// index: string
|
|
51
|
-
// hash: string
|
|
52
|
-
// }
|
|
53
|
-
// } = {}
|
|
54
|
-
// Object.keys(assets).forEach(path => {
|
|
55
|
-
// const [appName, ...others] = path.split('/')
|
|
56
|
-
// const filePath = others[others.length - 1]
|
|
57
|
-
// // console.log(appName, filePath)
|
|
58
|
-
// // if (appMap[appName] == null) {
|
|
59
|
-
// // appMap[appName] = {
|
|
60
|
-
// // appName,
|
|
61
|
-
// // // assets: [],
|
|
62
|
-
// // }
|
|
63
|
-
// // }
|
|
64
|
-
// if (filePath.match(/\.js$/i)) {
|
|
65
|
-
// appMap[appName] = {
|
|
66
|
-
// appName,
|
|
67
|
-
// index: filePath,
|
|
68
|
-
// hash: filePath.replace('.js', '')
|
|
69
|
-
// }
|
|
70
|
-
// // appMap[appName].index = filePath
|
|
71
|
-
// // appMap[appName].hash = filePath.replace('.js', '')
|
|
72
|
-
// }
|
|
73
|
-
// // else if (filePath.match(/\.(png|jpe?g|gif|svg)$/i)) {
|
|
74
|
-
// // appMap[appName].assets.push(path.replace(appName + '/', ''))
|
|
75
|
-
// // }
|
|
76
|
-
// })
|
|
77
|
-
// Object.entries(appMap).forEach(([appName, content]) => {
|
|
78
|
-
// compilation.emitAsset(
|
|
79
|
-
// appName + '/script.json',
|
|
80
|
-
// new RawSource(JSON.stringify(content, null, 2))
|
|
81
|
-
// )
|
|
82
|
-
// })
|
|
83
|
-
// "assets" 是一个包含 compilation 中所有资源(assets)的对象。
|
|
84
|
-
// 该对象的键是资源的路径,
|
|
85
|
-
// 值是文件的源码
|
|
86
|
-
// 遍历所有资源,
|
|
87
|
-
// 生成 Markdown 文件的内容
|
|
88
|
-
// const content =
|
|
89
|
-
// '# In this build:\n\n' +
|
|
90
|
-
// Object.keys(assets)
|
|
91
|
-
// .map((filename) => `- ${filename}`)
|
|
92
|
-
// .join('\n');
|
|
93
|
-
// // 向 compilation 添加新的资源,
|
|
94
|
-
// // 这样 webpack 就会自动生成并输出到 output 目录
|
|
95
|
-
// compilation.emitAsset(
|
|
96
|
-
// this.options.outputFile,
|
|
97
|
-
// new RawSource(content)
|
|
98
|
-
// );
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
exports.default = AppJsonPlugin;
|
|
@@ -1,103 +0,0 @@
|
|
|
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 node_path_1 = __importDefault(require("node:path"));
|
|
7
|
-
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
9
|
-
const terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin"));
|
|
10
|
-
const webpack_1 = __importDefault(require("webpack"));
|
|
11
|
-
const AppJsonPlugin_1 = __importDefault(require("./AppJsonPlugin"));
|
|
12
|
-
function getConfig() {
|
|
13
|
-
const baseDir = process.cwd();
|
|
14
|
-
const srcDir = node_path_1.default.resolve(baseDir, 'src');
|
|
15
|
-
const outputDir = node_path_1.default.resolve(baseDir, 'build');
|
|
16
|
-
const pkgPath = node_path_1.default.resolve(baseDir, 'package.json');
|
|
17
|
-
if (!node_fs_1.default.existsSync(pkgPath)) {
|
|
18
|
-
console.log(chalk_1.default.red('✖️ package.json not found.'));
|
|
19
|
-
process.exit(404);
|
|
20
|
-
}
|
|
21
|
-
const pkg = JSON.parse(node_fs_1.default.readFileSync(pkgPath, 'utf8'));
|
|
22
|
-
const entry = {
|
|
23
|
-
[pkg.name]: node_path_1.default.join(srcDir, 'index.tsx'),
|
|
24
|
-
};
|
|
25
|
-
const config = {
|
|
26
|
-
mode: 'production',
|
|
27
|
-
entry,
|
|
28
|
-
optimization: {
|
|
29
|
-
moduleIds: 'deterministic',
|
|
30
|
-
chunkIds: 'named',
|
|
31
|
-
minimizer: [
|
|
32
|
-
new terser_webpack_plugin_1.default({
|
|
33
|
-
extractComments: false,
|
|
34
|
-
terserOptions: {
|
|
35
|
-
format: {
|
|
36
|
-
comments: false,
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
}),
|
|
40
|
-
],
|
|
41
|
-
},
|
|
42
|
-
module: {
|
|
43
|
-
rules: [
|
|
44
|
-
{
|
|
45
|
-
test: /\.tsx?$/,
|
|
46
|
-
use: 'ts-loader',
|
|
47
|
-
exclude: /node_modules/,
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
test: /\.(png|jpe?g|gif|svg)$/i,
|
|
51
|
-
loader: 'file-loader',
|
|
52
|
-
options: {
|
|
53
|
-
name: '[path][contenthash:8].[ext]',
|
|
54
|
-
outputPath: (url, resourcePath, context) => {
|
|
55
|
-
// if (url.startsWith('src/app/')) {
|
|
56
|
-
// return url.replace('src/app/', '')
|
|
57
|
-
// }
|
|
58
|
-
return url.replace('src/', '');
|
|
59
|
-
// return url.replace(/src\/([^/]+)\//, '')
|
|
60
|
-
},
|
|
61
|
-
publicPath: (url, resourcePath, context) => {
|
|
62
|
-
// console.log([url, resourcePath])
|
|
63
|
-
// return url.replace(/.*src\/app\/(\w+\/)?/, '')
|
|
64
|
-
// if (url.startsWith('src/app/')) {
|
|
65
|
-
// return url.replace('src/app/', '')
|
|
66
|
-
// }
|
|
67
|
-
return url.replace('src/', '');
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
],
|
|
72
|
-
},
|
|
73
|
-
resolve: {
|
|
74
|
-
extensions: ['.tsx', '.ts', '.js'],
|
|
75
|
-
modules: [
|
|
76
|
-
'node_modules',
|
|
77
|
-
srcDir,
|
|
78
|
-
],
|
|
79
|
-
alias: {
|
|
80
|
-
'@': srcDir,
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
output: {
|
|
84
|
-
filename: '[name]/[contenthash:8].js',
|
|
85
|
-
// filename: '[contenthash:8].js',
|
|
86
|
-
path: outputDir,
|
|
87
|
-
clean: true,
|
|
88
|
-
},
|
|
89
|
-
plugins: [
|
|
90
|
-
new AppJsonPlugin_1.default(),
|
|
91
|
-
new webpack_1.default.BannerPlugin({
|
|
92
|
-
banner: 'const Scripting = require("scripting");',
|
|
93
|
-
raw: true
|
|
94
|
-
}),
|
|
95
|
-
],
|
|
96
|
-
devtool: false,
|
|
97
|
-
externals: {
|
|
98
|
-
'scripting': 'Scripting',
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
return config;
|
|
102
|
-
}
|
|
103
|
-
exports.default = getConfig;
|
package/dist/createScript.js
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
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.createScript = void 0;
|
|
16
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
17
|
-
const node_path_1 = __importDefault(require("node:path"));
|
|
18
|
-
const node_child_process_1 = require("node:child_process");
|
|
19
|
-
const node_fs_1 = __importDefault(require("node:fs"));
|
|
20
|
-
const promises_1 = require("node:fs/promises");
|
|
21
|
-
function getScriptDir(scriptName) {
|
|
22
|
-
return node_path_1.default.join(process.cwd(), scriptName);
|
|
23
|
-
}
|
|
24
|
-
function createScriptDir(scriptName) {
|
|
25
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
const appDir = getScriptDir(scriptName);
|
|
27
|
-
if (node_fs_1.default.existsSync(appDir)) {
|
|
28
|
-
throw new Error(`Directory "./${scriptName}" already exists.`);
|
|
29
|
-
}
|
|
30
|
-
// const dir = path.resolve(appDir, 'src/svgs')
|
|
31
|
-
const srcDir = node_path_1.default.resolve(appDir, 'src');
|
|
32
|
-
// const typesDir = path.resolve(appDir, 'types')
|
|
33
|
-
yield Promise.all([
|
|
34
|
-
(0, promises_1.mkdir)(srcDir, { recursive: true }),
|
|
35
|
-
// mkdir(typesDir, { recursive: true })
|
|
36
|
-
]);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
function createEntryFile(scriptName) {
|
|
40
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
console.log(` > Creating ${scriptName}/src/index.tsx ...`);
|
|
42
|
-
const filePath = node_path_1.default.join(getScriptDir(scriptName), '/src/index.tsx');
|
|
43
|
-
yield (0, promises_1.writeFile)(filePath, `import { Column, CupertinoNavigationBar, CupertinoPageScaffold, Text, Container, useCupertinoColors, navigator, GestureDetector, Script, Center, CupertinoButton, } from 'scripting'
|
|
44
|
-
|
|
45
|
-
function App() {
|
|
46
|
-
const cupertinoColors = useCupertinoColors()
|
|
47
|
-
const backgroundColor = cupertinoColors.systemBackground
|
|
48
|
-
|
|
49
|
-
return (
|
|
50
|
-
<CupertinoPageScaffold
|
|
51
|
-
navigationBar={
|
|
52
|
-
<CupertinoNavigationBar
|
|
53
|
-
middle={
|
|
54
|
-
<Text>My Script</Text>
|
|
55
|
-
}
|
|
56
|
-
backgroundColor={backgroundColor}
|
|
57
|
-
/>
|
|
58
|
-
}
|
|
59
|
-
>
|
|
60
|
-
<Center>
|
|
61
|
-
<Text
|
|
62
|
-
color={cupertinoColors.label}
|
|
63
|
-
>Welcome to Scripting!</Text>
|
|
64
|
-
</Center>
|
|
65
|
-
</CupertinoPageScaffold>
|
|
66
|
-
)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async function run() {
|
|
70
|
-
const result = await navigator.push({
|
|
71
|
-
element: <App />
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
Script.exit(result)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
run()
|
|
78
|
-
`, 'utf-8');
|
|
79
|
-
console.log(chalk_1.default.green(` ✔️ Created ${scriptName}/src/index.tsx`));
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
function createPackageJson(scriptName) {
|
|
83
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
-
console.log(` > Creating ${scriptName}/package.json ...`);
|
|
85
|
-
const filePath = node_path_1.default.join(getScriptDir(scriptName), 'package.json');
|
|
86
|
-
yield (0, promises_1.writeFile)(filePath, `{
|
|
87
|
-
"name": "${scriptName}",
|
|
88
|
-
"version": "0.0.1",
|
|
89
|
-
"private": true,
|
|
90
|
-
"scripts": {
|
|
91
|
-
"dev": "npx scripting-cli dev",
|
|
92
|
-
"build": "npx scripting-cli build"
|
|
93
|
-
}
|
|
94
|
-
}`, 'utf-8');
|
|
95
|
-
console.log(chalk_1.default.green(` ✔️ Created ${scriptName}/package.json`));
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
function createReadme(scriptName) {
|
|
99
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
-
console.log(` > Creating ${scriptName}/README.md ...`);
|
|
101
|
-
const filePath = node_path_1.default.join(getScriptDir(scriptName), 'README.md');
|
|
102
|
-
yield (0, promises_1.writeFile)(filePath, `# ${scriptName} - A Scripting script
|
|
103
|
-
|
|
104
|
-
## Start dev server
|
|
105
|
-
|
|
106
|
-
\`npm run dev\`
|
|
107
|
-
|
|
108
|
-
## Build App
|
|
109
|
-
|
|
110
|
-
\`npm run build\`
|
|
111
|
-
`, 'utf-8');
|
|
112
|
-
console.log(chalk_1.default.green(` ✔️ Created ${scriptName}/README.md`));
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
function createTSConfig(scriptName) {
|
|
116
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
-
console.log(` > Creating ${scriptName}/tsconfig.json ...`);
|
|
118
|
-
const filePath = node_path_1.default.join(getScriptDir(scriptName), 'tsconfig.json');
|
|
119
|
-
yield (0, promises_1.writeFile)(filePath, `{
|
|
120
|
-
"compilerOptions": {
|
|
121
|
-
"target": "ESNext",
|
|
122
|
-
"lib": [
|
|
123
|
-
"esnext"
|
|
124
|
-
],
|
|
125
|
-
"allowJs": true,
|
|
126
|
-
"strict": true,
|
|
127
|
-
"esModuleInterop": true,
|
|
128
|
-
"allowSyntheticDefaultImports": true,
|
|
129
|
-
"forceConsistentCasingInFileNames": true,
|
|
130
|
-
"noFallthroughCasesInSwitch": true,
|
|
131
|
-
"module": "CommonJS",
|
|
132
|
-
"resolveJsonModule": true,
|
|
133
|
-
"skipLibCheck": true,
|
|
134
|
-
"jsx": "react",
|
|
135
|
-
"jsxFactory": "createElement",
|
|
136
|
-
"baseUrl": "src",
|
|
137
|
-
// "typeRoots": ["./types"],
|
|
138
|
-
// "paths": {
|
|
139
|
-
// "@/*": [
|
|
140
|
-
// "src/*"
|
|
141
|
-
// ]
|
|
142
|
-
// }
|
|
143
|
-
},
|
|
144
|
-
"include": [
|
|
145
|
-
"src"
|
|
146
|
-
],
|
|
147
|
-
}`, 'utf-8');
|
|
148
|
-
console.log(chalk_1.default.green(` ✔️ Created ${scriptName}/tsconfig.json`));
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
function copyAsset(scriptName, fileName) {
|
|
152
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
-
console.log(` > Creating ${scriptName}/src/${fileName}.d.ts ...`);
|
|
154
|
-
yield (0, promises_1.copyFile)(node_path_1.default.join(__dirname, `../public/${fileName}.d.ts`), node_path_1.default.join(getScriptDir(scriptName), `src/${fileName}.d.ts`));
|
|
155
|
-
console.log(chalk_1.default.green(` ✔️ Created ${scriptName}/src/${fileName}.d.ts`));
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
function copyAssets(scriptName) {
|
|
159
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
160
|
-
yield copyAsset(scriptName, 'scripting');
|
|
161
|
-
yield copyAsset(scriptName, 'global');
|
|
162
|
-
yield copyAsset(scriptName, 'assets');
|
|
163
|
-
// console.log(` > Creating ${scriptName}/src/svgs/logo.svg ...`)
|
|
164
|
-
// await copyFile(
|
|
165
|
-
// path.join(__dirname, '../public/logo.svg'),
|
|
166
|
-
// path.join(getScriptDir(scriptName), 'src/svgs/logo.svg')
|
|
167
|
-
// )
|
|
168
|
-
// console.log(
|
|
169
|
-
// chalk.green(` ✔️ Created ${scriptName}/src/svgs/logo.svg`)
|
|
170
|
-
// )
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
function createVSCodeSettings(scriptName) {
|
|
174
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
175
|
-
const destDir = node_path_1.default.join(getScriptDir(scriptName), '.vscode');
|
|
176
|
-
if (!node_fs_1.default.existsSync(destDir)) {
|
|
177
|
-
yield (0, promises_1.mkdir)(destDir, { recursive: true });
|
|
178
|
-
}
|
|
179
|
-
yield (0, promises_1.copyFile)(node_path_1.default.join(__dirname, '../.vscode/settings.json'), node_path_1.default.join(destDir, 'settings.json'));
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
function installDeps(scriptName) {
|
|
183
|
-
console.log(` > cd ${scriptName} && npm i -D ts-loader typescript file-loader`);
|
|
184
|
-
(0, node_child_process_1.execSync)(`cd ${scriptName} && npm i -D ts-loader typescript file-loader`);
|
|
185
|
-
console.log(chalk_1.default.green(` ✔️ Scripting script ${chalk_1.default.bold(scriptName)} is ready!`));
|
|
186
|
-
}
|
|
187
|
-
function createScript(scriptName) {
|
|
188
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
189
|
-
if (!/^[a-zA-Z]+[a-zA-Z0-9-_]+$/.test(scriptName)) {
|
|
190
|
-
console.log(chalk_1.default.red(`✖️ Invalid Scripting script name "${scriptName}"`));
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
console.log(`Start creating ${scriptName} ...`);
|
|
194
|
-
try {
|
|
195
|
-
yield createScriptDir(scriptName);
|
|
196
|
-
yield createPackageJson(scriptName);
|
|
197
|
-
yield createTSConfig(scriptName);
|
|
198
|
-
yield createReadme(scriptName);
|
|
199
|
-
yield createEntryFile(scriptName);
|
|
200
|
-
yield copyAssets(scriptName);
|
|
201
|
-
yield createVSCodeSettings(scriptName);
|
|
202
|
-
installDeps(scriptName);
|
|
203
|
-
console.log(`All done! Run ${chalk_1.default.bold(chalk_1.default.green(`cd ${scriptName} && npm run dev`))} to start the dev server.`);
|
|
204
|
-
}
|
|
205
|
-
catch (e) {
|
|
206
|
-
console.log(chalk_1.default.red(`✖️ Create script failed: ${e.message}`));
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
exports.createScript = createScript;
|
package/dist/startDevServer.js
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
-
};
|
|
37
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
-
exports.startDevServer = void 0;
|
|
39
|
-
const node_http_1 = __importDefault(require("node:http"));
|
|
40
|
-
const node_path_1 = __importDefault(require("node:path"));
|
|
41
|
-
const promises_1 = require("node:fs/promises");
|
|
42
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
43
|
-
const express_1 = __importDefault(require("express"));
|
|
44
|
-
const minimist_1 = __importDefault(require("minimist"));
|
|
45
|
-
const qrcode_terminal_1 = __importDefault(require("qrcode-terminal"));
|
|
46
|
-
const socket_io_1 = require("socket.io");
|
|
47
|
-
const utils = __importStar(require("./utils"));
|
|
48
|
-
const webpack_1 = __importDefault(require("webpack"));
|
|
49
|
-
const webpack_config_1 = __importDefault(require("./config/webpack.config"));
|
|
50
|
-
function startDevServer() {
|
|
51
|
-
const args = (0, minimist_1.default)(process.argv.slice(2));
|
|
52
|
-
const ipAddress = utils.getIpAddress();
|
|
53
|
-
const port = args['port'] || 8000;
|
|
54
|
-
const app = (0, express_1.default)();
|
|
55
|
-
const server = node_http_1.default.createServer(app);
|
|
56
|
-
const io = new socket_io_1.Server(server);
|
|
57
|
-
let isStarted = false;
|
|
58
|
-
let connectedSockets = [];
|
|
59
|
-
const webpackConfig = (0, webpack_config_1.default)();
|
|
60
|
-
webpackConfig.mode = 'development';
|
|
61
|
-
const compiler = (0, webpack_1.default)(webpackConfig);
|
|
62
|
-
console.log('Start running webpack compiler in watch mode...');
|
|
63
|
-
compiler.watch({}, (err, stats) => {
|
|
64
|
-
utils.clearConsole();
|
|
65
|
-
if (err) {
|
|
66
|
-
console.log(chalk_1.default.red(' ✖️ webpack compile error: \n') + err);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
console.log(stats === null || stats === void 0 ? void 0 : stats.toString({
|
|
70
|
-
chunks: false,
|
|
71
|
-
colors: true,
|
|
72
|
-
}));
|
|
73
|
-
if ((stats === null || stats === void 0 ? void 0 : stats.toJson().errorsCount) === 0) {
|
|
74
|
-
onCompileDone();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
function onCompileDone() {
|
|
79
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
-
if (isStarted) {
|
|
81
|
-
printDevServerAddress();
|
|
82
|
-
console.log(chalk_1.default.gray(` ~ Sent update event to ${connectedSockets.length} client(s)`));
|
|
83
|
-
const scriptJson = yield (0, promises_1.readFile)(node_path_1.default.resolve(process.cwd(), 'build/script.json'), 'utf-8');
|
|
84
|
-
for (const socket of connectedSockets) {
|
|
85
|
-
socket.emit('update', scriptJson);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
start();
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
function printDevServerAddress() {
|
|
94
|
-
console.log(`Server running at ${chalk_1.default.bold(chalk_1.default.green(`http://${ipAddress}:${port}`))}`);
|
|
95
|
-
console.log(`Use the Scripting app to scan the QR code below:`);
|
|
96
|
-
qrcode_terminal_1.default.generate(`http://${ipAddress}:${port}`, { small: true });
|
|
97
|
-
}
|
|
98
|
-
function start() {
|
|
99
|
-
if (isStarted) {
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
app.all('*', function (req, res, next) {
|
|
103
|
-
res.header('Access-Control-Allow-Origin', '*');
|
|
104
|
-
res.header('Access-Control-Allow-Headers', '*');
|
|
105
|
-
res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, OPTIONS, PUT, DELETE');
|
|
106
|
-
next();
|
|
107
|
-
});
|
|
108
|
-
app.use('/', express_1.default.static(node_path_1.default.resolve(process.cwd(), 'build')));
|
|
109
|
-
//有新的客户端连接时触发
|
|
110
|
-
io.on('connection', function (socket) {
|
|
111
|
-
console.log(chalk_1.default.green(` + A client(${socket.id}) connected.`));
|
|
112
|
-
connectedSockets.push(socket);
|
|
113
|
-
socket.on('error', function (err) {
|
|
114
|
-
console.log(chalk_1.default.red(' ✖️ Server Error: ', err));
|
|
115
|
-
});
|
|
116
|
-
socket.on('disconnect', function () {
|
|
117
|
-
var index = connectedSockets.indexOf(socket);
|
|
118
|
-
if (index > -1) {
|
|
119
|
-
connectedSockets.splice(index, 1);
|
|
120
|
-
console.log(chalk_1.default.gray(` - A client(${socket.id}) disconnected.`));
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
server.listen(port);
|
|
125
|
-
printDevServerAddress();
|
|
126
|
-
isStarted = true;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
exports.startDevServer = startDevServer;
|
package/dist/utils.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
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;
|