Monday, July 21, 2014

Recursively npm uninstall all modules

How to recursively do a npm uninstall on all modules? 

I came across a problem where we updated our version of nodejs, then the suggestion was to remove all of the installed node modules of your project after upgrading and before doing a "npm install". I looked at the web and it seemed it is not supported by npm yet (at the time of this writing). Doing manual npm uninstall of all of our modules is a very daunting task.

So I created a powershell script to recursively loop all of the folders inside the node_module and execute a "npm uninstall" passing the folder name as parameter.

Here is the code.
$arr = Get-ChildItem .\node_modules| Where-Object {$_.PSIsContainer} | Foreach-Object {$_.Name}; for($i = 0; $i -le $arr.count -1; $i++) { if($arr[$i] -ne ".bin") { Invoke-Command -Script {npm uninstall $arr[$i]}}}
It first get all of the folder names under node_modules and store it in the $arr variable, then invoke-command on all items in the array.

So just open your powershell terminal and paste this code in your project folder. Please note that I have a condition to test if the $arr[$i] is not equal to ".bin", this is because my node_modules has this folder name which is not a node package - this was generated by our grunt task.

 Hope this helps!

Monday, July 7, 2014

NodeJs: Call API through proxy

How to call an API inside NodeJs through a proxy?

I came across a problem where our box (vagrant box) was configured in a way that we could not call any site (website or API) directly, we have to use our proxy.

So the solution I found was to use a tunnel npm module.

Below is a mocha test I created to test calling an API through a proxy:
var tunnel = require('tunnel');
var https = require('https');
var url = require('url');

describe('call api through proxy', function () {
    it('should be able to access http', function (done) {
        var inputToken = 'YOUR_INPUT_TOKEN'; // this should be from config/redis
        var accessToken = 'ACCESS_TOKEN';

        var urlParsed = url.parse(process.env.https_proxy); // get the proxy

// declare the tunnel
        var tunnelingAgent = tunnel.httpsOverHttp({
            proxy: {
                host: urlParsed.hostname,
                port: urlParsed.port
            }
        });

// FB option
        var options = {
            host: "graph.facebook.com",
            port: 443,
            path: '/debug_token?input_token=' + inputToken + '&access_token=' + accessToken,
            agent: tunnelingAgent,

        };
        var reqGet = https.get(options, function (res) {
            var chunk = '';
            res.on('data', function (d) {
                console.info('GET result:\n');
                chunk += d;
                process.stdout.write(d);
            });
            res.on('end', function () {
                var fbResponse = JSON.parse(chunk);
                console.log("Got response: ", fbResponse);
                done();
            });
        });

        reqGet.end();
        reqGet.on('error', function (e) {
            console.error(e);
        });
    });
});

Hope this helps!


Tuesday, June 17, 2014

Sublime-text-3 on Ubuntu

Installing sublime-text-3 on Ubuntu via PPA


Do this following on your terminal window in Ubuntu:

sudo add-apt-repository ppa:webupd8team/sublime-text-3
sudo apt-get update
sudo apt-get install sublime-text-installer

And to run the sublime text on from your terminal, just type:

subl <folder or file name>

Hope this helps!

Monday, June 16, 2014

Deleting a UFW rule in Ubuntu?

How to delete a ufw rule in Ubuntu?


You could just do a "sudo ufw delete ####" and the role would be deleted. 

But how could you get the rule number?

You simply do a "sudo ufw status numbered", this would display you the role number on the left most of the list. Doing just the "sudo ufw status" won't display the rule number.

When you have the rule number already, you could call the "sudo ufw delete  ####" (specifying the rule number from the list)

Hope this helps!

Saturday, April 12, 2014

Make your Ubuntu Virtualbox increase it's screen resolution on Windows

You might find out that installing Ubuntu on a Virtualbox on Windows would give your Ubuntu an unpleasant screen resolution. To remedy this, follow these steps after installing Unbuntu on VirtualBox.

1. Make sure you login with an administrative rights.
2. Open the Terminal. (can be opened by pressing 'Ctrl' + 'Alt' + 'T')
3. Type this command "sudo apt-get install virtualbox-guest-dkms" then press Enter.
4. After installation is complete, restart your Ubuntu on the Virtualbox.

UPDATE (for Ubuntu 14.04 LTS):
On step 3, you would run this command "sudo apt-get install virtualbox-guest-x11" instead.


After restart, you would notice that your Ubuntu screen resolution would now adopt to your Host OS screen.

Thursday, February 4, 2010

C# - Change compilation debug value of the configuration file at runtime

Below is a C# code snippet to demonstrate on how to change the compilation-debug value at runtime.
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebPersonalExercises");
            
CompilationSection compSection = ((CompilationSection)(config.GetSection("system.web/compilation")));
            
compSection.Debug = true;

config.Save();

Please note that there might be times that this section is locked, so it is always helpful to check them first. Below is a revision C# code snippet to show that.
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebPersonalExercises");
CompilationSection compSection = ((CompilationSection)(config.GetSection("system.web/compilation")));
            
compSection.Debug = true;
            
if (!(compileSection.SectionInformation.IsLocked))
{
     config.Save();
}

Tuesday, February 2, 2010

C# - Manipulating the appSettings section of the configuration file at runtime

There might be a time wherein you have to manipulate the configuration at runtime, either adding or removing key-value pair. Below are some code snippets in C# to do this.

Adding a new appSetting value:
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebPersonalExercises");

AppSettingsSection appSection = ((AppSettingsSection)(config.GetSection("appSettings")));

KeyValueConfigurationCollection keyVal = appSection.Settings;

keyVal.Add("myNewValue", "value" + ++intCounter);

config.Save();

Removing the value:
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebPersonalExercises");

AppSettingsSection appSection = ((AppSettingsSection)(config.GetSection("appSettings")));

KeyValueConfigurationCollection keyVal = appSection.Settings;

keyVal.Remove("myNewValue");

config.Save();

Note that if you add the same appSetting key multiple times, it would be presented in the file like this (concatenated with a comma separator):
<appSettings>
  <add key="myNewValue" value="value1,value2,value3"/>
</appSettings>

But when you call the remove method, all of the appSetting key-value would be removed, regardless of how many times you add the same appSetting key of different values.