@@ -35,6 +35,7 @@ export interface LoaderOptions {
35
35
path : string ;
36
36
sourceRoot : string ;
37
37
targetPath : string ;
38
+ useStale : boolean ;
38
39
}
39
40
40
41
export abstract class Loader {
@@ -57,10 +58,16 @@ export abstract class Loader {
57
58
*/
58
59
readonly targetPath : string ;
59
60
60
- constructor ( { path, sourceRoot, targetPath} : LoaderOptions ) {
61
+ /**
62
+ * Should the loader use a stale cache. true when building.
63
+ */
64
+ readonly useStale ?: boolean ;
65
+
66
+ constructor ( { path, sourceRoot, targetPath, useStale} : LoaderOptions ) {
61
67
this . path = path ;
62
68
this . sourceRoot = sourceRoot ;
63
69
this . targetPath = targetPath ;
70
+ this . useStale = useStale ;
64
71
}
65
72
66
73
/**
@@ -70,8 +77,8 @@ export abstract class Loader {
70
77
* abort if we find a matching folder or reach the source root; for example,
71
78
* if docs/data exists, we won’t look for a docs/data.zip.
72
79
*/
73
- static find ( sourceRoot : string , targetPath : string ) : Loader | undefined {
74
- const exact = this . findExact ( sourceRoot , targetPath ) ;
80
+ static find ( sourceRoot : string , targetPath : string , { useStale = false } = { } ) : Loader | undefined {
81
+ const exact = this . findExact ( sourceRoot , targetPath , { useStale } ) ;
75
82
if ( exact ) return exact ;
76
83
let dir = dirname ( targetPath ) ;
77
84
for ( let parent : string ; true ; dir = parent ) {
@@ -88,23 +95,25 @@ export abstract class Loader {
88
95
inflatePath : targetPath . slice ( archive . length - ext . length + 1 ) ,
89
96
path : join ( sourceRoot , archive ) ,
90
97
sourceRoot,
91
- targetPath
98
+ targetPath,
99
+ useStale
92
100
} ) ;
93
101
}
94
- const archiveLoader = this . findExact ( sourceRoot , archive ) ;
102
+ const archiveLoader = this . findExact ( sourceRoot , archive , { useStale } ) ;
95
103
if ( archiveLoader ) {
96
104
return new Extractor ( {
97
105
preload : async ( options ) => archiveLoader . load ( options ) ,
98
106
inflatePath : targetPath . slice ( archive . length - ext . length + 1 ) ,
99
107
path : archiveLoader . path ,
100
108
sourceRoot,
101
- targetPath
109
+ targetPath,
110
+ useStale
102
111
} ) ;
103
112
}
104
113
}
105
114
}
106
115
107
- private static findExact ( sourceRoot : string , targetPath : string ) : Loader | undefined {
116
+ private static findExact ( sourceRoot : string , targetPath : string , { useStale } ) : Loader | undefined {
108
117
for ( const [ ext , [ command , ...args ] ] of Object . entries ( languages ) ) {
109
118
if ( ! existsSync ( join ( sourceRoot , targetPath + ext ) ) ) continue ;
110
119
if ( extname ( targetPath ) === "" ) {
@@ -117,7 +126,8 @@ export abstract class Loader {
117
126
args : command == null ? args : [ ...args , path ] ,
118
127
path,
119
128
sourceRoot,
120
- targetPath
129
+ targetPath,
130
+ useStale
121
131
} ) ;
122
132
}
123
133
}
@@ -127,6 +137,7 @@ export abstract class Loader {
127
137
* to the source root; this is within the .observablehq/cache folder within
128
138
* the source root.
129
139
*/
140
+
130
141
async load ( effects = defaultEffects ) : Promise < string > {
131
142
const key = join ( this . sourceRoot , this . targetPath ) ;
132
143
let command = runningCommands . get ( key ) ;
@@ -137,8 +148,10 @@ export abstract class Loader {
137
148
const loaderStat = await maybeStat ( this . path ) ;
138
149
const cacheStat = await maybeStat ( cachePath ) ;
139
150
if ( ! cacheStat ) effects . output . write ( faint ( "[missing] " ) ) ;
140
- else if ( cacheStat . mtimeMs < loaderStat ! . mtimeMs ) effects . output . write ( faint ( "[stale] " ) ) ;
141
- else return effects . output . write ( faint ( "[fresh] " ) ) , outputPath ;
151
+ else if ( cacheStat . mtimeMs < loaderStat ! . mtimeMs ) {
152
+ if ( this . useStale ) return effects . output . write ( faint ( "[using stale] " ) ) , outputPath ;
153
+ else effects . output . write ( faint ( "[stale] " ) ) ;
154
+ } else return effects . output . write ( faint ( "[fresh] " ) ) , outputPath ;
142
155
const tempPath = join ( this . sourceRoot , ".observablehq" , "cache" , `${ this . targetPath } .${ process . pid } ` ) ;
143
156
await prepareOutput ( tempPath ) ;
144
157
const tempFd = await open ( tempPath , "w" ) ;
0 commit comments