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

Load towers dynamically #169

Merged
merged 6 commits into from
May 31, 2018
Merged

Load towers dynamically #169

merged 6 commits into from
May 31, 2018

Conversation

olistic
Copy link
Owner

@olistic olistic commented May 29, 2018

This adds support for community towers, as well as official towers that don't ship with the game. WarriorJS CLI searches for towers (@warriorjs/tower-* and warriorjs-tower-*) in the nearest parent node_modules directory. This should work for both global and local installations of the CLI.

Closes #100
Fixes #168

@codecov-io
Copy link

codecov-io commented May 29, 2018

Codecov Report

Merging #169 into master will increase coverage by 0.15%.
The diff coverage is 89.58%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #169      +/-   ##
==========================================
+ Coverage   92.19%   92.35%   +0.15%     
==========================================
  Files          83       85       +2     
  Lines        1051     1072      +21     
  Branches      177      179       +2     
==========================================
+ Hits          969      990      +21     
  Misses         67       67              
  Partials       15       15
Impacted Files Coverage Δ
packages/warriorjs-cli/src/loadTowers.js 100% <100%> (ø)
packages/warriorjs-cli/src/Tower.js 100% <100%> (ø) ⬆️
packages/warriorjs-cli/src/Profile.js 96.36% <100%> (+0.06%) ⬆️
packages/warriorjs-cli/src/utils/getLevelConfig.js 100% <100%> (ø) ⬆️
packages/warriorjs-cli/src/utils/getTowerId.js 100% <100%> (ø)
packages/warriorjs-cli/src/Game.js 52.63% <68.75%> (+1.28%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fc038df...9a37067. Read the comment docs.

@olistic olistic force-pushed the load-towers branch 2 times, most recently from d59b1b0 to 1b746ac Compare May 29, 2018 16:43
);
const towerPaths = towerPackageJsonPaths.map(path.dirname);
const towerRequirePaths = towerPaths.map(towerPath =>
resolve.sync(towerPath, { basedir: towerSearchDir }),
Copy link
Owner Author

Choose a reason for hiding this comment

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

Is resolve needed here?

Copy link

@kachkaev kachkaev May 30, 2018

Choose a reason for hiding this comment

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

Hi @olistic 👋

resolve.sync maps paths to directories into paths to the actual js files, which you will need later when calling require. In most cases this mapping simply adds index.js at the path's the end, however, if you dir's package.js says "main": "something.js", the resolved path becomes dir/something.js. E.g.:

https://github.com/prettier/prettier/tree/master/tests_integration/plugins/bespoke/node_modules/%40company/prettier-plugin-bespoke

https://github.com/prettier/prettier/blob/19cdd5fde2dda40b8667225dbab8c400348d6c69/tests_integration/__tests__/plugin-resolution.js#L155

Copy link
Owner Author

Choose a reason for hiding this comment

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

@kachkaev My understanding is that require() has the same resolution algorithm built-in, so I guess I still want to call resolve to be able to pass the basedir and make sure the module I'm loading is the one I found previously with globby (and not a different one in the default node_modules recursive path)?

resolve.sync(towerPath, { basedir: towerSearchDir }),
);
const towers = towerRequirePaths.map(
towerRequirePath => require(towerRequirePath), // eslint-disable-line global-require, import/no-dynamic-require
Copy link
Owner Author

Choose a reason for hiding this comment

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

Is the require() call safe? I saw this same thing being done like this in other places:

eval('require')(towerRequirePath)

Copy link

@kachkaev kachkaev May 30, 2018

Choose a reason for hiding this comment

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

eval('require') is used in Prettier because that library is built with Rollup. eval prevents towerRequirePath from an attempt to be glued together with the current module. If you don't have Rollup in your workflow, feel free to just require(towerRequirePath).

Hope this helps! 👋

Copy link
Owner Author

Choose a reason for hiding this comment

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

Ah! Makes perfect sense, thanks!

@olistic olistic force-pushed the load-towers branch 9 times, most recently from 4b71204 to dce4a2d Compare May 31, 2018 00:21
Instead of tower name.

This change is needed to prevent conflicts when loading profiles.
Tower names can be duplicated, whereas identifier uniqueness is
enforced by npm (because identifiers are derived from the package
name).
@olistic olistic merged commit 83cecf0 into master May 31, 2018
@olistic olistic deleted the load-towers branch May 31, 2018 01:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants