-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
clone.js
executable file
·80 lines (65 loc) · 2.33 KB
/
clone.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
#! /usr/bin/env node
// NPM ecosystem includes
const parseArgs = require("minimist")
const path = require("path")
const fs = require("fs")
const os = require("os")
const { spawn } = require("child_process")
// Particles Includes
const { Disk } = require("scrollsdk/products/Disk.node.js")
const { Particle } = require("scrollsdk/products/Particle.js")
const { ScrollCli, ScrollFile, ScrollFileSystem, SimpleCLI } = require("./scroll.js")
const packageJson = require("./package.json")
class CloneCli extends SimpleCLI {
welcomeMessage = `\n👯 WELCOME TO CLONE`
clone(cwd, gitUrl, folderName) {
return new Promise((resolve, reject) => {
const protocolPrefix = gitUrl.startsWith("http") ? "" : "https://"
const url = new URL(protocolPrefix + gitUrl)
const { hostname, pathname } = url
if (!folderName) {
folderName = pathname
.split("/")
.pop()
.replace(/\.git$/, "")
if (pathname.length < 2) folderName = hostname
}
// Allow cloning of domains like: clone capitaldb.togger.com
let cloneUrl = protocolPrefix + gitUrl
if (pathname.length < 2) cloneUrl = url + hostname
cloneUrl = cloneUrl.replace(/\.git$/, "") + ".git"
console.log(`Running: git clone ${cloneUrl} ${folderName}`)
const cloneProcess = spawn("git", ["clone", cloneUrl, folderName], { cwd })
cloneProcess.stdout.on("data", data => {
process.stdout.write(data.toString())
})
cloneProcess.stderr.on("data", data => {
process.stderr.write(data.toString())
})
cloneProcess.on("error", error => {
reject(error)
})
cloneProcess.on("close", async code => {
if (code === 0) {
console.log(`Cloned successfully into ${folderName}`)
try {
const scrollCli = new ScrollCli()
await scrollCli.buildCommand(path.join(cwd, folderName))
resolve()
} catch (error) {
reject(error)
}
} else {
reject(new Error(`git clone failed with code ${code}`))
}
})
})
}
async cloneCommand(cwd, urls) {
for (const gitUrl of urls) {
await this.clone(cwd, gitUrl)
}
}
}
if (module && !module.parent) new CloneCli().cloneCommand(process.cwd(), parseArgs(process.argv.slice(2))._)
module.exports = { CloneCli }