3
3
const { promises : fs } = require ( 'fs' ) ;
4
4
const path = require ( 'path' ) ;
5
5
6
- const versions = [
7
- '@openzeppelin/contracts' ,
8
- '@openzeppelin/contracts-upgradeable' ,
9
- ] ;
10
-
11
6
const pathUpdates = {
12
7
// 'access/AccessControl.sol': undefined,
13
8
// 'access/Ownable.sol': undefined,
@@ -112,15 +107,15 @@ async function main (paths = [ 'contracts' ]) {
112
107
}
113
108
}
114
109
115
- async function listFilesRecursively ( paths , filter = undefined ) {
116
- const queue = Array . isArray ( paths ) ? paths : [ paths ] ;
110
+ async function listFilesRecursively ( paths , filter ) {
111
+ const queue = paths ;
117
112
const files = [ ] ;
118
113
119
114
while ( queue . length > 0 ) {
120
115
const top = queue . shift ( ) ;
121
116
const stat = await fs . stat ( top ) ;
122
117
if ( stat . isFile ( ) ) {
123
- if ( ! filter || top . match ( filter ) ) {
118
+ if ( top . match ( filter ) ) {
124
119
files . push ( top ) ;
125
120
}
126
121
} else if ( stat . isDirectory ( ) ) {
@@ -146,18 +141,29 @@ async function updateFile (file, update) {
146
141
147
142
function updateImportPaths ( source ) {
148
143
for ( const [ oldPath , newPath ] of Object . entries ( pathUpdates ) ) {
149
- for ( const version of versions ) {
150
- source = source . replace (
151
- path . join ( version , oldPath ) ,
152
- path . join ( version , newPath ) ,
153
- ) ;
154
- }
144
+ source = source . replace (
145
+ path . join ( '@openzeppelin/contracts' , oldPath ) ,
146
+ path . join ( '@openzeppelin/contracts' , newPath ) ,
147
+ ) ;
148
+ source = source . replace (
149
+ path . join ( '@openzeppelin/contracts-upgradeable' , getUpgradeablePath ( oldPath ) ) ,
150
+ path . join ( '@openzeppelin/contracts-upgradeable' , getUpgradeablePath ( newPath ) ) ,
151
+ ) ;
155
152
}
156
153
157
154
return source ;
158
155
}
159
156
160
- module . exports = main ;
157
+ function getUpgradeablePath ( file ) {
158
+ const { dir, name, ext } = path . parse ( file ) ;
159
+ const upgradeableName = name + 'Upgradeable' ;
160
+ return path . format ( { dir, ext, name : upgradeableName } ) ;
161
+ }
162
+
163
+ module . exports = {
164
+ pathUpdates,
165
+ updateImportPaths,
166
+ } ;
161
167
162
168
if ( require . main === module ) {
163
169
const args = process . argv . length > 2 ? process . argv . slice ( 2 ) : undefined ;
0 commit comments