-
Notifications
You must be signed in to change notification settings - Fork 28
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
auser
wants to merge
6
commits into
FWeinb:master
Choose a base branch
from
auser:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9886cd7
Added onUpdateCallback
auser 8a06112
Added callback for updates (additional optional rendering for post-up…
auser f0c2e42
Added README note about onUpdateCallback
auser 4ddb58a
Fixed frontmatter from not being updated
auser 6b5984d
Updates to support permalinkMapping
auser 86bf408
Load originalFilename
auser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
{ | ||
"stage": 0 | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { | |
relative as relativePath, | ||
resolve as resolvePath, | ||
isAbsolute as isAbsolutePath, | ||
normalize as normalizePath | ||
} from "path" | ||
|
||
import async from "async" | ||
|
@@ -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) { | ||
|
@@ -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)) | ||
// } | ||
// }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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. | ||
|
@@ -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) { | ||
|
@@ -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] | ||
|
@@ -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) => { | ||
|
@@ -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() | ||
}) | ||
}, | ||
|
@@ -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}`)) | ||
|
@@ -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) | ||
}) | ||
} | ||
|
||
|
@@ -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) { | ||
|
||
|
@@ -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 => { | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?