Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit 69d5123

Browse files
authored
Add GitHub Actions testing, JSHint -> ESLint, rewrite tests in ESM
Add GitHub Actions testing, JSHint -> ESLint, rewrite tests in ESM Also: * switch to Node 20 * update dependencies Closes gh-16
1 parent 71ccc63 commit 69d5123

14 files changed

+2189
-1302
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
indent_style = tab
8+
end_of_line = lf
9+
charset = utf-8
10+
trim_trailing_whitespace = true
11+
insert_final_newline = true
12+
13+
[*.yml]
14+
indent_style = space
15+
indent_size = 2

.github/workflows/node.js.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Node
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches-ignore: ["dependabot/**"]
7+
8+
permissions:
9+
contents: read # to fetch code (actions/checkout)
10+
11+
env:
12+
NODE_VERSION: 20.x
13+
14+
jobs:
15+
build-and-test:
16+
runs-on: ubuntu-latest
17+
name: test
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
21+
22+
- name: Use Node.js ${{ env.NODE_VERSION }}
23+
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
24+
with:
25+
node-version: ${{ env.NODE_VERSION }}
26+
27+
- name: Cache
28+
uses: actions/cache@ab5e6d0c87105b4c9c2047343972218f562e4319 # v4.0.1
29+
with:
30+
path: ~/.npm
31+
key: ${{ runner.os }}-node-${{ env.NODE_VERSION }}-npm-lock-${{ hashFiles('**/package-lock.json') }}
32+
restore-keys: |
33+
${{ runner.os }}-node-${{ env.NODE_VERSION }}-npm-lock-
34+
35+
- name: Install dependencies
36+
run: npm install
37+
38+
- name: Run tests
39+
run: npm test

.jshintrc

-16
This file was deleted.

.nvmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10
1+
20

eslint.config.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import jqueryConfig from "eslint-config-jquery";
2+
import globals from "globals";
3+
4+
export default [
5+
{
6+
ignores: [
7+
"test/fixtures/**"
8+
]
9+
},
10+
11+
{
12+
languageOptions: {
13+
globals: {
14+
...globals.node
15+
}
16+
},
17+
rules: {
18+
...jqueryConfig.rules,
19+
strict: [ "error", "global" ],
20+
21+
// Too many errors
22+
"max-len": "off"
23+
}
24+
},
25+
26+
{
27+
files: [
28+
"test/*.js"
29+
],
30+
languageOptions: {
31+
globals: {
32+
...globals.node,
33+
...globals.mocha
34+
}
35+
},
36+
rules: {
37+
...jqueryConfig.rules,
38+
39+
// Chai `expect` API violates this rule
40+
"no-unused-expressions": "off",
41+
42+
// Too many errors
43+
"max-len": "off"
44+
}
45+
}
46+
];

lib/cache.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ caches = [];
55

66
cacheCron = function() {
77
var currentTime = Date.now();
8-
caches.forEach(function( cache ) {
8+
caches.forEach( function( cache ) {
99
var count = {
1010
cached: 0,
1111
deleted: 0
1212
};
1313

14-
cache.each(function( value, key ) {
14+
cache.each( function( _value, key ) {
1515
count.cached++;
1616
if ( cache.expires[ key ] < currentTime ) {
1717
cache.destroy( key );
1818
count.deleted++;
1919
}
20-
});
21-
});
20+
} );
21+
} );
2222
cacheCronTimeout = setTimeout( cacheCron, cacheExpiresTime );
2323
};
2424

lib/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}

lib/themeroller-image.js

+35-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var cache, imVersion,
22
async = require( "async" ),
33
Cache = require( "./cache" ),
4-
im = require( "gm" ).subClass({ imageMagick: true }),
4+
im = require( "gm" ).subClass( { imageMagick: true } ),
55
semver = require( "semver" ),
66
dimensionLimit = 3000,
77
namedColors = require( "./themeroller-colors" );
@@ -10,9 +10,9 @@ cache = new Cache( "Image Cache" );
1010

