Skip to content

Commit c077e59

Browse files
save file
1 parent d6bef1c commit c077e59

1 file changed

Lines changed: 77 additions & 3 deletions

File tree

utils/misc/nodejs-terminal/html/snippet-nodejs/v1.0/snippet-nodejs-v1.0.html

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@
1313
{flex:1}
1414

1515

16+
.con-btn
17+
{display:flex;align-items:center;gap:10px;border:1px solid lightgray;padding:0 10px;cursor:pointer}
18+
19+
1620
input
1721
{font-size:16px;padding:5px 10px;box-sizing:border-box}
22+
input[type=button]
23+
{cursor:pointer}
24+
input[type=checkbox]
25+
{width:20px;height:20px;margin:0}
1826

1927
</style>
2028

@@ -27,7 +35,18 @@
2735
</web-editor>
2836

2937
<div id=btns>
30-
<input value=test type=button>
38+
<input value=run type=button>
39+
<input value=kill type=button>
40+
<div id=persist class=con-btn>
41+
persist
42+
<input type=checkbox>
43+
</div>
44+
<div id=echo class=con-btn>
45+
echo
46+
<input type=checkbox checked>
47+
</div>
48+
<input value=clear type=button>
49+
3150
</div>
3251

3352
<web-console component>
@@ -47,13 +66,15 @@
4766

4867
var df=true,did='webcontainer-terminal';
4968

50-
var $,vm
69+
var $,vm,ace,wc
5170
;
5271

5372
obj.initmod = function(params){
5473

5574
$ = mod.rd(params,'$',$);
5675
vm = mod.rd(params,'vm',vm);
76+
ace = mod.rd(params,'ace',ace);
77+
wc = mod.rd(params,'webcontainer',wc);
5778
debug = mod.rd(params,'debug',debug);
5879

5980
}//initmod
@@ -63,6 +84,12 @@
6384

6485

6586
var viewport;
87+
var editor;
88+
var cons;
89+
90+
var tmpfile = '~/tmp.js';
91+
92+
var btn = {};
6693

6794

6895
//:
@@ -73,6 +100,10 @@
73100

74101
await mod.auto();
75102

103+
editor = mod['web-editor'];
104+
cons = mod['web-console'];
105+
cons.log('ready');
106+
76107
await initdom();
77108

78109

@@ -91,7 +122,7 @@
91122

92123
shadow = host.shadowRoot;
93124

94-
$(shadow,'[value=test]').onclick = e=>alert('test');
125+
$(shadow,'[value=run]').onclick = btn.run;
95126

96127

97128
}//initdom
@@ -100,6 +131,49 @@
100131
//:
101132

102133

134+
btn.run = async function(){
135+
136+
var js = editor.get();
137+
await wc.fs.writeFile(tmpfile,js);
138+
139+
var cmdline = `node ${tmpfile}`;
140+
var args = cmdline.split(' ');
141+
var cmd = args.shift();
142+
process = await webcontainer.spawn(cmd,args);//,{cwd,output:true});
143+
144+
var stream = new WritableStream({write(data){
145+
146+
console.log(data);
147+
cons.log(data);
148+
149+
}});
150+
process.output.pipeTo(stream)
151+
152+
var code = await process.exit;
153+
154+
console.log('process exited :',code);
155+
cons.log('process exited :',code);
156+
157+
}//run
158+
159+
160+
btn.kill = function(){
161+
162+
if(!process){
163+
cons.log('no process');
164+
return;
165+
}
166+
167+
await process.kill();
168+
169+
}//kill
170+
171+
172+
//:
173+
174+
175+
176+
103177
return obj;
104178

105179
})

0 commit comments

Comments
 (0)