forked from obytes/react-native-template-obytes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from rootstrap/refactor/cli_move_file_operatio…
…ns_to_project_files_manager_class refactor(cli): move file operations to ProjectFilesManager class
- Loading branch information
Showing
4 changed files
with
165 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
|
||
class ProjectFilesManager { | ||
#projectName; | ||
|
||
constructor( | ||
/** | ||
* @type {string} | ||
*/ | ||
projectName | ||
) { | ||
this.#projectName = projectName; | ||
} | ||
|
||
static withProjectName( | ||
/** | ||
* @type {string} | ||
*/ | ||
projectName | ||
) { | ||
return new this(projectName); | ||
} | ||
|
||
getAbsoluteFilePath( | ||
/** | ||
* A file path relative to project's root path | ||
* @type {string} | ||
*/ | ||
relativeFilePath | ||
) { | ||
return path.join(process.cwd(), `${this.#projectName}/${relativeFilePath}`); | ||
} | ||
|
||
removeFiles( | ||
/** | ||
* An array of file paths relative to project's root path | ||
* @type {Array<string>} | ||
*/ | ||
files | ||
) { | ||
files.forEach((fileName) => { | ||
const absoluteFilePath = this.getAbsoluteFilePath(fileName); | ||
|
||
fs.removeSync(absoluteFilePath); | ||
}); | ||
} | ||
|
||
renameFiles( | ||
/** | ||
* An array of objects containing the old and the new file names | ||
* relative to project's root path | ||
* | ||
* @type {Array<{ | ||
* oldFileName: string; | ||
* newFileName: string; | ||
* }>} | ||
*/ | ||
files | ||
) { | ||
files.forEach(({ oldFileName, newFileName }) => { | ||
const oldAbsoluteFilePath = this.getAbsoluteFilePath(oldFileName); | ||
const newAbsoluteFilePath = this.getAbsoluteFilePath(newFileName); | ||
|
||
fs.renameSync(oldAbsoluteFilePath, newAbsoluteFilePath); | ||
}); | ||
} | ||
|
||
replaceFilesContent( | ||
/** | ||
* An array of objects containing the file name relative to project's | ||
* root path and the replacement patterns to be applied | ||
* | ||
* @type {Array<{ | ||
* fileName: string; | ||
* replacements: Array<{ | ||
* searchValue: string; | ||
* replaceValue: string; | ||
* }> | ||
* }>} | ||
*/ | ||
files | ||
) { | ||
files.forEach(({ fileName, replacements }) => { | ||
const absoluteFilePath = this.getAbsoluteFilePath(fileName); | ||
|
||
const contents = fs.readFileSync(absoluteFilePath, { | ||
encoding: 'utf-8', | ||
}); | ||
|
||
let replaced = contents; | ||
|
||
replacements.forEach(({ searchValue, replaceValue }) => { | ||
replaced = replaced.replace(searchValue, replaceValue); | ||
}); | ||
|
||
fs.writeFileSync(absoluteFilePath, replaced, { spaces: 2 }); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ProjectFilesManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters