secrez 1.0.4 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,203 +0,0 @@
1
- const {execSync} = require('child_process')
2
- const {execAsync} = require('@secrez/utils')
3
- const chalk = require('chalk')
4
- const path = require('path')
5
- const fs = require('fs-extra')
6
- const _ = require('lodash')
7
-
8
- class Git extends require('../Command') {
9
-
10
- setHelpAndCompletion() {
11
- this.cliConfig.completion.git = {
12
- _self: this
13
- }
14
- this.cliConfig.completion.help.git = true
15
- this.optionDefinitions = [
16
- {
17
- name: 'help',
18
- alias: 'h',
19
- type: Boolean
20
- },
21
- {
22
- name: 'push',
23
- alias: 'p',
24
- type: Boolean
25
- },
26
- {
27
- name: 'pull',
28
- alias: 'P',
29
- type: Boolean
30
- },
31
- {
32
- name: 'info',
33
- alias: 'i',
34
- type: Boolean
35
- },
36
- {
37
- name: 'message',
38
- type: String
39
- },
40
- {
41
- name: 'init',
42
- type: Boolean
43
- },
44
- {
45
- name: 'remote-url',
46
- type: String
47
- },
48
- {
49
- name: 'main-branch',
50
- type: String
51
- }
52
- ]
53
- }
54
-
55
- help() {
56
- return {
57
- description: ['Minimalistic git repo managements.',
58
- 'For missing commands use "shell" instead, like:',
59
- 'shell "cd ~/.secrez && git history"'],
60
- examples: [
61
- ['git -i', 'get info about the repo, like the remote url'],
62
- ['git -p', 'adds all changes and commits. If there is a remote origin, it also pushes to the remote repo'],
63
- ['git -p --message "before migrating the db"', 'adds a message to the push; in general, it is better to not specify messages, but in some special case, it can be useful to add a message'],
64
- ['git --init', 'initiates a git repo using "master" as main branch'],
65
- ['git --init --main-branch main', 'initiates a git repo using "main" as main branch'],
66
- ['git --main-branch main', 'sets the main branch'],
67
- ['git --remote-url git@github.com:sullof/priv-repo.git', 'sets the origin of the local repo'],
68
- ['git -P', '(notice the uppercase P) pulls from origin and merges']
69
- ]
70
- }
71
- }
72
-
73
- async git(options) {
74
- let result = await execAsync('which', __dirname, ['git'])
75
- if (!result.message || result.code === 1) {
76
- throw new Error('Git not installed')
77
- }
78
- let containerPath = this.secrez.config.container
79
- let repoExists = await fs.pathExists(path.join(containerPath, '.git'))
80
-
81
- if (options.init) {
82
- if (repoExists) {
83
- throw new Error('Repo already initiated.')
84
- }
85
- let res = (await execAsync('git', containerPath, ['init'])).message
86
- await execAsync('git', containerPath, ['add', '-A'])
87
- res += '\n' + (await execAsync('git', containerPath, ['commit', '-m', 'first-commit'])).message
88
- await execAsync('git', containerPath, ['branch', '-M', options.mainBranch || 'master'])
89
- return res
90
- }
91
-
92
- if (options.mainBranch) {
93
- await execAsync('git', containerPath, ['branch', '-M', options.mainBranch])
94
- return `New main branch: ${options.mainBranch}`
95
- }
96
-
97
- if (!repoExists) {
98
- throw new Error('Repo not found. Run "git --init" to initiate the repo')
99
- }
100
-
101
- result = await execAsync('git', containerPath, ['remote', '-v'])
102
- let remoteUrl
103
- if (result.message && result.code !== 1) {
104
- for (let line of result.message.split('\n')) {
105
- if (/^origin/.test(line) && /\(push\)/.test(line)) {
106
- remoteUrl = line.replace(/origin\s+(.+)\s+\(push.+/, '$1')
107
- break
108
- }
109
- }
110
- }
111
- if (options.remoteUrl) {
112
- if (remoteUrl) {
113
- await execAsync('git', containerPath, ['remote', 'remove', 'origin'])
114
- }
115
- await execAsync('git', containerPath, ['remote', 'add', 'origin', options.remoteUrl])
116
- return `Remote url: ${options.remoteUrl}`
117
- }
118
-
119
- result = await execAsync('git', containerPath, ['rev-parse', '--abbrev-ref', 'HEAD'])
120
- if (!result.message || result.code === 1) {
121
- throw new Error('No active branch found')
122
- }
123
- let branch = _.trim(result.message)
124
-
125
- result = await execAsync('git', containerPath, ['status'])
126
- let count = 0
127
-
128
- if (_.trim(result.message) && /untracked files/i.test(result.message)) {
129
- let message = _.trim(result.message).split('\n')
130
- for (let row of message) {
131
- if (/\s+(data(\w+|)|keys|cache)\//.test(row)) {
132
- count++
133
- }
134
- }
135
- }
136
-
137
- this.internalFs.cleanPreviousRootEntry()
138
-
139
- if (options.info) {
140
- return `
141
- Current branch: ${chalk.bold(branch)}
142
- Number of changed files: ${chalk.bold(count)}
143
- `
144
- }
145
-
146
- let strResult = 'Nothing done'
147
- if (options.push) {
148
- await execAsync('git', containerPath, ['add', '-A'])
149
- strResult = (await execAsync('git', containerPath, ['commit', '-m', options.message || 'another-commit'])).message
150
- }
151
-
152
- if (!remoteUrl) {
153
- return strResult
154
- }
155
-
156
- result = await execAsync('git', containerPath, ['remote', '-v', 'update'])
157
- if (!result.message || result.code === 1) {
158
- throw new Error('Error checking for remote updates')
159
- }
160
- let message = (result.message + result.error).split('\n')
161
- let isRemotelyChanged = true
162
- for (let row of message) {
163
- if (/\[up to date\]/.test(row)) {
164
- row = row.split(']')[1].replace(/ +/g, ' ').split(' ')
165
- if (row[1] === branch) {
166
- isRemotelyChanged = false
167
- }
168
- }
169
- }
170
- if (isRemotelyChanged) {
171
- throw new Error(`${chalk.black(strResult)}
172
-
173
- The repo has been remotely changed.
174
- Please, quit Secrez and merge your repo manually to avoid conflicts.
175
- `)
176
- }
177
-
178
- if (options.push) {
179
- return `${strResult}
180
- ${execSync(`cd ${containerPath} && git ${options.pull ? 'pull' : 'push'} origin ${branch}`).toString()}
181
- `
182
- } else {
183
- throw new Error('Wrong parameters')
184
- }
185
- }
186
-
187
- async exec(options = {}) {
188
- if (options.help) {
189
- return this.showHelp()
190
- }
191
- try {
192
- this.validate(options)
193
- this.Logger.reset(await this.git(options))
194
- } catch (e) {
195
- this.Logger.red(e.message)
196
- }
197
- await this.prompt.run()
198
- }
199
- }
200
-
201
- module.exports = Git
202
-
203
-