Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 50fb087f21980d425e4f7e4b6d8735d47236416b @vesln committed Jan 1, 2012
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
@@ -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
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - 0.6
@@ -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
@@ -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.
@@ -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
+ */
@@ -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
+ */
No changes.
@@ -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');
@@ -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;
@@ -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")
+};
@@ -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;
Oops, something went wrong.

0 comments on commit 50fb087

Please sign in to comment.