scripting-cli 1.0.3 → 1.0.4

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/dist/build.js CHANGED
@@ -7,6 +7,8 @@ exports.buildApp = void 0;
7
7
  const chalk_1 = __importDefault(require("chalk"));
8
8
  const webpack_1 = __importDefault(require("webpack"));
9
9
  const webpack_config_1 = __importDefault(require("./config/webpack.config"));
10
+ const cross_zip_1 = require("cross-zip");
11
+ const node_path_1 = __importDefault(require("node:path"));
10
12
  function buildApp() {
11
13
  const compiler = (0, webpack_1.default)(webpack_config_1.default);
12
14
  console.log('Start running build...\n'
@@ -26,7 +28,20 @@ function buildApp() {
26
28
  chunks: false,
27
29
  colors: false,
28
30
  }));
31
+ createAppFile();
29
32
  }
30
33
  });
31
34
  }
32
35
  exports.buildApp = buildApp;
36
+ function createAppFile() {
37
+ const appFile = `${Object.keys(webpack_config_1.default.entry)[0]}.zip`;
38
+ console.log(` ~ Creating ${appFile}....`);
39
+ (0, cross_zip_1.zip)(node_path_1.default.resolve(process.cwd(), './build'), node_path_1.default.resolve(process.cwd(), `./build/${appFile}`), (error) => {
40
+ if (error != null) {
41
+ console.log(chalk_1.default.red('Error zipping app files: \n') + error);
42
+ }
43
+ else {
44
+ console.log(chalk_1.default.green(` ✔️ ${appFile} created.`));
45
+ }
46
+ });
47
+ }
@@ -22,33 +22,64 @@ class AppJsonPlugin {
22
22
  // 确保所有资源已被插件添加到 compilation
23
23
  stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE,
24
24
  }, (assets) => {
25
- const appMap = {};
26
- Object.keys(assets).forEach(path => {
27
- const [appName, ...others] = path.split('/');
25
+ const appJson = {
26
+ name: '',
27
+ index: '',
28
+ hash: '',
29
+ assets: Object.keys(assets),
30
+ };
31
+ const indexJs = appJson.assets.find(filePath => filePath.match(/\.js$/i) != null);
32
+ if (indexJs != null) {
33
+ const [appName, ...others] = indexJs.split('/');
28
34
  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
- });
35
+ appJson.name = appName;
36
+ appJson.index = filePath;
37
+ appJson.hash = filePath.replace('.js', '');
38
+ const indexJsSource = assets[indexJs];
39
+ appJson.assets = [
40
+ ...appJson.assets.filter(e => e !== indexJs),
41
+ filePath,
42
+ ];
43
+ compilation.deleteAsset(indexJs);
44
+ compilation.emitAsset(filePath, indexJsSource);
45
+ }
46
+ compilation.emitAsset('app.json', new RawSource(JSON.stringify(appJson, 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 + '/app.json',
80
+ // new RawSource(JSON.stringify(content, null, 2))
81
+ // )
82
+ // })
52
83
  // "assets" 是一个包含 compilation 中所有资源(assets)的对象。
53
84
  // 该对象的键是资源的路径,
54
85
  // 值是文件的源码
@@ -3,15 +3,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const path_1 = __importDefault(require("path"));
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"));
7
9
  const terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin"));
8
10
  const AppJsonPlugin_1 = __importDefault(require("./AppJsonPlugin"));
9
11
  const baseDir = process.cwd();
10
- const srcDir = path_1.default.resolve(baseDir, 'src');
11
- const buildDir = path_1.default.resolve(baseDir, 'build');
12
+ const srcDir = node_path_1.default.resolve(baseDir, 'src');
13
+ const buildDir = node_path_1.default.resolve(baseDir, 'build');
14
+ const pkgPath = node_path_1.default.resolve(baseDir, 'package.json');
15
+ if (!node_fs_1.default.existsSync(pkgPath)) {
16
+ console.log(chalk_1.default.red('✖️ package.json not found.'));
17
+ process.exit(404);
18
+ }
19
+ const pkg = JSON.parse(node_fs_1.default.readFileSync(pkgPath, 'utf8'));
20
+ const entry = {
21
+ [pkg.name]: node_path_1.default.join(srcDir, 'index.tsx'),
22
+ };
12
23
  const config = {
13
24
  mode: 'production',
14
- entry: path_1.default.join(srcDir, 'index.tsx'),
25
+ entry,
15
26
  optimization: {
16
27
  moduleIds: 'deterministic',
17
28
  chunkIds: 'named',
@@ -68,9 +79,8 @@ const config = {
68
79
  }
69
80
  },
70
81
  output: {
71
- // filename: 'index.js',
72
- // filename: '[name]/index.js',
73
82
  filename: '[name]/[contenthash:8].js',
83
+ // filename: '[contenthash:8].js',
74
84
  path: buildDir,
75
85
  clean: true,
76
86
  },
@@ -78,5 +88,8 @@ const config = {
78
88
  new AppJsonPlugin_1.default(),
79
89
  ],
80
90
  devtool: false,
91
+ externals: {
92
+ 'scripting': 'Scripting',
93
+ }
81
94
  };
82
95
  exports.default = config;
package/dist/createApp.js CHANGED
@@ -18,16 +18,16 @@ const node_path_1 = __importDefault(require("node:path"));
18
18
  const node_child_process_1 = require("node:child_process");
19
19
  const node_fs_1 = __importDefault(require("node:fs"));
20
20
  const promises_1 = require("node:fs/promises");
21
- console.log(process.cwd());
22
21
  function getAppDir(appName) {
23
22
  return node_path_1.default.join(process.cwd(), appName);
24
23
  }
25
24
  function createAppDir(appName) {
26
25
  return __awaiter(this, void 0, void 0, function* () {
27
- const dir = node_path_1.default.join(getAppDir(appName), 'src');
28
- if (node_fs_1.default.existsSync(dir)) {
26
+ const appDir = getAppDir(appName);
27
+ if (node_fs_1.default.existsSync(appDir)) {
29
28
  throw new Error(`Directory "./${appName}" already exists.`);
30
29
  }
30
+ const dir = node_path_1.default.resolve(appDir, 'src/svgs');
31
31
  yield (0, promises_1.mkdir)(dir, { recursive: true });
32
32
  });
33
33
  }
@@ -36,37 +36,66 @@ function createEntryFile(appName) {
36
36
  console.log(` > Creating ${appName}/src/index.tsx ...`);
37
37
  const filePath = node_path_1.default.join(getAppDir(appName), '/src/index.tsx');
38
38
  yield (0, promises_1.writeFile)(filePath, `
39
- const { runApp, createElement, RouterProvider, Router, } = Scripting
40
- const { Center, Text, CupertinoPageScaffold, CupertinoNavigationBar, } = Scripting.Widgets
41
39
 
42
- function IndexPage() {
43
- return (
44
- <CupertinoPageScaffold
45
- navigationBar={
46
- <CupertinoNavigationBar
47
- middle={
48
- <Text>${appName}</Text>
40
+ import { runApp, createElement, RouterProvider, Router, Column, CupertinoNavigationBar, CupertinoPageScaffold, Svg, Text, Container, Color } from 'scripting'
41
+ import Logo from './svgs/logo.svg'
42
+
43
+ function ScriptingLogo() {
44
+ return (
45
+ <Container
46
+ clipBehavior={'hardEdge'}
47
+ width={80}
48
+ height={80}
49
+ margin={{
50
+ vertical: 100,
51
+ }}
52
+ borderRadius={16}
53
+ >
54
+ <Svg src={Logo} />
55
+ </Container>
56
+ )
57
+ }
58
+
59
+ function HomePage() {
60
+ const backgroundColor: Color = '#ffffff'
61
+ return (
62
+ <CupertinoPageScaffold
63
+ navigationBar={
64
+ <CupertinoNavigationBar
65
+ middle={
66
+ <Text>${appName}</Text>
67
+ }
68
+ backgroundColor={backgroundColor}
69
+ />
49
70
  }
50
- />
51
- }
52
- >
53
- <Center>
54
- <Text>Welcome to Scripting!</Text>
55
- </Center>
56
- </CupertinoPageScaffold>
57
- )
58
- }
59
-
60
- const router = new Router([
61
- {
62
- path: '/',
63
- element: <IndexPage />,
64
- },
65
- ])
66
-
67
- runApp(
68
- <RouterProvider router={router} />
69
- )
71
+ >
72
+ <Container
73
+ width={'infinity'}
74
+ height={'infinity'}
75
+ color={backgroundColor}
76
+ >
77
+ <Column
78
+ crossAxisAlignment={'center'}
79
+ >
80
+ <ScriptingLogo />
81
+ <Text>Welcome to Scripting!</Text>
82
+ </Column>
83
+ </Container>
84
+ </CupertinoPageScaffold>
85
+ )
86
+ }
87
+
88
+ const router = new Router([
89
+ {
90
+ path: '/',
91
+ element: <HomePage />,
92
+ },
93
+ ])
94
+
95
+ runApp(
96
+ <RouterProvider router={router} />
97
+ )
98
+
70
99
  `, 'utf-8');
71
100
  console.log(chalk_1.default.green(` ✔️ Created ${appName}/src/index.tsx`));
72
101
  });
@@ -93,17 +122,11 @@ function createReadme(appName) {
93
122
  const filePath = node_path_1.default.join(getAppDir(appName), 'README.md');
94
123
  yield (0, promises_1.writeFile)(filePath, `# ${appName} - A Scripting application
95
124
 
96
- ## Quick Start
97
-
98
- ### Installation
99
-
100
- \`npm install\`
101
-
102
- ### Start dev server
125
+ ## Start dev server
103
126
 
104
127
  \`npm run dev\`
105
128
 
106
- ### Build App
129
+ ## Build App
107
130
 
108
131
  \`npm run build\`
109
132
  `, 'utf-8');
@@ -147,13 +170,26 @@ function createTSConfig(appName) {
147
170
  console.log(chalk_1.default.green(` ✔️ Created ${appName}/tsconfig.json`));
148
171
  });
149
172
  }
150
- function createScriptingDeclaration(appName) {
173
+ function copyAssets(appName) {
151
174
  return __awaiter(this, void 0, void 0, function* () {
152
175
  console.log(` > Creating ${appName}/src/scripting.d.ts ...`);
153
- const srcPath = node_path_1.default.join(__dirname, '../public/scripting.d.ts');
154
- const destPath = node_path_1.default.join(getAppDir(appName), 'src/scripting.d.ts');
155
- yield (0, promises_1.copyFile)(srcPath, destPath);
176
+ yield (0, promises_1.copyFile)(node_path_1.default.join(__dirname, '../public/scripting.d.ts'), node_path_1.default.join(getAppDir(appName), 'src/scripting.d.ts'));
156
177
  console.log(chalk_1.default.green(` ✔️ Created ${appName}/src/scripting.d.ts`));
178
+ console.log(` > Creating ${appName}/src/assets.d.ts ...`);
179
+ yield (0, promises_1.copyFile)(node_path_1.default.join(__dirname, '../public/assets.d.ts'), node_path_1.default.join(getAppDir(appName), 'src/assets.d.ts'));
180
+ console.log(chalk_1.default.green(` ✔️ Created ${appName}/src/assets.d.ts`));
181
+ console.log(` > Creating ${appName}/src/svgs/logo.svg ...`);
182
+ yield (0, promises_1.copyFile)(node_path_1.default.join(__dirname, '../public/logo.svg'), node_path_1.default.join(getAppDir(appName), 'src/svgs/logo.svg'));
183
+ console.log(chalk_1.default.green(` ✔️ Created ${appName}/src/svgs/logo.svg`));
184
+ });
185
+ }
186
+ function createVSCodeSettings(appName) {
187
+ return __awaiter(this, void 0, void 0, function* () {
188
+ const destDir = node_path_1.default.join(process.cwd(), '.vscode');
189
+ if (!node_fs_1.default.existsSync(destDir)) {
190
+ yield (0, promises_1.mkdir)(destDir);
191
+ }
192
+ yield (0, promises_1.copyFile)(node_path_1.default.join(__dirname, '../.vscode/settings.json'), node_path_1.default.join(getAppDir(appName), '.vscode/settings.json'));
157
193
  });
158
194
  }
159
195
  function installDeps(appName) {
@@ -172,8 +208,10 @@ function createApp(appName) {
172
208
  yield createTSConfig(appName);
173
209
  yield createReadme(appName);
174
210
  yield createEntryFile(appName);
175
- yield createScriptingDeclaration(appName);
211
+ yield copyAssets(appName);
212
+ yield createVSCodeSettings(appName);
176
213
  installDeps(appName);
214
+ console.log(` ✔️ All done! Run ${chalk_1.default.bold(chalk_1.default.green('npm run dev'))} to start the dev server.`);
177
215
  }
178
216
  catch (e) {
179
217
  console.log(chalk_1.default.red(`✖️ Create Scripting application failed: ${e.message}`));
@@ -22,6 +22,15 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
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
+ };
25
34
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
36
  };
@@ -29,9 +38,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
38
  exports.startDevServer = void 0;
30
39
  const node_http_1 = __importDefault(require("node:http"));
31
40
  const node_path_1 = __importDefault(require("node:path"));
41
+ const promises_1 = require("node:fs/promises");
32
42
  const chalk_1 = __importDefault(require("chalk"));
33
43
  const express_1 = __importDefault(require("express"));
34
44
  const minimist_1 = __importDefault(require("minimist"));
45
+ const qrcode_terminal_1 = __importDefault(require("qrcode-terminal"));
35
46
  const socket_io_1 = require("socket.io");
36
47
  const utils = __importStar(require("./utils"));
37
48
  const webpack_1 = __importDefault(require("webpack"));
@@ -51,7 +62,7 @@ function startDevServer() {
51
62
  compiler.watch({}, (err, stats) => {
52
63
  utils.clearConsole();
53
64
  if (err) {
54
- console.log(chalk_1.default.red('[DevServer] webpack compile error: \n') + err);
65
+ console.log(chalk_1.default.red(' ✖️ webpack compile error: \n') + err);
55
66
  }
56
67
  else {
57
68
  console.log(stats === null || stats === void 0 ? void 0 : stats.toString({
@@ -64,19 +75,24 @@ function startDevServer() {
64
75
  }
65
76
  });
66
77
  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
- }
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ if (isStarted) {
80
+ printDevServerAddress();
81
+ console.log(chalk_1.default.gray(` ~ Sent update event to ${connectedSockets.length} client(s)`));
82
+ const appJson = yield (0, promises_1.readFile)(node_path_1.default.resolve(process.cwd(), 'build/app.json'), 'utf-8');
83
+ for (const socket of connectedSockets) {
84
+ socket.emit('update', appJson);
85
+ }
86
+ }
87
+ else {
88
+ start();
89
+ }
90
+ });
77
91
  }
78
92
  function printDevServerAddress() {
79
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:`);
95
+ qrcode_terminal_1.default.generate(`http://${ipAddress}:${port}`, { small: true });
80
96
  }
81
97
  function start() {
82
98
  if (isStarted) {
@@ -85,23 +101,22 @@ function startDevServer() {
85
101
  app.all('*', function (req, res, next) {
86
102
  res.header('Access-Control-Allow-Origin', '*');
87
103
  res.header('Access-Control-Allow-Headers', '*');
88
- // res.header('Content-Type', 'application/jsoncharset=utf-8')
89
104
  res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, OPTIONS, PUT, DELETE');
90
105
  next();
91
106
  });
92
- app.use('/', express_1.default.static(node_path_1.default.resolve(process.cwd(), '../build')));
107
+ app.use('/', express_1.default.static(node_path_1.default.resolve(process.cwd(), 'build')));
93
108
  //有新的客户端连接时触发
94
109
  io.on('connection', function (socket) {
95
- console.log(chalk_1.default.green('[DevServer] A client connected.'));
110
+ console.log(chalk_1.default.green(` + A client(${socket.id}) connected.`));
96
111
  connectedSockets.push(socket);
97
112
  socket.on('error', function (err) {
98
- console.log(chalk_1.default.red('[DevServer] Error: ', err));
113
+ console.log(chalk_1.default.red(' ✖️ Server Error: ', err));
99
114
  });
100
115
  socket.on('disconnect', function () {
101
116
  var index = connectedSockets.indexOf(socket);
102
117
  if (index > -1) {
103
118
  connectedSockets.splice(index, 1);
104
- console.log(chalk_1.default.gray('[DevServer] A client disconnected.'));
119
+ console.log(chalk_1.default.gray(` - A client(${socket.id}) disconnected.`));
105
120
  }
106
121
  });
107
122
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scripting-cli",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "This plugin include all the commands for developing scripting-js apps.",
5
5
  "main": "./dist/index.js",
6
6
  "bin": {
@@ -27,13 +27,17 @@
27
27
  "homepage": "https://github.com/thomfang/scripting-cli#readme",
28
28
  "dependencies": {
29
29
  "chalk": "^4.1.2",
30
+ "cross-zip": "^4.0.1",
30
31
  "express": "^4.18.2",
31
32
  "minimist": "^1.2.8",
33
+ "qrcode-terminal": "^0.12.0",
32
34
  "socket.io": "^4.7.5",
33
35
  "webpack": "^5.88.2"
34
36
  },
35
37
  "devDependencies": {
38
+ "@types/cross-zip": "^4.0.2",
36
39
  "@types/express": "^4.17.21",
37
- "@types/minimist": "^1.2.5"
40
+ "@types/minimist": "^1.2.5",
41
+ "@types/qrcode-terminal": "^0.12.2"
38
42
  }
39
- }
43
+ }
@@ -0,0 +1,40 @@
1
+
2
+ declare module '*.avif' {
3
+ const src: string;
4
+ export default src;
5
+ }
6
+
7
+ declare module '*.bmp' {
8
+ const src: string;
9
+ export default src;
10
+ }
11
+
12
+ declare module '*.gif' {
13
+ const src: string;
14
+ export default src;
15
+ }
16
+
17
+ declare module '*.jpg' {
18
+ const src: string;
19
+ export default src;
20
+ }
21
+
22
+ declare module '*.jpeg' {
23
+ const src: string;
24
+ export default src;
25
+ }
26
+
27
+ declare module '*.png' {
28
+ const src: string;
29
+ export default src;
30
+ }
31
+
32
+ declare module '*.webp' {
33
+ const src: string;
34
+ export default src;
35
+ }
36
+
37
+ declare module '*.svg' {
38
+ const src: string;
39
+ export default src;
40
+ }
@@ -0,0 +1,4 @@
1
+ <svg width="512" height="512" viewBox="0 0 512 512" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <rect width="512" height="512" fill="#1E1E1E"/>
3
+ <path d="M153.613 333L75 256L153.636 179H184.355L105.718 256L184.355 333H153.613ZM357.34 333H326.645L405.282 256L326.645 179H357.364L436 256L357.34 333ZM305.954 179L230.456 333H204.999L280.497 179H305.954Z" fill="white"/>
4
+ </svg>