38 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			38 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import {StoreComponent} from "../StoreComponent";
 | |
| import {FileUtils} from "../FileUtils";
 | |
| import * as path from "node:path";
 | |
| import * as fs from "fs";
 | |
| 
 | |
| export class ScriptAccountStorage {
 | |
| 
 | |
|   private scriptAccountDir: string
 | |
| 
 | |
|   constructor(scriptAccountDir: string) {
 | |
|     this.scriptAccountDir = scriptAccountDir;
 | |
|     FileUtils.prepareDirectoryFroWriting(this.scriptAccountDir)
 | |
|   }
 | |
| 
 | |
|   storeScriptAccounts(scriptAccounts: StoreComponent[]) {
 | |
|     this.persistDeletedScriptAccounts(scriptAccounts)
 | |
|     scriptAccounts.forEach(scriptAccount => {
 | |
|       this.storeScriptAccount(scriptAccount)
 | |
|     })
 | |
|   }
 | |
| 
 | |
|   private persistDeletedScriptAccounts(existingScriptAccount: StoreComponent[]) {
 | |
|     const scriptAccountFiles = FileUtils.listFilesInDirectory(this.scriptAccountDir);
 | |
|     scriptAccountFiles.forEach(scriptAccountFile => {
 | |
|       const scriptAccountFileName = path.parse(path.basename(scriptAccountFile)).name
 | |
|       if(existingScriptAccount.find(scriptAccount => scriptAccount.fileName === scriptAccountFileName) == undefined) {
 | |
|         //No scriptAccountFile was found with that nae of the file. So the scriptAccount was deleted. Remove file
 | |
|         fs.unlinkSync(scriptAccountFile)
 | |
|       }
 | |
|     })
 | |
|   }
 | |
| 
 | |
|   private storeScriptAccount(scriptAccount: StoreComponent) {
 | |
|     const completeScriptAccountFile = path.join(this.scriptAccountDir, scriptAccount.fileName + ".json")
 | |
|     fs.writeFileSync(completeScriptAccountFile, scriptAccount.jsonString, 'utf-8')
 | |
|   }
 | |
| }
 |