1- import fs from 'fs' ;
21import { ReleaseType } from 'semver' ;
32import { Updater } from '../interface' ;
4- import { calculateNextVersion } from '../utils' ;
3+ import { calculateNextVersion , FileHandler } from '../utils' ;
54
65export class PHPUpdater implements Updater {
76 platform = 'php' ;
87
98 canHandle ( ) : boolean {
109 return (
11- fs . existsSync ( 'composer.json' ) ||
12- fs . existsSync ( 'VERSION' ) ||
13- fs . existsSync ( 'version.php' ) ||
14- fs . existsSync ( 'config.php' )
10+ FileHandler . fileExists ( 'composer.json' ) ||
11+ FileHandler . fileExists ( 'VERSION' ) ||
12+ FileHandler . fileExists ( 'version.php' ) ||
13+ FileHandler . fileExists ( 'config.php' )
1514 ) ;
1615 }
1716
1817 getCurrentVersion ( ) : string | null {
1918 // composer.json
20- if ( fs . existsSync ( 'composer.json' ) ) {
21- const composer = JSON . parse ( fs . readFileSync ( 'composer.json' , 'utf8 ') ) ;
19+ if ( FileHandler . fileExists ( 'composer.json' ) ) {
20+ const composer = JSON . parse ( FileHandler . readFile ( 'composer.json' ) ) ;
2221 return composer . version || null ;
2322 }
2423
2524 // VERSION file
26- if ( fs . existsSync ( 'VERSION' ) ) {
27- return fs . readFileSync ( 'VERSION' , 'utf8 ') . trim ( ) ;
25+ if ( FileHandler . fileExists ( 'VERSION' ) ) {
26+ return FileHandler . readFile ( 'VERSION' ) . trim ( ) ;
2827 }
2928
3029 // version.php
31- if ( fs . existsSync ( 'version.php' ) ) {
32- const content = fs . readFileSync ( 'version.php' , 'utf8 ') ;
30+ if ( FileHandler . fileExists ( 'version.php' ) ) {
31+ const content = FileHandler . readFile ( 'version.php' ) ;
3332 const match = content . match ( / [ ' " ] ( [ \d . ] + ) [ ' " ] / ) ;
3433 return match ? match [ 1 ] : null ;
3534 }
3635
3736 // config.php
38- if ( fs . existsSync ( 'config.php' ) ) {
39- const content = fs . readFileSync ( 'config.php' , 'utf8 ') ;
37+ if ( FileHandler . fileExists ( 'config.php' ) ) {
38+ const content = FileHandler . readFile ( 'config.php' ) ;
4039 const match = content . match ( / ' v e r s i o n ' \s * = > \s * ' ( [ \d . ] + ) ' / ) ;
4140 return match ? match [ 1 ] : null ;
4241 }
@@ -49,32 +48,32 @@ export class PHPUpdater implements Updater {
4948 const newVersion = calculateNextVersion ( version , releaseType ) ;
5049
5150 // composer.json
52- if ( fs . existsSync ( 'composer.json' ) ) {
53- const composer = JSON . parse ( fs . readFileSync ( 'composer.json' , 'utf8 ') ) ;
51+ if ( FileHandler . fileExists ( 'composer.json' ) ) {
52+ const composer = JSON . parse ( FileHandler . readFile ( 'composer.json' ) ) ;
5453 composer . version = newVersion ;
55- fs . writeFileSync ( 'composer.json' , JSON . stringify ( composer , null , 2 ) ) ;
54+ FileHandler . writeFile ( 'composer.json' , JSON . stringify ( composer , null , 2 ) ) ;
5655 return newVersion ;
5756 }
5857
5958 // VERSION file
60- if ( fs . existsSync ( 'VERSION' ) ) {
61- fs . writeFileSync ( 'VERSION' , newVersion ) ;
59+ if ( FileHandler . fileExists ( 'VERSION' ) ) {
60+ FileHandler . writeFile ( 'VERSION' , newVersion ) ;
6261 return newVersion ;
6362 }
6463
6564 // version.php
66- if ( fs . existsSync ( 'version.php' ) ) {
67- const content = fs . readFileSync ( 'version.php' , 'utf8 ') ;
65+ if ( FileHandler . fileExists ( 'version.php' ) ) {
66+ const content = FileHandler . readFile ( 'version.php' ) ;
6867 const updated = content . replace ( / ( [ ' " ] ) [ \d . ] + ( [ ' " ] ) / , `$1${ newVersion } $2` ) ;
69- fs . writeFileSync ( 'version.php' , updated ) ;
68+ FileHandler . writeFile ( 'version.php' , updated ) ;
7069 return newVersion ;
7170 }
7271
7372 // config.php
74- if ( fs . existsSync ( 'config.php' ) ) {
75- const content = fs . readFileSync ( 'config.php' , 'utf8 ') ;
73+ if ( FileHandler . fileExists ( 'config.php' ) ) {
74+ const content = FileHandler . readFile ( 'config.php' ) ;
7675 const updated = content . replace ( / ' v e r s i o n ' \s * = > \s * ' [ \d . ] + ' / , `'version' => '${ newVersion } '` ) ;
77- fs . writeFileSync ( 'config.php' , updated ) ;
76+ FileHandler . writeFile ( 'config.php' , updated ) ;
7877 return newVersion ;
7978 }
8079
0 commit comments