Skip to content

Commit

Permalink
Added optional chaining to potentially empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlui committed Mar 11, 2024
1 parent 08eb598 commit 82b461c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions minify.js/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function minify(input, options = {}) {
} else { // dir path passed
return findJS(input, { recursive: options.recursive,
dotFolders: options.dotFolders, dotFiles: options.dotFiles })
.map(jsPath => { // minify found JS files
?.map(jsPath => { // minify found JS files
if (options.verbose) console.info(`Minifying ${ jsPath }...`);
const srcCode = fs.readFileSync(jsPath, 'utf8'),
minifyResult = uglifyJS.minify(srcCode, minifyOptions);
Expand Down Expand Up @@ -142,14 +142,14 @@ else { // run as CLI tool

// Build array of minification data
const failedPaths = [];
const minifyData = unminnedJSfiles.map(jsPath => {
const minifyData = unminnedJSfiles?.map(jsPath => {
const minifyResult = minify(jsPath, { verbose: !config.quietMode, mangle: !config.noMangle });
if (minifyResult.error) failedPaths.push(jsPath);
return minifyResult;
}).filter(minifyResult => !minifyResult.error); // filter out failed minifications

// Write array data to files
minifyData.forEach(({ code, srcPath }) => {
minifyData?.forEach(({ code, srcPath }) => {
const outputDir = path.join(
path.dirname(srcPath), // path of file to be minified
/so?u?rce?$/.test(path.dirname(srcPath)) ? '../min' // + ../min/ if in *(src|source)/
Expand All @@ -167,7 +167,7 @@ else { // run as CLI tool
});

// Print final summary
if (minifyData.length > 0) {
if (minifyData?.length > 0) {
printIfNotQuiet(`\n${bg}Minification complete!${nc}`);
printIfNotQuiet(
`${ minifyData.length } file${ minifyData.length > 1 ? 's' : '' } minified.`);
Expand Down
10 changes: 5 additions & 5 deletions scss-to-css/scss-to-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function compile(inputPath, options = {}) {
} catch (err) { console.error(`\nERROR: ${ err.message }\n`); return { error: err }; }
} else { // dir path passed
return findSCSS(inputPath, { recursive: options.recursive, dotFolders: options.dotFolders })
.map(scssPath => { // compile found SCSS files
?.map(scssPath => { // compile found SCSS files
if (options.verbose) console.info(`Compiling ${ scssPath }...`);
try { // to compile found file
const compileResult = sass.compile(scssPath, compileOptions);
Expand Down Expand Up @@ -129,22 +129,22 @@ else { // run as CLI tool

if (config.dryRun) { // print files to be processed
console.info(`\n${by}SCSS files to be compiled:${nc}`);
scssFiles.forEach(file => console.info(file));
scssFiles?.forEach(file => console.info(file));

} else if (scssFiles?.length > 0) { // actually compile SCSS files
printIfNotQuiet(''); // line break before first log

// Build array of compilation data
const failedPaths = [];
const compileData = scssFiles.map(scssPath => {
const compileData = scssFiles?.map(scssPath => {
const compileResult = compile(scssPath, {
minify: !config.noMinify, sourceMaps: !config.noSourceMaps, verbose: !config.quietMode });
if (compileResult.error) failedPaths.push(scssPath);
return compileResult;
}).filter(data => !data.error ); // filter out failed compilations

// Write array data to files
compileData.forEach(({ code, srcMap, srcPath }) => {
compileData?.forEach(({ code, srcMap, srcPath }) => {
const outputDir = path.join(
path.dirname(srcPath), // path of file to be minified
/(?:src|s[ac]ss)$/.test(path.dirname(srcPath)) ? '../css' // + ../css/ if in *(src|sass|scss)/
Expand All @@ -163,7 +163,7 @@ else { // run as CLI tool
});

// Print final summary
if (compileData.length > 0) {
if (compileData?.length > 0) {
const cssCntSuffix = compileData.length > 1 ? 's' : '';
printIfNotQuiet(`\n${bg}Compilation complete!${nc}`);
printIfNotQuiet(`${ compileData.length } CSS file${ cssCntSuffix }`
Expand Down

0 comments on commit 82b461c

Please sign in to comment.