dewctl 0.9.2 → 0.9.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dewctl",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
4
4
  "description": "Build and publish three.js games on Dewdrop.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/logs.js CHANGED
@@ -6,13 +6,19 @@ import {sleep} from './client.js';
6
6
  const WAIT_MS=25000,MOST_REST_MS=30000;
7
7
  // The characters but tab and line breaks that a terminal takes as commands rather than text, shown as `?`.
8
8
  export const CONTROL=/[\x00-\x08\x0b-\x1f\x7f-\x9f]/g;
9
+ // Text from Dewdrop as printed: a game's own words reach it — its console, what its code threw, its compile errors —
10
+ // so each control character is shown as `?`, and no game moves the cursor, retitles the window or writes the clipboard.
11
+ export const printable=text=>String(text).replace(CONTROL,'?');
12
+ // A value printed as JSON: JSON.stringify writes the controls below space as escapes itself, but not DEL and the C1
13
+ // controls, which a terminal may take as commands too; they are written as escapes as well, the JSON reading the same.
14
+ export const json=(value,indent)=>JSON.stringify(value,null,indent).replace(/[\x7f-\x9f]/g,c=>`\\u00${c.charCodeAt(0).toString(16)}`);
9
15
  const two=n=>String(n).padStart(2,'0');
10
16
  // A line as printed: this computer's time of it, where it came from — the page of a seat, named by its player, the
11
17
  // backend, or a seat's session — its level, and its text, positions mapped by `maps` for the release that wrote it.
12
18
  export function format(line,maps) {
13
19
  const at=new Date(line.at),time=Number.isNaN(at.getTime())?'':`${two(at.getHours())}:${two(at.getMinutes())}:${two(at.getSeconds())} `;
14
20
  const text=maps?maps.text(line.release,String(line.text)):String(line.text);
15
- return `${time}[${line.source}${line.seat?` ${line.seat}`:''}] ${line.level}: ${text}`.replace(CONTROL,'?');
21
+ return printable(`${time}[${line.source}${line.seat?` ${line.seat}`:''}] ${line.level}: ${text}`);
16
22
  }
17
23
  // Prints `game`'s dev console from the start of what Dewdrop keeps: those lines alone, or with `follow` each new line
18
24
  // as it comes until `signal` ends it, starting again from the start when the dev version restarts. Following, a