1111
function expandColor( color ) {
1212
if ( color.length === 3 && /^[0-9a-f]+$/i.test( color ) ) {
13-
return [ 0, 0, 1, 1, 2, 2 ].map(function( i ) {
13+
return [ 0, 0, 1, 1, 2, 2 ].map( function( i ) {
1414
return color[ i ];
15-
}).join( "" );
15+
} ).join( "" );
1616
}
1717
return color;
1818
}
@@ -28,11 +28,11 @@ function hashColor( color ) {
2828
function pick( obj ) {
2929
var copy = {};
3030
var keys = [].concat.apply( [], [].slice.call( arguments, 1 ) );
31-
keys.forEach(function( key ) {
31+
keys.forEach( function( key ) {
3232
if ( key in obj ) {
3333
copy[ key ] = obj[ key ];
3434
}
35-
});
35+
} );
3636
return copy;
3737
}
3838

@@ -49,56 +49,58 @@ function stream2Buffer( callback ) {
4949
stdin.on( "data", function( chunk ) {
5050
chunks.push( chunk );
5151
dataLen += chunk.length;
52-
});
52+
} );
5353

5454
stderr.on( "data", function( chunk ) {
5555
err += chunk;
56-
});
56+
} );
5757

5858
stdin.on( "end", function() {
5959
var i = 0,
6060
buffer = Buffer.alloc( dataLen );
6161
if ( err.length ) {
6262
return callback( new Error( err ) );
6363
}
64-
chunks.forEach(function ( chunk ) {
64+
chunks.forEach( function( chunk ) {
6565
chunk.copy( buffer, i, 0, chunk.length );
6666
i += chunk.length;
67-
});
67+
} );
6868
callback( null, buffer );
69-
});
69+
} );
7070

7171
stdin.on( "error", function( err ) {
7272
callback( err );
73-
});
73+
} );
7474
};
7575
}
7676

