ts-query
Version:
its simple library for dom control.
125 lines • 4.35 kB
JavaScript
/// <reference path="../type/fs-extra.d.ts" />
/// <reference path="../type/es6-promise.d.ts" />
var fs = require('fs-extra');
var path = require('path');
var PromiseModule = require('es6-promise');
var Promise = PromiseModule.Promise;
var Make = (function () {
function Make() {
this.path = path.join('./');
this.tasks = ['dom', 'events', 'css', 'animation'];
this.paths = {
dom: {
path: path.join('src/Query-dom.js'),
name: 'Query-dom.js'
},
events: {
path: path.join('src/Query-events.js'),
name: 'Query-events.js'
},
css: {
path: path.join('src/Query-css.js'),
name: 'Query-css.js'
},
animation: {
path: path.join('src/Query-animation.js'),
name: 'Query-animation.js'
},
build: path.join('build')
};
}
Make.prototype.dom = function () {
return Make.copy(this.paths.dom.path, path.join(this.paths.build, this.paths.dom.name));
};
Make.prototype.build = function (tasks, callback) {
var _this = this;
if (!fs.existsSync('build')) {
fs.mkdirpSync(('build'));
}
tasks = this.checkTasks(tasks);
this.dom().then(function () {
Make.read(path.join(_this.paths.build, _this.paths.dom.name)).then(function (domSource) {
return Promise.all(tasks.map(function (task) {
return _this.make(_this.paths[task].path);
})).then(function (filesSources) {
filesSources.unshift(domSource);
return Make.concat(filesSources);
}).then(function (fileText) {
Make.write(path.join(_this.paths.build, 'query.js'), fileText).then(callback);
});
});
});
};
Make.prototype.checkTasks = function (tasks) {
var _this = this;
return tasks.filter(function (task) {
if (_this.tasks.indexOf(task) == -1) {
console.warn('Неожиданный таск!');
return false;
}
else {
return true;
}
}).filter(function (task) { return (task !== 'dom'); });
};
Make.prototype.make = function (filePath) {
var _this = this;
return Make.read(filePath).then(function (fileText) {
var files = (fileText.match(/\/\/\/IMPORT:(.+)/g) || []).map(function (localPath) {
return Make.read(path.join(_this.path, localPath.replace('///IMPORT:', '')));
});
return Promise.all(files).then(function (filesTexts) {
filesTexts.forEach(function (text) {
fileText = fileText.replace(/\/\/\/IMPORT:(.+)/, text);
});
return fileText;
});
});
};
Make.concat = function (sources) {
return sources.join('\n');
};
Make.copy = function (from, to) {
return new Promise(function (resolve, reject) {
fs.copy(from, to, function (err) {
if (err) {
console.error(err.toString());
reject(err);
}
else {
resolve();
}
});
});
};
Make.read = function (path) {
return new Promise(function (resolve, reject) {
fs.readFile(path, 'utf-8', function (err, text) {
if (err) {
console.error(err.toString());
reject(err);
}
else {
resolve(text);
}
});
});
};
Make.write = function (path, data) {
return new Promise(function (resolve, reject) {
fs.writeFile(path, data, 'utf-8', function (err) {
if (err) {
console.error(err.toString());
reject(err);
}
else {
resolve();
}
});
});
};
return Make;
})();
var maker = new Make();
module.exports = maker;
//# sourceMappingURL=Make.js.map