-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·90 lines (76 loc) · 2.07 KB
/
Copy pathcli.js
File metadata and controls
executable file
·90 lines (76 loc) · 2.07 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var path = require('path');
var _ = require('lodash');
var filenamify = _.partialRight(require('filenamify'), {replacement: '_'});
var superb = require('superb');
var procreateSwatchGenerator = require('./');
// var filenamify = _.partialRight(require('filenamify'), {replacement: '_'});
var Promise = require('bluebird');
var isDir = Promise.promisify(require('is-dir'));
var fs = Promise.promisifyAll(require('fs'));
var randomFilename = function() {
return filenamify(_.startCase('My ' + superb() + ' Swatch'));
};
var validateDirectory = function(directory) {
var originalVal = _.constant(directory);
return Promise.resolve(directory)
.then(isDir)
.then(originalVal)
.catch(
function(err) {
originalVal = process.cwd;
return originalVal();
}
)
.then(
function(directory) {
return fs.accessAsync(directory, fs.constants.W_OK);
}
)
.then(
function() {
return originalVal();
}
);
};
var validateFilename = function(filename, directory) {
var filePath = path.join(directory, filenamify(filename + '.swatches'));
return fs.accessAsync(filePath)
.then(
function() {
// There was no error, which means the file exists
// so we'll need to keep trying
return validateFilename(randomFilename(), directory);
}
)
// if there is an error, it means the name doesn't exist
// so let's use that
.catch(_.constant(filePath));
};
module.exports = function(input, options) {
if (_.isString(options)) {
options = {
filename: options
};
}
options = options || {};
var filename = options.filename;
var outputDirectory = options.outputDirectory;
if (filename && !outputDirectory) {
outputDirectory = path.dirname(filename);
filename = path.basename(filename, '.swatches');
}
if (!filename) {
filename = randomFilename();
}
return validateDirectory(outputDirectory)
.then(_.partial(validateFilename, filename))
.then(
function(filePath) {
var name = path.basename(filePath, '.swatches');
return {
stream: procreateSwatchGenerator(input, name),
filePath: filePath
};
}
);
};