Book Image

CoffeeScript Application Development Cookbook

By : Mike Hatfield
Book Image

CoffeeScript Application Development Cookbook

By: Mike Hatfield

Overview of this book

Table of Contents (18 chapters)
CoffeeScript Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Archiving files and directories


In this recipe, we will look at creating archives of our files and directories.

Getting ready

We will be using the archiver NPM module. Install it with the following command:

npm install archiver --save

How to do it...

With the archiver module installed, we can use it to create a backup file of our entire workspace tree by following these steps:

  1. Load the fs and archiver modules:

    fs = require 'fs'
    archiver = require 'archiver'
    
  2. Create a write stream:

    output = fs.createWriteStream 'backup.zip'
    
  3. Create an instance of a ZIP archive:

    archive = archiver 'zip'
    
  4. Add event handlers to the stream and archive:

    output.on 'close', ->
      console.log "Total bytes: #{archive.pointer()}"
    
    archive.on 'error', (err) ->
      console.error err
    
  5. Set the archive's output pipe to our stream writer:

    archive.pipe output
    
  6. Perform the compression:

    archive.bulk
      expand: yes
      cwd: 'workspace'
      src: ['**']
      dest: 'src'
    archive.finalize()
    

How it works...

We start by requiring the fs and archiver...