forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate-and-step-into-directory.js
46 lines (40 loc) · 1.32 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
'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 existsSync = require('exists-sync');
var mkdir = Promise.denodeify(fs.mkdir);
var Task = require('../models/task');
var SilentError = require('silent-error');
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 };
});
}
});