Run example
npm run import -- "cache all"
cache all
var path = require('path')
var fs = require('fs')
var assert = require('assert')
var importer = require('../Core')
var {listInProject} = importer.import("list project files");
var {
cacheCells, cellCache, refreshCache,
updateCache, updateCode
} = importer.import("cache notebook",
"cell cache",
"refresh regular cache",
"update regular cache",
"update code cell")
function findNotebooks(dirname) {
return listInProject(dirname, '{,*,*/,*/*/*,*/*/*/*}*.ipynb')
}
function cacheAll() {
// include the path to this notebook first
var notebooks = [
path.resolve(__dirname, './cache.ipynb')
].concat(findNotebooks(path.resolve(__dirname, '../')))
// all ids are the ones in the cache plus missing notebooks
var allIds = notebooks
.reduce((arr, n) => arr
.concat([n + '[0]'])
.concat(cellCache
.filter(c => c[1].substr(0, n.length) === n)
.map(c => c[1])), [])
// update by loading every notebook
var updates = notebooks.reduce((arr, n) => {
return arr.concat(cacheCells(n)
.map(({mtime, id, filename, from, to, questions}) => [
mtime,
path.join(path.dirname(filename), id),
{
from,
to,
questions,
}
]))
}, [])
// save the cache in this notebook
updateCache(updates, cellCache, updates.map(u => u[1]))
saveCache()
}
function saveCache() {
var code = `
// cell cache automatically replaced
var cellCache = ${JSON.stringify(cellCache, null, 4)}
module.exports = {
cellCache
}
`
var cacheCell = importer.interpret('cell cache')
updateCode(cacheCell, code)
}
module.exports = {
findNotebooks,
cacheAll,
saveCache
}