• Jump To … +
    Array.litcoffee Boolean.litcoffee Function.litcoffee Number.litcoffee Object.litcoffee String.litcoffee _register.js _to_map_and_write_Tests.litcoffee assert_Array.litcoffee assert_Boolean.litcoffee assert_Function.litcoffee assert_Number.litcoffee assert_Object.litcoffee assert_String.litcoffee console.litcoffee fs.litcoffee globals.litcoffee http.litcoffee index.md path.litcoffee process.litcoffee Array.test.coffee Boolean.test.coffee Function.test.coffee Number.test.coffee Object.test.coffee String.test.coffee Array.test.coffee Boolean.test.coffee Function.test.coffee Number.test.coffee Object.test.coffee String.test.coffee console.test.coffee fs.test.coffee globals.test.coffee http.test.coffee path.test.coffee process.test.coffee
  • fs.litcoffee

  • ¶

    dependencies

    require './path'
    fs = require('fs')
  • ¶

    @.folder_Create

    Creates a folder on @, with checks to create parent folders recusively (i.e. it will also create all parents up until it finds a valid directory)

    String::folder_Create    = ->
      target = @.valueOf()
      if target.folder_Not_Exists()           # only do anyhing id the folder doesn't exist
        target.parent_Folder()                # check if the parent folder exists
              .folder_Create()
        fs.mkdirSync(target)                  # use node fs.mkdirSync to creat the folder
      return target.realPath()                # return full normalized path to target folder
  • ¶

    @.folder_Delete

    String::folder_Delete       = ->
                                    folder = @.toString()
                                    if fs.existsSync(folder) then fs.rmdirSync(folder)
                                    return folder.not_Exists()
    
    String::folder_Delete_Recursive = ->
                                    folder = @.toString()
                                    if folder.exists()
                                      for file in folder.files()
                                        file.file_Delete()
                                      for subFolder in folder.folders()
                                        subFolder.folder_Delete_Recursive()
                                      folder.folder_Delete()
                                    return folder.not_Exists()
  • ¶

    @.file_Copy target

    Copy @ file to target

    if target is an existing folder, the file will be copied into that folder with the filename of @

    returns target path

    String::file_Copy = (target)->
      if not (target)
        return null
      if (@.valueOf().file_Not_Exists())
        return null;
    
      if target.folder_Exists()
        target = target.path_Combine(@.valueOf().file_Name())
      else
        target.parent_Folder().folder_Create()
    
      content = fs.readFileSync(@.valueOf());
      fs.writeFileSync(target, content)
    
      target
  • ¶

    @.file_Create

    Creates and empty file at @

    String::file_Create         = ->
                                      path = @.toString()
                                      if path.not_Exists()
                                          fs.writeFileSync(path,'')
                                      return path.realPath();
    
    String::file_Delete         = ->
                                      file = @.toString().realPath()
                                      return true if not file
                                      fs.unlinkSync file
                                      return file.file_Not_Exists()
    
    String::file_Contents        = ->
                                      file = @.valueOf().realPath()
                                      return null if not file
                                      return fs.readFileSync(file,"utf8")
  • ¶

    @.file_Exists

    Returns true if @ is a file and it exists

    String::file_Exists = ->
      return fs.existsSync  @.valueOf()
  • ¶

    @.file_Write content

    Writes content as file @

    String::file_Write  = (content)->
      content.saveAs(@.str())
      @
  • ¶

    @.file_Not_Exists

    Returns true if @ is not a file

    String::file_Not_Exists     = ->
      (fs.existsSync @.valueOf()) == false
  • ¶

    @.files_and_Folders

    returns a list of files and folders from the folder @

    String::files_And_Folders   = ->
                                      path = @.valueOf()
                                      try
                                        path.path_Combine(item).realPath() for item in fs.readdirSync path
                                      catch
                                        []
    
    String::files               = (extension)->
                                      files = (item for item in @.files_And_Folders() when item.is_File())
                                      if extension
                                        return (file for file in files when file.file_Extension() is extension)
                                      return files
    String::files_Recursive     = (extension)->
                                      files = []
                                      for item in @.str().files_And_Folders()
                                        if (item.is_Folder())
                                          files = files.concat(item.files_Recursive(extension))
                                        else
                                          if (not extension or item.file_Extension() is extension)
                                            files.push(item)
                                      return files
    
    String::folders             = ->  item for item in @.files_And_Folders() when item.is_Folder()
  • ¶

    @.is_Folder

    Returns true is @ is a folder

    String::is_Folder = ->
      try
        fs.lstatSync(@.valueOf()).isDirectory()
      catch
        false
  • ¶

    @.is_Not_Folder

    Returns true if @ is not a folder

    String::is_Not_Folder = ->
      @.is_Folder() is false
    
    String::is_File             = ->  try fs.lstatSync(@.valueOf()).isFile()      catch then false
    
    String::realPath            = ->  try fs.realpathSync @.valueOf() catch then null
    String::temp_Name_In_Folder = ->  @.valueOf().realPath().path_Combine("_tmp_".add_Random_String(10))
    
    String::saveAs              = (targetFile) ->
                                      return false if targetFile is null
                                      contents = @.valueOf()
                                      if (targetFile.exists())
                                        targetFile.file_Delete()
                                      fs.writeFileSync(targetFile,contents)
                                      return targetFile.exists()
  • ¶

    String::saveAs

    String::exists              = String::file_Exists
    String::create_Dir          = String::folder_Create
    String::delete_File         = String::file_Delete
    String::delete_Folder       = String::folder_Delete
    String::folder_Exists       = String::is_Folder
    String::folder_Not_Exists   = String::is_Not_Folder
    String::fullPath            = String::realPath
    String::is_Directory        = String::is_Folder
    String::touch               = String::file_Create
    String::not_Exists          = String::file_Not_Exists
  • ¶

  • ¶

    back to index