runtime-shell
Version:
Some useful shell commands for runtime.js
191 lines (142 loc) • 5.31 kB
JavaScript
;
// replaceAll() from http://stackoverflow.com/a/1144788/4713791
function replaceAll(find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
}
function removeChar(index, str) {
return str.slice(0, index) + str.slice(index + 1);
}
module.exports = function(runtime) {
runtime.shell.setCommand('date', function(args, env, cb) {
var d = new Date();
if (args == '') {
env.stdio.writeLine(d);
cb(0);
} else {
var i = args.indexOf('+');
if (i === 0 || i === 1) {
var ds = removeChar(i, args);
ds = removeChar(0, ds);
ds = removeChar(ds.length-1, ds);
replaceAll('%%', '%', ds);
// Uses only 'nu' locale. TODO: fix this.
var days = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
];
var shortdays = [
'Sun',
'Mon',
'Tue',
'Wed',
'Thur',
'Fri',
'Sat'
];
// Weekday
ds = replaceAll('%a', shortdays[d.getDay()], ds);
ds = replaceAll('%A', shortdays[d.getDay()], ds);
// Month
ds = replaceAll('%b', d.toLocaleDateString('nu', {
month: 'short'
}), ds);
ds = replaceAll('%B', d.toLocaleDateString('nu', {
month: 'long'
}), ds);
// Date & Time
ds = replaceAll('%c', d.toLocaleDateString('nu', {
weekday: 'short',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
year: 'numeric'
}), ds);
// Century
ds = replaceAll('%C', parseInt(String(d.getFullYear()).substring(0, 2)) + 1, ds);
// Day of month
ds = replaceAll('%d', d.getDate(), ds);
// Month/Day/Year
ds = replaceAll('%D', d.getMonth() + '/' + d.getDate() + '/' + String(d.getFullYear()).substring(2), ds);
// (space)Day
ds = replaceAll('%e', ' ' + d.getDate(), ds);
// Full year-Month-Day
ds = replaceAll('%F', d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate(), ds);
// TODO: this, the ISO Week number (%g)
ds = replaceAll('%g', '', ds);
// Last two digits of year of ISO week number
ds = replaceAll('%G', String(d.getFullYear()).substring(2), ds);
// Same as %b
ds = replaceAll('%h', d.toLocaleDateString('nu', {
month: 'short'
}), ds);
// Hour
ds = replaceAll('%H', String(d.getHours()), ds);
// 12-Hour
var dhours = d.getHours() > 12 ? d.getHours() - 12 : d.getHours();
ds = replaceAll('%I', String(dhours), ds);
// TODO: this, the day number relative to the year (001...366, 366 for a leap year)
ds = replaceAll('%j', '', ds);
// Hour
ds = replaceAll('%k', String(d.getHours()), ds);
// 12-Hour
ds = replaceAll('%l', dhours < 10 ? '0' + String(dhours) : String(dhours), ds);
// month
ds = replaceAll('%m', d.getMonth(), ds);
// minute
ds = replaceAll('%M', d.getMinutes(), ds);
ds = replaceAll('%n', '\n', ds);
// TODO: this stuff
ds = replaceAll('%N', '', ds);
// AM or PM
ds = replaceAll('%p', d.getHours() < 13 ? 'AM' : 'PM', ds);
// TODO: this stuff
ds = replaceAll('%P', '', ds);
// 12:59:59 PM
ds = replaceAll('%r', d.toLocaleTimeString('nu', { hour12: true }), ds);
ds = replaceAll('%R', String(d.getHours()) + ':' + d.getMinutes(), ds);
// TODO: this, the number of seconds since Jan 1, 1990
ds = replaceAll('%s', '', ds);
// Seconds (0-60)
ds = replaceAll('%S', d.getSeconds(), ds);
// Tab
ds = replaceAll('%t', '\t', ds);
// Same as %H:%M:%S
ds = replaceAll('%T', d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds(), ds);
// Number of the day of the week
ds = replaceAll('%u', d.getDay() === 0 ? 7 : d.getDay(), ds);
// TODO: this, the week number of the year
ds = replaceAll('%U', '', ds);
// TODO: this, the ISO week number of the year
ds = replaceAll('%V', '', ds);
// 0-indexed number of the day of the week
ds = replaceAll('%w', d.getDay(), ds);
// TODO: this, the monday-first week number of the year
ds = replaceAll('%W', '', ds);
ds = replaceAll('%x', d.toLocaleDateString('nu'), ds);
var tt = d.toLocaleTimeString('nu');
tt = tt.substring(0, tt.length-3);
ds = replaceAll('%X', tt, ds);
ds = replaceAll('%y', String(d.getFullYear()).substring(2), ds);
ds = replaceAll('%Y', d.getFullYear(), ds);
// TODO: this stuff, time zones
ds = replaceAll('%z', '', ds);
ds = replaceAll('%:z', '', ds);
ds = replaceAll('%::z', '', ds);
ds = replaceAll('%:::z', '', ds);
ds = replaceAll('%Z', '', ds);
env.stdio.writeLine(ds);
cb(0);
} else {
env.stdio.writeError(new Error('Format string must begin with +'));
return cb(1);
}
}
});
}