1- const compareVersions = require ( 'compare-versions' ) ;
2- const { markdownTable } = require ( 'markdown-table' ) ;
3-
4- const ASSETS_URL = {
5- ADDED : 'https://git.io/J38HP' ,
6- DOWNGRADED : 'https://git.io/J38ds' ,
7- REMOVED : 'https://git.io/J38dt' ,
8- UPDATED : 'https://git.io/J38dY'
9- } ;
1+ const semverCompare = require ( 'semver/functions/compare' ) ;
2+ const semverCoerce = require ( 'semver/functions/coerce' ) ;
3+ const semverValid = require ( 'semver/functions/valid' ) ;
104
115export const STATUS = {
126 ADDED : 'ADDED' ,
@@ -15,52 +9,31 @@ export const STATUS = {
159 UPDATED : 'UPDATED'
1610} ;
1711
18- const getStatusLabel = status =>
19- `[<sub><img alt="${ status } " src="${ ASSETS_URL [ status ] } " height="16" /></sub>](#)` ;
20-
21- export const createTable = ( lockChanges , plainStatuses = false ) =>
22- markdownTable (
23- [
24- [ 'Name' , 'Status' , 'Previous' , 'Current' ] ,
25- ...Object . entries ( lockChanges )
26- . map ( ( [ key , { status, previous, current } ] ) => [
27- '`' + key + '`' ,
28- plainStatuses ? status : getStatusLabel ( status ) ,
29- previous ,
30- current
31- ] )
32- . sort ( ( a , b ) => a [ 0 ] . localeCompare ( b [ 0 ] ) )
33- ] ,
34- { align : [ 'l' , 'c' , 'c' , 'c' ] , alignDelimiters : false }
35- ) ;
36-
3712export const countStatuses = ( lockChanges , statusToCount ) =>
3813 Object . values ( lockChanges ) . filter ( ( { status } ) => status === statusToCount ) . length ;
3914
40- const createSummaryRow = ( lockChanges , status ) => {
41- const statusCount = countStatuses ( lockChanges , status ) ;
42- return statusCount ? [ getStatusLabel ( status ) , statusCount ] : undefined ;
43- } ;
15+ const formatForNameCompare = key => key . substr ( 0 , key . lastIndexOf ( '@' ) ) ;
4416
45- export const createSummary = lockChanges =>
46- markdownTable (
47- [
48- [ 'Status' , 'Count' ] ,
49- createSummaryRow ( lockChanges , STATUS . ADDED ) ,
50- createSummaryRow ( lockChanges , STATUS . UPDATED ) ,
51- createSummaryRow ( lockChanges , STATUS . DOWNGRADED ) ,
52- createSummaryRow ( lockChanges , STATUS . REMOVED )
53- ] . filter ( Boolean ) ,
54- { align : [ 'l' , 'c' ] , alignDelimiters : false }
55- ) ;
17+ const formatForVersionCompare = key => {
18+ const version = key . substr ( key . lastIndexOf ( '@' ) + 1 ) ;
19+ return semverValid ( semverCoerce ( version ) ) || '0.0.0' ;
20+ } ;
5621
5722const formatLockEntry = obj =>
5823 Object . fromEntries (
59- Object . keys ( obj . object ) . map ( key => {
60- const nameParts = key . split ( '@' ) ;
61- const name = nameParts [ 0 ] === '' ? '@' + nameParts [ 1 ] : nameParts [ 0 ] ;
62- return [ name , { name, version : obj . object [ key ] . version } ] ;
63- } )
24+ Object . keys ( obj . object )
25+ . sort ( ( a , b ) => {
26+ const nameCompare = formatForNameCompare ( a ) . localeCompare ( formatForNameCompare ( b ) ) ;
27+ if ( nameCompare === 0 ) {
28+ return semverCompare ( formatForVersionCompare ( a ) , formatForVersionCompare ( b ) ) ;
29+ }
30+ return nameCompare ;
31+ } )
32+ . map ( key => {
33+ const nameParts = key . split ( '@' ) ;
34+ const name = nameParts [ 0 ] === '' ? '@' + nameParts [ 1 ] : nameParts [ 0 ] ;
35+ return [ name , { name, version : obj . object [ key ] . version } ] ;
36+ } )
6437 ) ;
6538
6639export const diffLocks = ( previous , current ) => {
@@ -88,7 +61,7 @@ export const diffLocks = (previous, current) => {
8861 delete changes [ key ] ;
8962 } else {
9063 changes [ key ] . current = currentPackages [ key ] . version ;
91- if ( compareVersions ( changes [ key ] . previous , changes [ key ] . current ) === 1 ) {
64+ if ( semverCompare ( changes [ key ] . previous , changes [ key ] . current ) === 1 ) {
9265 changes [ key ] . status = STATUS . DOWNGRADED ;
9366 } else {
9467 changes [ key ] . status = STATUS . UPDATED ;
0 commit comments