Skip to content

Commit 9fdad2d

Browse files
committed
fix: directory, path, pathname
1 parent ca012d0 commit 9fdad2d

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

src/server.js

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module.exports = async function file(CoCreateConfig, configPath, match) {
162162
return
163163
}
164164

165-
async function runFiles(directory, entry, exclude, parentDirectory = '') {
165+
async function runFiles(directory, entry, exclude, Path, directoryName) {
166166
const entryPath = path.resolve(configDirectoryPath, entry)
167167
let files = fs.readdirSync(entryPath);
168168

@@ -181,7 +181,6 @@ module.exports = async function file(CoCreateConfig, configPath, match) {
181181
const filePath = path.resolve(entryPath, file);
182182
if (filePath.startsWith(match[i])) {
183183
skip = false
184-
console.log('Uploaded: ', filePath)
185184
break;
186185
}
187186
}
@@ -191,48 +190,48 @@ module.exports = async function file(CoCreateConfig, configPath, match) {
191190
let isDirectory = fs.existsSync(`${entryPath}/${file}`) && fs.lstatSync(`${entryPath}/${file}`).isDirectory();
192191
let name = file
193192
let source = ''
194-
let directoryName = parentDirectory || '';
195193

196194
const fileExtension = path.extname(file);
197195
let mimeType = mimeTypes[fileExtension]
198196

199-
if (!directoryName && directory.object && directory.object.directory)
200-
directoryName = directory.object.directory.replace('{{directory}}', '').trim()
201-
else if (!directoryName)
202-
directoryName = '/'
197+
if (!directoryName) {
198+
if (directory.object && directory.object.directory) {
199+
if (directory.object.directory === "{{directory}}") {
200+
directoryName = entry.split('/')
201+
directoryName = directoryName[directoryName.length - 1]
202+
} else
203+
directoryName = directory.object.directory
204+
} else
205+
directoryName = '/'
206+
}
203207

204208
if (exclude && exclude.includes(directoryName)) continue
205209

206-
let Path = '';
207-
if (directoryName.endsWith("/"))
208-
Path = directoryName
209-
else if (directoryName)
210-
Path = directoryName + '/'
211-
else
212-
Path = '/'
213-
214-
let pathname = Path + name;
210+
if (!Path) {
211+
if (directoryName === '/')
212+
Path = directoryName
213+
else
214+
Path = '/' + directoryName
215+
}
215216

216-
let folder = ''
217+
let pathname
217218
if (Path === '/')
218-
folder = Path
219-
else {
220-
let result = Path.split('/');
221-
folder = result[result.length - 2];
222-
}
219+
pathname = Path + name;
220+
else
221+
pathname = Path + '/' + name;
223222

224223
if (isDirectory)
225224
mimeType = "text/directory"
226225
else
227226
source = getSource(`${entryPath}/${file}`, mimeType)
228227

229228
let values = {
230-
'{{name}}': name,
231-
'{{source}}': source,
232-
'{{directory}}': folder,
233-
'{{path}}': Path,
229+
'{{name}}': name || '',
230+
'{{source}}': source || '',
231+
'{{directory}}': directoryName || '',
232+
'{{path}}': Path || '',
234233
'{{pathname}}': pathname,
235-
'{{content-type}}': mimeType
234+
'{{content-type}}': mimeType || ''
236235
}
237236

238237
let object = { ...directory.object }
@@ -241,7 +240,7 @@ module.exports = async function file(CoCreateConfig, configPath, match) {
241240
if (!object.src)
242241
object.src = "{{source}}"
243242
if (!object.directory)
244-
object.directory = "/{{directory}}"
243+
object.directory = "{{directory}}"
245244
if (!object.path)
246245
object.path = "{{path}}"
247246
if (!object.pathname)
@@ -261,16 +260,7 @@ module.exports = async function file(CoCreateConfig, configPath, match) {
261260
let variables = directory.object[key].match(/{{([A-Za-z0-9_.,\[\]\-\/ ]*)}}/g);
262261
if (variables) {
263262
for (let variable of variables) {
264-
if (variable == '{{directory}}') {
265-
if (folder)
266-
newObject.object[key] = values[variable]
267-
else
268-
newObject.object[key] = newObject.object[key].replace(variable, '');
269-
}
270-
else if (isDirectory && variable == '{{source}}')
271-
delete newObject.object[key]
272-
else
273-
newObject.object[key] = newObject.object[key].replace(variable, values[variable]);
263+
newObject.object[key] = newObject.object[key].replace(variable, values[variable]);
274264
}
275265
}
276266

@@ -283,6 +273,8 @@ module.exports = async function file(CoCreateConfig, configPath, match) {
283273
}
284274

285275
response = await runStore(newObject);
276+
console.log(`Uploaded: ${entryPath}/${file}`, `To: ${pathname}`)
277+
286278
if (response.error)
287279
errorLog.push(response.error)
288280

@@ -293,7 +285,7 @@ module.exports = async function file(CoCreateConfig, configPath, match) {
293285
else
294286
newEntry = entry + '/' + name
295287

296-
await runFiles(directory, newEntry, exclude, pathname)
288+
await runFiles(directory, newEntry, exclude, pathname, name)
297289
}
298290
}
299291
if (errorLog.length)

0 commit comments

Comments
 (0)