mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Add a function to make a temporary directory we can write to.
This commit is contained in:
@@ -297,6 +297,32 @@ var files = module.exports = {
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Make a temporary directory. Returns the path to the newly created
|
||||
// directory. Caller is responsible for deleting the directory later.
|
||||
mkdtemp: function (prefix) {
|
||||
prefix = prefix || 'meteor-temp-';
|
||||
// find /tmp
|
||||
var tmp_dir = _.first(_.map(['TMPDIR', 'TMP', 'TEMP'], function (t) {
|
||||
return process.env[t];
|
||||
}).filter(_.identity)) || '/tmp';
|
||||
tmp_dir = fs.realpathSync(tmp_dir);
|
||||
|
||||
// make the directory. give it 3 tries in case of collisions from
|
||||
// crappy random.
|
||||
var tries = 3;
|
||||
while (tries > 0) {
|
||||
var dir_path = path.join(
|
||||
tmp_dir, prefix + (Math.random() * 0x100000000 + 1).toString(36));
|
||||
try {
|
||||
fs.mkdirSync(dir_path, 0755);
|
||||
return dir_path;
|
||||
} catch (err) {
|
||||
tries--;
|
||||
}
|
||||
}
|
||||
throw new Error("failed to make tempory directory in " + tmp_dir);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user