-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
244 lines (226 loc) · 7.62 KB
/
Copy pathindex.js
File metadata and controls
244 lines (226 loc) · 7.62 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
/*******************************
*
* /homedir
* interface.json
* /drives
* /<drive>
* /<drive>
* /<drive>
* /<drive>
* /<drive>
*
*******************************/
const fs = require('fs')
const path = require('path')
const symlinkDir = require('symlink-dir')
class Pdrive {
constructor(home) {
/******************************************
*
* const drive = new Pdrive(drive_dir)
*
******************************************/
this.home = home
}
async create(params) {
/**************************************************************************************************************************
*
* # SYNTAX
*
* ```
* await drive.create(LINK_PATH, {
* uri: DRIVE_GIT_URL,
* path: DRIVE_PATH,
* peers: [ PEER_DRIVE_GIT_URL,... ],
* })
* await drive.create({
* uri: DRIVE_GIT_URL,
* interface: {
* DRIVE_PATH: LINK_PATH,
* DRIVE_PATH: LINK_PATH,
* DRIVE_PATH: LINK_PATH,
* },
* peers: [ PEER_DRIVE_GIT_URL,... ],
* })
* ```
*
* - LINK_PATH: the symlink path(s) to create, which will point to the resolved (or created) virtual drive
* - DRIVE_PATH: The file path within the virtual drive. The LINK_PATH will point to the resolved DRIVE_PATH
* - DRIVE_GIT_URL: The git url for the virtual drive
* - PEER_DRIVE_GIT_URL: Other git URLs shared with the current DRIVE
*
* # What it does
*
* # Example:
* // link a single path to an interface
* await drive.create({
* uri: "https://github.com/comfyanonymous/ComfyUI.git",
* interface: {
* vae: path.resolve("apps/models/vae")
* },
* peers: [ "https://github.com/automatic1111/stable-diffusion-webui.git" ]
* })
*
* // link multiple paths to a single interface
* await drive.create({
* uri: "https://github.com/comfyanonymous/ComfyUI.git",
* interface: {
* lora: [
* path.resolve("apps/comfyui/app/models/LORA"),
* path.resolve("apps/comfyui/app/models/LyCORIS"),
* ]
* },
* peers: [ "https://github.com/automatic1111/stable-diffusion-webui.git" ]
* })
*
**************************************************************************************************************************/
// 1. create interface.json if it doesn't exist
let configPath = path.resolve(this.home, "interface.json")
let exists = await fs.promises.access(configPath).then(() => true).catch(() => false);
if (!exists) {
let drive_exists = await fs.promises.access(this.home).then(() => true).catch(() => false);
if (!drive_exists) {
await fs.promises.mkdir(this.home, { recursive: true })
}
await fs.promises.writeFile(configPath, JSON.stringify({}), "utf8")
}
// 2. load interface.json
//
// interface.json := {
// "https://github.com/cocktailpeanut/comfyui.git": {
// "app/models": "A"
// },
// "https://github.com/cocktailpeanut/sd-webui.git": {
// "app/models/Stable-diffusion": "A"
// }
// }
let config
try {
let configStr = await fs.promises.readFile(configPath, "utf8")
config = JSON.parse(configStr)
} catch (e) {
config = {}
}
// 3. look for existence of peers
// => if one of the declared peers already exists in the interface.json, use that drive
// => if no peers, generate a drive from scratch
for(const route in params.interface) {
// drive => points to the absolute path of the actual path the drive at route is stored
let peers = (params.peers ? params.peers : []).concat(params.uri)
// get a drive path that matches the condition
// if not, create one and return
let drive = await this.getDrive(route, config, peers)
// 4. Create a symbolic link
// iterate through all links and symlink
let links = params.interface[route]
if (!Array.isArray(links)) {
links = [links]
}
for(let link of links) {
const result = await symlinkDir(drive, link)
console.log("symlink", result)
}
// 5. Update interface.json
if (config[params.uri]) {
config[params.uri][route] = drive
} else {
config[params.uri] = {
[route]: drive
}
}
await fs.promises.writeFile(configPath, JSON.stringify(config, null, 2), "utf8")
}
}
async getDrive(route, config, peers) {
// check if one of the declared peers (or self URI) already exists in the drive
let drive
for(let peer of peers) {
if (config[peer] && config[peer][route]) {
drive = config[peer][route]
break;
}
}
console.log("Found drive?", { drive })
if (drive) {
// if the exact path is found among the peers, use that drive path
await fs.promises.mkdir(drive, { recursive: true }).catch((e) => { })
return drive
} else {
// if the exact path is not found, generate a path in the drive (not generating a new drive, just generating a folder)
let driveName
// see if there's an existing peer drive that matches the params.peers
// if there is,
for(let peer of peers) {
if (config[peer] && Object.keys(config[peer]).length > 0) {
const r0 = Object.keys(config[peer])[0]
const relativePath = path.relative(path.resolve(this.home, "drives"), config[peer][r0])
console.log({ relativePath, r0 })
driveName = relativePath.split(path.sep)[0]
break;
}
}
console.log("Generate", { route, driveName })
drive = await this._generate(route, driveName)
console.log("generated", { drive })
return drive
}
}
/*******************************************************************
*
* generates a new folder with a unique name using unix timestamp
* await drive.generate()
*
*******************************************************************/
async _generate(relativePath, driveName) {
console.log("generate", { relativePath, driveName })
let ts = Date.now()
if (!driveName) {
driveName = "d" + ts
}
// check if it exists
let drivePath = path.resolve(this.home, "drives", driveName, relativePath)
let exists = await fs.promises.access(drivePath).then(() => true).catch(() => false);
console.log({ exists, drivePath })
if (exists) {
while(true) {
ts++;
let name = "d" + ts
drivePath = path.resolve(this.home, "drives", name, relativePath)
let exists = await fs.promises.access(drivePath).then(() => true).catch(() => false);
if (!exists) {
break;
}
}
}
await fs.promises.mkdir(drivePath, { recursive: true })
return drivePath
}
// async resolve(fullPath) {
// console.log("resolve", fullPath)
// let currentPath = fullPath
// while (true) {
// try {
// console.log("trying", currentPath)
// let gitRemote = await git.getConfig({
// fs,
// dir: currentPath,
// path: 'remote.origin.url'
// })
// console.log("gitRemote", gitRemote)
// const relativePath = path.relative(currentPath, fullPath)
// return {
// url: gitRemote,
// path: relativePath
// }
// } catch (e) {
// console.log("E", e)
// if (currentPath === path.dirname(currentPath)) {
// return null
// } else {
// currentPath = path.dirname(currentPath)
// }
// }
// }
// }
}
module.exports = Pdrive