Coder Social home page Coder Social logo

temp-write's Introduction

temp-write

Write string/buffer/stream to a random temp file

Install

$ npm install temp-write

Usage

import fs from 'node:fs';
import tempWrite from 'temp-write';

const filePath = tempWrite.sync('unicorn');
//=> '/var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b'

fs.readFileSync(filePath, 'utf8');
//=> 'unicorn'

tempWrite.sync('unicorn', 'pony.png');
//=> '/var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b/pony.png'

tempWrite.sync('unicorn', 'rainbow/cake/pony.png');
//=> '/var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b/rainbow/cake/pony.png'

API

tempWrite(fileContent, filePath?)

Returns a Promise for the file path of the temp file.

tempWrite.sync(fileContent, filePath?)

Returns the file path of the temp file.

fileContent

Type: string | Buffer | stream.Readable

Data to write to the temp file. Streams are supported only with the async API.

filePath

Type: string
Examples: 'img.png' 'foo/bar/baz.png'

Optionally supply a file path which is appended to the random path.

Related

  • tempy - Get a random temporary file or directory path

Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

temp-write's People

Contributors

bendingbender avatar coreyfarrell avatar samverschueren avatar sholladay avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

temp-write's Issues

On macOS, os.tmpdir() isn't the real path

❯ node --version
v7.4.0
❯ node -pe 'os.tmpdir()'
/var/folders/_6/p8qxp_3n62zg9081tvb0lcc80000gn/T
❯ node -pe 'fs.realpathSync(os.tmpdir())'
/private/var/folders/_6/p8qxp_3n62zg9081tvb0lcc80000gn/T

When writing a Node.js module using temp-write, __filename inside that module is different than the file path that is returned by temp-write.

Using macOS Sierra 10.12.3.

Support streams

Would you be open to allowing input to be a stream?

In an Electron app, I have a little save-url module that looks like this:

const fs = require('fs');
const path = require('path');
const got = require('got');
const tempfile = require('tempfile');

const saveUrl = (urlStr) => {
    const filePath = tempfile(path.extname(urlStr));

    return new Promise((resolve, reject) => {
        got.stream(urlStr)
            .pipe(fs.createWriteStream(filePath))
            .on('error', reject)
            .on('finish', () => {
                resolve(filePath);
            });
    });
};

I based this on some very similar code from you in wallpaper-cli.

Both of these would benefit if temp-write supported taking a stream as input.

In my case, my module would be reduced to:

const path = require('path');
const got = require('got');
const tempWrite = require('temp-write');

const saveUrl = (urlStr) => {
    return tempWrite(got.stream(urlStr), path.basename(urlStr));
};

Error ERR_REQUIRE_ESM

I get the following error:

Uncaught Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\MyApp\node_modules\temp-write\index.js

I Use Latest NodeJS: 16.2.0
I Use Latest temp-write: 5.0.0

Path instead of fileName

I can't use this module because filename is not descriptive enough. For instance, this is perfect readable:

/var/folders/j3/5b59rnl11p98dhkyfv5b9nkr0000gn/T/1396396004094.2021/client/app/js/main.js:12: WARNING - app.ok is never defined
>>   return console.log(app.ok());

while this is no go:

/var/folders/j3/5b59rnl11p98dhkyfv5b9nkr0000gn/T/3a4238c7-cfb2-48a7-a413-194a7adcb962/59rnl11p98dhkyfv5b9n.js:11: ERROR - variable global is undeclared
  return global.React = require('react');

Which file failed? I don't know.

Cannot use import statement outside a module

Hello, I apologise for this question as it may be unrelated to your code and more because of my inexperience with JavaScript modules.

Recently I changed from temp-write 4.x to 5.x and encountered the following error:

Cannot use import statement outside a module

/Users/samuel/.atom/packages/script-runner/node_modules/temp-write/index.js:1
(function (exports, require, module, __filename, __dirname, process, global, Buffer) { return function (exports, require, module, __filename, __dirname) { import {promisify} from 'node:util';
                                                                                                                                                           ^^^^^^
SyntaxError: Cannot use import statement outside a module
    at new Script (vm.js:84:7)

It seems like maybe I'm using this incorrectly? But going back to 4.x the error goes away.

Windows CI

Does this work on Windows? This does work on Windows. You're not using a Windows-based CI (AppVeyor) for anyone to see if tests pass.

Promisify the library

Remove the callback and add promises.

Will add a PR for this (which includes AVA for testing).

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.