Init script example


#1

Continuing the discussion from Is it possible to change the system timezone in the terminal?:

@harutyun posted a snippet of his init script and I’m just wondering, how do I make that work for me. I think it’s a cool idea but I’m not all that familiar with the javascript. I’ve modified the username and git repo url but it doesn’t seem to do anything.

if (/-jms1989\.c9.io$/.test(services.c9.hostname)) {
    setTimeout(function() {
        services.fs.exists("~/dotfiles", function(exists) {
            if (exists == false) {
                services.tabManager.open({
                    editorType: "terminal",
                    active: true,
                    document: {
                        terminal: {
                            cwd: "~"
                        }
                    }
                }, function(e, tab) {
                    var repo = "git@bitbucket.org:<user>/<repo>.git";
                    tab.editor.write([
                        "echo -e '#!/bin/bash\\nssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \"\$@\"' > /tmp/git_ssh",
                        "chmod 755 /tmp/git_ssh",
                        "cd ~",
                        "GIT_SSH=/tmp/git_ssh git clone " + repo,
                        "cd dotfiles",
                        "bash ./init.sh",
                    ].join(" &&\\\n") + "\n");
                });
            }
        });
    }, 1000);
}

Any advise? Tips?


How to auto run a script when open or restart workspace?
How to set bash script on boot cloud9 instance
Please don't set email in .gitconfig on workspace creation
#2

Open init script from Cloud9 menu, add some debugger statements to the script above, and press ctrl-enter (cmd-enter on mac) to see what gets executed.

Note that if ~/dotfiles folder exists in your vm the script won’t do anything, also the script assumes <repo> is dotfiles and there is an init.sh in the repo which does all the actual work of setting timezone, modifying .bashrc etc.

If it works you should see a new terminal tab opened and the command typed into it.


#3

Right so, I’ve added a few console.log strings to kinda show me what’s happening since I don’t understand how the debugger is supposed to work (the docs wasn’t very helpful) and I have the browser console open to look for the output.

Ctrl+Enter doesn’t do anything for me and reloading the browser tab doesn’t have any affect but when I clicked the “Run Project” button, I get this interesting error. Is the init.js script supposed to be in ~/.c9?

Error: Cannot find module '/home/ubuntu/.c9/init.js'                                                                                                            
    at Function.Module._resolveFilename (module.js:338:15)                                                                                                      
    at Function.Module._load (module.js:289:25)                                                                                                                 
    at Module.runMain [as _onTimeout] (module.js:467:10)                                                                                                        
    at Timer.listOnTimeout (timers.js:92:15)

Also, am I supposed to change anything in if (/-jms1989\.c9.io$/.test(services.c9.hostname)) { ?


#4

A screenshot of what I have so far.


#5

i forgot that on new workspace c9.hostname is on c9users.io
testing location.pathname is more reliable

// You can access plugins via the 'services' global variable
/*global services, plugin*/
console.log("begin init script");
if (window.location.pathname.startsWith("/jms1989/")) {
    setTimeout(function() {
        var repoName = "c9.config"
        var repoUrl = "git@bitbucket.org:jms1989/" + repoName + ".git";
        services.fs.exists("~/" + repoName, function(exists) {
            if (exists == false) {
                console.log("dotfiles do not exist"),
                services.console.show();
                services.console.open({
                    editorType: "terminal",
                    active: true
                }, function(e, tab) {
                    console.log(repo);
                    tab.editor.write([
                        "echo -e '#!/bin/bash\\nssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \"\$@\"' > /tmp/git_ssh",
                        "chmod 755 /tmp/git_ssh",
                        "cd ~",
                        "GIT_SSH=/tmp/git_ssh git clone " + repo + " " + repoName,
                        "cd " + repoName,
                        "bash ./init.sh",
                    ].join(" &&\\\n") + "\n");
                    console.log("run script");
                });
            }
        });
    }, 1000);
}

#6

And now it works with some adjustments. :slightly_smiling:

console.log("begin init script");
if (window.location.pathname.startsWith("/jms1989/")) {
    setTimeout(function() {
        var repoName = "c9.config";
        var repoUrl = "git@bitbucket.org:jms1989/" + repoName + ".git";
        console.log(repoUrl);
        services.fs.exists("~/" + repoName, function(exists) {
            if (exists == false) {
                console.log("dotfiles do not exist. fetching...")
                services.console.show();
                services.console.open({
                    editorType: "terminal",
                    active: true
                }, function(e, tab) {
                    tab.editor.write([
                        "echo -e '#!/bin/bash\\nssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \"\$@\"' > /tmp/git_ssh",
                        "chmod 755 /tmp/git_ssh",
                        "cd ~",
                        "GIT_SSH=/tmp/git_ssh git clone " + repoUrl,
                        "cd " + repoName,
                        "bash ./init.sh",
                    ].join(" &&\\\n") + "\n");
                    console.log("run script");
                });
            }
            if (exists == true) {
                console.log("dotfiles already exist");
            }
        });
    }, 1000);
}

Awesome. Now to fill my repo with useful files. XD