-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
75 lines (64 loc) · 2.88 KB
/
index.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
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
var path = require('path'),
mv = require('mv'),
del = require('del'),
Promise = require('pinkie-promise'),
magicPlatform = require('enb-magic-platform'),
installBowerDeps = require('./lib/install-bower-deps'),
introspect = require('./lib/introspect'),
generateDataJson = require('./lib/generate-data-json'),
bemConfig = require('bem-config')(),
config = bemConfig.moduleSync('bem-lib-site') || {};
module.exports = function(pathToLib, version) {
var initialCwd = process.cwd(),
absPathToLib = path.resolve(pathToLib);
// NOTE: needed for magicPlatform
process.chdir(__dirname);
var lib, packageJson, bowerJson;
try {
packageJson = require(path.join(absPathToLib, 'package'));
bowerJson = require(path.join(absPathToLib, 'bower'));
lib = packageJson.name || bowerJson.name;
version || (version = packageJson.version);
} catch(err) {}
lib || (lib = path.basename(pathToLib));
typeof version === 'undefined' && (version = '');
var libConfig = config.libs && config.libs[lib],
libVer = lib + (version ? '-' + version : '');
process.env.BEM_LIB_SITE_PATH = absPathToLib;
process.env.BEM_LIB_SITE_LIB = libVer;
process.env.BEM_TEMPLATE_ENGINE = (libConfig && libConfig.templateEngine) || config.templateEngine;
return installBowerDeps(absPathToLib)
.then(function() {
return Promise.all([
introspect(absPathToLib),
magicPlatform.runTasks(['examples', 'docs'])
]);
})
.then(function(data) {
// move built data to dest folder
// NOTE: there's no obvious way to build it there beforehand with magicPlatform
return new Promise(function(resolve, reject) {
var destPath = path.resolve(initialCwd,
config.data && config.data.outputFolder || 'output-data', libVer),
tempFolder = config.data && config.data.tempFolder || 'tmp';
del(destPath, { force: true }).then(function() {
mv(path.resolve(tempFolder, 'data', libVer), destPath, { mkdirp: true }, function(err) {
if (err) return reject(err);
Promise.all([
del(tempFolder),
generateDataJson(destPath, lib, version, absPathToLib)
]).then(function() {
process.chdir(initialCwd);
console.log('Data was collected at', destPath);
resolve({
path: path.join(destPath)
});
}).catch(reject);
});
}).catch(reject);
});
})
.catch(function(err) {
console.error(err.stack);
});
};