UNPKG

2.82 kBJavaScriptView Raw
1var PATH = require('path');
2var VOW = require('vow');
3var VOWFS = require('vow-fs');
4
5exports.pathToUnix = function(path) {
6 if (PATH.sep === '\\') return path.replace(/\\/g, '/');
7 return path;
8};
9
10exports.writeFile = function(output, res) {
11
12 return VOW.when(res)
13 .then(function(res){
14
15 // save res to file
16 if (typeof output === 'string') {
17 return VOWFS.write(output, res);
18 }
19
20 // write res to writable stream of opened file
21 var defer = VOW.defer();
22
23 // output res to stdout
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
50exports.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 * Check if url processable
59 * @description
60 * Check whetever url is not absolute: http://example.com, //example.com, /favicon.ico
61 * @param {string} url URL to check
62 * @returns {boolean}
63 */
64exports.isLinkProcessable = function(url) {
65 return !(~['#', '?', '/'].indexOf(url.charAt(0)) || isAbsoluteUrl(url));
66};
67
68function isAbsoluteUrl(url) {
69 return /^\w+:/.test(url);
70}
71
72function _getTech(tech, use_cwd) {
73 if (!tech) {
74 return require('./tech');
75 }
76
77 // load tech from given path
78 // borschik -t ./somepath/tech.js
79 if (/^[\/.]/.test(tech)) {
80 return require(PATH.resolve(tech));
81 }
82
83 // load tech from given path
84 try {
85 return require('./techs/' + tech);
86 } catch(e) {
87 }
88
89 // add more node_modules paths
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 // try to load external tech from npm "borschik-tech-<mytech-name>"
100 tech = require('borschik-tech-' + tech);
101 if (use_cwd) {
102 module.paths = old_paths;
103 }
104 return tech;
105}
106
107exports.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};