dewctl 0.6.5 → 0.6.6
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/examples/AGENTS.md +3 -0
- package/package.json +1 -1
- package/src/client.js +31 -5
- package/src/project.js +9 -1
- package/src/watch.js +32 -8
package/examples/AGENTS.md
CHANGED
|
@@ -83,6 +83,9 @@ The code ([limits](https://alpha.dewdrop.world/docs/limits)):
|
|
|
83
83
|
Web Audio; `<audio>` and `<video>` make no sound.
|
|
84
84
|
- `localStorage` and `indexedDB` throw. Storage is `saves.storage` and `saves.indexedDB` from `dewdrop-sdk`, open
|
|
85
85
|
once `await saves.load()` is done, and kept only by `await saves.save()` ([saves](https://alpha.dewdrop.world/docs/saves)).
|
|
86
|
+
- `sessionStorage`, `location` navigation, and `alert`, `confirm` and `prompt` throw: keep state in memory, restart in the
|
|
87
|
+
game or call `session.exit()`, and draw dialogs on the page. The console warns once about anything else that isn't
|
|
88
|
+
available; [what works](https://alpha.dewdrop.world/docs/limits) lists it.
|
|
86
89
|
- Keep `three` at 0.185.1, which Dewdrop provides ([project](https://alpha.dewdrop.world/docs/project)).
|
|
87
90
|
|
|
88
91
|
## 4. The working loop
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -16,6 +16,8 @@ export function sleep(ms,signal) {
|
|
|
16
16
|
}
|
|
17
17
|
// An upload is sent in parts of at most this many bytes.
|
|
18
18
|
export const PART_BYTES=32*1024*1024;
|
|
19
|
+
// How long a poll asks the platform to hold it for news (`Prefer: wait`), under what the edge holds a request open.
|
|
20
|
+
const WAIT_S=25;
|
|
19
21
|
// `chunks` cut into runs of exactly `bytes`, but the last, which is what remains and never empty: an archive given
|
|
20
22
|
// as a run of buffers of any lengths is sent in parts of one size, holding about one part at a time.
|
|
21
23
|
export function* parts(chunks,bytes=PART_BYTES) {
|
|
@@ -31,7 +33,9 @@ export function* parts(chunks,bytes=PART_BYTES) {
|
|
|
31
33
|
}
|
|
32
34
|
export class Client {
|
|
33
35
|
// `partBytes` is for the tests, which cut at a few bytes; an upload is sent in parts of PART_BYTES.
|
|
34
|
-
|
|
36
|
+
// `pollMs` is the least a wait on the platform takes a round: a platform that holds the request answers when there
|
|
37
|
+
// is news, within WAIT_S; one that answers at once instead is asked again after that long.
|
|
38
|
+
constructor(origin,credentials,{fetcher=fetch,pollMs=1000,timeoutMs=180000,storeMs=15*60*1000,partBytes=PART_BYTES}={}){this.origin=origin;this.credentials=credentials;this.fetcher=fetcher;this.pollMs=pollMs;this.timeoutMs=timeoutMs;this.storeMs=storeMs;this.partBytes=partBytes;this.holds=true;}
|
|
35
39
|
// A request signed in with `token`. When Dewdrop can't be reached the failure is marked transient, which a watcher
|
|
36
40
|
// or a sign-in waits out; a refused token asks for a new sign-in, whatever Dewdrop's words.
|
|
37
41
|
async request(path,{method='GET',body,token,signal,type,prefer}={}) {
|
|
@@ -94,9 +98,11 @@ export class Client {
|
|
|
94
98
|
let result=await this.api(path,{method:'POST',body:{parts:count},signal,prefer:'respond-async'}),phase;
|
|
95
99
|
while(result.bytes===undefined) {
|
|
96
100
|
if(Date.now()>=deadline)throw new Error(`Dewdrop is taking longer than fifteen minutes to store the upload. Run ${RUN} releases to see whether it arrived.`);
|
|
97
|
-
await sleep(this.pollMs,signal);
|
|
98
|
-
result=await this.api(path,{signal});
|
|
99
101
|
if(result.phase!==undefined&&result.phase!==phase){phase=result.phase;progress?.(phase);}
|
|
102
|
+
// Held by the platform until the completion ends, or answered at once by one that does not hold a poll.
|
|
103
|
+
const asked=Date.now();
|
|
104
|
+
result=await this.api(path,{signal,prefer:`wait=${WAIT_S}`});
|
|
105
|
+
if(result.bytes===undefined)await this.#rest(asked,signal);
|
|
100
106
|
}
|
|
101
107
|
if(result.release!==bundle.id)throw new Error('Dewdrop stored a different release than was uploaded, so nothing was changed. Try again.');
|
|
102
108
|
return result;
|
|
@@ -117,10 +123,30 @@ export class Client {
|
|
|
117
123
|
if(current.error)throw new Error(current.error);
|
|
118
124
|
if(current.ready)return state;
|
|
119
125
|
if(Date.now()>=deadline)throw new Error(`${doing} is taking longer than three minutes. Run ${RUN} status to follow it.`);
|
|
120
|
-
|
|
121
|
-
state=await this.
|
|
126
|
+
const asked=Date.now();
|
|
127
|
+
state=await this.held(game,version,signal);
|
|
128
|
+
// An answer with news is acted on at once; one without is asked again after the round.
|
|
129
|
+
const seen=state[version];
|
|
130
|
+
if(seen.release===release&&!seen.ready&&!seen.error)await this.#rest(asked,signal);
|
|
122
131
|
}
|
|
123
132
|
}
|
|
133
|
+
// The game as the platform answers it once `version` is ready or refused, holding the request for it; a platform from
|
|
134
|
+
// before that route answers the game as it stands, and is asked that way from then on.
|
|
135
|
+
async held(game,version,signal) {
|
|
136
|
+
if(!this.holds)return this.game(game,signal);
|
|
137
|
+
try{return await this.api(`/v1/games/${game}/${version}`,{signal,prefer:`wait=${WAIT_S}`});}
|
|
138
|
+
catch(error){
|
|
139
|
+
if(error.status!==405&&error.status!==404)throw error;
|
|
140
|
+
this.holds=false;
|
|
141
|
+
return this.game(game,signal);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// The rest of a round that came back at once: a platform holding the request has answered with news, one that
|
|
145
|
+
// answered at once is asked again `pollMs` after it was asked.
|
|
146
|
+
async #rest(asked,signal) {
|
|
147
|
+
const left=this.pollMs-(Date.now()-asked);
|
|
148
|
+
if(left>0)await sleep(left,signal);
|
|
149
|
+
}
|
|
124
150
|
// Sets the game's thumbnail to the image, sent as itself under its type.
|
|
125
151
|
thumbnail(game,bytes,type,signal){return this.api(`/v1/games/${game}/thumbnail`,{method:'PUT',body:bytes,type,signal});}
|
|
126
152
|
async member(game,email,on,signal) {
|
package/src/project.js
CHANGED
|
@@ -31,6 +31,14 @@ export function projectPath(root, path) {
|
|
|
31
31
|
if (!file.startsWith(`${resolve(root)}/`)) throw new Error(`Path leaves the project: ${path}`);
|
|
32
32
|
return file;
|
|
33
33
|
}
|
|
34
|
+
// Whether a change at `path`, relative to the project's root, is one `dev --watch` follows: the walk's rule by path —
|
|
35
|
+
// not under the bundle folder, node_modules, dist or target, and no name starting with a dot.
|
|
36
|
+
export function followed(project, path) {
|
|
37
|
+
const names = path.split(/[\\/]/);
|
|
38
|
+
if (names.some(name => name.startsWith('.') || ignored.has(name))) return false;
|
|
39
|
+
const output = relative(project.root, projectPath(project.root, project.bundle)).split('\\').join('/'), joined = names.join('/');
|
|
40
|
+
return joined !== output && !joined.startsWith(`${output}/`);
|
|
41
|
+
}
|
|
34
42
|
export function readProject(root = process.cwd()) {
|
|
35
43
|
root = realpathSync(root);
|
|
36
44
|
const file = join(root,'dewdrop.json');
|
|
@@ -172,7 +180,7 @@ export async function build(project, signal) {
|
|
|
172
180
|
const STARTER_FILES=['package.json','src','index.html','vite.config.js'];
|
|
173
181
|
// The SDK a new game depends on — the range moves with the SDK's minor, since a caret on 0.x stays within one —
|
|
174
182
|
// and this CLI, at its own version.
|
|
175
|
-
const SDK_RANGE='^0.
|
|
183
|
+
const SDK_RANGE='^0.8.0';
|
|
176
184
|
const CLI_RANGE=`^${JSON.parse(readFileSync(new URL('../package.json',import.meta.url),'utf8')).version}`;
|
|
177
185
|
// The games a new one can start from: Blank, which ships with this CLI, and the starter games in the public
|
|
178
186
|
// starter-templates repository, a folder each, at the commit this release pins.
|
package/src/watch.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Poll the project's files across edits, deletions and atomic saves; only one build and activation runs at a time.
|
|
2
|
+
import {watch as watchDirectory} from 'node:fs';
|
|
2
3
|
import {PART_BYTES} from './client.js';
|
|
3
|
-
import {fingerprint,build,snapshot} from './project.js';
|
|
4
|
+
import {fingerprint,build,snapshot,followed} from './project.js';
|
|
4
5
|
import {devPlayUrl} from './play.js';
|
|
5
6
|
import {RUN} from './command.js';
|
|
6
7
|
|
|
@@ -29,17 +30,39 @@ export async function deploy(project,client,{signal,log=console.log}={}) {
|
|
|
29
30
|
return {changed:false};
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
//
|
|
33
|
-
//
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
// Follows the tree of the project `current` gives by the system's file events, calling `changed` on each event in a
|
|
34
|
+
// file dev follows (`followed`, judged by the project as dewdrop.json says it at the event, so a bundle folder renamed
|
|
35
|
+
// is left out from the next build on), and `lost` should the watch fail later; null where the tree cannot be watched,
|
|
36
|
+
// when dev looks the files over instead.
|
|
37
|
+
export function watchTree(current,changed,lost=()=>{}) {
|
|
38
|
+
try {
|
|
39
|
+
const watcher=watchDirectory(current().root,{recursive:true,persistent:false},(event,filename)=>{
|
|
40
|
+
if(filename===null||followed(current(),filename.toString()))changed();
|
|
41
|
+
});
|
|
42
|
+
watcher.on('error',()=>{watcher.close();lost();});
|
|
43
|
+
return watcher;
|
|
44
|
+
}catch{return null;}
|
|
45
|
+
}
|
|
46
|
+
// Rebuilds on every save: told of each by the system's file events, a save settled EVENT_DEBOUNCE_MS after its last
|
|
47
|
+
// event — an editor writes a file in several steps, and a save-all many files — or, where the tree cannot be watched,
|
|
48
|
+
// by looking the files over every `pollMs`, a save settled `debounceMs` after the last change found. `reread` gives
|
|
49
|
+
// the project as dewdrop.json says it now, so an edit to it applies to the next build; a dewdrop.json that no longer
|
|
50
|
+
// reads waits, like a failed build, for the next save.
|
|
51
|
+
export const EVENT_DEBOUNCE_MS=100;
|
|
52
|
+
export async function watch(project,client,{signal,log=console.log,deployGame=deploy,readFingerprint=fingerprint,reread=()=>project,watchFiles=watchTree,pollMs=200,debounceMs=500}={}) {
|
|
53
|
+
let changedAt=Date.now()-debounceMs,dirty=true;
|
|
36
54
|
let running=null;
|
|
55
|
+
const noted=()=>{changedAt=Date.now();dirty=true;};
|
|
56
|
+
let watching=watchFiles?.(()=>project,noted,()=>{watching=null;observed=readFingerprint(project);});
|
|
57
|
+
let observed=watching?null:readFingerprint(project);
|
|
37
58
|
log(`Watching ${project.game} for changes. Ctrl-C stops.`);
|
|
38
59
|
while(!signal?.aborted) {
|
|
39
60
|
try {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
61
|
+
if(!watching) {
|
|
62
|
+
const next=readFingerprint(project);
|
|
63
|
+
if(next!==observed){observed=next;noted();}
|
|
64
|
+
}
|
|
65
|
+
if(dirty&&!running&&Date.now()-changedAt>=(watching?EVENT_DEBOUNCE_MS:debounceMs)) {
|
|
43
66
|
dirty=false;
|
|
44
67
|
project=reread();
|
|
45
68
|
running=deployGame(project,client,{signal,log}).then(result=>{
|
|
@@ -56,5 +79,6 @@ export async function watch(project,client,{signal,log=console.log,deployGame=de
|
|
|
56
79
|
await new Promise(resolve=>setTimeout(resolve,pollMs));
|
|
57
80
|
}
|
|
58
81
|
await running;
|
|
82
|
+
watching?.close();
|
|
59
83
|
log('Stopped watching. The dev version keeps running.');
|
|
60
84
|
}
|