Skip to content

ViddaDigital/afst

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Abstract File System Tree

Define an abstract tree of directories and files with content and create it in one go.

Requirements

  • node version 8 or higher

Installation

$ git clone git@github.com:ViddaDigital/afst.git
$ yarn install

How to use

$ yarn build

Linking afst during development

Change directory to where your cloned afst.

$ yarn afst

Change directory to your local project.

$ yarn link afst

Usage

   let tree: AbstractFileSystemTree = {
      path: '/some/kind/of/path',
      children: [
        {
          type: 'dir',
          name: 'dir1',
          children: [
            {
              type: 'file',
              name: 'file2a.txt',
              content: 'File 2 a content'
            },
            {
              type: 'file',
              name: 'file2b.txt',
              content: 'File 2 b content'
            },
            {
              type: 'dir',
              name: 'dir2c',
              children: [
                {
                  type: 'file',
                  name: 'file3a.txt',
                  content: 'File 3 a content'
                },
                {
                  type: 'file',
                  name: 'file3b.txt',
                  content: 'File 3 b content'
                }
              ]
            }
          ]
        },
        {
          type: 'file',
          name: 'file2.txt',
          content: 'File 2 content'
        }
      ]
   }

  new AFST(tree, { log: true }).write()

This will create these files and directories:

├── dir1
│    ├── file2a.txt
│    ├── file2b.txt
│    └── dir2c
│        ├── file3a.txt
│        └── file3b.txt
└── file2.txt

Builder

You can also use the builder to create the tree like this:

new AFST({path: '/some/kind/of/path'})
  .dir('a', a => a
    .file('c.txt', 'Text content')
    .file('d.md', 'Markdown content')
    .dir('e', e => {
      e.file('f', 'File without extension')
      e.file('g', 'File without extension')
      return e
    })
  )
  .file('.b', 'Some dotfile content')
  .write()

This will create these files and directories:

├── a
│    ├── c.txt
│    ├── d.md
│    └── e
│        ├── f
│        └── g
└── .b

Updating existing file content

To update file content provide a function like so:

new AFST({path: '/some/kind/of/path'})
  .file('c.txt', 'Content always replaces content')
  .file('d.txt', content =>
    content
      .replace("foo", "bar")
      .replace("baz", "baq")
  )
  .write()

If a file exists the content will be read and provided as a string to your function during writing.

Conditional directories and files

new AFST({path: '/some/kind/of/path'})
  .file_if(false, "1.txt", 'This file is not created')
  .file_if(true, "2.txt", 'This file is created')
  .dir_if(true, dir =>
    dir
      .file("a.txt", 'This file is created')
      .file("b.txt", 'This file is created')
  )
  .dir_if(false, dir =>
    dir
      .file("a.txt", 'This file is not created')
      .file("b.txt", 'This file is not created')
  )
  .file_unless(false, "3.txt", 'You can flip the condition with file_unless')
  .dir_unless(false, 'four', 'You can flip the condition with dir_unless')
  .write()