Skip to content

Commit 6467cfa

Browse files
committed
Added doc URL to all invalid arg errors
1 parent cd14c1e commit 6467cfa

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

scss-to-css/src/scss-to-css.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,35 @@ const fs = require('fs'),
99
path = require('path'),
1010
sass = require('sass');
1111

12-
// Define MAIN functions
12+
// Define API functions
1313

1414
function findSCSS(searchDir, options = {}) {
1515

16-
const exampleCall = 'findSCSS(\'assets/scss\', { verbose: false, dotFolders: true })';
16+
const docURL = 'https://github.com/adamlui/js-utils/tree/main/scss-to-css#findscsssearchdir-options',
17+
exampleCall = 'findSCSS(\'assets/scss\', { verbose: false, dotFolders: true })';
18+
1719
const defaultOptions = {
1820
recursive: true, // recursively search for nested files in searchDir passed
1921
verbose: true, // enable logging
2022
dotFolders: false // include dotfolders in file search
2123
};
2224

2325
// Validate searchDir
24-
if (typeof searchDir !== 'string')
25-
return console.error('findSCSS() » ERROR: 1st arg <searchDir> must be a string.');
26-
else { // verify searchDir path existence
26+
if (typeof searchDir !== 'string') {
27+
console.error('findSCSS() » ERROR: 1st arg <searchDir> must be a string.');
28+
console.info('findSCSS() » For more help, please visit ' + docURL);
29+
return;
30+
} else { // verify searchDir path existence
2731
const searchPath = path.resolve(process.cwd(), searchDir);
2832
if (!fs.existsSync(searchPath)) {
29-
console.error('findSCSS() » ERROR: 1st arg <searchDir> must be an existing directory.');
30-
console.error(`findSCSS() » ${ searchPath } does not exist.`);
31-
return;
33+
console.error('findSCSS() » ERROR: 1st arg <searchDir> must be an existing directory.');
34+
console.error(`findSCSS() » ${ searchPath } does not exist.`);
35+
console.info('findSCSS() » For more help, please visit ' + docURL);
36+
return;
3237
}}
3338

3439
// Validate/init options
35-
if (!validateOptions(options, defaultOptions, exampleCall)) return;
40+
if (!validateOptions(options, defaultOptions, docURL, exampleCall)) return;
3641
options = { ...defaultOptions, ...options }; // merge validated options w/ missing default ones
3742

3843
// Search for SCSS
@@ -62,7 +67,9 @@ function findSCSS(searchDir, options = {}) {
6267

6368
function compile(inputPath, options = {}) {
6469

65-
const exampleCall = 'compile(\'assets/scss\', { recursive: false, minify: false })';
70+
const docURL = 'https://github.com/adamlui/js-utils/tree/main/scss-to-css#compileinputpath-options',
71+
exampleCall = 'compile(\'assets/scss\', { recursive: false, minify: false })';
72+
6673
const defaultOptions = {
6774
recursive: true, // recursively search for nested files if dir path passed
6875
verbose: true, // enable logging
@@ -72,18 +79,22 @@ function compile(inputPath, options = {}) {
7279
};
7380

7481
// Validate inputPath
75-
if (typeof inputPath !== 'string')
76-
return console.error('compile() » ERROR: 1st arg <inputPath> must be a string.');
82+
if (typeof inputPath !== 'string') {
83+
console.error('compile() » ERROR: 1st arg <inputPath> must be a string.');
84+
console.info('compile() » For more help, please visit ' + docURL);
85+
return;
86+
}
7787
else { // verify inputPath path existence
7888
inputPath = path.resolve(process.cwd(), inputPath);
7989
if (!fs.existsSync(inputPath)) {
8090
console.error('compile() » ERROR: 1st arg <inputPath> must be an existing directory or file.');
8191
console.error(`compile() » ${ inputPath } does not exist.`);
92+
console.info('compile() » For more help, please visit ' + docURL);
8293
return;
8394
}}
8495

8596
// Validate/init options
86-
if (!validateOptions(options, defaultOptions, exampleCall)) return;
97+
if (!validateOptions(options, defaultOptions, docURL, exampleCall)) return;
8798
options = { ...defaultOptions, ...options }; // merge validated options w/ missing default ones
8899

89100
// Compile SCSS based on inputPath
@@ -121,43 +132,51 @@ function compile(inputPath, options = {}) {
121132

122133
// Define INTERNAL validation function
123134

124-
function validateOptions(options, defaultOptions, exampleCall) {
125-
const logPrefix = ( validateOptions.caller?.name || 'validateOptions' ) + '() » ';
135+
function validateOptions(options, defaultOptions, docURL, exampleCall) {
136+
137+
// Init option strings/types
126138
const strDefaultOptions = JSON.stringify(defaultOptions, null, 2)
127139
.replace(/"([^"]+)":/g, '$1:') // strip quotes from keys
128140
.replace(/"/g, '\'') // replace double quotes w/ single quotes
129141
.replace(/\n\s*/g, ' '); // condense to single line
130142
const strValidOptions = Object.keys(defaultOptions).join(', '),
131143
booleanOptions = Object.keys(defaultOptions).filter(key => typeof defaultOptions[key] === 'boolean'),
132144
integerOptions = Object.keys(defaultOptions).filter(key => Number.isInteger(defaultOptions[key]));
145+
146+
// Define print functions
147+
const logPrefix = ( validateOptions.caller?.name || 'validateOptions' ) + '() » ';
133148
const printValidOptions = () => {
134149
console.info(`${ logPrefix }Valid options: [ ${ strValidOptions } ]`);
135150
console.info(`${ logPrefix }If omitted, default settings are: ${ strDefaultOptions }`);
136151
};
152+
const printDocURL = () => {
153+
console.info(`${ logPrefix }For more help, please visit ${docURL}`); };
154+
155+
// Validate options
137156
if (typeof options != 'object') { // validate as obj
138157
console.error(`${ logPrefix }ERROR: [options] can only be an object of key/values.`);
139158
console.info(`${ logPrefix }Example valid call: ${ exampleCall }`);
140-
printValidOptions(); return false;
159+
printValidOptions(); printDocURL(); return false;
141160
}
142161
for (const key in options) { // validate each key
143162
if (key != 'isRecursing' && !Object.prototype.hasOwnProperty.call(defaultOptions, key)) {
144163
console.error(
145164
`${ logPrefix }ERROR: \`${ key }\` is an invalid option.`);
146-
printValidOptions(); return false;
165+
printValidOptions(); printDocURL(); return false;
147166
} else if (booleanOptions.includes(key) && typeof options[key] !== 'boolean') {
148167
console.error(
149168
`${ logPrefix }ERROR: [${ key }] option can only be \`true\` or \`false\`.`);
150-
return false;
169+
printDocURL(); return false;
151170
} else if (integerOptions.includes(key)) {
152171
options[key] = parseInt(options[key], 10);
153172
if (isNaN(options[key]) || options[key] < 1) {
154173
console.error(`${ logPrefix }ERROR: [${ key }] option can only be an integer > 0.`);
155-
return false;
174+
printDocURL(); return false;
156175
}
157176
}
158177
}
159178
return true;
160179
}
161180

162-
// EXPORT main functions
181+
// EXPORT API functions
163182
module.exports = { compile, find: findSCSS, findSCSS };

0 commit comments

Comments
 (0)