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

refactor: untangling lib/index.js part 1, monitor #443

Merged
merged 1 commit into from
Apr 9, 2019

Conversation

kyegupov
Copy link
Contributor

@kyegupov kyegupov commented Apr 9, 2019

  • Ready for review
  • Follows CONTRIBUTING rules
  • Reviewed by Snyk internal team

What does this PR do?

  • add a npm script to track circular imports
  • replace indirect references to lib/monitor.ts through snyk (which is lib/index.js) with direct imports
  • tidy-up monitor.ts module

see more in Background context

Where should the reviewer start?

lib/index.js

How should this be manually tested?

All the functionality of Snyk CLI should remain intact.

We have a highly problematic module, "lib/index.js" aka "snyk".
It's stateful, it's a proxy for other modules, it introduces a lot of import circles.

Background context

The import cycles we have at the moment are:

1) lib/index.js > lib/capture.js
2) lib/index.js > lib/monitor.js > lib/analytics.js
3) lib/analytics.js > lib/request/index.js > lib/request/request.js
4) lib/index.js > lib/monitor.js
5) lib/index.js > lib/snyk-test/index.js > lib/snyk-test/npm/index.js
6) lib/index.js > lib/snyk-test/index.js > lib/snyk-test/run-test.js
7) lib/index.js > lib/snyk-test/index.js > lib/snyk-test/run-test.js > lib/plugins/index.js > lib/plugins/npm/index.js
8) lib/index.js > lib/snyk-test/index.js > lib/snyk-test/run-test.js > lib/plugins/index.js > lib/plugins/yarn/index.js

This PR is a first baby step towards untangling index.js. It replaces "snyk.monitor" references with direct imports of "monitor", thus expanding the cycle 4 to:

lib/index.js > lib/capture.js > lib/monitor.js

Removing other cyclic imports will follow.

Copy link
Contributor

@miiila miiila left a comment

Choose a reason for hiding this comment

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

Thank you for this effort! I added some tiny suggestions, feel free to consider and ignore :-)

@@ -25,6 +25,7 @@
"build": "tsc",
"build-watch": "tsc -w",
"eslint": "eslint -c .eslintrc src",
"find-circular": "npm run build && madge --circular ./dist",
Copy link
Contributor

Choose a reason for hiding this comment

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

Registry uses  madge --extensions js,ts --circular ./src ./test, not sure if there's a benefit of doing that, just sharing :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I've copied this from the registry. It looks like madge is not 100% comfy with Typepscript, so the result was that the cycles were found twice - once in src, once in dist.

src/lib/index.js Outdated
// The properties represent other modules - so, it's some kind of "world object"
// that is used to indirectly import modules.
// The function modifies the object and returns it.
// This also indtroduces some circular imports.
Copy link
Contributor

Choose a reason for hiding this comment

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

typo, introduces (sorry for nitpicking)

@@ -1,4 +1,4 @@
module.exports = monitor;
export = monitor;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to keep this or shall we export function monitor on line 40?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm. While we're at it, we can patch all the clients, I guess...

@kyegupov kyegupov force-pushed the refactor/snyk-monitor-direct-import branch from 2968831 to b1b514f Compare April 9, 2019 15:54
@kyegupov kyegupov merged commit 0403375 into master Apr 9, 2019
@kyegupov kyegupov deleted the refactor/snyk-monitor-direct-import branch April 9, 2019 16:42
@snyksec
Copy link

snyksec commented Apr 10, 2019

🎉 This PR is included in version 1.150.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants