Skip to content

Commit

Permalink
Merge pull request #7 from wide/cached-files
Browse files Browse the repository at this point in the history
Cached files
  • Loading branch information
jdacosta authored Jan 27, 2021
2 parents c961afc + 1777e86 commit 7084c10
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 34 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wide/forge",
"version": "2.1.7",
"version": "2.2.0",
"description": "Zero-based configuration builder for frontend integration projects.",
"license": "MIT",
"author": "Aymeric Assier (https://github.com/myeti)",
Expand All @@ -25,9 +25,9 @@
},
"dependencies": {
"autoprefixer": "^9.7.6",
"browser-sync": "^2.26.7",
"browser-sync": "^2.26.14",
"chalk": "^3.0.0",
"chokidar": "^3.3.0",
"chokidar": "^3.5.1",
"columnify": "^1.5.4",
"cpy": "^7.3.0",
"dotenv": "^8.2.0",
Expand All @@ -37,8 +37,8 @@
"js-beautify": "^1.10.3",
"lodash": "^4.17.15",
"parcel-bundler": "^1.12.4",
"rimraf": "^3.0.0",
"sass": "^1.29.0",
"rimraf": "^3.0.2",
"sass": "^1.32.5",
"svg-sprite": "^1.5.0",
"twig": "^1.15.4",
"yargs": "^14.2.0"
Expand All @@ -47,6 +47,6 @@
"babel-cli": "6.26.0",
"babel-preset-es2015": "6.24.1",
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
"rimraf": "^3.0.0"
"rimraf": "^3.0.2"
}
}
6 changes: 3 additions & 3 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ try {

// hello world - only for builtin commands
if(command in nativeCommands) {
console.log(chalk`{blue.bold ${pkg.name}} v${pkg.version} {blueBright.bold (${process.env.NODE_ENV})}`)
console.log(chalk`{blue ${pkg.name}} v${pkg.version} {yellow (${process.env.NODE_ENV})}`)
}

// debug mode
if(env.debug) {
console.log(chalk`{gray.bold [debug]} config {cyan.bold ${cwd}}`)
console.log(chalk`{gray [debug]} config {cyan ${cwd}}`)
console.log(config)
}

// execute command
commands[command](argv, config)
}
catch(err) {
console.error(chalk`{red.bold ${err}}`)
console.error(chalk`{red ${err}}`)
process.exit(1)
}
34 changes: 18 additions & 16 deletions src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ export default async function(targets = []) {

// ignore target
if(targetConfig === false) {
console.log(chalk` {yellow.bold ~ skip} {magenta.bold ${target}} {yellow.bold (falsy config)}`)
console.log(chalk` {yellow ~ skip} {magenta ${target}} {yellow (falsy config)}`)
continue;
}

// bad config
if(!targetConfig || !targetConfig.entries) {
console.log(chalk` {red.bold ✕ skip} {magenta.bold ${target}} {red.bold (missing config)}`)
console.log(chalk` {red ✕ skip} {magenta ${target}} {red (missing config)}`)
continue;
}

Expand All @@ -48,14 +48,14 @@ export default async function(targets = []) {
// run before hook
if(targetConfig.hooks && targetConfig.hooks.before) {
try {
console.log(chalk` {gray.bold +} run *before* hook`)
console.log(chalk` {gray +} run *before* hook`)
await execHook(targetConfig.hooks.before, targetConfig, compiled)
}
catch(err) {
process.exitCode = 1
console.log(chalk` {red.bold ✕ *before* hook failed, try --debug for more info}`)
console.log(chalk` {red ✕ *before* hook failed, try --debug for more info}`)
if(env.debug) {
console.log(chalk` {red.bold${err}}`)
console.log(chalk` {red ✕ ${err}}`)
}
}
}
Expand All @@ -74,7 +74,7 @@ export default async function(targets = []) {

// debug context
if(env.debug) {
console.log(chalk` {gray.bold [debug]} {magenta.bold ${target}} input context`)
console.log(chalk` {gray [debug]} {magenta ${target}} input context`)
console.log(ctx)
}

Expand All @@ -85,29 +85,29 @@ export default async function(targets = []) {
done += generated.length
}
catch(err) {
console.error(chalk`{red.bold Error!}`, err)
console.error(chalk`{red Error!}`, err)
process.exitCode = 1
}
}

// run after hook
if(targetConfig.hooks && targetConfig.hooks.after) {
try {
console.log(chalk` {gray.bold +} run *after* hook`)
console.log(chalk` {gray +} run *after* hook`)
await execHook(targetConfig.hooks.after, targetConfig, compiled)
}
catch(err) {
process.exitCode = 1
console.log(chalk` {red.bold ✕ *after* hook failed, try --debug for more info}`)
console.log(chalk` {red ✕ *after* hook failed, try --debug for more info}`)
if(env.debug) {
console.log(chalk` {red.bold${err}}`)
console.log(chalk` {red ✕ ${err}}`)
}
}
}

// warn if no file compiled
if(!done) {
console.log(chalk` {yellow.bold ~ skip} {magenta.bold ${target}} {yellow.bold (no entries)}`)
console.log(chalk` {yellow ~ skip} {magenta ${target}} {yellow (no entries)}`)
continue
}
}
Expand All @@ -129,19 +129,21 @@ function logResults(compiled) {
const dirname = path.dirname(path.relative(cwd, o.filename))
const basename = path.basename(o.filename)
return {
status: chalk`{green.bold ✓}`,
output: chalk`${dirname}${path.sep}{cyan.bold ${basename}}`,
status: chalk`{green ✓}`,
output: chalk`${dirname}${path.sep}{cyan ${basename}}`,
size: (o.size > MO)
? chalk`{blueBright.bold ${(o.size / MO).toFixed(2)} mo}`
: chalk`{blueBright.bold ${(o.size / KO).toFixed(2)} ko}`
? chalk`{blue ${(o.size / MO).toFixed(2)} mo}`
: chalk`{blue ${(o.size / KO).toFixed(2)} ko}`,
cached: o.cached ? chalk`{yellow (cached)}` : ''
}
})

