1
1
import { spawn } from "node:child_process" ;
2
- import fs from "node:fs" ;
3
2
import * as path from "node:path" ;
4
3
import * as util from "node:util" ;
5
4
import { watch } from "chokidar" ;
@@ -9,7 +8,6 @@ import { Box, Text, useApp, useInput, useStdin } from "ink";
9
8
import React , { useEffect , useRef , useState } from "react" ;
10
9
import { useErrorHandler , withErrorBoundary } from "react-error-boundary" ;
11
10
import onExit from "signal-exit" ;
12
- import tmp from "tmp-promise" ;
13
11
import { fetch } from "undici" ;
14
12
import { runCustomBuild } from "../deployment-bundle/run-custom-build" ;
15
13
import {
@@ -20,6 +18,7 @@ import {
20
18
} from "../dev-registry" ;
21
19
import { logger } from "../logger" ;
22
20
import openInBrowser from "../open-in-browser" ;
21
+ import { getWranglerTmpDir } from "../paths" ;
23
22
import { openInspector } from "./inspect" ;
24
23
import { Local } from "./local" ;
25
24
import { Remote } from "./remote" ;
@@ -31,6 +30,7 @@ import type { Entry } from "../deployment-bundle/entry";
31
30
import type { CfModule , CfWorkerInit } from "../deployment-bundle/worker" ;
32
31
import type { WorkerRegistry } from "../dev-registry" ;
33
32
import type { EnablePagesAssetsServiceBindingOptions } from "../miniflare-cli/types" ;
33
+ import type { EphemeralDirectory } from "../paths" ;
34
34
import type { AssetPaths } from "../sites" ;
35
35
36
36
/**
@@ -164,6 +164,7 @@ export type DevProps = {
164
164
firstPartyWorker : boolean | undefined ;
165
165
sendMetrics : boolean | undefined ;
166
166
testScheduled : boolean | undefined ;
167
+ projectRoot : string | undefined ;
167
168
} ;
168
169
169
170
export function DevImplementation ( props : DevProps ) : JSX . Element {
@@ -248,7 +249,7 @@ type DevSessionProps = DevProps & {
248
249
function DevSession ( props : DevSessionProps ) {
249
250
useCustomBuild ( props . entry , props . build ) ;
250
251
251
- const directory = useTmpDir ( ) ;
252
+ const directory = useTmpDir ( props . projectRoot ) ;
252
253
253
254
const workerDefinitions = useDevRegistry (
254
255
props . name ,
@@ -284,6 +285,7 @@ function DevSession(props: DevSessionProps) {
284
285
targetConsumer : "dev" ,
285
286
testScheduled : props . testScheduled ?? false ,
286
287
experimentalLocal : props . experimentalLocal ,
288
+ projectRoot : props . projectRoot ,
287
289
} ) ;
288
290
289
291
// TODO(queues) support remote wrangler dev
@@ -376,37 +378,23 @@ function DevSession(props: DevSessionProps) {
376
378
) ;
377
379
}
378
380
379
- export interface DirectorySyncResult {
380
- name : string ;
381
- removeCallback : ( ) => void ;
382
- }
383
-
384
- function useTmpDir ( ) : string | undefined {
381
+ function useTmpDir ( projectRoot : string | undefined ) : string | undefined {
385
382
const [ directory , setDirectory ] = useState < string > ( ) ;
386
383
const handleError = useErrorHandler ( ) ;
387
384
useEffect ( ( ) => {
388
- let dir : DirectorySyncResult | undefined ;
385
+ let dir : EphemeralDirectory | undefined ;
389
386
try {
390
- // const tmpdir = path.resolve(".wrangler", "tmp");
391
- // fs.mkdirSync(tmpdir, { recursive: true });
392
- // dir = tmp.dirSync({ unsafeCleanup: true, tmpdir });
393
- dir = tmp . dirSync ( { unsafeCleanup : true } ) as DirectorySyncResult ;
394
- // Make sure we resolve all files relative to the actual temporary
395
- // directory, without symlinks, otherwise `esbuild` will generate invalid
396
- // source maps.
397
- const realpath = fs . realpathSync ( dir . name ) ;
398
- setDirectory ( realpath ) ;
387
+ dir = getWranglerTmpDir ( projectRoot , "dev" ) ;
388
+ setDirectory ( dir . path ) ;
399
389
return ;
400
390
} catch ( err ) {
401
391
logger . error (
402
392
"Failed to create temporary directory to store built files."
403
393
) ;
404
394
handleError ( err ) ;
405
395
}
406
- return ( ) => {
407
- dir ?. removeCallback ( ) ;
408
- } ;
409
- } , [ handleError ] ) ;
396
+ return ( ) => dir ?. remove ( ) ;
397
+ } , [ projectRoot , handleError ] ) ;
410
398
return directory ;
411
399
}
412
400
0 commit comments