1 | var PATH = require('path');
|
2 | var VOW = require('vow');
|
3 | var VOWFS = require('vow-fs');
|
4 |
|
5 | exports.pathToUnix = function(path) {
|
6 | if (PATH.sep === '\\') return path.replace(/\\/g, '/');
|
7 | return path;
|
8 | };
|
9 |
|
10 | exports.writeFile = function(output, res) {
|
11 |
|
12 | return VOW.when(res)
|
13 | .then(function(res){
|
14 |
|
15 |
|
16 | if (typeof output === 'string') {
|
17 | return VOWFS.write(output, res);
|
18 | }
|
19 |
|
20 |
|
21 | var defer = VOW.defer();
|
22 |
|
23 |
|
24 | if (output === process.stdout) {
|
25 | output.write(res);
|
26 | return defer.resolve();
|
27 | }
|
28 |
|
29 | output.on('error', function(err) {
|
30 | defer.reject(err);
|
31 | });
|
32 |
|
33 | output.once('close', function() {
|
34 | defer.resolve();
|
35 | });
|
36 |
|
37 | output.once('end', function() {
|
38 | defer.resolve();
|
39 | });
|
40 |
|
41 | output.write(res);
|
42 | output.end();
|
43 |
|
44 | return defer.promise();
|
45 |
|
46 | });
|
47 |
|
48 | };
|
49 |
|
50 | exports.stringToBoolean = function(s, def) {
|
51 | if (typeof s === 'boolean') return s;
|
52 | if (s == 'yes' || s == 'true') return true;
|
53 | if (s == 'no' || s == 'false') return false;
|
54 | return !!def;
|
55 | };
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | exports.isLinkProcessable = function(url) {
|
65 | return !(~['#', '?', '/'].indexOf(url.charAt(0)) || isAbsoluteUrl(url));
|
66 | };
|
67 |
|
68 | function isAbsoluteUrl(url) {
|
69 | return /^\w+:/.test(url);
|
70 | }
|
71 |
|
72 | function _getTech(tech, use_cwd) {
|
73 | if (!tech) {
|
74 | return require('./tech');
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 | if (/^[\/.]/.test(tech)) {
|
80 | return require(PATH.resolve(tech));
|
81 | }
|
82 |
|
83 |
|
84 | try {
|
85 | return require('./techs/' + tech);
|
86 | } catch(e) {
|
87 | }
|
88 |
|
89 |
|
90 | if (use_cwd) {
|
91 | var old_paths = module.paths.slice();
|
92 | var parts = PATH.resolve().split(PATH.sep);
|
93 | for (var tip = 0, PL = parts.length; tip < PL; tip++) {
|
94 | if (parts[tip] === 'node_modules') continue;
|
95 | module.paths.unshift(parts.slice(0, tip + 1).concat('node_modules').join(PATH.sep));
|
96 | }
|
97 | }
|
98 |
|
99 |
|
100 | tech = require('borschik-tech-' + tech);
|
101 | if (use_cwd) {
|
102 | module.paths = old_paths;
|
103 | }
|
104 | return tech;
|
105 | }
|
106 |
|
107 | exports.getTech = function getTech(tech, use_cwd) {
|
108 | tech = _getTech(tech, use_cwd);
|
109 | if (!tech.Tech) {
|
110 | tech = tech(require('..'));
|
111 | }
|
112 | return tech;
|
113 | };
|