Skip to content

Commit

Permalink
repo files sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Sep 1, 2018
1 parent c5b7a99 commit 1c12eab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/controller/git/repos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const FS = require('fs-extra')
const Models = require('../../../../conf/sequelize');
const { readFile } = require('../../../utils/fsExtra');

const { getFiles } = require('./util');
const { getFiles, repoFilesSort } = require('./util');

module.exports = {
created: async (ctx) => {
Expand Down Expand Up @@ -212,7 +212,7 @@ module.exports = {
const blob = await readme.entry.getBlob();
body.readmeContent = await blob.toString();
}
body.tree = [...oldTree];
body.tree = repoFilesSort([...oldTree]);
ctx.body = body;
} catch (err) {
ctx.response.status = err.statusCode || err.status || 500;
Expand Down
23 changes: 23 additions & 0 deletions src/controller/git/repos/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,27 @@ exports.getFiles = (treeWalker, recursive = false) => {
treeWalker.start();
if (!recursive) resolve(trees);
});
}

// function mySorter(a, b) {
// if (/^\d/.test(a) ^ /^\D/.test(b)) return a > b ? 1 : (a == b ? 0 : -1);
// return a > b ? -1 : (a == b ? 0 : 1);
// }
// const pyArray = ["a", "d", "fa", "5", "t", "fw2", "a31", "b", "e", "2fs", "4", "0"]

/**
* Nodegit 返回的目录对象,进行排序
* 1. 隐藏文件夹,排在第一位
* 2. 文件夹,排在第二位
* 3. 隐藏文件,排第三位
* 4. 文件,排第四位
* @param {Array} tree
*/
exports.repoFilesSort = (tree = []) => {
const hiddenFolder = tree.filter(_item => /^\./.test(_item.name) && /^(tree|commit)$/.test(_item.type));
const folder = tree.filter(_item => !/^\./.test(_item.name) && /^(tree|commit)$/.test(_item.type));
const hiddenFile = tree.filter(_item => /^\./.test(_item.name) && /^(blob)$/.test(_item.type));
const file = tree.filter(_item => !/^\./.test(_item.name) && /^(blob)$/.test(_item.type));
console.log('tree:', file);
return [].concat(hiddenFolder, folder, hiddenFile, file);
}

0 comments on commit 1c12eab

Please sign in to comment.