Skip to content

Commit

Permalink
refactor(koa): ⬆️Upgrade koa to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
vivaxy committed Oct 12, 2018
1 parent 543457a commit 1b3e054
Show file tree
Hide file tree
Showing 16 changed files with 565 additions and 488 deletions.
160 changes: 80 additions & 80 deletions lib/build-file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ const HTML_EXTENSION = '.html';
* @returns {number} sort value
*/
const compare = (_a, _b) => {
if (_a < _b) {
return AHEAD;
} else if (_a > _b) {
return ABACK;
}
return STILL;
if (_a < _b) {
return AHEAD;
} else if (_a > _b) {
return ABACK;
}
return STILL;
};

/**
Expand All @@ -40,83 +40,83 @@ const compare = (_a, _b) => {
* @returns {String} html content
*/
module.exports = (files, pathname, absoluteWorkingDirectory) => {
const baseDir = join(absoluteWorkingDirectory, pathname);

const filesFiltered = files.filter((file) => {
if (file.indexOf('.') === 0) {
return false;
}
// #3 remove inaccessible files from file list
// win下access任何文件都会返回可访问
try {
if (isWindows) {
fs.statSync(join(baseDir, file));
} else {
fs.accessSync(join(baseDir, file), fs.R_OK);
}
} catch (e) {
return false;
}
return true;
});
const baseDir = join(absoluteWorkingDirectory, pathname);

filesFiltered.sort((a, b) => {
const _isDirectoryA = fs.lstatSync(join(baseDir, a)).isDirectory();
const _isDirectoryB = fs.lstatSync(join(baseDir, b)).isDirectory();
// order by priority
if (_isDirectoryA && _isDirectoryB) {
return compare(a, b);
}
if (_isDirectoryA && !_isDirectoryB) {
return AHEAD;
}
if (_isDirectoryB && !_isDirectoryA) {
return ABACK;
}
const _extensionA = extname(a);
const _extensionB = extname(b);
const _isHtmlA = _extensionA === HTML_EXTENSION;
const _isHtmlB = _extensionB === HTML_EXTENSION;
if (_isHtmlA && _isHtmlB) {
return compare(a, b);
}
if (_isHtmlA && !_isHtmlB) {
return AHEAD;
}
if (_isHtmlB && !_isHtmlA) {
return ABACK;
}
if (_extensionA === _extensionB) {
return compare(a, b);
}
return compare(a, b);
});
const filesFiltered = files.filter((file) => {
if (file.indexOf('.') === 0) {
return false;
}
// #3 remove inaccessible files from file list
// win下access任何文件都会返回可访问
try {
if (isWindows) {
fs.statSync(join(baseDir, file));
} else {
fs.accessSync(join(baseDir, file), fs.R_OK);
}
} catch (e) {
return false;
}
return true;
});

if (pathname !== '/') {
filesFiltered.unshift('..');
filesFiltered.sort((a, b) => {
const _isDirectoryA = fs.lstatSync(join(baseDir, a)).isDirectory();
const _isDirectoryB = fs.lstatSync(join(baseDir, b)).isDirectory();
// order by priority
if (_isDirectoryA && _isDirectoryB) {
return compare(a, b);
}
if (_isDirectoryA && !_isDirectoryB) {
return AHEAD;
}
if (_isDirectoryB && !_isDirectoryA) {
return ABACK;
}
const _extensionA = extname(a);
const _extensionB = extname(b);
const _isHtmlA = _extensionA === HTML_EXTENSION;
const _isHtmlB = _extensionB === HTML_EXTENSION;
if (_isHtmlA && _isHtmlB) {
return compare(a, b);
}
if (_isHtmlA && !_isHtmlB) {
return AHEAD;
}
if (_isHtmlB && !_isHtmlA) {
return ABACK;
}
if (_extensionA === _extensionB) {
return compare(a, b);
}
return compare(a, b);
});

if (pathname !== '/') {
filesFiltered.unshift('..');
}

const list = filesFiltered.map((file) => {
const ext = extname(file);
let _ext = 'other';
if (ext === HTML_EXTENSION) {
_ext = 'html';
}
if (fs.lstatSync(join(baseDir, file)).isDirectory()) {
_ext = 'dir';
}
if (file === '..') {
_ext = 'null';
}
return {
// remove http://ip:port to redirect to correct ip:port
href: join(pathname, file),
className: _ext,
fileName: file
};
});
const list = filesFiltered.map((file) => {
const ext = extname(file);
let _ext = 'other';
if (ext === HTML_EXTENSION) {
_ext = 'html';
}
if (fs.lstatSync(join(baseDir, file)).isDirectory()) {
_ext = 'dir';
}
if (file === '..') {
_ext = 'null';
}
return {
// remove http://ip:port to redirect to correct ip:port
href: join(pathname, file),
className: _ext,
fileName: file,
};
});

return pug.compileFile(join(__dirname, '../res/list.pug'), {
pretty: ' '
})({list});
return pug.compileFile(join(__dirname, '../res/list.pug'), {
pretty: ' ',
})({ list });
};
12 changes: 9 additions & 3 deletions lib/get-file-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
const fs = require('fs');

module.exports = (file) => {
return (done) => {
return fs.lstat(file, done);
};
return new Promise((resolve, reject) => {
return fs.lstat(file, (err, stat) => {
if (err) {
reject(err);
} else {
resolve(stat);
}
});
});
};
20 changes: 10 additions & 10 deletions lib/open-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ const logPrefix = require('../constant/log-prefix.js');
const configKeys = require('../constant/config.js');

module.exports = () => {
// always use current ip address
// always use current ip address

const protocol = config.get(configKeys.SSL) ? 'https:' : 'http:';
const port = config.get(configKeys.PORT);
const protocol = config.get(configKeys.SSL) ? 'https:' : 'http:';
const port = config.get(configKeys.PORT);

const openUrl = `${protocol}//${ip.address()}:${port}/`;
const execCommand = process.platform === 'darwin' ? 'open' :
process.platform === 'win32' ? 'start' : 'xdg-open';
const openUrl = `${protocol}//${ip.address()}:${port}/`;
const execCommand = process.platform === 'darwin' ? 'open' :
process.platform === 'win32' ? 'start' : 'xdg-open';

childProcess.exec(`${execCommand} ${openUrl}`, () => {
// open browser succeeded
log.debug(logPrefix.BROWSER, execCommand, openUrl);
});
childProcess.exec(`${execCommand} ${openUrl}`, () => {
// open browser succeeded
log.debug(logPrefix.BROWSER, execCommand, openUrl);
});
};
12 changes: 9 additions & 3 deletions lib/read-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
const fs = require('fs');

module.exports = (file) => {
return (done) => {
return fs.readFile(file, done);
};
return new Promise((resolve, reject) => {
return fs.readFile(file, (err, content) => {
if (err) {
reject(err);
} else {
resolve(content);
}
});
});
};
12 changes: 9 additions & 3 deletions lib/read-folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
const fs = require('fs');

module.exports = (file) => {
return (done) => {
return fs.readdir(file, done);
};
return new Promise((resolve, reject) => {
return fs.readdir(file, (err, files) => {
if (err) {
reject(err);
} else {
resolve(files);
}
});
});
};
Loading

0 comments on commit 1b3e054

Please sign in to comment.