-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.js
129 lines (104 loc) · 3.3 KB
/
commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const { mkdirSync, existsSync } = require('fs');
const { which, exec, cd, ls } = require('shelljs');
const { argv } = require('yargs');
const { WORKSPACE_PATH, BRANCH, VERSION } = require('./config');
const SHELL_OPTIONS = { silent: true };
const COMMANDS = { rollback: 'rollback' };
const DEFAULT_COMMAND = 'deploy';
const [COMMAND] = argv._;
const OPTIONS = {
COMMAND: COMMANDS[COMMAND] || DEFAULT_COMMAND,
SHOW_HELP: !!argv.h,
SHOW_VERSION: !!argv.v,
WORKSPACE: argv.w || argv.workspace,
SFTP_HOSTNAME: argv.H || argv.hostname,
SFTP_USER: argv.u || argv.user,
SFTP_PASSWORD: argv.p || argv.password,
SFTP_PORT: argv.port,
SFTP_DEST: argv.d || argv.dest,
GIT_REPOSITORY: argv.r || argv.repository,
BRANCH: argv.b || argv.branch,
};
const SELECTED_BRANCH = OPTIONS.BRANCH || BRANCH;
const trimOutput = stdout => stdout.trim();
const splitLines = output => output.split('\n');
const getNameStatus = output => {
const [status, ...fileNameArray] = output.split(/\t/);
return { status: status.toUpperCase(), file: fileNameArray.join(' ').trim() };
};
const run = command =>
new Promise((resolve, reject) => {
exec(command, SHELL_OPTIONS, (code, stdout, stderr) =>
code === 0 ? resolve(stdout) : reject({ code, error: stderr }),
);
});
const checkGit = () => {
if (!which('git')) throw new Error('Git Not found');
};
const createWorkspace = () => {
if (!existsSync(WORKSPACE_PATH)) mkdirSync(WORKSPACE_PATH);
};
const goToWorkspaceFolder = () => cd(WORKSPACE_PATH);
const goToProjectFolder = project => cd(`${WORKSPACE_PATH}/${project}`);
const updateRepository = () => run('git pull').then(trimOutput);
const getEmptyTreeHash = () =>
run("printf '' | git hash-object -t tree --stdin").then(trimOutput);
const getCommits = () =>
run('git log --oneline')
.then(trimOutput)
.then(splitLines);
const getChangedFiles = ({ startHash, endHash }) =>
run(`git diff --name-status ${startHash} ${endHash}`)
.then(trimOutput)
.then(splitLines)
.then(lines => lines.map(getNameStatus));
const cloneToWorkspace = (repository, name = '') => {
goToWorkspaceFolder();
return run(
`git clone ${repository} -b ${SELECTED_BRANCH} --single-branch ${name}`.trim(),
).then(() => name || repository.match(/^.+\/(.+).git$/)[1]);
};
const listWorkspaces = () => ls(WORKSPACE_PATH);
const checkout = (hash = SELECTED_BRANCH) =>
run(`git checkout ${hash}`).then(trimOutput);
const help = () => {
console.log(`
CLI tool for deploying code from a Git repository via SFTP in a controlled way
usage:
git2sftp [options]
options:
-w, --workspace Select Workspace
-H, --hostname SFTP Hostname
-u, --user SFTP User
-p, --password SFTP Password
--port SFTP Port (Default: 22)
-d, --dest SFTP Remote Destiny
-r, --repository Git repository
-b, --branch Git branch (Default: master)
-h Show Help
-v Show Version
`);
process.exit(0);
};
const version = () => {
console.log(`
git2sftp -> ${VERSION}
`);
process.exit(0);
};
module.exports = {
OPTIONS,
COMMANDS,
help,
version,
checkGit,
createWorkspace,
cloneToWorkspace,
goToProjectFolder,
updateRepository,
getEmptyTreeHash,
getCommits,
getChangedFiles,
listWorkspaces,
checkout,
};