2018年8月17日 星期五

[NodeJS] Import Module Without Executing the Script

When working on my nodeJS project, I encounter some kind of difficulties on the following scenario

I wrote an expressJS application, which in turn being allocated to be unit tested, the problem is I want to pass the expressJS application to my test program, so I figure out I can export the "app" from the "index.js", so I add

export default app


in my index.js, so finally I can export the app variable and import it to my unit test. But the problem is when I import the module, I also execute the code associated with module which is not what I want, is there anything like Python's if __name__ == "__main__"?

I just want to import the module without executing it, so I end up with the following strategies

I wrap all the actions in a function and return my app at the end

export function initApp() {
   app.use...
   app.use...
   return app;
}

Then I add this important snippet

if (require.main === module) {
    initApp();
}

This is important ! It tells nodeJS only if this file being run by node can the initApp being executed, so in my unit test case, I could just safely import this module without unintentionally execute the initApp function

2018年8月10日 星期五

[Ubuntu] Fix Microsoft Mouse Scroll Distance

Background
When using Microsoft mouse in Ubuntu 16, I found the scrolling distance is too large to adapt to, the following helps to fix it to normal

Solution
1. Run xinput list
2. Then run xinput list-props x | grep 'Scrolling Distance' where x is the id of your input device
3. Run xinput set-prop x 'Evdev Scrolling Distance' 10, 10, 10 to save the desired value
4. Add the command to .profile file if you want to save the settings permenantly

2018年1月17日 星期三

[Webpack] Limitation of number of files watch

When watching files with webpack, please make sure there are default limits on the number of files it can watch, (i.e.: 8192), if you want to watch more, please follow the below instructions

The current watch limit can be seen through the command
$ cat /proc/sys/fs/inotify/max_user_watches
In my case it was 8192 which is the default value for linux X64 systems
To change it temporarily we need to run the following commands
$ sudo sysctl fs.inotify.max_user_watches=524288
$ sudo sysctl -p
For permanently setting it we should run run
$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
$ sudo sysctl -p