@@ -29,7 +35,7 @@ export async function printLogs(client,game,{signal,follow=false,maps,log=consol
29
35
  catch(error) {
30
36
  if(signal?.aborted)break;
31
37
  if(!follow||!(error.transient||[429,503].includes(error.status)||error.status>=500))throw error;
32
- if(!failures++)note(error.status===503?`Waiting for ${game}'s dev version to run…`:`${error.message} Following again once Dewdrop answers.`);
38
+ if(!failures++)note(error.status===503?`Waiting for ${game}'s dev version to run…`:printable(`${error.message} Following again once Dewdrop answers.`));
33
39
  try { await sleep(Math.min(restMs*2**(failures-1),MOST_REST_MS),signal); } catch { break; }
34
40
  continue;
35
41
  }
package/src/main.js CHANGED
@@ -12,7 +12,7 @@ import {signInLink,waitForApproval} from './login.js';
12
12
  import {RUN} from './command.js';
13
13
  import {syncThumbnail} from './thumbnail.js';
14
14
  import {SourceMaps} from './sourcemap.js';
15
- import {printLogs} from './logs.js';
15
+ import {printLogs,printable,json} from './logs.js';
16
16
  import {findSeat,seatTable,codeOf,shown,machine,roomLine,frames,factor,repl} from './session.js';
17
17
 
18
18
  const HELP=`${RUN} — make three.js games on Dewdrop
@@ -194,13 +194,13 @@ async function main() {
194
194
  if(command==='login') {
195
195
  credentials.prepare();
196
196
  const link=signInLink(origin);
197
- console.log(`Open this link to sign in the Dewdrop CLI:\n\n ${link.url}\n\nCheck that the page shows ${link.code}, then choose Sign in the CLI. Waiting…`);
197
+ console.log(printable(`Open this link to sign in the Dewdrop CLI:\n\n ${link.url}\n\nCheck that the page shows ${link.code}, then choose Sign in the CLI. Waiting…`));
198
198
  if(atScreen())open(link.url);
199
199
  let me;
200
200
  try { me=await waitForApproval(client,link.token,{signal}); }
201
201
  catch(error) { throw signal.aborted?new Error('Sign-in cancelled.'):error; }
202
202
  credentials.write({token:link.token,email:me.email,expires:me.expires});
203
- console.log(`Signed in as ${me.username??me.email}.`);
203
+ console.log(printable(`Signed in as ${me.username??me.email}.`));
204
204
  return;
205
205
  }
206
206
  if(command==='logout') {
@@ -229,21 +229,21 @@ async function main() {
229
229
  if(flags.out?.split(/[\\/]/).includes('..'))throw new Error(`Name the --out file without .., such as .dewdrop-seat.json in the game's folder.`);
230
230
  if(flags.out&&project&&insideBundle(project,flags.out))throw new Error(`${flags.out} is in ${project.bundle}, which ${RUN} dev uploads, so the pass would be uploaded with the game. Write it outside that folder: --out .dewdrop-seat.json writes it in the game's folder, which uploads skip.`);
231
231
  const pass=await client.seat(game,signal);
232
- if(!flags.out){process.stdout.write(`${JSON.stringify(pass,null,2)}\n`);return;}
232
+ if(!flags.out){process.stdout.write(`${json(pass,2)}\n`);return;}
233
233
  writePrivate(flags.out,`${JSON.stringify(pass,null,2)}\n`);
234
- console.log(`Wrote a seat pass for ${game} to ${flags.out}. It expires at ${pass.expires}.`);
234
+ console.log(printable(`Wrote a seat pass for ${game} to ${flags.out}. It expires at ${pass.expires}.`));
235
235
  return;
236
236
  }
237
237
  if(command==='sessions') {
238
238
  const answer=await client.sessions(game,signal);
239
- console.log(flags.json?JSON.stringify(answer):seatTable(answer.sessions,game));
239
+ console.log(flags.json?json(answer):seatTable(answer.sessions,game));
240
240
  return;
241
241
  }
242
242
  if(command==='eval') {
243
243
  const code=await codeOf(flags.code,process.stdin,signal);
244
244
  const seat=flags.seat??(await findSeat(client,game,undefined,signal)).seat;
245
245
  const answer=await client.evaluate(game,seat,code,Boolean(flags.backend),signal);
246
- if(flags.json)console.log(JSON.stringify(await machine(client,game,answer,maps,signal)));
246
+ if(flags.json)console.log(json(await machine(client,game,answer,maps,signal)));
247
247
  else {
248
248
  const printed=await shown(client,game,answer,maps,signal);
249
249
  (printed.error?console.error:console.log)(printed.text);
@@ -255,7 +255,7 @@ async function main() {
255
255
  const fields=command==='step'?{frames:frames(flags.frames)}:command==='speed'?{k:factor(flags.k)}:{};
256
256
  const {room}=await findSeat(client,game,flags.seat,signal);
257
257
  const answer=await client.room(game,room,command,fields,signal);
258
- console.log(flags.json?JSON.stringify(answer):roomLine(room,answer,fields.k));
258
+ console.log(flags.json?json(answer):roomLine(room,answer,fields.k));
259
259
  return;
260
260
  }
261
261
  if(command==='repl') {
@@ -267,7 +267,7 @@ async function main() {
267
267
  try { state=await client.game(game); }
268
268
  catch(error) { if(error.status===404)throw new Error(`${game} isn't on Dewdrop yet. Run ${RUN} dev to create it.`); throw error; }
269
269
  const url=devPlayUrl(state);
270
- console.log(`${game}\n Dev version: ${describe(state.dev,'stopped')}${state.dev.release?` — ${url}`:''}\n Published: ${describe(state.live,'not published')}${state.live.release?` — ${state.urls.play}`:''}\n Thumbnail: ${state.urls.thumbnail??'none'}`);
270
+ console.log(printable(`${game}\n Dev version: ${describe(state.dev,'stopped')}${state.dev.release?` — ${url}`:''}\n Published: ${describe(state.live,'not published')}${state.live.release?` — ${state.urls.play}`:''}\n Thumbnail: ${state.urls.thumbnail??'none'}`));
271
271
  return;
272
272
  }
273
273
  if(command==='releases'){
@@ -276,14 +276,14 @@ async function main() {
276
276
  // What each release is to the game: the versions running it, else whether it was published before, is an older
277
277
  // dev build, or is the latest upload.
278
278
  const status=release=>[release.sha256===state.dev.release&&'dev',release.sha256===state.live.release&&'published'].filter(Boolean).join(', ')||(release.published?'published before':release.devBuild?'dev build':'latest upload');
279
- console.log(columns([['RELEASE','UPLOADED (UTC)','SIZE','STATUS','BY'],...releases.map(release=>[release.sha256.slice(0,12),new Date(release.created).toISOString().slice(0,16).replace('T',' '),size(release.bytes),status(release),release.email])]));
279
+ console.log(printable(columns([['RELEASE','UPLOADED (UTC)','SIZE','STATUS','BY'],...releases.map(release=>[release.sha256.slice(0,12),new Date(release.created).toISOString().slice(0,16).replace('T',' '),size(release.bytes),status(release),release.email])])));
280
280
  return;
281
281
  }
282
282
  if(command==='delete'){
283
283
  if(!flags.release)throw new Error(`delete needs a release id from ${RUN} releases.`);
284
284
  const release=await releaseId(client,game,flags.release);
285
285
  await client.deleteRelease(game,release);
286
- console.log(`Deleted ${release.slice(0,12)} from ${game}.`);return;
286
+ console.log(printable(`Deleted ${release.slice(0,12)} from ${game}.`));return;
287
287
  }
288
288
  if(command==='delete-game'){
289
289
  if(flags.name!==game)throw new Error(`delete-game takes the game's name to confirm. Run ${RUN} delete-game ${game} to delete ${game}.`);
@@ -308,11 +308,11 @@ async function main() {
308
308
  // The thumbnail goes first, so an image Dewdrop refuses stops the publish before anything changes for players.
309
309
  if(project.thumbnail&&await syncThumbnail(project,client,current,signal))console.log(`Uploaded the thumbnail, ${project.thumbnail}.`);
310
310
  const state=await client.publish(game,release,signal);
311
- console.log(`Published ${release.slice(0,12)}. Players can open ${game} at ${state.urls.play}`);return;
311
+ console.log(printable(`Published ${release.slice(0,12)}. Players can open ${game} at ${state.urls.play}`));return;
312
312
  }
313
313
  if(flags.release){
314
314
  const release=await releaseId(client,game,flags.release);
315
- try { await client.activate(game,release,signal,state=>console.log(`Starting ${release.slice(0,12)} at ${devPlayUrl(state)}`)); }
315
+ try { await client.activate(game,release,signal,state=>console.log(printable(`Starting ${release.slice(0,12)} at ${devPlayUrl(state)}`))); }
316
316
  catch(error) { error.message=maps.text(release,error.message); throw error; }
317
317
  console.log('Ready to play.');return;
318
318
  }
@@ -330,4 +330,4 @@ async function main() {
330
330
  }
331
331
  }
332
332
  }
333
- main().catch(error=>{console.error(`dewctl: ${error.message}`);process.exitCode=1;});
333
+ main().catch(error=>{console.error(`dewctl: ${printable(error.message)}`);process.exitCode=1;});
package/src/session.js CHANGED
@@ -5,7 +5,7 @@
5
5
  import {createInterface} from 'node:readline';
6
6
  import {readFileSync} from 'node:fs';
7
7
  import {RUN} from './command.js';
8
- import {CONTROL} from './logs.js';
8
+ import {printable,json} from './logs.js';
9
9
 
10
10
  // No seat of the developer's playing, in their words: how to get one.
11
11
  const noSeat=game=>`No dev seat of yours is playing ${game}: open one in a browser (${RUN} seat) first.`;
@@ -23,7 +23,7 @@ export async function findSeat(client,game,seat,signal) {
23
23
  export function seatTable(sessions,game) {
24
24
  if(!sessions.length)return noSeat(game);
25
25
  const width=Math.max(4,...sessions.map(each=>each.seat.length));
26
- return [`${'SEAT'.padEnd(width)} ROOM`,...sessions.map((each,i)=>`${each.seat.padEnd(width)} ${each.room}${i===0?' (default)':''}`)].join('\n').replace(CONTROL,'?');
26
+ return printable([`${'SEAT'.padEnd(width)} ROOM`,...sessions.map((each,i)=>`${each.seat.padEnd(width)} ${each.room}${i===0?' (default)':''}`)].join('\n'));
27
27
  }
28
28
  // The code `eval` is given: the argument itself, everything on standard input for `-`, or a file's text for `@FILE`.
29
29
  // Reading standard input ends at `signal`, the input let go so the CLI can end.
@@ -49,9 +49,9 @@ export async function shown(client,game,answer,maps,signal) {
49
49
  if(typeof answer.error==='string') {
50
50
  let text=answer.error;
51
51
  if(maps)try { text=maps.text((await client.game(game,signal)).dev.release,text); } catch {}
52
- return {error:true,text:text.replace(CONTROL,'?')};
52
+ return {error:true,text:printable(text)};
53
53
  }
54
- return {error:false,text:'value' in answer?JSON.stringify(answer.value,null,2):'undefined'};
54
+ return {error:false,text:'value' in answer?json(answer.value,2):'undefined'};
55
55
  }
56
56
  // The answer to an evaluate, `--json`: as Dewdrop gave it, with the error's positions mapped as printed.
57
57
  export async function machine(client,game,answer,maps,signal) {
@@ -61,7 +61,7 @@ export async function machine(client,game,answer,maps,signal) {
61
61
  // A room's state after an action, in words, and after a speed the speed it runs at: the room rounds a factor `k`
62
62
  // down to 1/2, 1/3, 1/4 and so on, and a paused room runs at it once resumed.
63
63
  export function roomLine(room,{frame,paused},k) {
64
- const line=`Room ${room}: frame ${frame}, ${paused?'paused':'running'}.`;
64
+ const line=printable(`Room ${room}: frame ${frame}, ${paused?'paused':'running'}.`);
65
65
  if(k===undefined)return line;
66
66
  const every=Math.ceil(1/k);
67
67
  return `${line} It runs at ${every===1?'full':`1/${every}`} speed${paused?' once resumed':''}.`;
@@ -94,8 +94,9 @@ export async function repl(client,game,{seat,backend=false,maps,input=process.st
94
94
  const terminal=Boolean(input.isTTY&&output.isTTY);
95
95
  const stop=new AbortController(),{signal}=stop;
96
96
  let current=(await findSeat(client,game,seat)).seat;
97
- const say=text=>output.write(`${text}\n`);
98
- const prompt=()=>`${game} ${current}${backend?' backend':''}> `;
97
+ // Everything the console prints names what Dewdrop answered — a seat, a room, a value — so all of it is printable.
98
+ const say=text=>output.write(`${printable(text)}\n`);
99
+ const prompt=()=>printable(`${game} ${current}${backend?' backend':''}> `);
99
100
  say(`Evaluating in ${current}${backend?'\'s backend':''} of ${game}. .help lists the commands.`);
100
101
  const lines=createInterface({input,output:terminal?output:undefined,terminal,prompt:prompt()});
101
102
  // Ctrl-C at a terminal; a console reading a pipe ends at its end, once every line read is answered.
package/src/watch.js CHANGED
@@ -3,6 +3,7 @@ import {watch as watchDirectory} from 'node:fs';
3
3
  import {fingerprint,build,snapshot,followed} from './project.js';
4
4
  import {devPlayUrl} from './play.js';
5
5
  import {RUN} from './command.js';
6
+ import {printable} from './logs.js';
6
7
 
7
8
  // A byte count as a person reads it.
8
9
  export function size(bytes) {
@@ -16,7 +17,7 @@ const count=(n,noun)=>`${n} ${noun}${n===1?'':'s'}`;
16
17
  // What a push says as it goes, in the developer's terms: the files going up, or checked with Dewdrop first, the ones
17
18
  // Dewdrop turned out to lack, and each phase of a file stored over minutes.
18
19
  const telling=log=>event=>{
19
- if(event.phase!==undefined)log(PHASES[event.phase]??`Dewdrop is ${event.phase} the upload…`);
20
+ if(event.phase!==undefined)log(PHASES[event.phase]??printable(`Dewdrop is ${event.phase} the upload…`));
20
21
  else if(event.checking!==undefined)log(`Checking ${count(event.checking,'file')} (${size(event.bytes)}) with Dewdrop…`);
21
22
  else if(event.missing!==undefined)log(`Uploading ${count(event.missing,'changed file')} (${size(event.bytes)})…`);
22
23
  else if(event.files===0)log('Dewdrop has every file of the build; recording the release…');
@@ -33,7 +34,7 @@ export async function deploy(project,client,{signal,log=console.log,maps}={}) {
33
34
  const bundle=await snapshot(project);
34
35
  const {release}=await client.push(project.game,bundle,signal,telling(log));
35
36
  maps?.remember(release,bundle.directory,bundle.maps);
36
- try { await client.activate(project.game,release,signal,game=>log(`Starting ${release.slice(0,12)} at ${devPlayUrl(game)}`)); }
37
+ try { await client.activate(project.game,release,signal,game=>log(printable(`Starting ${release.slice(0,12)} at ${devPlayUrl(game)}`))); }
37
38
  catch(error) { if(maps)error.message=maps.text(release,error.message); throw error; }
38
39
  log('Ready to play.');
39
40
  return {changed:false};
@@ -69,7 +70,7 @@ export async function watch(project,client,{signal,log=console.log,deployGame=de
69
70
  following?.controller.abort();
70
71
  const controller=new AbortController(),current={game,controller};
71
72
  current.done=follow(game,signal?AbortSignal.any([signal,controller.signal]):controller.signal).catch(error=>{
72
- if(!controller.signal.aborted&&!signal?.aborted)log(`dewctl: ${error.message}`);
73
+ if(!controller.signal.aborted&&!signal?.aborted)log(`dewctl: ${printable(error.message)}`);
73
74
  }).finally(()=>{if(following===current)following=null;});
74
75
  following=current;
75
76
  };
@@ -95,13 +96,13 @@ export async function watch(project,client,{signal,log=console.log,deployGame=de
95
96
  else startFollowing(game);
96
97
  }).catch(error=>{
97
98
  if(signal?.aborted)return;
98
- log(`dewctl: ${error.message}`);
99
+ log(`dewctl: ${printable(error.message)}`);
99
100
  if(error.status===401)log(`Paused until you sign in: run ${RUN} login in another terminal, then save a file.`);
100
101
  else if(error.transient){log('Trying again in a moment.');dirty=true;changedAt=Date.now()+2000;}
101
102
  else log('Fix it and save to try again.');
102
103
  }).finally(()=>{running=null;});
103
104
  }
104
- }catch(error){log(`dewctl: ${error.message}`);log('Fix it and save to try again.');}
105
+ }catch(error){log(`dewctl: ${printable(error.message)}`);log('Fix it and save to try again.');}
105
106
  await new Promise(resolve=>setTimeout(resolve,pollMs));
106
107
  }
107
108
  await running;