Skip to content

Commit

Permalink
♻️ Replace moment with dayjs
Browse files Browse the repository at this point in the history
- Dayjs is lighter than moment and produces same results for what we need.
- Dayjs is recommended by moment for newer projects
  • Loading branch information
abhishekdev committed Apr 7, 2021
1 parent 9782385 commit cdb6981
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import path from 'path';
import moment from 'moment';
import dayjs from 'dayjs';
import semver from 'semver';
import username from 'username';
import readPkgUp from 'read-pkg-up';
Expand All @@ -20,7 +20,7 @@ const semverString = (str) => {
// e.g. 2017-Jan-01, 15:00:01.100 => "20170130T1500Z"
const semverMoment = () => {
// Get ISO date after ignoring the millisecond precision
const isoDate = moment().milliseconds(0).toISOString();
const isoDate = dayjs().millisecond(0).toISOString();

// Replace millisecond information as the 'dot' notation for it is incompatible with semver
return semverString(isoDate.replace(REGEX_ISODATE_MILLISECONDS, ''));
Expand Down
20 changes: 17 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import fs from 'fs';
import test from 'ava';
import semver from 'semver';
import moment from 'moment';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import customParseFormat from 'dayjs/plugin/customParseFormat';

import buildRevision from '../index';

dayjs.extend(utc);
dayjs.extend(customParseFormat);

// Expect ISO 8601 compatible format which has no separators for semver compatibility
const TIMESTAMP_FORMAT = 'YYYYMMDD[T]HHmmss[Z]';

test('build revision is semver compatible', async (t) => {
const option = { cwd: 'test/fixture/package-valid/target' };

Expand Down Expand Up @@ -69,7 +77,10 @@ test('generates ISO 8601 UTC timestamp for dirty builds', async (t) => {

const revision = await buildRevision(option);
const timestamp = revision.split('.').pop();
t.true(moment(timestamp).isValid(), 'could not parse a valid timestamp');
t.true(
dayjs.utc(timestamp, TIMESTAMP_FORMAT).isValid(),
'could not parse a valid timestamp'
);

// Test Teardown: Reset repository
fs.unlink('test/fixture/package-valid/change.txt', (err) => {
Expand Down Expand Up @@ -97,7 +108,10 @@ test.serial('works when package is not a git repo', async (t) => {
semver.valid(revision) && revision.includes(`+${PREFIX}`),
'could not parse default buildinfo prefix for un-versioned repo'
);
t.true(moment(timestamp).isValid(), 'could not parse a valid timestamp');
t.true(
dayjs.utc(timestamp, TIMESTAMP_FORMAT).isValid(),
'could not parse a valid timestamp'
);

// Test Teardown: Re-instate .git directory
if (fs.existsSync('backup.git')) {
Expand Down

0 comments on commit cdb6981

Please sign in to comment.