7777
function validateColor( color ) {
7878
color = color.replace( /^#/, "" );
7979
if ( ( color.length === 3 || color.length === 6 ) && /^[0-9a-f]+$/i.test( color ) ) {
80+
8081
// ok
8182
} else if ( namedColors.indexOf( color.toLowerCase() ) !== -1 ) {
83+
8284
// ok
8385
} else {
8486
throw new Error( "invalid color \"" + color + "\"" );
8587
}
8688
}
8789

8890
function validateDimension( params, dimensionParams ) {
89-
var invalidParams = dimensionParams.filter(function( param ) {
91+
var invalidParams = dimensionParams.filter( function( param ) {
9092
return parseInt( params[ param ], 10 ) > dimensionLimit;
91-
});
93+
} );
9294

9395
if ( invalidParams.length ) {
9496
throw new Error( "dimension bigger than allowed limit " + JSON.stringify( pick( params, invalidParams ) ) );
9597
}
9698
}
9799

98100
function validateInteger( params, integerParams ) {
99-
var invalidParams = integerParams.filter(function( param ) {
100-
return isNaN( parseInt( params[ param ], 10 ) ) || (/[^0-9]/).test( params[ param ] );
101-
});
101+
var invalidParams = integerParams.filter( function( param ) {
102+
return isNaN( parseInt( params[ param ], 10 ) ) || ( /[^0-9]/ ).test( params[ param ] );
103+
} );
102104

103105
if ( invalidParams.length ) {
104106
throw new Error( "got a non-integer " + JSON.stringify( pick( params, invalidParams ) ) );
@@ -113,9 +115,9 @@ function validateOpacity( opacity ) {
113115
}
114116

115117
function validatePresence( params, requiredParams ) {
116-
var missingParams = requiredParams.filter(function( param ) {
118+
var missingParams = requiredParams.filter( function( param ) {
117119
return !params[ param ];
118-
});
120+
} );
119121

120122
if ( missingParams.length ) {
121123
throw new Error( "missing \"" + missingParams.join( "\", \"" ) + "\"" );
@@ -151,7 +153,7 @@ generateIcon = function( params, callback ) {
151153
// IM > 6.7.9: (see #132 http://git.io/gfSacg)
152154
// $ convert <icons_mask_filename> -set colorspace RGB -background <color> -alpha shape -set colorspace sRGB output.png
153155

154-
imageQueue.push(function( innerCallback ) {
156+
imageQueue.push( function( innerCallback ) {
155157
try {
156158
if ( semver.gt( imVersion, "6.7.9" ) ) {
157159
im( __dirname + "/../assets/icon/mask.png" )
@@ -166,7 +168,7 @@ generateIcon = function( params, callback ) {
166168
.out( "-alpha", "shape" )
167169
.stream( "png", stream2Buffer( innerCallback ) );
168170
}
169-
} catch( err ) {
171+
} catch ( err ) {
170172
return innerCallback( err );
171173
}
172174
}, callback );
@@ -183,12 +185,12 @@ generateTexture = function( params, callback ) {
183185
// http://www.imagemagick.org/Usage/compose/#dissolve
184186
// $ convert -size <width>x<height> 'xc:<color>' <texture_filename> -compose dissolve -define compose:args=<opacity>,100 -composite output.png
185187

186-
imageQueue.push(function( innerCallback ) {
188+
imageQueue.push( function( innerCallback ) {
187189
try {
188190
im( params.width, params.height, color )
189191
.out( __dirname + "/../assets/texture/" + filename, "-compose", "dissolve", "-define", "compose:args=" + params.opacity + ",100", "-composite" )
190192
.stream( "png", stream2Buffer( innerCallback ) );
191-
} catch( err ) {
193+
} catch ( err ) {
192194
return innerCallback( err );
193195
}
194196
}, callback );
@@ -314,19 +316,19 @@ Image.prototype = {
314316
cached.data = data;
315317
delete cached.callbacks;
316318
}
317-
callbacks.forEach(function( callback ) {
319+
callbacks.forEach( function( callback ) {
318320
callback( err, filename, data );
319-
});
321+
} );
320322
delete cached.callbacks;
321323
if ( err ) {
322324
cache.destroy( filename );
323325
}
324-
});
326+
} );
325327
}
326328
};
327329

328330
// Check the ImageMagick installation using node-gm (in a hacky way).
329-
async.series([
331+
async.series( [
330332
function( callback ) {
331333
var wrappedCallback = function( err ) {
332334
if ( err ) {
@@ -335,15 +337,15 @@ async.series([
335337
callback();
336338
};
337339
try {
338-
im()._spawn([ "convert", "-version" ], true, wrappedCallback );
339-
} catch( err ) {
340+
im()._spawn( [ "convert", "-version" ], true, wrappedCallback );
341+
} catch ( err ) {
340342
return wrappedCallback( err );
341343
}
342344
},
343345
function( callback ) {
344-
im()._spawn([ "convert", "-version" ], false, stream2Buffer(function( err, buffer ) {
346+
im()._spawn( [ "convert", "-version" ], false, stream2Buffer( function( _err, buffer ) {
345347
var output = buffer.toString( "utf8" );
346-
if ( !(/ImageMagick/).test( output ) ) {
348+
if ( !( /ImageMagick/ ).test( output ) ) {
347349
return callback( new Error( "ImageMagick not installed.\n" + output ) );
348350
}
349351
imVersion = output.split( "\n" )[ 0 ].replace( /^Version: ImageMagick ([^ ]*).*/, "$1" );
@@ -352,13 +354,14 @@ async.series([
352354
}
353355
imageQueue.resume();
354356
callback();
355-
}));
357+
} ) );
356358
}
357359
], function( err ) {
358360
if ( err ) {
361+
359362
// On error, abort
360363
throw new Error( err );
361364
}
362-
});
365+
} );
363366

364367
module.exports = Image;

0 commit comments

Comments
 (0)