Skip to content
Browse files

First commit

  • Loading branch information...
0 parents commit 50fb087f21980d425e4f7e4b6d8735d47236416b @vesln committed
Showing with 811 additions and 0 deletions.
  1. +22 −0 .gitignore
  2. +3 −0 .travis.yml
  3. +12 −0 Makefile
  4. +82 −0 Readme.md
  5. 0 dir.js
  6. +18 −0 examples/dir.js
  7. +26 −0 examples/file.js
  8. 0 file.js
  9. +26 −0 index.js
  10. +80 −0 lib/base.js
  11. +32 −0 lib/detector.js
  12. +57 −0 lib/dir.js
  13. +113 −0 lib/file.js
  14. +46 −0 lib/generator.js
  15. +25 −0 package.json
  16. +54 −0 test/base.test.js
  17. +23 −0 test/detector.test.js
  18. +41 −0 test/dir.test.js
  19. +123 −0 test/file.test.js
  20. +28 −0 test/generator.test.js
22 .gitignore
@@ -0,0 +1,22 @@
+tmp
+node_modules
+*._
+*.tmp
+.monitor
+*.diff
+*.err
+*.orig
+*.log
+*.rej
+*.swo
+*.swp
+*.vi
+*~
+.DS_Store
+Thumbs.db
+.cache
+.project
+.settings
+.tmproj
+*.esproj
+nbproject
3 .travis.yml
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - 0.6
12 Makefile
@@ -0,0 +1,12 @@
+TESTS = test/*.test.js
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha \
+ --require should \
+ --reporter spec \
+ $(TESTS)
+
+clean:
+ rm -f examples/tmp/*
+
+.PHONY: test clean
82 Readme.md
@@ -0,0 +1,82 @@
+[![Build Status](https://secure.travis-ci.org/vesln/temporary.png)](http://travis-ci.org/vesln/temporary)
+
+# temporary - The lord of tmp.
+
+## Intro
+
+Temporary provides an easy way to create temporary files and directories.
+It will create a temporary file/directory with a unique name.
+
+## Features
+
+- Generates unique name.
+- Auto-discovers tmp dir.
+
+## Installation
+
+ $ npm install temporary
+
+## Usage
+
+ var Tempfile = require('tepmorary/file');
+ var Tempdir = require('tepmorary/dir');
+ var file = new Tempfile;
+ var dir = new Tempdir;
+
+ console.log(file.path); // path.
+ console.log(dir.path); // path.
+
+ file.unlink();
+ dir.rmdir();
+
+## Mehtods
+
+### File
+
+- File.readFile
+- File.readFileSync
+- File.writeFile
+- File.writeFileSync
+- File.open
+- File.openSync
+- File.close
+- File.closeSync
+- File.unlink
+- File.unlinkSync
+
+### Dir
+
+- Dir.rmdir
+- Dir.rmdirSync
+
+## Tests
+
+ $ make test
+
+## Contribution
+
+Bug fixes and features are welcomed.
+
+## License
+
+MIT License
+
+Copyright (C) 2012 Veselin Todorov
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
0 dir.js
No changes.
18 examples/dir.js
@@ -0,0 +1,18 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+var Tempdir = require('../lib/dir');
+var dir = new Tempdir('foo') // name - optional
+
+console.log(dir.path); // path.
+
+/**
+ * You can also use:
+ *
+ * dir.rmdir
+ * dir.rmdirSync
+ */
26 examples/file.js
@@ -0,0 +1,26 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+var Tempfile = require('../lib/file');
+var file = new Tempfile('foo') // name - optional
+
+console.log(file.path); // file path.
+
+/**
+ * You can also use:
+ *
+ * file.readFile
+ * file.readFileSync
+ * file.writeFile
+ * file.writeFileSync
+ * file.open
+ * file.openSync
+ * file.close
+ * file.closeSync
+ * file.unlink
+ * file.unlinkSync
+ */
0 file.js
No changes.
26 index.js
@@ -0,0 +1,26 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var package = require('package')();
+
+/**
+ * Version.
+ */
+module.exports.version = package.version;
+
+/**
+ * Exporting the temp file
+ */
+module.exports.File = require('./file');
+
+/**
+ * Exporting the temp directory.
+ */
+module.exports.Dir = require('./dir');
80 lib/base.js
@@ -0,0 +1,80 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var fs = require('fs');
+var path = require('path');
+var generator = require('./generator');
+var detector = require('./detector');
+
+/**
+ * Base constructor.
+ *
+ * @param {String|null} name
+ */
+function Base(name) {
+ this.init(name);
+};
+
+/**
+ * Initializes the class.
+ *
+ * @param {String|null} name
+ */
+Base.prototype.init = function(name) {
+ var filename = generator.build(name);
+ this.create(filename);
+ this.path = filename;
+};
+
+/**
+ * Converts the arguments object to array and
+ * append `this.path` as first element.
+ *
+ * @returns {Array}
+ */
+Base.prototype.prepareArgs = function(args) {
+ args = Array.prototype.slice.call(args);
+ args.unshift(this.path);
+ return args;
+};
+
+/**
+ * Renames the dir/file.
+ *
+ * @param {String} name
+ * @param {Function} cb Callback.
+ */
+Base.prototype.rename = function(name, cb) {
+ var self = this;
+ var args = arguments;
+ var tmp = path.normalize(path.dirname(self.path) + '/' + name);
+
+ fs.rename(this.path, tmp, function(err) {
+ self.path = tmp;
+ if (args.length === 2) cb(err);
+ });
+};
+
+/**
+ * Renames the dir/file sync.
+ *
+ * @param {String} name
+ */
+Base.prototype.renameSync = function(name) {
+ var tmp = path.normalize(path.dirname(this.path) + '/' + name);
+ var result = fs.renameSync(this.path, tmp);
+ this.path = tmp;
+ return result;
+};
+
+/**
+ * Exporting the lib.
+ */
+module.exports = Base;
32 lib/detector.js
@@ -0,0 +1,32 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Detection stolen from NPM (https://github.com/isaacs/npm/)
+ *
+ * Copyright 2009, 2010, 2011 Isaac Z. Schlueter (the "Author")
+ * MIT License (https://github.com/isaacs/npm/blob/master/LICENSE)
+ */
+
+/**
+ * Detector namespace.
+ *
+ * @type {Object}
+ */
+var detector = module.exports;
+
+/**
+ * Returns tmp dir. Thank you npm.
+ *
+ * @returns {String} tmp dir.
+ */
+detector.tmp = function() {
+ return process.env.TMPDIR
+ || process.env.TMP
+ || process.env.TEMP
+ || (process.platform === "win32" ? "c:\\windows\\temp" : "/tmp")
+};
57 lib/dir.js
@@ -0,0 +1,57 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var fs = require('fs');
+var path = require('path');
+var generator = require('./generator');
+var detector = require('./detector');
+var Base = require('./base');
+
+/**
+ * Dir constructor.
+ *
+ * @param {String|null} name
+ */
+function Dir(name) {
+ this.init(name);
+};
+
+/**
+ * Dir extends from tmp.
+ */
+Dir.prototype.__proto__ = Base.prototype;
+
+/**
+ * Creates new file.
+ *
+ * @param {String} dirname
+ */
+Dir.prototype.create = function(dirname) {
+ return fs.mkdirSync(path.normalize(dirname), 0777);
+};
+
+/**
+ * Asynchronous dir.
+ */
+Dir.prototype.rmdir = function() {
+ fs.rmdir.apply(fs, this.prepareArgs(arguments));
+};
+
+/**
+ * Synchronous rmdir.
+ */
+Dir.prototype.rmdirSync = function() {
+ return fs.rmdirSync.apply(fs, this.prepareArgs(arguments));
+};
+
+/**
+ * Exporting the lib.
+ */
+module.exports = Dir;
113 lib/file.js
@@ -0,0 +1,113 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var fs = require('fs');
+var path = require('path');
+var generator = require('./generator');
+var detector = require('./detector');
+var Base = require('./base');
+
+/**
+ * File constructor.
+ *
+ * @param {String|null} name
+ */
+function File(name) {
+ this.init(name);
+};
+
+/**
+ * File extends from tmp.
+ */
+File.prototype.__proto__ = Base.prototype;
+
+/**
+ * Creates new file.
+ *
+ * @param {String} filename
+ */
+File.prototype.create = function(filename) {
+ return fs.writeFileSync(path.normalize(filename), '');
+};
+
+/**
+ * Asynchronously reads the entire contents of a file.
+ */
+File.prototype.readFile = function() {
+ fs.readFile.apply(fs, this.prepareArgs(arguments));
+};
+
+/**
+ * Synchronous read.
+ */
+File.prototype.readFileSync = function() {
+ return fs.readFileSync.apply(fs, this.prepareArgs(arguments));
+};
+
+/**
+ * Asynchronously writes data to a file.
+ */
+File.prototype.writeFile = function() {
+ fs.writeFile.apply(fs, this.prepareArgs(arguments));
+};
+
+/**
+ * Synchronous writes data to a file.
+ */
+File.prototype.writeFileSync = function() {
+ return fs.writeFileSync.apply(fs, this.prepareArgs(arguments));
+};
+
+/**
+ * Asynchronous file open.
+ */
+File.prototype.open = function() {
+ fs.open.apply(fs, this.prepareArgs(arguments));
+};
+
+/**
+ * Synchronous open.
+ */
+File.prototype.openSync = function() {
+ return fs.openSync.apply(fs, this.prepareArgs(arguments));
+};
+
+/**
+ * Asynchronous close.
+ */
+File.prototype.close = function() {
+ fs.close.apply(fs, Array.prototype.slice.call(arguments));
+};
+
+/**
+ * Synchronous close.
+ */
+File.prototype.closeSync = function() {
+ return fs.closeSync.apply(fs, Array.prototype.slice.call(arguments));
+};
+
+/**
+ * Asynchronous unlink.
+ */
+File.prototype.unlink = function() {
+ fs.unlink.apply(fs, this.prepareArgs(arguments));
+};
+
+/**
+ * Synchronous unlink.
+ */
+File.prototype.unlinkSync = function() {
+ return fs.unlinkSync.apply(fs, this.prepareArgs(arguments));
+};
+
+/**
+ * Exporting the lib.
+ */
+module.exports = File;
46 lib/generator.js
@@ -0,0 +1,46 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var path = require('path');
+var detector = require('./detector');
+
+/**
+ * Generator namespace.
+ *
+ * @type {Object}
+ */
+var generator = module.exports;
+
+/**
+ * Generates random name.
+ *
+ * @returns {String}
+ */
+generator.name = function() {
+ var id = null;
+ var tmp = detector.tmp();
+ do {
+ id = Date.now() + Math.random();
+ } while(path.existsSync(tmp + '/' + id));
+
+ return id + '';
+};
+
+/**
+ * Buld a full name. (tmp dir + name).
+ *
+ * @param {String} name
+ * @returns {String}
+ */
+generator.build = function(name) {
+ var filename = detector.tmp();
+ if (name) filename += name + '.';
+ return filename + this.name();
+};
25 package.json
@@ -0,0 +1,25 @@
+{
+ "name": "temporary"
+ , "version": "0.0.1"
+ , "description": "The lord of tmp."
+ , "keywords": ["tmp", "temp", "tempfile", "tempdirectory"]
+ , "author": "Veselin Todorov <hi@vesln.com>"
+ , "dependencies": {
+ "package": "0.0.3"
+ }
+ , "devDependencies": {
+ "mocha": "0.3.3"
+ , "should": "0.3.2"
+ , "sinon": "1.2.0"
+ }
+ , "repository" : {
+ "type" : "git",
+ "url" : "http://github.com/vesln/temporary.git"
+ }
+ , "homepage": "http://github.com/vesln/temporary"
+ , "scripts": {
+ "test": "make test"
+ }
+ , "main": "index"
+ , "engines": { "node": ">= 0.6.0 < 0.7.0" }
+}
54 test/base.test.js
@@ -0,0 +1,54 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var path = require('path');
+var fs = require('fs');
+var Base = require('../lib/base');
+var generator = require('../lib/generator');
+
+Base.prototype.create = function() {};
+
+describe('Base', function() {
+ describe('rename', function() {
+ it('should rename the directory', function(done) {
+ var tmp = new Base;
+ tmp.path = generator.build();
+ fs.mkdirSync(path.normalize(tmp.path), 0777);
+ path.existsSync(tmp.path).should.be.ok;
+ tmp.rename('foo', function(err) {
+ path.existsSync(tmp.path).should.be.ok;
+ done();
+ });
+ });
+ });
+
+ describe('renameSync', function() {
+ it('should rename the directory', function() {
+ var tmp = new Base('foo');
+ tmp.path = generator.build();
+ fs.mkdirSync(path.normalize(tmp.path), 0777);
+ var oldPath = tmp.path;
+ path.existsSync(tmp.path).should.be.ok;
+ tmp.renameSync('foo3');
+ path.existsSync(tmp.path).should.be.ok;
+ path.should.not.eql(oldPath);
+ });
+ });
+
+ describe('prepareArgs', function() {
+ it('should convert object to array and append path as first element', function() {
+ var tmp = new Base('foo');
+ var args = { 0: 'foo' };
+ args.length = 1;
+ tmp.path = generator.build();
+ tmp.prepareArgs(args).should.eql([tmp.path, 'foo']);
+ });
+ });
+});
23 test/detector.test.js
@@ -0,0 +1,23 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var detector = require('../lib/detector');
+
+describe('detector', function() {
+ describe('tmp', function() {
+ it('should return the tmp dir of the system', function() {
+ var tmp = process.env.TMPDIR
+ || process.env.TMP
+ || process.env.TEMP
+ || (process.platform === "win32" ? "c:\\windows\\temp" : "/tmp");
+ detector.tmp().should.eql(tmp);
+ });
+ });
+});
41 test/dir.test.js
@@ -0,0 +1,41 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var path = require('path');
+var fs = require('fs');
+var Tempdir = require('../lib/dir');
+var sinon = require('sinon');
+
+describe('Tempdir', function() {
+ it('should create file', function() {
+ var tmp = new Tempdir('foo');
+ path.existsSync(tmp.path).should.be.ok;
+ });
+
+ describe('rmdir', function() {
+ it('should remove the directory', function() {
+ var tmp = new Tempdir('foo');
+ sinon.spy(fs, 'rmdir');
+ tmp.rmdir();
+ fs.rmdir.getCall(0).args[0].should.eql(tmp.path);
+ fs.rmdir.restore();
+ });
+ });
+
+ describe('rmdirSync', function() {
+ it('should remove the directory', function() {
+ var tmp = new Tempdir('foo');
+ sinon.spy(fs, 'rmdirSync');
+ tmp.rmdirSync();
+ fs.rmdirSync.getCall(0).args[0].should.eql(tmp.path);
+ fs.rmdirSync.restore();
+ });
+ });
+});
123 test/file.test.js
@@ -0,0 +1,123 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var path = require('path');
+var fs = require('fs');
+var Tempfile = require('../lib/file');
+var sinon = require('sinon');
+
+describe('Tempfile', function() {
+ it('should create file', function() {
+ var tmp = new Tempfile('foo');
+ path.existsSync(tmp.path).should.be.ok;
+ });
+
+ describe('readFile', function() {
+ it('should call fs.readfile', function() {
+ sinon.spy(fs, 'readFile');
+ var tmp = new Tempfile;
+ tmp.readFile();
+ fs.readFile.getCall(0).args[0].should.eql(tmp.path);
+ fs.readFile.restore();
+ });
+ });
+
+ describe('readFileSync', function() {
+ it('should call fs.readfileSync', function() {
+ sinon.spy(fs, 'readFileSync');
+ var tmp = new Tempfile;
+ tmp.readFileSync();
+ fs.readFileSync.getCall(0).args[0].should.eql(tmp.path);
+ fs.readFileSync.restore();
+ });
+ });
+
+ describe('writeFile', function() {
+ it('should call fs.readfile', function() {
+ sinon.spy(fs, 'writeFile');
+ var tmp = new Tempfile;
+ tmp.writeFile();
+ fs.writeFile.getCall(0).args[0].should.eql(tmp.path);
+ fs.writeFile.restore();
+ });
+ });
+
+ describe('writeFileSync', function() {
+ it('should call fs.writeFileSync', function() {
+ sinon.spy(fs, 'writeFileSync');
+ var tmp = new Tempfile;
+ tmp.writeFileSync();
+ fs.writeFileSync.getCall(0).args[0].should.eql(tmp.path);
+ fs.writeFileSync.restore();
+ });
+ });
+
+ describe('open', function() {
+ it('should call fs.open', function() {
+ sinon.spy(fs, 'open');
+ var tmp = new Tempfile;
+ tmp.open('r');
+ fs.open.getCall(0).args[0].should.eql(tmp.path);
+ fs.open.restore();
+ });
+ });
+
+ describe('openSync', function() {
+ it('should call fs.openSync', function() {
+ sinon.spy(fs, 'openSync');
+ var tmp = new Tempfile;
+ tmp.openSync('r');
+ fs.openSync.getCall(0).args[0].should.eql(tmp.path);
+ fs.openSync.restore();
+ });
+ });
+
+ describe('close', function() {
+ it('should call fs.close', function() {
+ sinon.spy(fs, 'close');
+ var tmp = new Tempfile;
+ var fd = tmp.openSync('r');
+ tmp.close(fd);
+ fs.close.getCall(0).args[0].should.eql(fd);
+ fs.close.restore();
+ });
+ });
+
+ describe('closeSync', function() {
+ it('should call fs.closeSync', function() {
+ sinon.spy(fs, 'closeSync');
+ var tmp = new Tempfile;
+ var fd = tmp.openSync('r');
+ tmp.closeSync(fd);
+ fs.closeSync.getCall(0).args[0].should.eql(fd);
+ fs.closeSync.restore();
+ });
+ });
+
+ describe('unlink', function() {
+ it('should call fs.unlink', function() {
+ sinon.spy(fs, 'unlink');
+ var tmp = new Tempfile;
+ tmp.unlink();
+ fs.unlink.getCall(0).args[0].should.eql(tmp.path);
+ fs.unlink.restore();
+ });
+ });
+
+ describe('unlinkSync', function() {
+ it('should call fs.readfileSync', function() {
+ sinon.spy(fs, 'unlinkSync');
+ var tmp = new Tempfile;
+ tmp.unlinkSync();
+ fs.unlinkSync.getCall(0).args[0].should.eql(tmp.path);
+ fs.unlinkSync.restore();
+ });
+ });
+});
28 test/generator.test.js
@@ -0,0 +1,28 @@
+/**
+ * Temporary - The lord of tmp.
+ *
+ * Author: Veselin Todorov <hi@vesln.com>
+ * Licensed under the MIT License.
+ */
+
+/**
+ * Dependencies.
+ */
+var generator = require('../lib/generator');
+var detector = require('../lib/detector');
+
+describe('generator', function() {
+ describe('name', function() {
+ it('should unique generate name', function() {
+ generator.name().should.be.ok;
+ });
+ });
+
+ describe('build', function() {
+ it('should build full names', function() {
+ var tmp = detector.tmp();
+ generator.build().should.match(new RegExp("^" + tmp));
+ generator.build('foo').should.match(new RegExp("^" + tmp + 'foo.'));
+ });
+ });
+});

0 comments on commit 50fb087

Please sign in to comment.
Something went wrong with that request. Please try again.