Run example
npm run import -- "create cell cache"
create cell cache
var importer = require('../Core')
var {getExports} = importer.import("get exports from source")
var {
refreshCache, memorySafe, updateCache,
updateCode, exportsCache, cellCache
} = importer.import()
function makeCamel(str) {
return str
.replace(/[^a-z0-9 ]/ig, '')
.split(' ')
.map((w, i) => i > 0
? (w[0].toUpperCase() + w.substr(1))
: w)
.join('')
}
function createCellCache(search, cache, cacheCell, callback) {
var camelCase = makeCamel(cacheCell.questions[0])
var allIds = cellCache
.filter(c => c.code.length < 100000)
.map(cell => cell.id)
if(!search) {
search = refreshCache(cache, allIds)
if(search) {
search = search.replace(/\.ipynb\[[0-9]+\]$/ig, '.ipynb')
}
}
var allCells = [].concat.apply([], importer.interpret(Array.isArray(search)
? search : [search]))
var allCellIds = allCells.map(c => c.id)
// get only first occurrence
allCells = allCells
.filter((c, i) => allCellIds.indexOf(c.id) == i)
.filter(c => c.code.length < 10000)
console.log(`caching ${search} - ${allCells.length} cells`)
return Promise
.all(allCells.map(cell => memorySafe(() => {
try {
console.log(cell.id)
return [Date.now(), cell.id, callback(cell.code)]
} catch (e) {
return [Date.now(), cell.id, e.message]
}
})))
.then(updates => {
updateCache(updates, cache, allIds)
var code = `
// ${cacheCell.questions[0]} automatically replaced
var ${camelCase} = ${JSON.stringify(cache, null, 4)}
module.exports = {
${camelCase}
}
`
updateCode(cacheCell, code)
})
}
function createExportCache(search) {
var cacheCell = importer.interpret('exports cache')
return createCellCache(search, exportsCache, cacheCell, getExports)
}
module.exports = {
createExportCache,
createCellCache,
}