UNPKG

1.81 kBJavaScriptView Raw
1const saltPath = path.join(process.cwd(),".salt");
2const salt = fs.fileExists(saltPath) ? fs.readFileSync(saltPath,"utf-8") : false;
3
4module.exports = function (data) {
5 return new Promise((resolve,reject)=>{
6 msgs.neu("Connecting to remote server via SSH");
7
8 var sshConfig = {
9 host:data.server_hostname,
10 };
11
12 if(data.server_authType === "key") {
13 sshConfig.key = fs.readFileSync(path.join(process.cwd(),data.server_key),"utf-8");
14 sshConfig.passphrase = data.server_passphrase;
15 }
16 else {
17 sshConfig.user = data.server_username;
18 sshConfig.pass = data.server_password;
19 }
20
21 var command = `` +
22 `mkdir -p ${data.server_path} && `+ // create the directory if it doesn't exist already
23 `cd ${data.server_path} && `+ // move to it
24 `git init && `+ // initialize (or reinitialize git)
25 `(git remote add origin ${data.gitUrlWithAuth} || pwd) && `+ // add origin
26 `git fetch && `+ // fetch
27 `git reset --hard origin/${data.repo_branch} && `+ // hard reset
28 `printf "${salt}" > .salt`; // put the salt file there too
29
30 var connection = new ssh(sshConfig);
31 connection
32 .exec(command,{
33 exit:function (code) {
34 if(code !== 0) {
35 msgs.err(("Error while executing code via SSH"));
36 msgs.err("are you sure you have git installed there?");
37 msgs.err("maybe the directory you supplied isn't correct");
38 } else {
39 msgs.ok("Everything is up to date now!");
40 resolve(data);
41 }
42 }
43 })
44 .start({
45 success:function () {
46 msgs.ok("successfully connected to SSH");
47 msgs.neu("Pulling from git repository");
48 },
49 fail:function () {
50 msgs.err("Error while connecting to SSH! please make sure the server and credentials are correct!");
51 }
52 });
53 });
54};
\No newline at end of file