Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onUpdateCallback option to call after every update #34

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
{
"stage": 0
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove this? I guess it's not needed anymore but did you ran into an issue?

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ To get live reload working properly, you should add the following `<script>` in

Make sure to update the port number in the script above accordingly to the port specified.

### onUpdateCallback (default: `undefined`)

Allows you to define a function to get called _after_ metalsmith has reloaded all the files. If a function is passed, this will be called with the updated files and all options passed.

```javascript
{
onUpdateCallback: function (files, options) {
// Called on every rebuild
}
}
```

### log (default: `function(...args) { console.log(prefix, ...args)}`)

Function used to display the logs.
Expand Down
55 changes: 43 additions & 12 deletions src/index.es
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
relative as relativePath,
resolve as resolvePath,
isAbsolute as isAbsolutePath,
normalize as normalizePath
} from "path"

import async from "async"
Expand Down Expand Up @@ -48,6 +49,12 @@ function livereloadFiles(livereload, files, options) {
}
}

function runOnUpdateCallback(onUpdateCallback, files, options) {
if (onUpdateCallback) {
onUpdateCallback(files, options);
}
}

// metalsmith-collections fix: collections are mutable
// fuck mutability
function backupCollections(collections) {
Expand Down Expand Up @@ -75,8 +82,15 @@ function updateCollections(metalsmith, collections) {
}

// metalsmith-collections fix: helps to update fix collections
function saveFilenameInFilesData(files) {
function saveFilenameInFilesData(metalsmith, files, options) {
addFilenames(files)
// const relativeRoot = options.relativeRoot ? options.relativeRoot : metalsmith.source();
// Object.keys(files).forEach(filename => {
// if (!files[filename].filename) {
// console.log('svaing.... ', normalizePath(relativePath(relativeRoot, filename)))
// files[filename].filename = normalizePath(relativePath(relativeRoot, filename))
// }
// })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove thsi commented code?

}

// metalsmith-collections fix: remove items from collections that will be readded by the partial build
Expand All @@ -96,7 +110,7 @@ function removeFilesFromCollection(files, collections) {
})
}

function runAndUpdate(metalsmith, files, livereload, options, previousFilesMap) {
function runAndUpdate(metalsmith, files, livereload, onUpdateCallback, options, previousFilesMap) {
// metalsmith-collections fix: metalsmith-collections plugin add files to
// collection when run() is called which create problem since we use run()
// with only new files.
Expand All @@ -106,7 +120,7 @@ function runAndUpdate(metalsmith, files, livereload, options, previousFilesMap)
// (file already in the collections)
// we iterate on collections with reference to previous files data
// and skip old files that match the paths that will be updated
saveFilenameInFilesData(files)
saveFilenameInFilesData(metalsmith, files, options)
const collections = metalsmith.metadata().collections
const collectionsBackup = backupCollections(collections)
if (collections) {
Expand All @@ -133,7 +147,6 @@ function runAndUpdate(metalsmith, files, livereload, options, previousFilesMap)
return
}


// metalsmith-collections fix: update ref for future tests
Object.keys(freshFiles).forEach(path => {
previousFilesMap[path] = freshFiles[path]
Expand All @@ -143,12 +156,14 @@ function runAndUpdate(metalsmith, files, livereload, options, previousFilesMap)
if(writeErr) {throw writeErr}

livereloadFiles(livereload, freshFiles, options)
runOnUpdateCallback(onUpdateCallback, freshFiles, options)
})
})
}

function buildFiles(metalsmith, paths, livereload, options, previousFilesMap) {
function buildFiles(metalsmith, paths, livereload, onUpdateCallback, options, previousFilesMap) {
const files = {}
const metadata = metalsmith.metadata();
async.each(
paths,
(path, cb) => {
Expand All @@ -158,7 +173,18 @@ function buildFiles(metalsmith, paths, livereload, options, previousFilesMap) {
return
}

files[path] = file
if (metadata && metadata.permalinkMapping) {
const originalFilename = metadata.permalinkMapping[path];
if (originalFilename && previousFilesMap[originalFilename]) {
file = Object.assign({}, previousFilesMap[originalFilename], file);
file[originalFilename] = file
} else {
files[path] = file
}
} else {
files[path] = file
}

cb()
})
},
Expand All @@ -170,12 +196,12 @@ function buildFiles(metalsmith, paths, livereload, options, previousFilesMap) {

const nbOfFiles = Object.keys(files).length
options.log(color.gray(`- Updating ${nbOfFiles} file${nbOfFiles > 1 ? "s" : ""}...`))
runAndUpdate(metalsmith, files, livereload, options, previousFilesMap)
runAndUpdate(metalsmith, files, livereload, onUpdateCallback, options, previousFilesMap)
}
)
}

function buildPattern(metalsmith, patterns, livereload, options, previousFilesMap) {
function buildPattern(metalsmith, patterns, livereload, onUpdateCallback, options, previousFilesMap) {
unyield(metalsmith.read())((err, files) => {
if (err) {
options.log(color.red(`${nok} ${err}`))
Expand All @@ -186,7 +212,7 @@ function buildPattern(metalsmith, patterns, livereload, options, previousFilesMa
multimatch(Object.keys(files), patterns).forEach(path => filesToUpdate[path] = files[path])
const nbOfFiles = Object.keys(filesToUpdate).length
options.log(color.gray(`- Updating ${nbOfFiles} file${nbOfFiles > 1 ? "s" : ""}...`))
runAndUpdate(metalsmith, filesToUpdate, livereload, options, previousFilesMap)
runAndUpdate(metalsmith, filesToUpdate, livereload, onUpdateCallback, options, previousFilesMap)
})
}

Expand All @@ -212,6 +238,11 @@ export default function(options) {
livereload = livereloadServer(options.livereload, options.log)
}

let onUpdateCallback
if (options.onUpdateCallback && typeof options.onUpdateCallback === 'function') {
onUpdateCallback = options.onUpdateCallback.bind(this);
}

let watched = false
const plugin = function metalsmithWatch(files, metalsmith, cb) {

Expand All @@ -223,7 +254,7 @@ export default function(options) {
watched = true

// metalsmith-collections fix: keep filename as metadata
saveFilenameInFilesData(files)
saveFilenameInFilesData(metalsmith, files, options)

const patterns = {}
Object.keys(options.paths).map(pattern => {
Expand Down Expand Up @@ -288,7 +319,7 @@ export default function(options) {
return relativePath(metalsmith.source(), filepath)
})
if (filesToUpdate.length) {
buildFiles(metalsmith, filesToUpdate, livereload, options, previousFilesMap)
buildFiles(metalsmith, filesToUpdate, livereload, onUpdateCallback, options, previousFilesMap)
}

const patternsToUpdatePattern = Object.keys(patterns)
Expand All @@ -297,7 +328,7 @@ export default function(options) {
.map(pattern => patterns[pattern])

if (patternsToUpdatePattern.length) {
buildPattern(metalsmith, patternsToUpdatePattern, livereload, options, previousFilesMap)
buildPattern(metalsmith, patternsToUpdatePattern, livereload, onUpdateCallback, options, previousFilesMap)
}
// console.log(pathsToUpdate, filesToUpdate, patternsToUpdatePattern)

Expand Down