1 | const promiseGit = require('simple-git/promise');
|
2 | const remote = `https://github.com/nodefony/nodefony-core.git`;
|
3 | const useNodefonyVersion = `v${nodefony.version}`;
|
4 |
|
5 | module.exports = class git extends nodefony.Service {
|
6 |
|
7 | constructor(container) {
|
8 | super("git", container, container.get("notificationsCenter"));
|
9 | this.project = nodefony.projectName;
|
10 | this.gitKernel = promiseGit(this.kernel.git._baseDir);
|
11 | this.clonePath = path.resolve(__dirname, "..", "clones");
|
12 | this.nodefonyClonePath = path.resolve(this.clonePath, "nodefony-core");
|
13 | this.tags = null;
|
14 | this.nodefonyTags = null;
|
15 | this.cloneGit = null;
|
16 | this.currentVersion = useNodefonyVersion;
|
17 | if (!fs.existsSync(this.clonePath)) {
|
18 | this.kernel.cli.mkdir(this.clonePath);
|
19 | }
|
20 | if (this.kernel.type === "SERVER" && this.kernel.isCore) {
|
21 | this.getProjectTags(true)
|
22 | .catch((err) => {
|
23 | this.logger(err, "ERROR");
|
24 | });
|
25 | this.cloneNodefony()
|
26 | .then((current) => {
|
27 | this.currentVersion = current;
|
28 | return this.getNodefonyTags(true)
|
29 | .then((tags) => {
|
30 | if (useNodefonyVersion !== current) {
|
31 | this.logger(`Change Documentation version to : ${useNodefonyVersion}`);
|
32 | if (tags.all.indexOf(useNodefonyVersion) >= 0) {
|
33 | return this.checkoutVersion(useNodefonyVersion, "nodefony")
|
34 | .then((newCurrent) => {
|
35 | this.currentVersion = newCurrent;
|
36 | return newCurrent;
|
37 | }).catch((err) => {
|
38 | throw err;
|
39 | });
|
40 | }
|
41 | }
|
42 | return current;
|
43 | }).catch((err) => {
|
44 | throw err;
|
45 | });
|
46 | }).catch((err) => {
|
47 | this.logger(err, "ERROR");
|
48 | });
|
49 | }
|
50 | }
|
51 |
|
52 | getRepo(name) {
|
53 | if (!name) {
|
54 | return this.gitKernel;
|
55 | }
|
56 | const type = typeof (name);
|
57 | if (type === "string") {
|
58 | switch (name) {
|
59 | case "nodefony":
|
60 | if (this.cloneGit) {
|
61 | return this.cloneGit;
|
62 | }
|
63 | return this.gitKernel;
|
64 | case this.project:
|
65 | return this.gitKernel;
|
66 | default:
|
67 | return this.gitKernel;
|
68 | }
|
69 | }
|
70 | return name;
|
71 | }
|
72 |
|
73 | getClonePath() {
|
74 | return this.nodefonyClonePath;
|
75 | }
|
76 |
|
77 | cloneNodefony() {
|
78 | if (fs.existsSync(this.nodefonyClonePath)) {
|
79 | return this.initCloneRepo(true);
|
80 | }
|
81 | const Git = promiseGit(this.clonePath);
|
82 | this.logger(`git clone nodefony documentation in ${this.clonePath}`);
|
83 | return Git
|
84 | .silent(true)
|
85 | .clone(remote)
|
86 | .then(() => {
|
87 | this.logger(`git clone ok nodefony documentation`);
|
88 | return this.initCloneRepo();
|
89 | })
|
90 | .catch((err) => {
|
91 | this.logger(err, "ERROR");
|
92 | });
|
93 | }
|
94 |
|
95 | initCloneRepo(pull) {
|
96 | this.cloneGit = promiseGit(this.nodefonyClonePath);
|
97 | if (pull) {
|
98 | return this.pull(this.cloneGit, this.currentVersion)
|
99 | .then((PullSummary) => {
|
100 | this.logger(PullSummary, "DEBUG");
|
101 | return this.getCurrentBranch(this.cloneGit)
|
102 | .then((current) => {
|
103 | this.currentVersion = current;
|
104 | return current;
|
105 | }).catch(e => {
|
106 | throw e;
|
107 | });
|
108 | }).catch(e => {
|
109 | throw e;
|
110 | });
|
111 | }
|
112 | return this.getCurrentBranch(this.cloneGit)
|
113 | .then((current) => {
|
114 | this.currentVersion = current;
|
115 | return current;
|
116 | }).catch(e => {
|
117 | throw e;
|
118 | });
|
119 | }
|
120 |
|
121 | getReleases(repo, force) {
|
122 | if (!repo) {
|
123 | return this.getProjectTags(force);
|
124 | } else {
|
125 | if (typeof repo === "string") {
|
126 | switch (repo) {
|
127 | case "nodefony":
|
128 | return this.getNodefonyTags(force);
|
129 | default:
|
130 | return this.getProjectTags(force);
|
131 | }
|
132 | }
|
133 | }
|
134 | return repo.tags()
|
135 | .then((tags) => {
|
136 | return tags;
|
137 | }).catch(e => {
|
138 | throw e;
|
139 | });
|
140 | }
|
141 |
|
142 | getNodefonyTags(force) {
|
143 | if (this.nodefonyTags && force !== true) {
|
144 | return new Promise((resolve) => {
|
145 | return resolve(this.nodefonyTags);
|
146 | });
|
147 | }
|
148 | if (this.cloneGit) {
|
149 | return this.cloneGit.tags()
|
150 | .then((tags) => {
|
151 | this.nodefonyTags = tags;
|
152 | return tags;
|
153 | }).catch(e => {
|
154 | throw e;
|
155 | });
|
156 | } else {
|
157 | return Promise.resolve(this.currentVersion);
|
158 | }
|
159 | }
|
160 |
|
161 | getProjectTags(force) {
|
162 | if (this.tags && force !== true) {
|
163 | return new Promise((resolve) => {
|
164 | return resolve(this.tags);
|
165 | });
|
166 | } else {
|
167 | return this.gitKernel.tags()
|
168 | .then((tags) => {
|
169 | this.tags = tags;
|
170 | return tags;
|
171 | }).catch(e => {
|
172 | throw e;
|
173 | });
|
174 | }
|
175 | }
|
176 |
|
177 | checkoutVersion(version, repo) {
|
178 | this.logger(`Checkout Documentation : ${version}`);
|
179 | return this.getRepo(repo).checkout(version)
|
180 | .then(() => {
|
181 | return this.getCurrentBranch(this.cloneGit)
|
182 | .then((current) => {
|
183 | this.logger(`Documentation version: ${current}`);
|
184 | this.currentVersion = current;
|
185 | return current;
|
186 | });
|
187 | }).catch(e => {
|
188 | throw e;
|
189 | });
|
190 | }
|
191 |
|
192 | getStatus(repo) {
|
193 | return this.getRepo(repo).status()
|
194 | .then((status) => {
|
195 | return status;
|
196 | }).catch(e => {
|
197 | throw e;
|
198 | });
|
199 | }
|
200 |
|
201 | getCurrentBranch(repo) {
|
202 | return this.getRepo(repo).branch()
|
203 | .then((BranchSummary) => {
|
204 | return BranchSummary.current;
|
205 | }).catch(e => {
|
206 | throw e;
|
207 | });
|
208 | }
|
209 |
|
210 | getMostRecentCommit(repo) {
|
211 | return this.getRepo(repo).log()
|
212 | .then((ListLogSummary) => {
|
213 | return ListLogSummary;
|
214 | }).catch(e => {
|
215 | throw e;
|
216 | });
|
217 | }
|
218 |
|
219 | pull(repo, branch) {
|
220 | return this.getRepo(repo).pull(remote, branch)
|
221 | .then((PullSummary) => {
|
222 | return PullSummary;
|
223 | }).catch(e => {
|
224 | throw e;
|
225 | });
|
226 | }
|
227 |
|
228 | }; |
\ | No newline at end of file |