Skip to content

Commit 794d233

Browse files
committed
Add ESLint configuration and refactor grefplus to use ES modules
1 parent 49f8190 commit 794d233

File tree

6 files changed

+168
-140
lines changed

6 files changed

+168
-140
lines changed

.eslintrc.js

Lines changed: 0 additions & 96 deletions
This file was deleted.

.eslintrc.json

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"parserOptions": {
3+
"sourceType": "script",
4+
"ecmaFeatures": {
5+
"impliedStrict": true
6+
},
7+
"ecmaVersion": 2020
8+
},
9+
"extends": [ "eslint:recommended", "plugin:node/recommended" ],
10+
"env": {
11+
"node": true,
12+
"mocha": true,
13+
"es2020": true
14+
},
15+
"rules": {
16+
"array-bracket-spacing": [ "error", "always" ],
17+
"arrow-spacing": [ "error", { "before": true, "after": true } ],
18+
"block-scoped-var": "error",
19+
"brace-style": [ "error", "stroustrup" ],
20+
"camelcase": "error",
21+
"comma-spacing": [ "error", { "after": true } ],
22+
"comma-style": [ "error", "first" ],
23+
"curly": [ "warn" ],
24+
"default-case": "error",
25+
"dot-location": [ "error", "property" ],
26+
"dot-notation": [ "error" ],
27+
"eqeqeq": [ "error", "smart" ],
28+
"func-call-spacing": [ "error", "never" ],
29+
"handle-callback-err": "error",
30+
"indent": [ "error", "IndentSpaces", { "MemberExpression": 1, "ArrayExpression": 1, "ObjectExpression": 1 } ],
31+
"keyword-spacing": [ "error", { "overrides": { "if": { "after": false }, "for": { "after": false }, "while": { "after": false } } } ],
32+
"line-comment-position": [ "error", { "position": "above" } ],
33+
"newline-per-chained-call": [ "error", { "ignoreChainWithDepth": 2 } ],
34+
"no-alert": [ "error" ],
35+
"no-caller": "error",
36+
"no-console": "warn",
37+
"no-div-regex": "error",
38+
"no-duplicate-imports": [ "error", { "includeExports": true } ],
39+
"no-else-return": [ "error", { "allowElseIf": false } ],
40+
"no-empty-function": "error",
41+
"no-eq-null": "error",
42+
"no-floating-decimal": "error",
43+
"no-implicit-coercion": [ "error" ],
44+
"no-irregular-whitespace": [ "error", { "skipComments": true } ],
45+
"no-lone-blocks": [ "error" ],
46+
"no-lonely-if": "error",
47+
"no-magic-numbers": [ "error", { "ignore": [ 2, 1, 0, -1 ], "ignoreArrayIndexes": true } ],
48+
"no-multi-spaces": "error",
49+
"no-param-reassign": [ "error", { "props": false } ],
50+
"no-path-concat": "error",
51+
"no-return-await": "error",
52+
"no-script-url": "error",
53+
"no-self-compare": [ "error" ],
54+
"no-tabs": "error",
55+
"no-trailing-spaces": "error",
56+
"no-unneeded-ternary": [ "error" ],
57+
"no-unsafe-negation": [ "error" ],
58+
"no-unused-vars": [ "error", { "args": "all", "caughtErrors": "all" } ],
59+
"no-useless-return": "error",
60+
"no-var": "error",
61+
"no-whitespace-before-property": "error",
62+
"object-curly-spacing": [ "error", "always" ],
63+
"one-var": [ "error", { "var": "always", "let": "never", "const": "never" } ],
64+
"prefer-const": "error",
65+
"prefer-destructuring": [
66+
"error",
67+
{
68+
"VariableDeclarator": {
69+
"array": false,
70+
"object": true
71+
},
72+
"AssignmentExpression": {
73+
"array": true,
74+
"object": false
75+
}
76+
},
77+
{
78+
"enforceForRenamedProperties": false
79+
}
80+
],
81+
"prefer-template": "error",
82+
"quotes": [ "error", "single", { "allowTemplateLiterals": true } ],
83+
"semi": [ "error", "always", { "omitLastInOneLineBlock": true } ],
84+
"space-before-blocks": [ "error", "always" ],
85+
"space-in-parens": [ "error", "never" ],
86+
"space-infix-ops": [ "error" ],
87+
"space-unary-ops": [ "error", { "words": true, "nonwords": false } ],
88+
"spaced-comment": [ "error", "always" ],
89+
"yoda": "error",
90+
"node/callback-return": "warn",
91+
"node/global-require": "warn",
92+
"node/no-deprecated-api": "warn",
93+
"node/no-unpublished-require": "off"
94+
}
95+
}

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/.eslintrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../../.eslintrc.json",
3+
"parserOptions": {
4+
"sourceType": "module"
5+
}
6+
}

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 };

0 commit comments

Comments
 (0)