console.log(columnify(results, {
showHeaders: false,
config: {
status: { align: 'right', minWidth: 3 },
size: { align: 'right', minWidth: 12 }
size: { align: 'right', minWidth: 12 },
cached: { align: 'right', minWidth: 10 }
}
}))
}
2 changes: 1 addition & 1 deletion src/tasks/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import chalk from 'chalk'
export default async function(...targets) {
const _targets = targets.length ? targets : config.targets // no targets = all targets
if(_targets.length) {
console.log(chalk`{blue.bold #} compile {magenta.bold ${_targets.join(', ')}}`)
console.log(chalk`{blue #} compile {magenta ${_targets.join(', ')}}`)
await compiler(_targets)
}
}
4 changes: 2 additions & 2 deletions src/tasks/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import cpy from 'cpy'
* Copy assets
*/
export default async function() {
console.log(chalk`{blue.bold #} copy static assets`)
console.log(chalk`{blue #} copy static assets`)
const entries = [...config.copy.entries, ...config.copy.exclude.map(ex => `!${ex}`)]
const output = path.resolve(absOutput, config.copy.output)
const done = await cpy(entries, output, {
cwd: config.input,
overwrite: true,
parents: true
})
console.log(chalk` {green.bold ✓} ${done.length} assets copied`)
console.log(chalk` {green ✓} ${done.length} assets copied`)
}
4 changes: 2 additions & 2 deletions src/tasks/nuke.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import chalk from 'chalk'
* Nuke dist folder
*/
export default async function() {
console.log(chalk`{blue.bold #} nuke {cyan.bold ${config.output}}`)
console.log(chalk`{blue #} nuke {cyan ${config.output}}`)
rimraf.sync(config.output)
console.log(chalk` {green.bold ✓} done`)
console.log(chalk` {green ✓} done`)
}
2 changes: 1 addition & 1 deletion src/tasks/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const browser = BrowserSync.create()
*/
export default function(opts = {}) {

console.log(chalk`{blue.bold #} serve {cyan.bold ${config.output}}`)
console.log(chalk`{blue #} serve {cyan ${config.output}}`)
const rc = loadRC('bsync')

return browser.init({
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function watcher(globpath, fn) {
export default async function(...targets) {
const _targets = targets.length ? targets : config.targets // no targets = all targets
if(_targets.length) {
console.log(chalk`{blue.bold #} watch {magenta.bold ${_targets.join(', ')}}`)
console.log(chalk`{blue #} watch {magenta ${_targets.join(', ')}}`)
for(const target of _targets) {
const _observe = Array.isArray(config[target].observe) ? config[target].observe : [config[target].observe]
for(let file of _observe) {
Expand Down
27 changes: 25 additions & 2 deletions src/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,16 @@ export async function read(filename) {
*/
export async function write(filename, content) {
await fse.ensureFile(filename)
await writeFile(filename, content)
const cached = await newContentIsIdentical(filename, content)

if(!cached) {
await writeFile(filename, content)
}

return {
filename,
size: fs.statSync(filename).size
size: fs.statSync(filename).size,
cached
}
}

Expand Down Expand Up @@ -207,4 +213,21 @@ export function merge(a, b) {
export function execHook(hook, ...params) {
if(typeof hook === 'function') return hook(...params)
if(typeof hook === 'string') return execSync(hook, env.debug ? { stdio: 'inherit' } : {})
}


/**
* Check if the new content is identical to the old one if the file exists
* @param {String} filename
* @param {String} newContent
* @return {Boolean}
*/
async function newContentIsIdentical(filename, newContent) {
try {
return !Buffer.isBuffer(newContent)
? await read(filename) === newContent
: false
} catch (err) {
return false
}
}

0 comments on commit 7084c10

Please sign in to comment.