Skip to content

Commit 734d674

Browse files
Merge pull request #7 from stackbithq/disable-cache-by-default
Disable cache by default when not in watch mode
2 parents 85f61b6 + cd8a81a commit 734d674

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

bin/sourcebit.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ commander
99
.version(pkg.version)
1010
.command("fetch")
1111
.option("-c, --configPath", "specify the location of the configuration file")
12+
.option(
13+
"-C, --cache",
14+
"force Sourcebit to use a filesystem cache, even when `watch` is disabled"
15+
)
1216
.option("-w, --watch", "run continuously in watch mode")
1317
.option("-q, --quiet", "disable logging messages to the console")
14-
.action(({ configPath: customConfigPath, watch }) => {
18+
.action(({ cache, configPath: customConfigPath, quiet, watch }) => {
1519
const configPath = path.resolve(
1620
process.cwd(),
1721
customConfigPath || "sourcebit.js"
1822
);
1923
const config = require(configPath);
2024
const runtimeParameters = {
25+
cache,
26+
quiet,
2127
watch
2228
};
2329

lib/sourcebit.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ class Sourcebit {
2929
this.pluginBlocks = [];
3030
this.pluginModules = {};
3131
this.runtimeParameters = runtimeParameters;
32+
33+
this.isCacheEnabled = Boolean(
34+
runtimeParameters.cache === undefined
35+
? runtimeParameters.watch
36+
: runtimeParameters.cache
37+
);
3238
}
3339

3440
async bootstrapAll() {
@@ -94,7 +100,7 @@ class Sourcebit {
94100
}
95101

96102
loadContextFromCache() {
97-
if (this.runtimeParameters.cache === false) return;
103+
if (!this.isCacheEnabled) return;
98104

99105
try {
100106
const data = fs.readFileSync(this.cacheFilePath, "utf8");
@@ -158,7 +164,7 @@ class Sourcebit {
158164
}
159165

160166
saveContextToCache() {
161-
if (this.runtimeParameters.cache === false) return;
167+
if (!this.isCacheEnabled) return;
162168

163169
const serializedCache = JSON.stringify(this.context);
164170

lib/sourcebit.test.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ describe("`bootstrapAll()`", () => {
189189
plugins: [{ module: mockPlugin1 }, { module: mockPlugin2 }]
190190
};
191191

192-
const sourcebit = new Sourcebit();
192+
const sourcebit = new Sourcebit({
193+
runtimeParameters: {
194+
cache: true
195+
}
196+
});
193197

194198
sourcebit.loadPlugins(config.plugins);
195199

@@ -268,23 +272,26 @@ describe("`bootstrapAll()`", () => {
268272
});
269273

270274
describe("cache", () => {
271-
describe("if `runtimeParameters.cache` is `false`", () => {
275+
describe("if `runtimeParameters.cache` is `false` or unset", () => {
272276
test("does not populate context with cache file's contents", () => {
273277
mockCache = {
274278
"sourcebit-source-periodic-table": {
275279
elements: ["Hydrogen", "Helium"]
276280
}
277281
};
278282

279-
const sourcebit = new Sourcebit({
283+
const sourcebit1 = new Sourcebit({
280284
runtimeParameters: {
281285
cache: false
282286
}
283287
});
288+
const sourcebit2 = new Sourcebit();
284289

285-
sourcebit.loadContextFromCache();
290+
sourcebit1.loadContextFromCache();
291+
sourcebit2.loadContextFromCache();
286292

287-
expect(sourcebit.context).toEqual({});
293+
expect(sourcebit1.context).toEqual({});
294+
expect(sourcebit2.context).toEqual({});
288295
});
289296

290297
test("does not persist context to cache file", async () => {
@@ -306,31 +313,41 @@ describe("cache", () => {
306313
}
307314
}
308315
];
309-
const sourcebit = new Sourcebit({
316+
const sourcebit1 = new Sourcebit({
310317
runtimeParameters: {
311318
cache: false
312319
}
313320
});
321+
const sourcebit2 = new Sourcebit();
314322

315-
sourcebit.loadPlugins(plugins);
323+
sourcebit1.loadPlugins(plugins);
324+
sourcebit2.loadPlugins(plugins);
316325

317-
await sourcebit.bootstrapAll();
326+
await sourcebit1.bootstrapAll();
327+
await sourcebit2.bootstrapAll();
318328

319329
expect(fs.writeFileSync).not.toHaveBeenCalled();
320330
});
321331
});
322332

323-
describe("if `runtimeParameters.cache` is `true` or unset", () => {
333+
describe("if `runtimeParameters.cache` or `runtimeParameters.watch` are `true`", () => {
324334
test("populates context with cache file's contents", () => {
325-
const sourcebit = new Sourcebit({
335+
const sourcebit1 = new Sourcebit({
326336
runtimeParameters: {
327337
cache: true
328338
}
329339
});
340+
const sourcebit2 = new Sourcebit({
341+
runtimeParameters: {
342+
watch: true
343+
}
344+
});
330345

331-
sourcebit.loadContextFromCache();
346+
sourcebit1.loadContextFromCache();
347+
sourcebit2.loadContextFromCache();
332348

333-
expect(sourcebit.context).toEqual(mockCache);
349+
expect(sourcebit1.context).toEqual(mockCache);
350+
expect(sourcebit2.context).toEqual(mockCache);
334351
});
335352

336353
test("fails gracefully if there's an error when reading/parsing the cache file", async () => {
@@ -446,7 +463,11 @@ describe("`getContext()`", () => {
446463
}
447464
};
448465

449-
const sourcebit = new Sourcebit();
466+
const sourcebit = new Sourcebit({
467+
runtimeParameters: {
468+
cache: true
469+
}
470+
});
450471

451472
await sourcebit.bootstrapAll();
452473

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sourcebit",
3-
"version": "0.7.1",
3+
"version": "0.8.0",
44
"description": "Sourcebit helps developers build data-driven JAMstack sites by pulling data from any third-party resource",
55
"main": "index.js",
66
"bin": {

0 commit comments

Comments
 (0)