feat: migrate to moment from date-fns, fix ls format
Date-fns is great, but too early for use
This commit is contained in:
@@ -47,9 +47,9 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"bunyan": "^1.8.9",
|
||||
"date-fns": "^1.28.2",
|
||||
"lodash": "^4.17.4",
|
||||
"minimist-string": "^1.0.2",
|
||||
"moment": "^2.18.1",
|
||||
"uuid": "^3.0.1",
|
||||
"when": "^3.7.8"
|
||||
},
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const when = require('when');
|
||||
const format = require('date-fns/format');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = {
|
||||
directive: 'MDTM',
|
||||
@@ -9,7 +9,7 @@ module.exports = {
|
||||
|
||||
return when.try(this.fs.get.bind(this.fs), command._[1])
|
||||
.then(fileStat => {
|
||||
const modificationTime = format(fileStat.mtime, 'YYYYMMDDHHmmss.SSS');
|
||||
const modificationTime = moment.utc(fileStat.mtime).format('YYYYMMDDHHmmss.SSS');
|
||||
return this.reply(213, modificationTime);
|
||||
})
|
||||
.catch(err => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const _ = require('lodash');
|
||||
const dateFns = require('date-fns');
|
||||
const moment = require('moment');
|
||||
const errors = require('../errors');
|
||||
|
||||
module.exports = function (fileStat, format = 'ls') {
|
||||
@@ -16,6 +16,10 @@ module.exports = function (fileStat, format = 'ls') {
|
||||
};
|
||||
|
||||
function ls(fileStat) {
|
||||
const now = moment.utc();
|
||||
const mtime = moment.utc(new Date(fileStat.mtime));
|
||||
const dateFormat = now.diff(mtime, 'months') < 6 ? 'MMM DD HH:mm' : 'MMM DD YYYY';
|
||||
|
||||
return [
|
||||
fileStat.mode !== null
|
||||
? [
|
||||
@@ -35,7 +39,7 @@ function ls(fileStat) {
|
||||
fileStat.uid,
|
||||
fileStat.gid,
|
||||
_.padStart(fileStat.size, 12),
|
||||
_.padStart(dateFns.format(fileStat.mtime, 'MMM DD HH:mm'), 12),
|
||||
_.padStart(mtime.format(dateFormat), 12),
|
||||
fileStat.name
|
||||
].join(' ');
|
||||
}
|
||||
@@ -44,7 +48,7 @@ function ep(fileStat) {
|
||||
const facts = [
|
||||
fileStat.dev && fileStat.ino ? `i${fileStat.dev.toString(16)}.${fileStat.ino.toString(16)}` : null,
|
||||
fileStat.size ? `s${fileStat.size}` : null,
|
||||
fileStat.mtime ? `m${dateFns.format(dateFns.parse(fileStat.mtime), 'X')}` : null,
|
||||
fileStat.mtime ? `m${moment.utc(new Date(fileStat.mtime)).format('X')}` : null,
|
||||
fileStat.mode ? `up${fileStat.mode.toString(8).substr(fileStat.mode.toString(8).length - 3)}` : null,
|
||||
fileStat.isDirectory() ? 'r' : '/'
|
||||
].join(',');
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const {expect} = require('chai');
|
||||
const dateFns = require('date-fns');
|
||||
|
||||
const fileStat = require('../../src/helpers/file-stat');
|
||||
const errors = require('../../src/errors');
|
||||
@@ -17,24 +16,48 @@ describe('helpers // file-stat', function () {
|
||||
size: 527,
|
||||
blksize: 4096,
|
||||
blocks: 8,
|
||||
atime: 'Mon, 10 Oct 2011 23:24:11 GMT',
|
||||
mtime: 'Mon, 10 Oct 2011 23:24:11 GMT',
|
||||
ctime: 'Mon, 10 Oct 2011 23:24:11 GMT',
|
||||
birthtime: 'Mon, 10 Oct 2011 23:24:11 GMT',
|
||||
atime: 'Mon, 10 Oct 2017 23:24:11 GMT',
|
||||
mtime: 'Mon, 10 Oct 2017 23:24:11 GMT',
|
||||
ctime: 'Mon, 10 Oct 2017 23:24:11 GMT',
|
||||
birthtime: 'Mon, 10 Oct 2017 23:24:11 GMT',
|
||||
isDirectory: () => false
|
||||
};
|
||||
|
||||
describe.skip('format - ls //', function () {
|
||||
const STAT_OLD = {
|
||||
name: 'test2',
|
||||
dev: 2114,
|
||||
ino: 48064969,
|
||||
mode: 33188,
|
||||
nlink: 1,
|
||||
uid: 84,
|
||||
gid: 101,
|
||||
rdev: 0,
|
||||
size: 530,
|
||||
blksize: 4096,
|
||||
blocks: 8,
|
||||
atime: 'Mon, 10 Oct 2011 14:05:12 GMT',
|
||||
mtime: 'Mon, 10 Oct 2011 14:05:12 GMT',
|
||||
ctime: 'Mon, 10 Oct 2011 14:05:12 GMT',
|
||||
birthtime: 'Mon, 10 Oct 2011 14:05:12 GMT',
|
||||
isDirectory: () => false
|
||||
};
|
||||
|
||||
describe('format - ls //', function () {
|
||||
it('formats correctly', () => {
|
||||
const format = fileStat(STAT, 'ls');
|
||||
expect(format).to.equal('-rwxrw-r-- 1 85 100 527 Oct 10 17:24 test1');
|
||||
expect(format).to.equal('-rwxrw-r-- 1 85 100 527 Oct 10 23:24 test1');
|
||||
});
|
||||
|
||||
it('formats correctly for files over 6 months old', () => {
|
||||
const format = fileStat(STAT_OLD, 'ls');
|
||||
expect(format).to.equal('-rwxrw-r-- 1 84 101 530 Oct 10 2011 test2');
|
||||
});
|
||||
});
|
||||
|
||||
describe.skip('format - ep //', function () {
|
||||
describe('format - ep //', function () {
|
||||
it('formats correctly', () => {
|
||||
const format = fileStat(STAT, 'ep');
|
||||
expect(format).to.equal('+i842.2dd69c9,s527,m1318289051,up644,/ test1');
|
||||
expect(format).to.equal('+i842.2dd69c9,s527,m1507677851,up644,/ test1');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user