-
Notifications
You must be signed in to change notification settings - Fork 684
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved out all remaining parts into files
- Loading branch information
Showing
5 changed files
with
274 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Native | ||
const path = require('path') | ||
|
||
// Packages | ||
const pathType = require('path-type') | ||
const filesize = require('filesize') | ||
const fs = require('fs-promise') | ||
|
||
// Ours | ||
const prepareView = require('./view') | ||
|
||
module.exports = async (flags, current, dir, ignoredFiles) => { | ||
let files = [] | ||
const subPath = path.relative(current, dir) | ||
|
||
if (!fs.existsSync(dir)) { | ||
return false | ||
} | ||
|
||
try { | ||
files = await fs.readdir(dir) | ||
} catch (err) { | ||
throw err | ||
} | ||
|
||
for (const file of files) { | ||
const filePath = path.resolve(dir, file) | ||
const index = files.indexOf(file) | ||
const details = path.parse(filePath) | ||
|
||
details.relative = path.join(subPath, details.base) | ||
|
||
if (await pathType.dir(filePath)) { | ||
details.base += '/' | ||
} else { | ||
details.ext = details.ext.split('.')[1] || 'txt' | ||
|
||
let fileStats | ||
|
||
try { | ||
fileStats = await fs.stat(filePath) | ||
} catch (err) { | ||
throw err | ||
} | ||
|
||
details.size = filesize(fileStats.size, {round: 0}) | ||
} | ||
|
||
if (ignoredFiles.indexOf(details.base) > -1) { | ||
delete files[index] | ||
} else { | ||
files[files.indexOf(file)] = details | ||
} | ||
} | ||
|
||
const directory = path.join(path.basename(current), subPath, '/') | ||
const pathParts = directory.split('/') | ||
|
||
if (dir.indexOf(current + '/') > -1) { | ||
const directoryPath = [...pathParts] | ||
directoryPath.shift() | ||
|
||
files.unshift({ | ||
base: '..', | ||
relative: path.join(...directoryPath, '..') | ||
}) | ||
} | ||
|
||
const render = prepareView() | ||
|
||
const paths = [] | ||
pathParts.pop() | ||
|
||
for (const part in pathParts) { | ||
if (!{}.hasOwnProperty.call(pathParts, part)) { | ||
continue | ||
} | ||
|
||
let before = 0 | ||
const parents = [] | ||
|
||
while (before <= part) { | ||
parents.push(pathParts[before]) | ||
before++ | ||
} | ||
|
||
parents.shift() | ||
|
||
paths.push({ | ||
name: pathParts[part], | ||
url: parents.join('/') | ||
}) | ||
} | ||
|
||
const details = { | ||
port: flags.port, | ||
files, | ||
assetDir: process.env.ASSET_DIR, | ||
directory, | ||
nodeVersion: process.version.split('v')[1], | ||
paths | ||
} | ||
|
||
return render(details) | ||
} |
Oops, something went wrong.