Skip to content

Commit 751ab5c

Browse files
committed
Refactor grefplus to use ES modules and update alias paths
1 parent 49f8190 commit 751ab5c

File tree

3 files changed

+67
-44
lines changed

3 files changed

+67
-44
lines changed

src/alias.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ alias ll='ls -ltrh'
1111
## tools
1212
alias resource='source $HOME/.bashrc'
1313
alias clean='node $DEVROOT/dev-tools/src/clean/index.js'
14-
alias grefp='node $DEVROOT/dev-tools/src/grefplus'
14+
alias grefp='node $DEVROOT/dev-tools/src/grefplus/index.mjs'
1515
alias branches='node $DEVROOT/dev-tools/src/branches'
1616
alias nt='node $DEVROOT/dev-tools/src/node-tools'
1717
alias yn='node $DEVROOT/dev-tools/src/yarn'

src/grefplus/cmdline.js renamed to src/grefplus/cmdline.mjs

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { accessSync, constants, lstatSync } = require('fs');
2-
const yargs = require('yargs');
3-
const { DateTime } = require('luxon');
1+
import { accessSync, constants, lstatSync } from 'node:fs';
2+
import yargs from 'yargs/yargs';
3+
import { DateTime } from 'luxon';
44

55
const options = {
66
dateOptions: 'yyyy-MM-dd hh:mm:ss a'
@@ -32,7 +32,7 @@ const cmdKeys = {
3232
alias: 'r'
3333
, describe: 'root folder of development environment (/c/blah/blah). Default is DEVROOT'
3434
, type: 'array'
35-
// eslint-disable-next-line no-process-env
35+
3636
, default: process.env.DEVROOT
3737
}
3838
, 'date': {
@@ -46,13 +46,14 @@ const cmdKeys = {
4646
/**
4747
* Validates from and to dates,
4848
*
49-
* @param {String} checkDate
49+
* @param {string} checkDate
50+
* @param {string} msg
5051
* @throws if date is not valid
5152
* @private
5253
*/
5354
const validateDate = (checkDate, msg) => {
5455
try {
55-
const parsed = DateTime.fromFormat(checkDate, options.allowedFormat);
56+
const parsed = DateTime.fromFormat(checkDate.toString(), options.allowedFormat);
5657
if(!parsed.isValid) {
5758
throw new Error('unknown format');
5859
}
@@ -69,11 +70,11 @@ const validateDate = (checkDate, msg) => {
6970
/**
7071
* Aborts build if dev root path does not exist
7172
*
72-
* @param {String} devRoot
73+
* @param {string[]} devRoot
7374
* @throws if path not accessible
7475
* @private
7576
*/
76-
const validatePath = ({ devRoot }) => {
77+
const validatePath = (devRoot) => {
7778
let problematicRoot = null;
7879

7980
devRoot.forEach(root => {
@@ -111,21 +112,23 @@ const isFuture = (checkDate) => {
111112
/**
112113
* Parse command line and configures options
113114
*
114-
* @param {Boolean} test - used for testing only
115+
* @param {Boolean} [test] - used for testing only
115116
*/
116117
const setOptions = (test) => {
117-
const argv = test || yargs
118+
const argv = test || yargs(process.argv.slice(2))
119+
// @ts-ignore
118120
.options(cmdKeys)
119121
.version(false)
120122
.help(true)
121123
.strict(true)
122124
.check((_argv) => {
123125
// super secret shortcut
124126
if(!_argv.date && !_argv.fromDate && !_argv.toDate && Number.isInteger(_argv._[0])) {
127+
const days = Number(_argv._[0]);
125128
const _date = DateTime
126-
.fromFormat(_argv._[0], options.allowedFormat)
127-
.plus({ days: _argv._[0] })
128-
.format('MM/DD/YY');
129+
.fromFormat(days.toString(), options.allowedFormat)
130+
.plus({ days })
131+
.toFormat('MM/DD/YY');
129132
_argv.date = _date;
130133
}
131134
else {
@@ -138,53 +141,66 @@ const setOptions = (test) => {
138141
}
139142
return true;
140143
})
141-
.check(({ date }) => {
142-
if(date) {
143-
validateDate(date, '--date');
144-
if(isFuture(date)) {
144+
.check((argv) => {
145+
/** @type {string} */
146+
const dt = `${argv.date}` || '';
147+
if(dt) {
148+
validateDate(dt, '--date');
149+
if(isFuture(dt)) {
145150
throw new Error('--date cannot exceed current date');
146151
}
147152
}
148153
return true;
149154
})
150-
.check(({ fromDate }) => {
151-
if(fromDate) {
152-
validateDate(fromDate, '--from-date');
153-
if(isFuture(fromDate)) {
155+
.check((argv) => {
156+
/** @type {string} */
157+
const dt = `${argv.fromDate}` || '';
158+
if(dt) {
159+
validateDate(dt, '--from-date');
160+
if(isFuture(dt)) {
154161
throw new Error('--from-date cannot exceed current date');
155162
}
156163
}
157164
return true;
158165
})
159-
.check(({ toDate }) => {
160-
if(toDate) {
161-
validateDate(toDate, '--to-date');
166+
.check((argv) => {
167+
/** @type {string} */
168+
const dt = `${argv.toDate}` || '';
169+
if(dt) {
170+
validateDate(dt, '--to-date');
162171
}
163172
return true;
164173
})
165-
.check(({ folderNames }) => {
174+
.check((argv) => {
175+
const folderNames = Array.isArray(argv.folderNames) ? argv.folderNames : [];
166176
if(folderNames && !folderNames.length) {
167177
throw new Error('--folder-names requires at least one name');
168178
}
169179
return true;
170180
})
171-
.check(validatePath)
181+
.check(argv => {
182+
const devRoot = `${argv.devRoot}`;
183+
return validatePath(devRoot);
184+
})
172185
.argv;
173186

187+
// @ts-ignore
174188
if(argv.date) {
189+
// @ts-ignore
175190
const date = DateTime.fromFormat(argv.date, options.allowedFormat);
176-
module.exports.options.fromDate = date.startOf('day');
177-
module.exports.options.toDate = date.endOf('day');
191+
options.fromDate = date.startOf('day');
192+
options.toDate = date.endOf('day');
178193
}
179194
else {
180-
module.exports.options.fromDate = argv.fromDate ? DateTime.fromFormat(argv.fromDate, options.allowedFormat).startOf('day') : null;
181-
module.exports.options.toDate = argv.toDate ? DateTime.fromFormat(argv.toDate, options.allowedFormat).endOf('day') : null;
195+
// @ts-ignore
196+
options.fromDate = argv.fromDate ? DateTime.fromFormat(argv.fromDate, options.allowedFormat).startOf('day') : null;
197+
// @ts-ignore
198+
options.toDate = argv.toDate ? DateTime.fromFormat(argv.toDate, options.allowedFormat).endOf('day') : null;
182199
}
183-
module.exports.options.devRoot = argv.devRoot;
184-
module.exports.options.folderNames = argv.folderNames || [];
200+
// @ts-ignore
201+
options.devRoot = argv.devRoot;
202+
// @ts-ignore
203+
options.folderNames = argv.folderNames || [];
185204
};
186205

187-
module.exports = {
188-
options
189-
, setOptions
190-
};
206+
export { options, setOptions };

src/grefplus/index.js renamed to src/grefplus/index.mjs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
/* eslint no-console:off */
2-
require('./cmdline').setOptions();
3-
const { basename } = require('path');
4-
const { allRepoPaths } = require('../common/repos');
5-
const { promisify } = require('util');
6-
const _exec = promisify(require('child_process').exec);
7-
const { DateTime } = require('luxon');
8-
const { options } = require('./cmdline');
2+
3+
import { setOptions, options } from './cmdline.mjs';
4+
import { basename } from 'node:path';
5+
import { promisify } from 'node:util';
6+
import { exec } from 'node:child_process';
7+
const _exec = promisify(exec);
8+
9+
import { DateTime } from 'luxon';
10+
11+
import repos from '../common/repos.js';
12+
const { allRepoPaths } = repos;
13+
914
const DateLength = 6;
1015

16+
setOptions();
17+
1118
/**
1219
* Creates command string
1320
* @param {String} repo - repository base name

0 commit comments

Comments
 (0)