11import { bytesToStr , strToBytes } from './common' ;
2+ import { addFileToIntelHex } from './fs-builder' ;
23
34interface FsInterface {
4- create ( filename : string , content ?: string ) : void ;
55 write ( filename : string , content : string ) : void ;
66 append ( filename : string , content : string ) : void ;
77 read ( filename : string ) : string ;
88 readBytes ( filename : string ) : Uint8Array ;
99 remove ( filename : string ) : void ;
10+ exists ( filename : string ) : boolean ;
1011 ls ( ) : string [ ] ;
1112}
1213
1314class SimpleFile {
1415 filename : string ;
15- data : Uint8Array ;
16+ private _dataBytes : Uint8Array ;
1617
1718 constructor ( filename : string , data : string | Uint8Array ) {
1819 this . filename = filename ;
1920 if ( typeof data === 'string' ) {
20- this . data = strToBytes ( data ) ;
21+ this . _dataBytes = strToBytes ( data ) ;
2122 } else {
22- this . data = data ;
23+ this . _dataBytes = data ;
2324 }
2425 }
2526
2627 getText ( ) : string {
27- return bytesToStr ( this . data ) ;
28+ return bytesToStr ( this . _dataBytes ) ;
2829 }
2930
3031 getBytes ( ) : Uint8Array {
31- return this . data ;
32+ return this . _dataBytes ;
3233 }
3334}
3435
36+ // TODO: Max filename size
3537// tslint:disable-next-line:max-classes-per-file
3638class FileSystem implements FsInterface {
3739 private _intelHex : string ;
3840 private _files : { [ id : string ] : SimpleFile } = { } ;
3941
4042 constructor ( intelHex : string ) {
4143 this . _intelHex = intelHex ;
42- }
4344
44- create ( filename : string , content ?: string ) : void {
45- // TODO: Create an empty file, with optional content
46- // TODO: Throw error if file already exists
47- // tslint:disable-next-line:no-console
48- console . log ( 'create() method unimplemented.' ) ;
45+ // TODO: Read present file system in Intel Hex and populate files here
4946 }
5047
5148 write ( filename : string , content : string | Uint8Array ) : void {
@@ -54,7 +51,7 @@ class FileSystem implements FsInterface {
5451
5552 append ( filename : string , content : string ) : void {
5653 // TODO: Append content to existing file
57- // TODO: Throw error if file does not exists
54+ // TODO: Do we throw error if file does not exists, or create it?
5855 // tslint:disable-next-line:no-console
5956 console . log ( 'append() method unimplemented.' ) ;
6057 }
@@ -74,15 +71,22 @@ class FileSystem implements FsInterface {
7471 delete this . _files [ filename ] ;
7572 }
7673
74+ exists ( filename : string ) : boolean {
75+ return this . _files . hasOwnProperty ( filename ) ;
76+ }
77+
7778 ls ( ) : string [ ] {
7879 const files : string [ ] = [ ] ;
7980 Object . values ( this . _files ) . forEach ( ( value ) => files . push ( value . filename ) ) ;
8081 return files ;
8182 }
8283
8384 getIntelHex ( ) : string {
84- // TODO: Generate filesystem and inject into Intel Hex string
85- return this . _intelHex ;
85+ let finalHex = this . _intelHex ;
86+ Object . values ( this . _files ) . forEach ( ( file ) => {
87+ finalHex = addFileToIntelHex ( finalHex , file . filename , file . getBytes ( ) ) ;
88+ } ) ;
89+ return finalHex ;
8690 }
8791}
8892
0 commit comments