forked from bandhavya/wm-reactnative-cli
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathproject-sync.service.js
More file actions
399 lines (378 loc) · 14.4 KB
/
Copy pathproject-sync.service.js
File metadata and controls
399 lines (378 loc) · 14.4 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
const { URL } = require('url');
const fs = require('fs-extra');
const path = require('path');
const logger = require('./logger');
const prompt = require('prompt');
const axios = require('axios');
const os = require('os');
const qs = require('qs');
const semver = require('semver');
const { exec } = require('./exec');
const { unzip } = require('./zip');
const taskLogger = require('./custom-logger/task-logger').spinnerBar;
const {previewSteps} = require('./custom-logger/steps');
const chalk = require('chalk');
//const PULL_URL = '/studio/services/projects/${projectId}/vcs/remoteChanges';
const STORE_KEY = 'user.auth.token';
const MAX_REQUEST_ALLOWED_TIME = 5 * 60 * 1000;
const loggerLabel = 'project-sync-service';
let remoteBaseCommitId = '';
let WM_PLATFORM_VERSION = '';
let isAuthenticated = false;
async function findProjectId(config) {
const projectList = (await axios.get(`${config.baseUrl}/edn-services/rest/users/projects/list`,
{headers: {
cookie: config.authCookie
}})).data;
const project = projectList.filter(p => p.displayName === config.projectName)
.filter(p => (config.appPreviewUrl.endsWith(p.name + "_" + p.vcsBranchId)));
if (project && project.length) {
WM_PLATFORM_VERSION = project[0].platformVersion;
return project[0].studioProjectId;
}
}
async function downloadFile(res, tempFile){
if (res.status !== 200) {
throw new Error('failed to download the project');
}
await new Promise((resolve, reject) => {
const fw = fs.createWriteStream(tempFile);
res.data.pipe(fw);
fw.on('error', err => {
reject(err);
fw.close();
});
fw.on('close', resolve);
});
}
async function downloadProject(projectId, config, projectDir) {
try {
const start = Date.now();
logger.info({label: loggerLabel,message: 'downloading the project...'});
taskLogger.start(previewSteps[2].start);
taskLogger.setTotal(previewSteps[2].total)
const tempFile = `${os.tmpdir()}/changes_${Date.now()}.zip`;
if (semver.lt(WM_PLATFORM_VERSION, '11.4.0')) {
const res = await axios.get(`${config.baseUrl}/studio/services/projects/${projectId}/vcs/gitInit`, {
responseType: 'stream',
headers: {
cookie: config.authCookie
}
});
taskLogger.incrementProgress(2);
await downloadFile(res, tempFile);
taskLogger.incrementProgress(1);
const gitDir = path.join(projectDir, '.git');
fs.mkdirpSync(gitDir);
await unzip(tempFile, gitDir);
await exec('git', ['restore', '.'], {cwd: projectDir});
taskLogger.incrementProgress(1);
}
else{
const gitInfo = await axios.get(`${config.baseUrl}/studio/services/projects/${projectId}/vcs/gitBare`, {
responseType: 'application/json',
headers: {
cookie: config.authCookie
}
});
taskLogger.incrementProgress(2);
if(gitInfo.status !== 200){
throw new Error('failed to download the project');
}
const fileId = gitInfo.data.fileId;
remoteBaseCommitId = gitInfo.data.remoteBaseCommitId;
const res = await axios.get(`${config.baseUrl}/file-service/${fileId}`, {
responseType: 'stream',
headers: {
cookie: config.authCookie
}
})
taskLogger.incrementProgress(2);
await downloadFile(res, tempFile);
const tempDir = path.join(`${os.tmpdir()}`, `project_${Date.now()}`);
fs.mkdirpSync(tempDir);
const gitDir = path.join(projectDir, '.git');
if(fs.existsSync(gitDir)){
await unzip(tempFile, gitDir);
await exec('git', ['config', '--local', '--unset', 'core.bare'], {cwd: projectDir});
await exec('git', ['restore', '.'], {cwd: projectDir});
}
else{
await unzip(tempFile, tempDir);
fs.rmSync(projectDir, { recursive: true, force: true });
await exec('git', ['clone', "-b", "master", tempDir, projectDir]);
}
fs.rmSync(tempDir, { recursive: true, force: true });
taskLogger.incrementProgress(1);
}
logger.info({
label: loggerLabel,
message: `downloaded the project in (${Date.now() - start} ms).`
});
taskLogger.incrementProgress(1);
taskLogger.succeed(`${previewSteps[2].succeed} in (${Date.now() - start} ms).`);
fs.unlink(tempFile);
const logDirectory = projectDir + '/output/logs/';
fs.mkdirSync(logDirectory, {
recursive: true
});
logger.info({
label: loggerLabel,
message: 'log directory = '+ logDirectory
});
global.logDirectory = logDirectory;
logger.setLogDirectory(logDirectory);
taskLogger.info("Full log details can be found in: " + chalk.blue(logDirectory));
} catch (e) {
logger.info({
label: loggerLabel,
message: e+` The download of the project has encountered an issue. Please ensure that the preview is active.`
});
taskLogger.fail(e+` ${previewSteps[2].fail}`)
}
}
async function gitResetAndPull(tempDir, projectDir){
await exec('git', ['clean', '-fd', '-e', 'output'], {cwd: projectDir});
await exec('git', ['fetch', path.join(tempDir, 'remoteChanges.bundle'), 'refs/heads/master'], {cwd: projectDir});
await exec('git', ['reset', '--hard', 'FETCH_HEAD'], {cwd: projectDir});
}
async function pullChanges(projectId, config, projectDir) {
isAuthenticated = await checkAuthCookie(config);
if (!isAuthenticated) {
console.log(chalk.yellow('\n⚠ Authentication Required'));
console.log(chalk.gray('━'.repeat(50)));
console.log(chalk.white('\nYour session has expired. Please authenticate to continue.'));
console.log(chalk.cyan(`\n→ Generate token: ${config.baseUrl}/studio/services/auth/token\n`));
config.authCookie = await authenticateWithToken(config, false);
global.localStorage.setItem(STORE_KEY, config.authCookie);
}
try {
const output = await exec('git', ['rev-parse', 'HEAD'], {
cwd: projectDir
});
const headCommitId = output[0];
logger.debug({label: loggerLabel, message: 'HEAD commit id is ' + headCommitId});
taskLogger.start('pulling new changes from studio...');
const tempDir = path.join(`${os.tmpdir()}`, `changes_${Date.now()}`);
if (semver.lt(WM_PLATFORM_VERSION, '11.4.0')) {
const tempFile = `${os.tmpdir()}/changes_${Date.now()}.zip`;
console.log(tempFile);
const res = await axios.get(`${config.baseUrl}/studio/services/projects/${projectId}/vcs/remoteChanges?headCommitId=${headCommitId}`, {
responseType: 'stream',
headers: {
cookie: config.authCookie
}
});
await downloadFile(res, tempFile);
fs.mkdirpSync(tempDir);
await unzip(tempFile, tempDir);
await gitResetAndPull(tempDir, projectDir);
await exec('git', ['apply', '--allow-empty', '--ignore-space-change', path.join(tempDir, 'patchFile.patch')], {cwd: projectDir});
logger.debug({label: loggerLabel, message: 'Copying any uncommitted binary files'});
copyContentsRecursiveSync(path.join(tempDir, 'binaryFiles'), projectDir);
fs.unlink(tempFile);
}
else{
const gitInfo = await axios.get(`${config.baseUrl}/studio/services/projects/${projectId}/vcs/pull?lastPulledWorkspaceCommitId=${headCommitId}&lastPulledRemoteHeadCommitId=${remoteBaseCommitId}`, {
responseType: 'application/json',
headers: {
cookie: config.authCookie
}
});
if (gitInfo.status !== 200) {
throw new Error('failed to pull project changes');
}
const fileId = gitInfo.data.fileId;
remoteBaseCommitId = gitInfo.data.remoteBaseCommitId;
const res = await axios.get(`${config.baseUrl}/file-service/${fileId}`, {
responseType: 'stream',
headers: {
cookie: config.authCookie
}
})
fs.mkdirpSync(tempDir);
const tempFile = `${tempDir}/remoteChanges.bundle`;
await downloadFile(res, tempFile);
await gitResetAndPull(tempDir, projectDir);
fs.unlink(tempFile);
}
fs.rmSync(tempDir, { recursive: true, force: true });
taskLogger.succeed(`pulled new changes from studio - head commit id ${headCommitId}`);
let filesChanged = await exec('git', ['diff','--name-status', 'HEAD~1', 'HEAD'], {cwd: projectDir});
filesChanged = filesChanged.filter(Boolean);
const changes = filesChanged.map((line) => {
const [status, ...fileParts] = line.trim().split(/\s+/);
const filePath = fileParts.join(' ').replace(/^.*webapp\//, '');
return { status, filePath };
});
const formatted = changes.map(({ status, filePath }) => {
const color = status === 'A' ? chalk.green
: status === 'D' ? chalk.red
: status === 'M' ? chalk.yellow
: chalk.cyan;
return `${color(status)}:${color(filePath)}`;
});
taskLogger.info("Files changed: \n\t" + formatted.join('\n\t'));
} catch (e) {
logger.info({
label: loggerLabel,
message: e+` The attempt to execute "git pull" was unsuccessful. Please verify your connections.`
});
taskLogger.succeed( e+` The attempt to execute "git pull" was unsuccessful. Please verify your connections.`);
}
}
function copyContentsRecursiveSync(src, dest) {
fs.readdirSync(src).forEach(function(file) {
var childSrc = path.join(src, file);
var childDest = path.join(dest, file);
var exists = fs.existsSync(childSrc);
var stats = exists && fs.statSync(childSrc);
var isDirectory = exists && stats.isDirectory();
if (isDirectory) {
if (!fs.existsSync(childDest)) {
fs.mkdirSync(childDest);
}
copyContentsRecursiveSync(childSrc, childDest);
} else {
fs.copyFileSync(childSrc, childDest);
}
});
}
function extractAuthCookie(res) {
const headers = res && res.response && res.response.headers;
if (!headers) {
return;
}
const result = headers['set-cookie'].filter(s => s.indexOf('auth_cookie') >= 0);
if (result.length) {
return result[0].split(';')[0];
}
}
async function authenticateWithUserNameAndPassword(config) {
const credentials = await getUserCredentials();
return axios.post(`${config.baseUrl}/login/authenticate`,
qs.stringify({
j_username: credentials.username,
j_password: credentials.password
}), {
maxRedirects: 0
}).catch((res) => {
const cookie = extractAuthCookie(res);
if (!cookie) {
console.log('Not able to login. Try again.');
return authenticate(config);
}
return cookie;
});
}
async function authenticateWithToken(config, showHelp) {
try {
if (showHelp) {
console.log('***************************************************************************************');
console.log('* Please open the below url in the browser, where your WaveMaker studio is opened. *');
console.log('* Copy the response content and paste in the terminal. *');
console.log('***************************************************************************************');
console.log(`\n\n`);
console.log(`${config.baseUrl}/studio/services/auth/token`);
console.log(`\n\n`);
}
const cookie = (await getAuthToken()).token.split(';')[0];
if (!cookie) {
console.log('Not able to login. Try again.');
return authenticateWithToken(config);
}
return 'auth_cookie='+cookie;
} catch (e) {
logger.info({
label: loggerLabel,
message: e+` Your authentication has failed. Please proceed with a valid token.`
});
}
}
function getUserCredentials() {
var schema = {
properties: {
username: {
required: true
},
password: {
required: true,
hidden: true
}
}
};
prompt.start();
return new Promise((resolve, reject) => {
prompt.get(schema, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
function getAuthToken() {
var schema = {
properties: {
token: {
required: true
}
}
};
prompt.start();
return new Promise((resolve, reject) => {
prompt.get(schema, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
async function checkAuthCookie(config) {
try {
await findProjectId(config);
logger.info({
label: loggerLabel,
message: `user authenticated.`
});
} catch(e) {
return false;
}
return true;
}
async function setup(previewUrl, projectName, authToken) {
if (authToken) {
authToken = 'auth_cookie=' + authToken;
}
if (previewUrl.endsWith('/')) {
previewUrl = previewUrl.slice(0, -1);
}
const config = {
authCookie : authToken || global.localStorage.getItem(STORE_KEY) || '',
baseUrl: new URL(previewUrl).origin,
appPreviewUrl: previewUrl,
projectName: projectName
};
isAuthenticated = await checkAuthCookie(config);
if (!isAuthenticated) {
//console.log(`Need to login to Studio (${config.baseUrl}). \n Please enter your Studio credentails.`);
//config.authCookie = await authenticateWithUserNameAndPassword(config);
config.authCookie = await authenticateWithToken(config, true);
}
global.localStorage.setItem(STORE_KEY, config.authCookie);
taskLogger.incrementProgress(1);
taskLogger.succeed(previewSteps[1].succeed);
return config;
}
async function setupProject(previewUrl, projectName, toDir, authToken) {
const config = await setup(previewUrl, projectName, authToken);
const projectId = await findProjectId(config);
await downloadProject(projectId, config, toDir);
return () => pullChanges(projectId, config, toDir);
};
module.exports = {
setupProject : setupProject
};