diff --git a/package-lock.json b/package-lock.json index 127bdca..acbc4d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,6 @@ "email-validator": "^2.0.4", "gradle-to-js": "2.0.1", "jsonwebtoken": "^9.0.2", - "moment": "^2.29.4", "opener": "^1.5.2", "parse-duration": "^2.1.6", "plist": "^3.1.0", @@ -2569,15 +2568,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/moment": { - "version": "2.31.0", - "resolved": "https://registry.npmjs.org/moment/-/moment-2.31.0.tgz", - "integrity": "sha512-0acOTfMiWOheYS4eoWb80yYMb/JLvVv9SHbs2PehaDzfUG0Bw855SKyk0IKTnPGa5+U2bmi3W68l1+sGLX/pvw==", - "license": "MIT", - "engines": { - "node": "*" - } - }, "node_modules/mute-stream": { "version": "0.0.8", "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz", diff --git a/package.json b/package.json index eb9e0e7..0c04468 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "email-validator": "^2.0.4", "gradle-to-js": "2.0.1", "jsonwebtoken": "^9.0.2", - "moment": "^2.29.4", "opener": "^1.5.2", "parse-duration": "^2.1.6", "plist": "^3.1.0", diff --git a/script/command-executor.ts b/script/command-executor.ts index 27d9cdd..181fd5e 100644 --- a/script/command-executor.ts +++ b/script/command-executor.ts @@ -7,7 +7,6 @@ const childProcess = require("child_process"); import debugCommand from "./commands/debug"; import * as fs from "fs"; import * as chalk from "chalk"; -import * as moment from "moment"; import * as os from "os"; import * as path from "path"; import * as semver from "semver"; @@ -153,7 +152,7 @@ async function accessKeyPatch(command: cli.IAccessKeyPatchCommand): Promise { deleteConnectionInfoCache(); } -function formatDate(unixOffset: number): string { - const date: moment.Moment = moment(unixOffset); - const now: moment.Moment = moment(); - if (Math.abs(now.diff(date, "days")) < 30) { - return date.fromNow(); // "2 hours ago" - } else if (now.year() === date.year()) { - return date.format("MMM D"); // "Nov 6" - } else { - return date.format("MMM D, YYYY"); // "Nov 6, 2014" - } -} - function printAppList(format: string, apps: App[]): void { if (format === "json") { printJson(apps); @@ -952,6 +939,31 @@ async function getAppVersionFromXcodeProject( return { appVersion: marketingVersion, buildNumber: String(currentProjectVersion) }; } +const relativeTimeFormat = new Intl.RelativeTimeFormat("en-US", { numeric: "auto" }); +const RELATIVE_TIME_UNITS: Array<[unit: Intl.RelativeTimeFormatUnit, ms: number]> = [ + ["day", 24 * 60 * 60 * 1000], + ["hour", 60 * 60 * 1000], + ["minute", 60 * 1000], + ["second", 1000], +]; + +// "2 hours ago" within 30 days, otherwise "Nov 6" in the current year and "Nov 6, 2014" before it +function formatDate(unixOffset: number): string { + if (!Number.isFinite(unixOffset)) { + return ""; + } + + const msFromNow: number = unixOffset - Date.now(); + if (Math.abs(msFromNow) < 30 * RELATIVE_TIME_UNITS[0][1]) { + const [unit, ms] = RELATIVE_TIME_UNITS.find(([, ms]) => Math.abs(msFromNow) >= ms) ?? ["second", 1000]; + return relativeTimeFormat.format(Math.round(msFromNow / ms), unit); + } + + const date = new Date(unixOffset); + const year = date.getFullYear() === new Date().getFullYear() ? undefined : "numeric"; + return date.toLocaleDateString("en-US", { month: "short", day: "numeric", year }); +} + function printJson(object: any): void { log(JSON.stringify(object, /*replacer=*/ null, /*spacing=*/ 2)); } diff --git a/script/commands/debug.ts b/script/commands/debug.ts index 4c556c1..a8880a5 100644 --- a/script/commands/debug.ts +++ b/script/commands/debug.ts @@ -3,7 +3,6 @@ import * as childProcess from "child_process"; import * as cli from "../../script/types/cli"; -import * as moment from "moment"; import * as path from "path"; const simctl = require("simctl"); @@ -111,7 +110,7 @@ function processLogData(logData: Buffer) { // log message prefix that is added to each entry. const message = line.substring(line.indexOf(logMessagePrefix) + logMessagePrefix.length); - const timeStamp = moment().format("hh:mm:ss"); + const timeStamp = new Date().toLocaleTimeString("en-US", { hourCycle: "h23" }); return `[${timeStamp}] ${message}`; }) .forEach((line: string) => console.log(line)); diff --git a/test/cli.ts b/test/cli.ts index aa8b636..026eed2 100644 --- a/test/cli.ts +++ b/test/cli.ts @@ -156,7 +156,7 @@ describe("CLI", () => { await execute(command); assertLoggedOnce( - 'Successfully changed the expiration date of the "Test name" access key to Wednesday, August 17, 2016 12:07 PM.' + 'Successfully changed the expiration date of the "Test name" access key to Wednesday, August 17, 2016 at 12:07 PM.' ); }); @@ -171,7 +171,7 @@ describe("CLI", () => { await execute(command); assertLoggedOnce( - 'Successfully renamed the access key "Test name" to "Updated name" and changed its expiration date to Wednesday, August 17, 2016 12:07 PM.' + 'Successfully renamed the access key "Test name" to "Updated name" and changed its expiration date to Wednesday, August 17, 2016 at 12:07 PM.' ); });