Skip to content

fix: switch commands.json -> commands.js#668

Merged
freitagbr merged 1 commit intomasterfrom
refactor-commands-list
Feb 28, 2017
Merged

fix: switch commands.json -> commands.js#668
freitagbr merged 1 commit intomasterfrom
refactor-commands-list

Conversation

@nfischer
Copy link
Copy Markdown
Member

@nfischer nfischer commented Feb 26, 2017

WebPack has issues with importing JSON directly and using JavaScript
methods on it. For this reason, using the .forEach() method on the
imported JSON array caused a method-not-found error.

Instead, we can make this a real JavaScript array by keeping it in a
JavaScript file and exporting it as a field.

Fixes #667

@nfischer nfischer added the fix Bug/defect, or a fix for such a problem label Feb 26, 2017
@nfischer nfischer requested a review from freitagbr February 26, 2017 08:45
@nfischer nfischer force-pushed the refactor-commands-list branch from e7a7ec0 to b615dba Compare February 26, 2017 09:15
commands.js Outdated
@@ -0,0 +1,29 @@
exports.list = [
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

exports is initially assigned to module.exports. This is why reassigning exports doesn't export anything, because exports is now whatever you assigned it to, and not module.exports.

Anyways, I think it makes more sense to use module.exports here, because all we want to export is the array.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah, ok. That explanation makes sense. I'll try that instead.

// Insert the docs for all the registered commands
docs = docs.replace(/\/\/@commands\n/g, function () {
return require('../commands.json').map(function (commandName) {
return require('../commands').list.map(function (commandName) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If module.exports is used in commands.js, this will become require('../commands').map(...).

shell.js Outdated

// Load all default commands
require('./commands.json').forEach(function (command) {
require('./commands').list.forEach(function (command) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here.

WebPack has issues with importing JSON directly and using JavaScript
methods on it. For this reason, using the `.forEach()` method on the
imported JSON array caused a method-not-found error.

Instead, we can make this a real JavaScript array by keeping it in a
JavaScript file and exporting it as a field.

It should be noted that exporting it as `exports = [...]` does not work,
because Node won't actually interpret it as a JavaScript array (with
the required methods). So instead we can export it as
`exports.list = [...]` and access the `list` field to get the real
JavaScript array of command names.

Fixes #667
@nfischer nfischer force-pushed the refactor-commands-list branch from b615dba to b087b8f Compare February 28, 2017 05:59
@nfischer
Copy link
Copy Markdown
Member Author

@freitagbr PTAL

@freitagbr
Copy link
Copy Markdown
Contributor

LGTM, CI failures are unrelated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug/defect, or a fix for such a problem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants