-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcloner.ts
161 lines (129 loc) · 5.19 KB
/
cloner.ts
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
import { PackageSetup } from './model'
import shelljs from "shelljs"
import fs from "fs"
import ora from "ora"
import { folderNameOf, outLog, slash } from './utils'
export class Cloner {
private setup: PackageSetup
private spinnerBar: ora.Ora
private folder: string
private currentPackageName = "com.sepack.basic"
private tempPackageName = "com.sepack"
constructor(setup: PackageSetup) {
this.setup = setup
this.spinnerBar = ora()
this.spinnerBar.color = "white"
this.folder = folderNameOf(this.setup.projectName)
}
async startClone(): Promise<boolean> {
this.spinnerBar.text = "Preparing..."
this.spinnerBar.start()
const template = this.setup.template
return new Promise((resolve) => {
const command = `git clone -b ${template.branch} ${template.url} ${this.folder}`
shelljs.exec(command, { silent: true, async: true }, async (_code, _stdout, stderr) => {
const isError = stderr.includes("fatal")
this.spinnerBar.stop()
if (!isError) {
const replace = await this.replacing()
resolve(replace)
} else {
resolve(false)
}
})
})
}
private async replacing(): Promise<boolean> {
return new Promise((resolve, _reject) => {
shelljs.cd(this.folder)
const prefixMain = slash("app/src/main/java/")
const prefixAndroidTest = slash("app/src/androidTest/java/")
const prefixUnitTest = slash("app/src/test/java/")
const prefixList = [prefixMain, prefixAndroidTest, prefixUnitTest]
prefixList.forEach(item => {
this.moving(item)
})
const files = shelljs.find(".").filter(file => {
return file.match(/\.kt$/) || file.match(/\/build.gradle$/) || file.match(/\.xml$/)
})
files.forEach(file => {
outLog(`Generated file: `, slash(file))
shelljs.sed("-i", this.currentPackageName, this.setup.packageName, slash(file))
})
this.addDependencies()
this.naming()
this.addConfigJson()
resolve(true)
})
}
private addConfigJson() {
const json = `
{
"project_name": "${this.setup.projectName}",
"package_name": "${this.setup.packageName}",
"sepack_template": {
"name": "${this.setup.template.name}",
"branch": "${this.setup.template.branch}",
"url": "${this.setup.template.url}"
}
}
`
const file = "sepack_config.json"
shelljs.touch(file)
shelljs.sed("-i", "", json, file)
}
private moving(prefixDir: string) {
const destinationPackage = this.setup.packageName
const currentDir = prefixDir + this.currentPackageName.split(".").join("/")
const temp = prefixDir + this.tempPackageName.split(".").join("/")
const destinationDir = prefixDir + destinationPackage.split(".").join("/")
const isExist = fs.existsSync(slash(currentDir))
const isContainsCom = !destinationPackage.includes("com")
if (isExist) {
shelljs.mkdir("-p", slash(destinationDir))
shelljs.mv("-f", slash(currentDir) + "/*", slash(destinationDir))
shelljs.rm("-rf", slash(temp))
if (isContainsCom) {
shelljs.rm("-rf", slash(prefixDir) + "/com")
}
}
const unusedFiles = [".DS_Store", ".idea", ".git", ".gitignore", ".gradle"]
unusedFiles.forEach(item => {
shelljs.rm("-rf", slash(item))
})
}
private addDependencies() {
const currentPoint = `// sepack-external-dependencies`
const destinationPoint = this.setup.dependencies.map(item => {
return `implementation '${item}'`
})
destinationPoint.forEach(item => {
outLog(`Adding dependencies: `, slash(item))
})
const destinationString = ` ${destinationPoint}`.split(",").join("\n ")
const files = shelljs.find(".").filter((file) => {
return file.match(/\/external.gradle$/)
})
if (destinationPoint.length != 0) {
files.forEach((file) => {
shelljs.sed("-i", currentPoint, destinationString, slash(file))
})
}
}
private naming() {
const currentPoint = `sepack-name-project`
const externalGradle = `apply from: "$rootDir/dependencies/external.gradle"`
const externalGradleFixer = `apply from: "$${slash("rootDir/dependencies/external.gradle")}`
const files = shelljs.find(".").filter((file) => {
return file.match(/\/strings.xml$/)
})
const appGradle = shelljs.find(slash('app/build.gradle'))
files.forEach(file => {
shelljs.sed("-i", currentPoint, this.setup.projectName, slash(file))
})
appGradle.forEach(file => {
shelljs.sed("-i", externalGradle, externalGradleFixer, slash(file))
})
shelljs.sed("-i", currentPoint, this.setup.projectName, "settings.gradle")
}
}