1 | # mktemp
|
2 |
|
3 | [![Build Status](https://travis-ci.org/sasaplus1/mktemp.svg?branch=master)](https://travis-ci.org/sasaplus1/mktemp)
|
4 | [![npm version](https://badge.fury.io/js/mktemp.svg)](https://badge.fury.io/js/mktemp)
|
5 | [![Try mktemp on RunKit](https://badge.runkitcdn.com/mktemp.svg)](https://npm.runkit.com/mktemp)
|
6 | [![renovate](https://badges.renovateapi.com/github/sasaplus1/mktemp)](https://renovatebot.com)
|
7 |
|
8 | mktemp command for node.js
|
9 |
|
10 | ## Installation
|
11 |
|
12 | ```console
|
13 | $ npm install mktemp
|
14 | ```
|
15 |
|
16 | ## Usage
|
17 |
|
18 | ```js
|
19 | var mktemp = require('mktemp');
|
20 |
|
21 | mktemp.createFile('XXXXX.txt', function(err, path) {
|
22 | if (err) throw err;
|
23 |
|
24 | // path match a /^[\da-zA-Z]{5}\.txt$/
|
25 | console.log(path);
|
26 | });
|
27 |
|
28 | // return value match a /^[\da-zA-Z]{5}\.tmp$/
|
29 | mktemp.createFileSync('XXXXX.tmp');
|
30 |
|
31 | mktemp.createDir('XXXXXXX', function(err, path) {
|
32 | if (err) throw err;
|
33 |
|
34 | // path match a /^[\da-zA-Z]{7}$/
|
35 | console.log(path);
|
36 | });
|
37 |
|
38 | // return value match a /^XXX-[\da-zA-Z]{3}$/
|
39 | mktemp.createDirSync('XXX-XXX');
|
40 | ```
|
41 |
|
42 | if support Promise, can use Promise style.
|
43 |
|
44 | ```js
|
45 | var mktemp = require('mktemp');
|
46 |
|
47 | mktemp
|
48 | .createFile('XXXXX.txt')
|
49 | .then(function(path) {
|
50 | // path match a /^[\da-zA-Z]{5}\.txt$/
|
51 | console.log(path);
|
52 | })
|
53 | .catch(function(err) {
|
54 | console.error(err);
|
55 | });
|
56 |
|
57 | mktemp
|
58 | .createDir('XXXXX')
|
59 | .then(function(path) {
|
60 | // path match a /^[\da-zA-Z]{5}$/
|
61 | console.log(path);
|
62 | })
|
63 | .catch(function(err) {
|
64 | console.error(err);
|
65 | });
|
66 | ```
|
67 |
|
68 | mktemp functions are replace to random string from placeholder "X" in template. see example:
|
69 |
|
70 | ```js
|
71 | mktemp.createFileSync('XXXXXXX'); // match a /^[\da-zA-Z]{7}$/
|
72 | mktemp.createFileSync('XXX.tmp'); // match a /^[\da-zA-Z]{3}\.tmp$/
|
73 | mktemp.createFileSync('XXX-XXX'); // match a /^XXX-[\da-zA-Z]{3}$/
|
74 | ```
|
75 |
|
76 | ## Functions
|
77 |
|
78 | ### createFile(template[, callback])
|
79 |
|
80 | * `template`
|
81 | * `String` - filename template
|
82 | * `callback`
|
83 | * `function(err, path)` - callback function
|
84 | * `err` : `Error|Null` - error object
|
85 | * `path` : `String` - path
|
86 |
|
87 | create blank file of unique filename. permission is `0600`.
|
88 |
|
89 | it throws TypeError if node.js unsupported Promise and callback is not a function.
|
90 |
|
91 | ### createFileSync(template)
|
92 |
|
93 | * `template`
|
94 | * `String` - filename template
|
95 | * `return`
|
96 | * `String` - path
|
97 |
|
98 | sync version createFile.
|
99 |
|
100 | ### createDir(template[, callback])
|
101 |
|
102 | * `template`
|
103 | * `String` - dirname template
|
104 | * `callback`
|
105 | * `function(err, path)` - callback function
|
106 | * `err` : `Error|Null` - error object
|
107 | * `path` : `String` - path
|
108 |
|
109 | create directory of unique dirname. permission is `0700`.
|
110 |
|
111 | it throws TypeError if node.js unsupported Promise and callback is not a function.
|
112 |
|
113 | ### createDirSync(template)
|
114 |
|
115 | * `template`
|
116 | * `String` - dirname template
|
117 | * `return`
|
118 | * `String` - path
|
119 |
|
120 | sync version createDir.
|
121 |
|
122 | ## Contributors
|
123 |
|
124 | * [Michael Ficarra](https://github.com/michaelficarra)
|
125 | * [rjz](https://github.com/rjz)
|
126 |
|
127 | ## License
|
128 |
|
129 | The MIT license.
|