Skip to content

Commit

Permalink
Merge pull request #9 from wide/cached-global-variable
Browse files Browse the repository at this point in the history
Cached global variable
  • Loading branch information
jdacosta authored Feb 1, 2021
2 parents 7084c10 + 4c45ad9 commit 689d660
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wide/forge",
"version": "2.2.0",
"version": "2.2.1",
"description": "Zero-based configuration builder for frontend integration projects.",
"license": "MIT",
"author": "Aymeric Assier (https://github.com/myeti)",
Expand Down
1 change: 1 addition & 0 deletions src/tasks/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function(opts = {}) {
open: opts.open,
ghostMode: false,
files: `${config.output}/**/*`,
ignore: '**/*.map',
server: {
baseDir: config.output,
directory: false
Expand Down
21 changes: 16 additions & 5 deletions src/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { isPlainObject } from 'lodash'
import { execSync } from 'child_process'
import BASE_CONFIG from './config'

/**
* Global Cache
*/
const globalCache = []


/**
* Enable await/async fron FS functions
Expand Down Expand Up @@ -173,9 +178,14 @@ export async function read(filename) {
*/
export async function write(filename, content) {
await fse.ensureFile(filename)
const cached = await newContentIsIdentical(filename, content)
const cached = env.prod
? false
: newContentIsIdentical(filename, content)

if(!cached) {
if(!Buffer.isBuffer(content) && !env.prod) {
globalCache[filename] = content
}
await writeFile(filename, content)
}

Expand Down Expand Up @@ -222,12 +232,13 @@ export function execHook(hook, ...params) {
* @param {String} newContent
* @return {Boolean}
*/
async function newContentIsIdentical(filename, newContent) {
try {
function newContentIsIdentical(filename, newContent) {
if(fs.existsSync(filename) && typeof globalCache[filename] !== 'undefined') {
return !Buffer.isBuffer(newContent)
? await read(filename) === newContent
? globalCache[filename] === newContent
: false
} catch (err) {
}
else {
return false
}
}

0 comments on commit 689d660

Please sign in to comment.