Skip to content

Commit

Permalink
chore: minor updates, date from now, current version, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
jakowenko committed Feb 26, 2024
1 parent 969478b commit 37c2edf
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 21 deletions.
13 changes: 13 additions & 0 deletions packages/lxp-package-helper/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/lxp-package-helper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@twentyfourg/cloud-sdk": "^2.9.1",
"axios": "^1.6.7",
"chalk": "^5.3.0",
"date-fns": "^3.3.1",
"enquirer": "^2.4.1"
}
}
47 changes: 41 additions & 6 deletions packages/lxp-package-helper/src/Prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const log = console;

class Prompt {
constructor() {
this.currentVersion = '';
this.packageName = 'lxp-base';
this.continuePagination = false;
this.matches = [];
Expand All @@ -25,8 +26,38 @@ class Prompt {
else if (!process.env.LXP_PACKAGE_HELPER_URL)
throw new Error('LXP_PACKAGE_HELPER_URL is required');

const packageJsonPath = `${process.cwd()}/package.json`;
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
const packageVersion = packageJson.dependencies[this.packageName] || '';
const matches = packageVersion.match(new RegExp(`${this.packageName}-([\\d.]+?)(?=\\.tgz)`));
if (matches) {
// eslint-disable-next-line prefer-destructuring
this.currentVersion = `v${matches[1]}`;
log.info(chalk.bold(`${chalk.green.bold('✔')} Current version: v${matches[1]}`));
} else {
log.info(`${chalk.red.bold('✖')} ${chalk.bold('Current version: N/A')}`);
}

const version = await this.promptVersion();
this.matches = (await this.fetchAllReleases()).filter((obj) => obj.name.includes(version));
this.matches.sort((a, b) => {
// Split version numbers into parts and convert them to integers
const partsA = a.tag_name.slice(1).split('.').map(Number);
const partsB = b.tag_name.slice(1).split('.').map(Number);

// Compare version number parts
for (let i = 0; i < Math.max(partsA.length, partsB.length); i++) {
if ((partsA[i] || 0) > (partsB[i] || 0)) return -1;
if ((partsA[i] || 0) < (partsB[i] || 0)) return 1;
}

// If tag_name is identical, compare published_at
if (a.published_at > b.published_at) return -1;
if (a.published_at < b.published_at) return 1;

return 0;
});

await this.selectVersion();
await this.downloadPackage();

Expand Down Expand Up @@ -93,8 +124,7 @@ class Prompt {

updatePackageJsonDependency() {
const packageJsonPath = `${process.cwd()}/package.json`;
const packageJsonText = fs.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonText);
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson.dependencies[this.packageName] = `file:${this.selection.package.name}`;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));

Expand Down Expand Up @@ -143,9 +173,13 @@ class Prompt {
type: 'select',
name: 'version',
message: 'Select a version',
choices: this.matches.slice(0, 10).map((obj) => ({
choices: this.matches.slice(0, 15).map((obj) => ({
name: obj.tag_name,
message: obj.tag_name + chalk.gray(` @ ${formatDateTime(obj.published_at)}`),
message:
(obj.tag_name === this.currentVersion ? `${chalk.cyan(obj.tag_name)}` : obj.tag_name) +
chalk.gray(
`: ${formatDateTime.ago(obj.published_at)} @ ${formatDateTime.format(obj.published_at)}`
),
})),
});
this.selection = this.matches.find((obj) => obj.tag_name === version);
Expand Down Expand Up @@ -204,14 +238,15 @@ class Prompt {
: null,
};
})
.filter((release) => release.package);
.filter((release) => release.package)
.sort((a, b) => b.tag_name - a.tag_name);
}

async promptVersion() {
const { version } = await prompt({
type: 'input',
name: 'version',
message: 'Package version',
message: 'Search for package version',
});
if (!version) {
log.warn(`${chalk.red.bold('✖')} ${chalk.bold('Version is required')}`);
Expand Down
38 changes: 23 additions & 15 deletions packages/lxp-package-helper/src/util/format.time.util.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
export default (dateString) => {
const date = new Date(dateString);
import { parseISO, formatDistanceToNow } from 'date-fns';

const pad = (num) => (num < 10 ? `0${num}` : num);
export default {
format: (dateString) => {
const date = new Date(dateString);

const year = date.getFullYear();
const month = pad(date.getMonth() + 1); // getMonth() is zero-indexed
const day = pad(date.getDate());
let hours = date.getHours();
const minutes = pad(date.getMinutes());
const seconds = pad(date.getSeconds());
const ampm = hours >= 12 ? 'PM' : 'AM';
const pad = (num) => (num < 10 ? `0${num}` : num);

// Convert hours to 12-hour format
hours %= 12;
hours = hours ? pad(hours) : '12'; // the hour '0' should be '12'
const year = date.getFullYear().toString().slice(-2);
const month = pad(date.getMonth() + 1); // getMonth() is zero-indexed
const day = pad(date.getDate());
let hours = date.getHours();
const minutes = pad(date.getMinutes());
const seconds = pad(date.getSeconds());
const ampm = hours >= 12 ? 'PM' : 'AM';

// Format date and time
return `${month}/${day}/${year} ${hours}:${minutes}:${seconds} ${ampm}`;
// Convert hours to 12-hour format
hours %= 12;
hours = hours ? pad(hours) : '12'; // the hour '0' should be '12'

// Format date and time
return `${month}/${day}/${year} ${hours}:${minutes}:${seconds} ${ampm}`;
},
ago: (ISO) => {
const date = parseISO(ISO);
return formatDistanceToNow(date, { addSuffix: true, includeSeconds: true });
},
};

0 comments on commit 37c2edf

Please sign in to comment.