22"use strict" ;
33
44const { getUserscriptsInFolder, readFile, writeFile } = require ( "./files" ) ;
5- const getVersion = require ( "./get-mutation-version" ) ;
5+ const mutations = require ( "./update-mutation" ) ;
6+ const utils = require ( "./update-utils" ) ;
67const getXrefs = require ( "./get-xrefs" ) ;
78
8- const regexpMutations = / m u t a t i o n s . j s \? v e r s i o n = \d + / ;
9- const regexpPatch = / ( @ v e r s i o n \s + ) ( [ \d . ] + ) / ;
10- const regexpName = / ( @ n a m e \s + ) ( [ \w ] + ) / ;
11- const today = ( new Date ( ) ) . toISOString ( ) . substring ( 0 , 10 ) . replace ( / - / g , "." ) ;
9+ let processes = [
10+ mutations ,
11+ utils
12+ ] ;
1213
14+ let usXref ;
1315const updatedList = [ ] ;
1416
15- let currentVersion , usXref ;
17+ const regexpPatch = / ( @ v e r s i o n \s + ) ( [ \d . ] + ) / ;
18+ const regexpName = / ( @ n a m e \s + ) ( [ \w ] + ) / ;
19+ const today = ( new Date ( ) ) . toISOString ( ) . substring ( 0 , 10 ) . replace ( / - / g, "." ) ;
1620
1721function updateFile ( name ) {
1822 return new Promise ( ( resolve , reject ) => {
1923 readFile ( "./" + name )
2024 . then ( data => {
21- if ( regexpMutations . test ( data ) ) {
22- let { content, updated} = updateMutationVersion ( data ) ;
23- if ( updated ) {
24- // Only update userscript patch if the mutation version was modified
25- content = updatePatch ( content ) ;
26- updatedList . push ( content . match ( regexpName ) [ 2 ] ) ;
25+ let wasUpdated = false ;
26+ let content = data ;
27+ processes . forEach ( process => {
28+ if ( process . regexp . test ( content ) ) {
29+ const result = process . replace ( content , process . version ) ;
30+ if ( result . updated ) {
31+ content = result . content ;
32+ wasUpdated = true ;
33+ }
2734 }
35+ } ) ;
36+ if ( wasUpdated ) {
37+ // Only update userscript patch if the file version was modified
38+ content = updatePatch ( content ) ;
39+ updatedList . push ( content . match ( regexpName ) [ 2 ] ) ;
2840 return writeFile ( name , content ) . then ( resolve ) ;
2941 }
3042 resolve ( ) ;
@@ -36,16 +48,6 @@ function updateFile(name) {
3648 } ) ;
3749}
3850
39- function updateMutationVersion ( content ) {
40- let updated = false ;
41- const replacement = `mutations.js?version=${ currentVersion } ` ;
42- if ( content . indexOf ( replacement ) < 0 ) {
43- updated = true ;
44- content = content . replace ( regexpMutations , replacement ) ;
45- }
46- return { content, updated} ;
47- }
48-
4951function updatePatch ( data ) {
5052 // @version x.x.0 => @version x.x.1
5153 let [ major , minor , patch ] = data . match ( regexpPatch ) [ 2 ] . split ( "." ) ;
@@ -87,7 +89,7 @@ function updateReadme() {
8789
8890function processUserscripts ( list ) {
8991 return Promise . all ( list . map ( file => updateFile ( file ) ) )
90- . then ( ( ) => console . log ( "\x1b[32m%s\x1b[0m" , "Mutation URLs updated" ) )
92+ . then ( ( ) => console . log ( "\x1b[32m%s\x1b[0m" , "Require URLs updated" ) )
9193 . then ( updateReadme )
9294 . then ( ( ) => console . log ( "\x1b[32m%s\x1b[0m" , "Readme updated" ) ) ;
9395}
@@ -99,10 +101,15 @@ function exit(err) {
99101 process . exit ( err ? 1 : 0 ) ;
100102}
101103
102- Promise . all ( [ getUserscriptsInFolder ( ) , getVersion ( ) , getXrefs ( ) ] )
103- . then ( ( [ list , version , xrefs ] ) => {
104- currentVersion = version ;
105- usXref = xrefs ;
106- return processUserscripts ( list ) ;
107- } )
108- . catch ( exit ) ;
104+ Promise . all ( [
105+ getUserscriptsInFolder ( ) ,
106+ getXrefs ( ) ,
107+ ...processes . map ( process => process . getVersion ( ) ) ,
108+ ] ) . then ( ( [ list , xrefs , ...versions ] ) => {
109+ usXref = xrefs ;
110+ processes = processes . map ( ( process , index ) => {
111+ process . version = versions [ index ] ;
112+ return process ;
113+ } ) ;
114+ return processUserscripts ( list ) ;
115+ } ) . catch ( exit ) ;
0 commit comments