1
- //! files-replace v0.1.0 ~~ https://github.com/center-key/files-replace ~~ MIT License
1
+ //! files-replace v0.1.1 ~~ https://github.com/center-key/files-replace ~~ MIT License
2
2
3
3
var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
4
4
return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
@@ -39,6 +39,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
39
39
concat : null ,
40
40
extensions : [ ] ,
41
41
find : null ,
42
+ regex : null ,
42
43
replacement : null ,
43
44
pkg : false ,
44
45
} ;
@@ -48,7 +49,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
48
49
const source = util . normalizeFolder ( startFolder + sourceFolder ) ;
49
50
const target = util . normalizeFolder ( startFolder + targetFolder ) ;
50
51
const concatFile = settings . concat ? path_1 . default . join ( target , settings . concat ) : null ;
51
- const missingFind = ! settings . find && ! ! settings . replacement ;
52
+ const missingFind = ! settings . find && ! settings . regex && ! ! settings . replacement ;
52
53
if ( targetFolder )
53
54
fs_1 . default . mkdirSync ( target , { recursive : true } ) ;
54
55
const errorMessage = ! sourceFolder ? 'Must specify the source folder path.' :
@@ -57,7 +58,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
57
58
! fs_1 . default . existsSync ( target ) ? 'Target folder cannot be created: ' + target :
58
59
! fs_1 . default . statSync ( source ) . isDirectory ( ) ? 'Source is not a folder: ' + source :
59
60
! fs_1 . default . statSync ( target ) . isDirectory ( ) ? 'Target is not a folder: ' + target :
60
- missingFind ? 'Must specify search text with --find' :
61
+ missingFind ? 'Must specify search text with --find or --regex ' :
61
62
null ;
62
63
if ( errorMessage )
63
64
throw Error ( '[files-replace] ' + errorMessage ) ;
@@ -76,10 +77,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
76
77
engine . registerFilter ( 'version-major' , versionFormatter ( 1 ) ) ;
77
78
const pkg = settings . pkg ? util . readPackageJson ( ) : null ;
78
79
const processFile = ( file , index ) => {
80
+ const content = fs_1 . default . readFileSync ( file . origin , 'utf-8' ) ;
79
81
const newStr = settings . replacement ?? '' ;
80
- const text = fs_1 . default . readFileSync ( file . origin , 'utf-8' ) ;
81
- const updated = settings . find ? text . replaceAll ( settings . find , newStr ) : text ;
82
- const final = settings . pkg ? engine . parseAndRenderSync ( updated , { pkg } ) : updated ;
82
+ const out1 = settings . find ? content . replaceAll ( settings . find , newStr ) : content ;
83
+ const out2 = settings . regex ? out1 . replace ( settings . regex , newStr ) : out1 ;
84
+ const final = settings . pkg ? engine . parseAndRenderSync ( out2 , { pkg } ) : out2 ;
83
85
fs_1 . default . mkdirSync ( path_1 . default . dirname ( file . dest ) , { recursive : true } ) ;
84
86
if ( settings . concat && index > 0 )
85
87
fs_1 . default . appendFileSync ( file . dest , final ) ;
0 commit comments