1 | 'use strict';
|
2 |
|
3 | var FS = require('fs'),
|
4 | UTIL = require('util'),
|
5 | bemUtil = require('./util'),
|
6 | PATH = require('./path');
|
7 |
|
8 | exports.Tech = function(path, techName) {
|
9 | this.path = path;
|
10 | var tech = require(this.path);
|
11 | for(var i in tech) tech.hasOwnProperty(i) && (this[i] = tech[i]);
|
12 | if(techName) this.techName = techName;
|
13 | };
|
14 |
|
15 | exports.Tech.prototype.old = true;
|
16 |
|
17 | exports.Tech.setContext = function(ctx) {
|
18 | exports.Tech.prototype.context = ctx;
|
19 | };
|
20 |
|
21 | exports.Tech.prototype.setContext = function(ctx) {
|
22 | this.context = ctx;
|
23 | return this;
|
24 | };
|
25 |
|
26 | exports.Tech.prototype.getContext = function() {
|
27 | return this.context;
|
28 | };
|
29 |
|
30 | exports.Tech.prototype.bemBuild = function(prefixes, outputDir, outputName) {
|
31 | var _this = this,
|
32 | content = this.filterExists(prefixes)
|
33 | .map(function(file) {
|
34 | return _this.outFile(
|
35 | PATH.relative(outputDir, file),
|
36 | file, outputDir, outputName);
|
37 | });
|
38 |
|
39 | var filename = this.fileByPrefix(PATH.join(outputDir, outputName));
|
40 | FS.writeFileSync(filename, content.join(''));
|
41 | };
|
42 |
|
43 | exports.Tech.prototype.bemCreate = function(prefix, vars, force) {
|
44 | var file = this.fileByPrefix(prefix);
|
45 | if(bemUtil.isFile(file) && !force) {
|
46 | UTIL.error('Уже существует ' + file);
|
47 | return;
|
48 | }
|
49 | bemUtil.mkdirs(PATH.dirname(file));
|
50 | FS.writeFileSync(file, this.newFileContent(vars));
|
51 | };
|
52 |
|
53 | exports.Tech.prototype.filterExists = function(prefixes) {
|
54 | var _this = this,
|
55 | res = [];
|
56 | prefixes.forEach(function(prefix){
|
57 | var file = _this.fileByPrefix(prefix);
|
58 | bemUtil.isFile(file) && res.push(file);
|
59 | });
|
60 | return res;
|
61 | };
|
62 |
|
63 | exports.Tech.prototype.getTechName = function() {
|
64 | if(this.techName) return this.techName;
|
65 | return bemUtil.stripModuleExt(PATH.basename(this.getTechPath()));
|
66 | };
|
67 |
|
68 | exports.Tech.prototype.getFileSuffix = function() {
|
69 | return '.' + this.getTechName();
|
70 | };
|
71 |
|
72 | exports.Tech.prototype.fileByPrefix = function(prefix) {
|
73 | return prefix + this.getFileSuffix();
|
74 | };
|
75 |
|
76 | exports.Tech.prototype.matchSuffix = function(suffix) {
|
77 | return this.getFileSuffix() === suffix;
|
78 | };
|
79 |
|
80 | exports.Tech.prototype.outFile = function(file) {
|
81 | return file + '\n';
|
82 | };
|
83 |
|
84 | exports.Tech.prototype.newFileContent = function() {
|
85 | return '';
|
86 | };
|
87 |
|
88 | exports.Tech.prototype.getFileContent = function(prefix) {
|
89 | var file = this.fileByPrefix(prefix);
|
90 | if(bemUtil.isFile(file)) {
|
91 | return FS.readFileSync(file, 'utf8');
|
92 | }
|
93 |
|
94 | return '';
|
95 | };
|
96 |
|
97 | exports.Tech.prototype.getTechRelativePath = function(from) {
|
98 | from = PATH.join(from || '.', PATH.dirSep);
|
99 | var absPath = this.getTechPath(),
|
100 | techPath = PATH.relative(PATH.join(__dirname, PATH.unixToOs('../../')), absPath),
|
101 |
|
102 | testDotRe = new RegExp('^[\\.' + PATH.dirSepRe + ']'),
|
103 | testLibRe = new RegExp('^.*?' + PATH.dirSepRe + 'lib'),
|
104 | replaceRe = new RegExp('^.*?' + PATH.dirSepRe);
|
105 |
|
106 |
|
107 | if(!testDotRe.test(techPath) && testLibRe.test(techPath)) {
|
108 | techPath = techPath.replace(replaceRe, PATH.unixToOs('bem/'));
|
109 | } else {
|
110 |
|
111 | var shortestPath = PATH.relative(from, absPath);
|
112 | shortestPath = shortestPath.split(PATH.dirSep);
|
113 | module.paths.concat(bemUtil.getNodePaths()).forEach(function(reqPath) {
|
114 | var relPath = PATH.relative(PATH.join(reqPath, PATH.dirSep), absPath);
|
115 | if(!/^\./.test(relPath)) {
|
116 | relPath = relPath.split(PATH.dirSep);
|
117 | if(relPath.length < shortestPath.length) {
|
118 | shortestPath = relPath;
|
119 | }
|
120 | }
|
121 | });
|
122 |
|
123 | techPath = PATH.join.apply(null, shortestPath);
|
124 |
|
125 |
|
126 | if(!/^\./.test(techPath)) techPath = '.' + PATH.dirSep + techPath;
|
127 | }
|
128 |
|
129 | techPath = bemUtil.stripModuleExt(techPath);
|
130 |
|
131 |
|
132 | if(techPath === bemUtil.getBemTechPath('default')) return '';
|
133 | return techPath;
|
134 | };
|
135 |
|
136 | exports.Tech.prototype.getTechPath = function() {
|
137 | return this.techModule.filename;
|
138 | };
|