Giter Site home page Giter Site logo

temporarily's Introduction

temporarily

Create temporary directories and files.

npm version npm version Build Status Coverage Status dependencies Status


Install

npm install --save temporarily

Why

There are a few other temporary file creation utilities. Here's why I made my own.

  • cleanup by default (on process exit), no opt-out (though you may clean up early if necessary)
  • sync by default, meant for testing
  • easy nested dir / file scaffolding with content

Usage

const temp = require('temporarily');
// or
const { dir, file, filepath } = require('temporarily');
// or
import { dir, file, filepath } from 'temporarily';

API

filepath

Generate a filepath. No file is created.

filepath( [options:object] )

  • options.dir :string – default os.tmpdir()
  • options.ext :string
  • options.name :string – default temporarily-{wwwwdddd}
filepath();
// '/var/folders/30/T/temporarily-tkEK6023'

filepath({ ext: 'json' });
// '/var/folders/30/T/temporarily-tkEK6023.json'

filepath({ dir: os.homedir() });
// '/home/myuser/temporarily-tkEK6023'

filepath({ name: 'file-{wwdd}' });
// '/var/folders/30/T/file-tk60'

file

Create a temporary file.

file( [options:object] )

  • options.data :string|Buffer – default ''
  • options.encoding :string – default 'utf8'
  • options.mode :string – default 0o666

All options from filepath can be applied as well.

file();
// { data: '',
//   filepath: '/var/folders/30/T/temporarily-RdgC6481',
//   mode: 438,
//   cleanup: [Function: cleanup] }

file({ mode: 0o777 });
// { data: '',
//   filepath: '/var/folders/30/T/temporarily-RdgC6481',
//   mode: 511,
//   cleanup: [Function: cleanup] }

file({ data: 'Hello World!' }); // write file contents
// { data: 'Hello World!',
//   filepath: '/var/folders/30/T/temporarily-RdgC6481',
//   mode: 438,
//   cleanup: [Function: cleanup] }

dir

Create a temporary directory.

dir( [options:object], [children:Array<object>] )

  • options.mode :string – default 0o777

All options from filepath can be applied as well.

dir();
// { filepath: '/var/folders/30/T/temporarily-tkEK6023',
//   mode: 511,
//   cleanup: [Function: cleanup] }

dir({ dir: os.homedir() });
// { filepath: '/home/myuser/temporarily-tkEK6023',
//   mode: 511,
//   cleanup: [Function: cleanup] }

dir({ mode: 0o666 });
// { filepath: '/var/folders/30/T/temporarily-tkEK6023',
//   mode: 438,
//   cleanup: [Function: cleanup] }

dir({ name: 'tempo' }, [
  dir([
    file({ name: 'nestedFile' }),
  ]),
  file({ data: 'Hello World!' }),
])
// { filepath: '/var/folders/30/T/tempo',
//   mode: 511,
//   cleanup: [Function: cleanup],
//   children:
//    [ { filepath: '/var/folders/30/T/tempo/temporarily-MwpX5662',
//        mode: 511,
//        cleanup: [Function: cleanup],
//        children:
//         [ { data: '',
//             filepath: '/var/folders/30/T/tempo/temporarily-MwpX5662/nestedFile',
//             mode: 438,
//             cleanup: [Function: cleanup] } ] },
//      { data: 'Hello World!',
//        filepath: '/var/folders/30/T/tempo/temporarily-yxYz6104',
//        mode: 438,
//        cleanup: [Function: cleanup] } ] }

temporarily's People

Contributors

moeriki avatar ryaninvents avatar

Stargazers

 avatar

Watchers

 avatar  avatar

temporarily's Issues

Document cleanup

Hello!

I see that cleanup is being exported, which is useful in some testing scenarios. However, it's not documented in the README and I wonder if it's safe for me to assume that it's part of the public API.

Thanks.

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

fs.js:167
    throw new ERR_INVALID_CALLBACK();
    ^

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
    at makeCallback (fs.js:167:11)
    at Object.fs.rmdir (fs.js:810:14)
    at D:\Dev\config-locator\node_modules\temporarily\build\index.js:91:20
    at Array.forEach (<anonymous>)
    at process.cleanup (D:\Dev\config-locator\node_modules\temporarily\build\ind
ex.js:87:9)
    at process.emit (events.js:187:15)
    at processEmit [as emit] (D:\Dev\config-locator\node_modules\nyc\node_module
s\signal-exit\index.js:149:35)

It seems that it should use the synced version of rmdir:
https://github.com/Moeriki/temporarily/blob/44831a329368ac7456f459fa96d8b845f7ecfc0d/build/index.js#L91

node v10.4.1

Duplicated filename problem

If the file tree contains duplicated filename, it would fail to create the file tree in some cases. This would work:

const {dir, file} = require("temporarily");
dir([
    dir([
        file({name: "foo.txt"})
    ]),
    file({name: "foo.txt"})
]);

https://runkit.com/embed/7z186vonwegw

This would not:

const {dir, file} = require("temporarily");
dir([
    file({name: "foo.txt"}),
    dir([
        file({name: "foo.txt"})
    ])
]);

https://runkit.com/embed/agf3dwbmamjs

The first foo.txt is overwritten by the second foo.txt. I think:

  1. The module should throw an error when a file is overwritten that it can't handle this situation.
  2. Maybe adding an option finalName? which acts like a filename but it is only used in renaming (i.e. use a random name for creation), so the overwritten would never happen.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.