@love-sqjm/magic 2026.4.16 → 2026.4.18
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/index.js +2 -1
- package/package.json +1 -1
- package/script/create-project.js +8 -6
- package/script/run-project.js +29 -21
- package/template/runtime/runtime.js +1 -0
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { program } from 'commander';
|
|
2
2
|
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
3
4
|
import { app } from "./app.js";
|
|
4
5
|
import { BuildProject } from "./script/build-project.js";
|
|
5
6
|
import { createProject } from "./script/create-project.js";
|
|
@@ -110,7 +111,7 @@ program
|
|
|
110
111
|
filename += ".m";
|
|
111
112
|
}
|
|
112
113
|
|
|
113
|
-
const filePath = app.project.dir
|
|
114
|
+
const filePath = path.join( app.project.dir, filename );
|
|
114
115
|
if ( fs.existsSync( filePath ) ) {
|
|
115
116
|
console.error( `错误: 文件已存在 [path:${ filePath }]` );
|
|
116
117
|
process.exit( 1 );
|
package/package.json
CHANGED
package/script/create-project.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
2
3
|
import { app } from "../app.js";
|
|
3
4
|
|
|
4
5
|
function getConfig( name, platform ) {
|
|
@@ -17,25 +18,26 @@ function getConfig( name, platform ) {
|
|
|
17
18
|
* @param platform {string}
|
|
18
19
|
*/
|
|
19
20
|
export function createProject( name, platform ) {
|
|
20
|
-
|
|
21
|
-
fs.mkdirSync(
|
|
21
|
+
const projectPath = path.join( app.project.dir, name );
|
|
22
|
+
fs.mkdirSync( projectPath, { recursive : true } );
|
|
23
|
+
fs.mkdirSync( path.join( projectPath, "app" ), { recursive : true } );
|
|
22
24
|
|
|
23
25
|
if ( platform === "module" ) {
|
|
24
26
|
let data = app.templateDir.projectBase.get( "build.module.toml" );
|
|
25
27
|
data = data.replaceAll( "$name", name )
|
|
26
28
|
.replace( "$target", platform );
|
|
27
|
-
fs.writeFileSync(
|
|
29
|
+
fs.writeFileSync( path.join( projectPath, "build.toml" ), data );
|
|
28
30
|
return;
|
|
29
31
|
}
|
|
30
32
|
let data = app.templateDir.projectBase.get( "build.toml" );
|
|
31
33
|
data = data.replace( "$name", name )
|
|
32
34
|
.replace( "$target", platform )
|
|
33
35
|
.replace( "$config", getConfig( name, platform ) );
|
|
34
|
-
fs.writeFileSync(
|
|
36
|
+
fs.writeFileSync( path.join( projectPath, "build.toml" ), data );
|
|
35
37
|
|
|
36
38
|
let data2 = app.templateDir.projectBase.get( "app.xml" );
|
|
37
39
|
data2 = data2.replace( "$name", name );
|
|
38
|
-
fs.writeFileSync(
|
|
40
|
+
fs.writeFileSync( path.join( projectPath, "app", "app.xml" ), data2 );
|
|
39
41
|
|
|
40
42
|
const indexMContent = `<import
|
|
41
43
|
root=""
|
|
@@ -69,5 +71,5 @@ export function createProject( name, platform ) {
|
|
|
69
71
|
}
|
|
70
72
|
</css>
|
|
71
73
|
`;
|
|
72
|
-
fs.writeFileSync(
|
|
74
|
+
fs.writeFileSync( path.join( projectPath, "app", "index.m" ), indexMContent );
|
|
73
75
|
}
|
package/script/run-project.js
CHANGED
|
@@ -11,10 +11,31 @@ import { macroReplace } from "./module/compiler/macro-replace.js";
|
|
|
11
11
|
import { examine_BuildConfig } from "./module/compiler/step/1.js";
|
|
12
12
|
import WebSocket from "ws"
|
|
13
13
|
|
|
14
|
+
function openBrowser( urlToOpen ) {
|
|
15
|
+
const platform = os.platform();
|
|
16
|
+
let command;
|
|
17
|
+
|
|
18
|
+
if ( platform === "win32" ) {
|
|
19
|
+
command = `start ${ urlToOpen }`;
|
|
20
|
+
} else if ( platform === "darwin" ) {
|
|
21
|
+
command = `open ${ urlToOpen }`;
|
|
22
|
+
} else {
|
|
23
|
+
command = `xdg-open ${ urlToOpen }`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
exec( command, ( error, stdout, stderr ) => {
|
|
27
|
+
if ( error ) {
|
|
28
|
+
printf.outFile.error( error );
|
|
29
|
+
}
|
|
30
|
+
printf.outFile.log( stdout );
|
|
31
|
+
printf.error( stderr );
|
|
32
|
+
} );
|
|
33
|
+
}
|
|
34
|
+
|
|
14
35
|
const platform = {
|
|
15
36
|
"node-webkit" : ( platform_config ) => {
|
|
16
37
|
try {
|
|
17
|
-
const pk =
|
|
38
|
+
const pk = path.join( app.project.dir, "package.json" );
|
|
18
39
|
if ( !fs.existsSync( pk ) ) {
|
|
19
40
|
fs.writeFileSync( pk, "{}" );
|
|
20
41
|
}
|
|
@@ -27,7 +48,7 @@ const platform = {
|
|
|
27
48
|
}
|
|
28
49
|
|
|
29
50
|
const newPk = Object.assign( pkJson, platform_config.build.platform.config );
|
|
30
|
-
newPk[ "main" ] = platform_config.build.out
|
|
51
|
+
newPk[ "main" ] = path.join( platform_config.build.out, platform_config.config.main + ".html" );
|
|
31
52
|
delete newPk[ "app" ];
|
|
32
53
|
fs.writeFileSync( pk, JSON.stringify( newPk ) );
|
|
33
54
|
} catch ( e ) {
|
|
@@ -45,14 +66,8 @@ const platform = {
|
|
|
45
66
|
},
|
|
46
67
|
"web" : ( platform_config ) => {
|
|
47
68
|
if ( platform_config.build.platform.config.browser ) {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
if ( error ) {
|
|
51
|
-
throw error;
|
|
52
|
-
}
|
|
53
|
-
printf.log( stdout );
|
|
54
|
-
printf.error( stderr );
|
|
55
|
-
} );
|
|
69
|
+
const buildDir = path.join( app.project.dir, platform_config.build.out );
|
|
70
|
+
openBrowser( path.join( buildDir, platform_config.config.main + ".html" ) );
|
|
56
71
|
} else if ( platform_config.build.platform.config.server ) {
|
|
57
72
|
const hostname = platform_config.build.platform.config.server[ "host" ];
|
|
58
73
|
const port = platform_config.build.platform.config.server[ "port" ];
|
|
@@ -78,7 +93,7 @@ const platform = {
|
|
|
78
93
|
|
|
79
94
|
let target = path.resolve( rootDir, `.${ targetPath }` );
|
|
80
95
|
if ( !target.startsWith( rootDir ) ) {
|
|
81
|
-
target = rootDir
|
|
96
|
+
target = path.join( rootDir, "index.html" );
|
|
82
97
|
}
|
|
83
98
|
if ( fs.existsSync( target ) ) {
|
|
84
99
|
const ext = path.extname( target ).toLowerCase();
|
|
@@ -213,14 +228,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
|
213
228
|
console.log( `HTTP 服务器已启动: http://localhost:${ port }/index.html` );
|
|
214
229
|
console.log( `使用 Ctrl + C 停止服务器` );
|
|
215
230
|
|
|
216
|
-
|
|
217
|
-
exec( command, ( error, stdout, stderr ) => {
|
|
218
|
-
if ( error ) {
|
|
219
|
-
printf.outFile.error( error );
|
|
220
|
-
throw error;
|
|
221
|
-
}
|
|
222
|
-
printf.outFile.log( stdout );
|
|
223
|
-
} );
|
|
231
|
+
openBrowser( `http://${ hostname }:${ port }/index.html` );
|
|
224
232
|
} );
|
|
225
233
|
}
|
|
226
234
|
}
|
|
@@ -232,7 +240,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
|
232
240
|
export function RunProject() {
|
|
233
241
|
printf.outFile.info( `运行项目` );
|
|
234
242
|
|
|
235
|
-
const file = path.normalize( app.project.dir
|
|
243
|
+
const file = path.normalize( path.join( app.project.dir, "build.toml" ) );
|
|
236
244
|
import(file ).then( root => {
|
|
237
245
|
printf.outFile.info( `预处理 Build 配置文件 [path:${ file }]` );
|
|
238
246
|
const config = examine_BuildConfig( macroReplace( root ) );
|
|
@@ -240,7 +248,7 @@ export function RunProject() {
|
|
|
240
248
|
if ( config.build.platform.target === "module" ) {
|
|
241
249
|
throw `无法运行模块`;
|
|
242
250
|
} else {
|
|
243
|
-
config[ "build-dir" ] = path.normalize( app.project.dir
|
|
251
|
+
config[ "build-dir" ] = path.normalize( path.join( app.project.dir, config.build.out ) );
|
|
244
252
|
platform[ config.build.platform.target ]( config );
|
|
245
253
|
}
|
|
246
254
|
}
|
|
@@ -863,6 +863,7 @@ window[ "magic" ] = ( function () {
|
|
|
863
863
|
console.log( "%cMagic ヾ(๑╹◡╹)ノ",
|
|
864
864
|
"color:#f8faff;font-weight:bold;font-size:6em;padding:10px 30px;background: linear-gradient(to right top, #FFC0CB, #ADD8E6);" );
|
|
865
865
|
console.log( "magic v" + magic_version );
|
|
866
|
+
console.log( "link https://www.npmjs.com/package/@love-sqjm/magic" );
|
|
866
867
|
|
|
867
868
|
const app = document.getElementById( "app" );
|
|
868
869
|
|