123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- const {assert} = require('../assert');
- const npm = {
- fs: require('fs'),
- path: require('path'),
- utils: require('./'),
- package: require('../../package.json')
- };
- function camelize(text) {
- text = text.replace(/[-_\s.]+(.)?/g, (_, c) => c ? c.toUpperCase() : '');
- return text.substring(0, 1).toLowerCase() + text.substring(1);
- }
- function camelizeVar(text) {
- text = text.replace(/[^a-zA-Z0-9$_\-\s.]/g, '').replace(/^[0-9_\-\s.]+/, '');
- return camelize(text);
- }
- function _enumSql(dir, options, cb, namePath) {
- const tree = {};
- npm.fs.readdirSync(dir).forEach(file => {
- let stat;
- const fullPath = npm.path.join(dir, file);
- try {
- stat = npm.fs.statSync(fullPath);
- } catch (e) {
-
-
-
- if (options.ignoreErrors) {
- return;
- }
-
- throw e;
- }
- if (stat.isDirectory()) {
- if (options.recursive) {
- const dirName = camelizeVar(file);
- const np = namePath ? (namePath + '.' + dirName) : dirName;
- const t = _enumSql(fullPath, options, cb, np);
- if (Object.keys(t).length) {
- if (!dirName.length || dirName in tree) {
- if (!options.ignoreErrors) {
- throw new Error('Empty or duplicate camelized folder name: ' + fullPath);
- }
- }
- tree[dirName] = t;
- }
- }
- } else {
- if (npm.path.extname(file).toLowerCase() === '.sql') {
- const name = camelizeVar(file.replace(/\.[^/.]+$/, ''));
- if (!name.length || name in tree) {
- if (!options.ignoreErrors) {
- throw new Error('Empty or duplicate camelized file name: ' + fullPath);
- }
- }
- tree[name] = fullPath;
- if (cb) {
- const result = cb(fullPath, name, namePath ? (namePath + '.' + name) : name);
- if (result !== undefined) {
- tree[name] = result;
- }
- }
- }
- }
- });
- return tree;
- }
- function enumSql(dir, options, cb) {
- if (!npm.utils.isText(dir)) {
- throw new TypeError('Parameter \'dir\' must be a non-empty text string.');
- }
- options = assert(options, ['recursive', 'ignoreErrors']);
- cb = (typeof cb === 'function') ? cb : null;
- return _enumSql(dir, options, cb, '');
- }
- function taskArgs(args) {
- if (!args || typeof args.length !== 'number') {
- throw new TypeError('Parameter \'args\' must be an array-like object of arguments.');
- }
- let options = args[0], cb;
- if (typeof options === 'function') {
- cb = options;
- options = {};
- if (cb.name) {
- options.tag = cb.name;
- }
- } else {
- if (typeof args[1] === 'function') {
- cb = args[1];
- }
- if (typeof options === 'string' || typeof options === 'number') {
- options = {tag: options};
- } else {
- options = (typeof options === 'object' && options) || {};
- if (!('tag' in options) && cb && cb.name) {
- options.tag = cb.name;
- }
- }
- }
- const res = [options, cb];
- Object.defineProperty(res, 'options', {
- get: function () {
- return this[0];
- },
- set: function (newValue) {
- this[0] = newValue;
- },
- enumerable: true
- });
- Object.defineProperty(res, 'cb', {
- get: function () {
- return this[1];
- },
- set: function (newValue) {
- this[1] = newValue;
- },
- enumerable: true
- });
- return res;
- }
- module.exports = {
- camelize,
- camelizeVar,
- enumSql,
- taskArgs
- };
|