-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathcreate-and-step-into-directory.js
55 lines (48 loc) · 1.4 KB
/
create-and-step-into-directory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
// Creates a directory with the name directoryName in cwd and then sets cwd to
// this directory.
var Promise = require('../ext/promise');
var fs = require('fs');
var mkdir = Promise.denodeify(fs.mkdir);
var Task = require('../models/task');
var SilentError = require('silent-error');
function existsSync(path) {
try {
fs.accessSync(path);
return true;
}
catch (e) {
return false;
}
}
module.exports = Task.extend({
// Options: String directoryName, Boolean: dryRun
warnDirectoryAlreadyExists: function warnDirectoryAlreadyExists() {
var message = 'Directory \'' + this.directoryName + '\' already exists.';
return new SilentError(message);
},
run: function(options) {
var directoryName = this.directoryName = options.directoryName;
if (options.dryRun) {
return new Promise(function(resolve, reject) {
if (existsSync(directoryName)) {
return reject(this.warnDirectoryAlreadyExists());
}
resolve();
}.bind(this));
}
return mkdir(directoryName)
.catch(function(err) {
if (err.code === 'EEXIST') {
throw this.warnDirectoryAlreadyExists();
} else {
throw err;
}
}.bind(this))
.then(function() {
var cwd = process.cwd();
process.chdir(directoryName);
return { initialDirectory: cwd };
});
}
});