From 6eb5cfc7185550a595ab11c73a9b3811c2785e4d Mon Sep 17 00:00:00 2001 From: Martin Hicks Date: Thu, 27 May 2021 19:43:35 +0100 Subject: [PATCH 1/2] Use Deno cache to hydrate the deno dependencies, so they're pre-cached on invocation --- src/actions/install-update.js | 13 +++++++++++-- src/index.js | 25 +++++++++++++++++++++++++ src/lib/index.js | 2 +- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/actions/install-update.js b/src/actions/install-update.js index 5eeae25..bb9e3d8 100644 --- a/src/actions/install-update.js +++ b/src/actions/install-update.js @@ -1,4 +1,4 @@ -let { dirname, join, sep } = require('path') +let { dirname, join, sep, basename } = require('path') let { existsSync } = require('fs') let child = require('child_process') let series = require('run-series') @@ -34,6 +34,7 @@ module.exports = function hydrator (params, callback) { let isJs = file.endsWith('package.json') let isPy = file.endsWith('requirements.txt') let isRb = file.endsWith('Gemfile') + let isDeno = file.endsWith('deps.ts') || file.endsWith('index.js') || file.endsWith('index.ts') || file.endsWith('index.tsx') || file.endsWith('mod.js') || file.endsWith('mod.ts') || file.endsWith('mod.tsx') series([ function clear (callback) { @@ -43,7 +44,9 @@ module.exports = function hydrator (params, callback) { if (isJs) dir = join(cwd, 'node_modules') if (isPy) dir = join(cwd, 'vendor') if (isRb) dir = join(cwd, 'vendor', 'bundle') - rm(dir, callback) + if (isDeno) callback() + else + rm(dir, callback) } else callback() }, @@ -103,6 +106,12 @@ module.exports = function hydrator (params, callback) { exec(`bundle update`, options, callback) } + // cache deno deps.ts + else if (isDeno) { + // should --reload be added? That would force re-caching everytime, so maybe not? + exec(`DENO_DIR=./vendor/.deno_cache deno cache --unstable ./${basename(file)}`, options, callback) + } + else { callback() } diff --git a/src/index.js b/src/index.js index 8bea66f..2027c6a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ let { sync: glob } = require('glob') let series = require('run-series') let { dirname, join } = require('path') +let { existsSync: exists } = require('fs') let stripAnsi = require('strip-ansi') let { pathToUnix, updater } = require('@architect/utils') let inventory = require('@architect/inventory') @@ -69,6 +70,30 @@ function hydrator (inventory, installing, params, callback) { if (viewsDir && file.includes(viewsDir)) return false return true }) + + // Add deno cacheable files if they exist and we're hyrdrating a Deno runtime + let lambda = inv.lambdasBySrcDir[basepath] + if (lambda !== undefined && lambda.config !== undefined) { + let isDeno = lambda.config.runtime === 'deno' + if (isDeno) { + let denoCacheableFiles = [ + 'index.js', + 'mod.js', + 'index.ts', + 'mod.ts', + 'index.tsx', + 'mod.tsx', + 'deps.ts' + ] + denoCacheableFiles.map(denoFile => { + let file = join(basepath, denoFile) + if (exists(file)) { + files.push(file) + } + }) + } + } + // Get shared + views (or skip if hydrating a single isolated function, e.g. sandbox startup) if (hydrateShared) { let sharedManifest = (sharedDir && glob(pattern(sharedDir)).filter(ignoreDeps)) || [] diff --git a/src/lib/index.js b/src/lib/index.js index 7d217a7..4c57391 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -1,5 +1,5 @@ let { pathToUnix } = require('@architect/utils') -let isDep = file => file.includes('node_modules') || file.includes('vendor/bundle') +let isDep = file => file.includes('node_modules') || file.includes('vendor/bundle') || file.includes('.deno_cache') let ignoreDeps = file => !isDep(pathToUnix(file)) // Relativize by stripping leading relative path + `.`, `/`, `./`, `\`, `.\` From 66bebb6f385fa7deb1037c77c956eaf720a1a949 Mon Sep 17 00:00:00 2001 From: Martin Hicks Date: Mon, 31 May 2021 17:53:43 +0100 Subject: [PATCH 2/2] various improvements linked to CR --- src/actions/install-update.js | 13 ++++++------ src/index.js | 38 ++++++++++------------------------- src/lib/index.js | 18 +++++++++++++++++ 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/actions/install-update.js b/src/actions/install-update.js index bb9e3d8..6e632d5 100644 --- a/src/actions/install-update.js +++ b/src/actions/install-update.js @@ -4,6 +4,7 @@ let child = require('child_process') let series = require('run-series') let rm = require('rimraf') let print = require('../_printer') +let { denoCacheable } = require('../lib') module.exports = function hydrator (params, callback) { let { file, action, update, env, shell, timeout, installing, verbose } = params @@ -34,7 +35,7 @@ module.exports = function hydrator (params, callback) { let isJs = file.endsWith('package.json') let isPy = file.endsWith('requirements.txt') let isRb = file.endsWith('Gemfile') - let isDeno = file.endsWith('deps.ts') || file.endsWith('index.js') || file.endsWith('index.ts') || file.endsWith('index.tsx') || file.endsWith('mod.js') || file.endsWith('mod.ts') || file.endsWith('mod.tsx') + let isDeno = denoCacheable.some(val => basename(file) === val) series([ function clear (callback) { @@ -44,9 +45,8 @@ module.exports = function hydrator (params, callback) { if (isJs) dir = join(cwd, 'node_modules') if (isPy) dir = join(cwd, 'vendor') if (isRb) dir = join(cwd, 'vendor', 'bundle') - if (isDeno) callback() - else - rm(dir, callback) + // if (isDeno) dir = join(cwd, 'vendor', '.deno_cache') + rm(dir, callback) } else callback() }, @@ -106,10 +106,9 @@ module.exports = function hydrator (params, callback) { exec(`bundle update`, options, callback) } - // cache deno deps.ts + // cache deno deps else if (isDeno) { - // should --reload be added? That would force re-caching everytime, so maybe not? - exec(`DENO_DIR=./vendor/.deno_cache deno cache --unstable ./${basename(file)}`, options, callback) + exec(`DENO_DIR=./vendor/.deno_cache deno cache --unstable --reload ./${basename(file)}`, options, callback) } else { diff --git a/src/index.js b/src/index.js index 2027c6a..c075bf8 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,10 @@ let { sync: glob } = require('glob') let series = require('run-series') -let { dirname, join } = require('path') -let { existsSync: exists } = require('fs') +let { dirname, join, basename, resolve } = require('path') let stripAnsi = require('strip-ansi') let { pathToUnix, updater } = require('@architect/utils') let inventory = require('@architect/inventory') -let { isDep, ignoreDeps, stripCwd } = require('./lib') +let { isDep, ignoreDeps, stripCwd, denoCacheable } = require('./lib') let shared = require('./shared') let actions = require('./actions') let cleanup = require('./_cleanup') @@ -55,12 +54,12 @@ function hydrator (inventory, installing, params, callback) { // From here on out normalize all file comparisons to Unix paths let sharedDir = inv.shared && inv.shared.src && pathToUnix(stripCwd(inv.shared.src)) let viewsDir = inv.views && inv.views.src && pathToUnix(stripCwd(inv.views.src)) - + // console.log(inv) /** * Find our dependency manifests */ // eslint-disable-next-line - let pattern = p => `${p}/**/@(package\.json|requirements\.txt|Gemfile)` + let pattern = p => `${p}/**/@(package\.json|requirements\.txt|Gemfile|${denoCacheable.join('|')})` let dir = basepath || '.' // Get everything except shared let files = glob(pattern(dir)).filter(file => { @@ -68,31 +67,16 @@ function hydrator (inventory, installing, params, callback) { if (isDep(file)) return false if (sharedDir && file.includes(sharedDir)) return false if (viewsDir && file.includes(viewsDir)) return false + if (denoCacheable.some(val => val === basename(file))) { + // basepath isn't always set (maybe just within the multi-auto install integration tests?) + let _basepath = basepath || resolve(file).replace(basename(file), '') + if (inv.lambdasBySrcDir[_basepath] === undefined) return false + if (inv.lambdasBySrcDir[_basepath].config.runtime !== 'deno') return false + } return true }) - // Add deno cacheable files if they exist and we're hyrdrating a Deno runtime - let lambda = inv.lambdasBySrcDir[basepath] - if (lambda !== undefined && lambda.config !== undefined) { - let isDeno = lambda.config.runtime === 'deno' - if (isDeno) { - let denoCacheableFiles = [ - 'index.js', - 'mod.js', - 'index.ts', - 'mod.ts', - 'index.tsx', - 'mod.tsx', - 'deps.ts' - ] - denoCacheableFiles.map(denoFile => { - let file = join(basepath, denoFile) - if (exists(file)) { - files.push(file) - } - }) - } - } + // Get shared + views (or skip if hydrating a single isolated function, e.g. sandbox startup) if (hydrateShared) { diff --git a/src/lib/index.js b/src/lib/index.js index 4c57391..2667c50 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -5,8 +5,26 @@ let ignoreDeps = file => !isDep(pathToUnix(file)) // Relativize by stripping leading relative path + `.`, `/`, `./`, `\`, `.\` let stripCwd = f => f.replace(process.cwd(), '').replace(/^\.?\/?\\?/, '') +let denoCacheable = [ + 'index.js', + 'mod.js', + 'index.ts', + 'mod.ts', + 'index.tsx', + 'mod.tsx', + 'deps.ts' +] + +let denoIgnore = [ + 'package.json', + 'requirements.tex', + 'Gemfile' +] + module.exports = { isDep, ignoreDeps, stripCwd, + denoCacheable, + denoIgnore }