UNPKG

3.28 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | qewd-up: Rapid QEWD API Development |
5 | |
6 | Copyright (c) 2018-20 M/Gateway Developments Ltd, |
7 | Redhill, Surrey UK. |
8 | All rights reserved. |
9 | |
10 | http://www.mgateway.com |
11 | Email: rtweed@mgateway.com |
12 | |
13 | |
14 | Licensed under the Apache License, Version 2.0 (the "License"); |
15 | you may not use this file except in compliance with the License. |
16 | You may obtain a copy of the License at |
17 | |
18 | http://www.apache.org/licenses/LICENSE-2.0 |
19 | |
20 | Unless required by applicable law or agreed to in writing, software |
21 | distributed under the License is distributed on an "AS IS" BASIS, |
22 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
23 | See the License for the specific language governing permissions and |
24 | limitations under the License. |
25 ----------------------------------------------------------------------------
26
27 14 May 2020
28
29 Emulation of Git Clone without any dependency on Git itself
30
31 eg:
32
33 let target = process.cwd() + '/adminui';
34 git_clone('https://github.com/robtweed/qewd-monitor-adminui', target, function() {
35 console.log('done!');
36 });
37
38*/
39
40let request = require('request');
41let fs = require('fs-extra');
42let unzip = require('extract-zip');
43let uuid = require('uuid/v4');
44
45function git_clone(url, targetDir, callback) {
46
47 let zipFile = 'temp-' + uuid() + '.zip';
48 let file = fs.createWriteStream(zipFile);
49 url = url + '/zipball/master/';
50
51 let st = request(url).pipe(file);
52 let tempDir = process.cwd() + '/temp-' + uuid();
53
54 st.on('finish', async function() {
55 try {
56 await unzip(zipFile, { dir: tempDir });
57 let dir = fs.readdirSync(tempDir)[0];
58 let src = tempDir + '/' + dir;
59 let dirs = fs.readdirSync(src);
60 if (fs.existsSync(targetDir)) {
61 fs.removeSync(targetDir);
62 fs.mkdirSync(targetDir);
63 }
64 else {
65 fs.mkdirSync(targetDir);
66 }
67 dirs.forEach(function(dir) {
68 fs.moveSync(src + '/' + dir, targetDir + '/' + dir);
69 });
70 fs.removeSync(tempDir);
71 fs.removeSync(zipFile);
72 if (callback) callback();
73 }
74 catch(err) {
75 console.log('Error occurred while cloning ' + url + ' to ' + targetDir);
76 console.log(err);
77 }
78
79 });
80}
81
82module.exports = git_clone;