This commit is contained in:
Gorji
2021-06-03 08:36:23 +04:30
parent 80189f9972
commit 061c645544
29 changed files with 758 additions and 1 deletions
+9
View File
@@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CREATE_INDEX_PATTERN = void 0;
const CREATE_INDEX_PATTERN = /(?:^|[\n\r]+)\/\/ @create-index\s?({.*})?[\n\r]+/;
exports.CREATE_INDEX_PATTERN = CREATE_INDEX_PATTERN;
//# sourceMappingURL=constants.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../src/utilities/constants.js"],"names":["CREATE_INDEX_PATTERN"],"mappings":";;;;;;AAAO,MAAMA,oBAAoB,GAAG,kDAA7B","sourcesContent":["export const CREATE_INDEX_PATTERN = /(?:^|[\\n\\r]+)\\/\\/ @create-index\\s?({.*})?[\\n\\r]+/;\n"],"file":"constants.js"}
+62
View File
@@ -0,0 +1,62 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _lodash = _interopRequireDefault(require("lodash"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const safeVariableName = fileName => {
const indexOfDot = fileName.indexOf('.');
if (indexOfDot === -1) {
return fileName;
} else {
return fileName.slice(0, indexOfDot);
}
};
const buildExportBlock = files => {
let importBlock;
importBlock = _lodash.default.map(files, fileName => {
if (fileName.indexOf(".") == -1) return "export * as " + safeVariableName(fileName) + " from './" + fileName + "';";
return "export { default as " + safeVariableName(fileName) + " } from './" + fileName + "';";
});
importBlock = importBlock.join('\n');
return importBlock;
};
var _default = function _default(filePaths) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
let code;
let configCode;
code = '';
configCode = '';
if (options.banner) {
const banners = _lodash.default.isArray(options.banner) ? options.banner : [options.banner];
banners.forEach(banner => {
code += banner + '\n';
});
code += '\n';
}
if (options.config && _lodash.default.size(options.config) > 0) {
configCode += ' ' + JSON.stringify(options.config);
}
code += '// @create-index' + configCode + '\n\n';
if (filePaths.length) {
const sortedFilePaths = filePaths.sort();
code += buildExportBlock(sortedFilePaths) + '\n\n';
}
return code;
};
exports.default = _default;
//# sourceMappingURL=createIndexCode.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../src/utilities/createIndexCode.js"],"names":["safeVariableName","fileName","indexOfDot","indexOf","slice","buildExportBlock","files","importBlock","_","map","join","filePaths","options","code","configCode","banner","banners","isArray","forEach","config","size","JSON","stringify","length","sortedFilePaths","sort"],"mappings":";;;;;;;AAAA;;;;AAEA,MAAMA,gBAAgB,GAAIC,QAAD,IAAc;AACrC,QAAMC,UAAU,GAAGD,QAAQ,CAACE,OAAT,CAAiB,GAAjB,CAAnB;;AAEA,MAAID,UAAU,KAAK,CAAC,CAApB,EAAuB;AACrB,WAAOD,QAAP;AACD,GAFD,MAEO;AACL,WAAOA,QAAQ,CAACG,KAAT,CAAe,CAAf,EAAkBF,UAAlB,CAAP;AACD;AACF,CARD;;AAUA,MAAMG,gBAAgB,GAAIC,KAAD,IAAW;AAClC,MAAIC,WAAJ;AAEAA,EAAAA,WAAW,GAAGC,gBAAEC,GAAF,CAAMH,KAAN,EAAcL,QAAD,IAAc;AACvC,QAAIA,QAAQ,CAACE,OAAT,CAAiB,GAAjB,KAAyB,CAAC,CAA9B,EACE,OACE,iBACAH,gBAAgB,CAACC,QAAD,CADhB,GAEA,YAFA,GAGAA,QAHA,GAIA,IALF;AAOF,WACE,yBACAD,gBAAgB,CAACC,QAAD,CADhB,GAEA,aAFA,GAGAA,QAHA,GAIA,IALF;AAOD,GAhBa,CAAd;AAkBAM,EAAAA,WAAW,GAAGA,WAAW,CAACG,IAAZ,CAAiB,IAAjB,CAAd;AAEA,SAAOH,WAAP;AACD,CAxBD;;eA0Be,kBAACI,SAAD,EAA6B;AAAA,MAAjBC,OAAiB,uEAAP,EAAO;AAC1C,MAAIC,IAAJ;AACA,MAAIC,UAAJ;AAEAD,EAAAA,IAAI,GAAG,EAAP;AACAC,EAAAA,UAAU,GAAG,EAAb;;AAEA,MAAIF,OAAO,CAACG,MAAZ,EAAoB;AAClB,UAAMC,OAAO,GAAGR,gBAAES,OAAF,CAAUL,OAAO,CAACG,MAAlB,IAA4BH,OAAO,CAACG,MAApC,GAA6C,CAACH,OAAO,CAACG,MAAT,CAA7D;AAEAC,IAAAA,OAAO,CAACE,OAAR,CAAiBH,MAAD,IAAY;AAC1BF,MAAAA,IAAI,IAAIE,MAAM,GAAG,IAAjB;AACD,KAFD;AAIAF,IAAAA,IAAI,IAAI,IAAR;AACD;;AAED,MAAID,OAAO,CAACO,MAAR,IAAkBX,gBAAEY,IAAF,CAAOR,OAAO,CAACO,MAAf,IAAyB,CAA/C,EAAkD;AAChDL,IAAAA,UAAU,IAAI,MAAMO,IAAI,CAACC,SAAL,CAAeV,OAAO,CAACO,MAAvB,CAApB;AACD;;AAEDN,EAAAA,IAAI,IAAI,qBAAqBC,UAArB,GAAkC,MAA1C;;AAEA,MAAIH,SAAS,CAACY,MAAd,EAAsB;AACpB,UAAMC,eAAe,GAAGb,SAAS,CAACc,IAAV,EAAxB;AAEAZ,IAAAA,IAAI,IAAIR,gBAAgB,CAACmB,eAAD,CAAhB,GAAoC,MAA5C;AACD;;AAED,SAAOX,IAAP;AACD,C","sourcesContent":["import _ from 'lodash';\n\nconst safeVariableName = (fileName) => {\n const indexOfDot = fileName.indexOf('.');\n\n if (indexOfDot === -1) {\n return fileName;\n } else {\n return fileName.slice(0, indexOfDot);\n }\n};\n\nconst buildExportBlock = (files) => {\n let importBlock;\n\n importBlock = _.map(files, (fileName) => {\n if (fileName.indexOf(\".\") == -1)\n return (\n \"export * as \" +\n safeVariableName(fileName) +\n \" from './\" +\n fileName +\n \"';\"\n );\n return (\n \"export { default as \" +\n safeVariableName(fileName) +\n \" } from './\" +\n fileName +\n \"';\"\n );\n });\n\n importBlock = importBlock.join('\\n');\n\n return importBlock;\n};\n\nexport default (filePaths, options = {}) => {\n let code;\n let configCode;\n\n code = '';\n configCode = '';\n\n if (options.banner) {\n const banners = _.isArray(options.banner) ? options.banner : [options.banner];\n\n banners.forEach((banner) => {\n code += banner + '\\n';\n });\n\n code += '\\n';\n }\n\n if (options.config && _.size(options.config) > 0) {\n configCode += ' ' + JSON.stringify(options.config);\n }\n\n code += '// @create-index' + configCode + '\\n\\n';\n\n if (filePaths.length) {\n const sortedFilePaths = filePaths.sort();\n\n code += buildExportBlock(sortedFilePaths) + '\\n\\n';\n }\n\n return code;\n};\n"],"file":"createIndexCode.js"}
+36
View File
@@ -0,0 +1,36 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _path = _interopRequireDefault(require("path"));
var _lodash = _interopRequireDefault(require("lodash"));
var _glob = _interopRequireDefault(require("glob"));
var _validateTargetDirectory = _interopRequireDefault(require("./validateTargetDirectory"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = function _default(directoryPath) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
let fileName;
let targetDirectories;
fileName = options.fileName || 'index.js';
fileName = './**/' + fileName;
targetDirectories = _glob.default.sync(_path.default.join(directoryPath, fileName));
targetDirectories = _lodash.default.filter(targetDirectories, targetDirectoryPath => {
return (0, _validateTargetDirectory.default)(_path.default.dirname(targetDirectoryPath), {
outputFile: options.fileName,
silent: options.silent
});
});
targetDirectories = _lodash.default.map(targetDirectories, _path.default.dirname);
return targetDirectories;
};
exports.default = _default;
//# sourceMappingURL=findIndexFiles.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../src/utilities/findIndexFiles.js"],"names":["directoryPath","options","fileName","targetDirectories","glob","sync","path","join","_","filter","targetDirectoryPath","dirname","outputFile","silent","map"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;;;eAEe,kBAACA,aAAD,EAAiC;AAAA,MAAjBC,OAAiB,uEAAP,EAAO;AAC9C,MAAIC,QAAJ;AACA,MAAIC,iBAAJ;AAEAD,EAAAA,QAAQ,GAAGD,OAAO,CAACC,QAAR,IAAoB,UAA/B;AACAA,EAAAA,QAAQ,GAAG,UAAUA,QAArB;AAEAC,EAAAA,iBAAiB,GAAGC,cAAKC,IAAL,CAAUC,cAAKC,IAAL,CAAUP,aAAV,EAAyBE,QAAzB,CAAV,CAApB;AAEAC,EAAAA,iBAAiB,GAAGK,gBAAEC,MAAF,CAASN,iBAAT,EAA6BO,mBAAD,IAAyB;AACvE,WAAO,sCAAwBJ,cAAKK,OAAL,CAAaD,mBAAb,CAAxB,EAA2D;AAChEE,MAAAA,UAAU,EAAEX,OAAO,CAACC,QAD4C;AAEhEW,MAAAA,MAAM,EAAEZ,OAAO,CAACY;AAFgD,KAA3D,CAAP;AAID,GALmB,CAApB;AAOAV,EAAAA,iBAAiB,GAAGK,gBAAEM,GAAF,CAAMX,iBAAN,EAAyBG,cAAKK,OAA9B,CAApB;AAEA,SAAOR,iBAAP;AACD,C","sourcesContent":["import path from 'path';\nimport _ from 'lodash';\nimport glob from 'glob';\nimport validateTargetDirectory from './validateTargetDirectory';\n\nexport default (directoryPath, options = {}) => {\n let fileName;\n let targetDirectories;\n\n fileName = options.fileName || 'index.js';\n fileName = './**/' + fileName;\n\n targetDirectories = glob.sync(path.join(directoryPath, fileName));\n\n targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => {\n return validateTargetDirectory(path.dirname(targetDirectoryPath), {\n outputFile: options.fileName,\n silent: options.silent,\n });\n });\n\n targetDirectories = _.map(targetDirectories, path.dirname);\n\n return targetDirectories;\n};\n"],"file":"findIndexFiles.js"}
+29
View File
@@ -0,0 +1,29 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = function _default(directoryPath) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
const indexPath = _path.default.resolve(directoryPath, options.outputFile || 'index.js');
try {
_fs.default.statSync(indexPath);
return true;
} catch {
return false;
}
};
exports.default = _default;
//# sourceMappingURL=hasIndex.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../src/utilities/hasIndex.js"],"names":["directoryPath","options","indexPath","path","resolve","outputFile","fs","statSync"],"mappings":";;;;;;;AAAA;;AACA;;;;eAEe,kBAACA,aAAD,EAAiC;AAAA,MAAjBC,OAAiB,uEAAP,EAAO;;AAC9C,QAAMC,SAAS,GAAGC,cAAKC,OAAL,CAAaJ,aAAb,EAA4BC,OAAO,CAACI,UAAR,IAAsB,UAAlD,CAAlB;;AAEA,MAAI;AACFC,gBAAGC,QAAH,CAAYL,SAAZ;;AAEA,WAAO,IAAP;AACD,GAJD,CAIE,MAAM;AACN,WAAO,KAAP;AACD;AACF,C","sourcesContent":["import fs from 'fs';\nimport path from 'path';\n\nexport default (directoryPath, options = {}) => {\n const indexPath = path.resolve(directoryPath, options.outputFile || 'index.js');\n\n try {\n fs.statSync(indexPath);\n\n return true;\n } catch {\n return false;\n }\n};\n"],"file":"hasIndex.js"}
+72
View File
@@ -0,0 +1,72 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "createIndexCode", {
enumerable: true,
get: function () {
return _createIndexCode2.default;
}
});
Object.defineProperty(exports, "findIndexFiles", {
enumerable: true,
get: function () {
return _findIndexFiles2.default;
}
});
Object.defineProperty(exports, "log", {
enumerable: true,
get: function () {
return _log2.default;
}
});
Object.defineProperty(exports, "readDirectory", {
enumerable: true,
get: function () {
return _readDirectory2.default;
}
});
Object.defineProperty(exports, "sortByDepth", {
enumerable: true,
get: function () {
return _sortByDepth2.default;
}
});
Object.defineProperty(exports, "validateTargetDirectory", {
enumerable: true,
get: function () {
return _validateTargetDirectory2.default;
}
});
Object.defineProperty(exports, "writeIndex", {
enumerable: true,
get: function () {
return _writeIndex2.default;
}
});
Object.defineProperty(exports, "writeIndexCli", {
enumerable: true,
get: function () {
return _writeIndexCli2.default;
}
});
var _createIndexCode2 = _interopRequireDefault(require("./createIndexCode"));
var _findIndexFiles2 = _interopRequireDefault(require("./findIndexFiles"));
var _log2 = _interopRequireDefault(require("./log"));
var _readDirectory2 = _interopRequireDefault(require("./readDirectory"));
var _sortByDepth2 = _interopRequireDefault(require("./sortByDepth"));
var _validateTargetDirectory2 = _interopRequireDefault(require("./validateTargetDirectory"));
var _writeIndex2 = _interopRequireDefault(require("./writeIndex"));
var _writeIndexCli2 = _interopRequireDefault(require("./writeIndexCli"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[],"file":"index.js"}
+24
View File
@@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _chalk = _interopRequireDefault(require("chalk"));
var _moment = _interopRequireDefault(require("moment"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = function _default() {
for (var _len = arguments.length, append = new Array(_len), _key = 0; _key < _len; _key++) {
append[_key] = arguments[_key];
}
// eslint-disable-next-line no-console
console.log(_chalk.default.dim('[' + (0, _moment.default)().format('HH:mm:ss') + ']'), ...append);
};
exports.default = _default;
//# sourceMappingURL=log.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../src/utilities/log.js"],"names":["append","console","log","chalk","dim","format"],"mappings":";;;;;;;AAAA;;AACA;;;;eAEe,oBAAe;AAAA,oCAAXA,MAAW;AAAXA,IAAAA,MAAW;AAAA;;AAC5B;AACAC,EAAAA,OAAO,CAACC,GAAR,CAAYC,eAAMC,GAAN,CAAU,MAAM,uBAASC,MAAT,CAAgB,UAAhB,CAAN,GAAoC,GAA9C,CAAZ,EAAgE,GAAGL,MAAnE;AACD,C","sourcesContent":["import chalk from 'chalk';\nimport moment from 'moment';\n\nexport default (...append) => {\n // eslint-disable-next-line no-console\n console.log(chalk.dim('[' + moment().format('HH:mm:ss') + ']'), ...append);\n};\n"],"file":"log.js"}
+139
View File
@@ -0,0 +1,139 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _lodash = _interopRequireDefault(require("lodash"));
var _hasIndex = _interopRequireDefault(require("./hasIndex"));
var _validateTargetDirectory = _interopRequireDefault(require("./validateTargetDirectory"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const hasNoExtension = fileName => {
const matches = fileName.match(/\./g);
return !matches;
};
const hasMultipleExtensions = fileName => {
const matches = fileName.match(/\./g);
return matches && matches.length > 1;
};
const isSafeName = fileName => {
return /^[_a-z][\w.]*$/i.test(fileName);
};
const stripExtension = fileName => {
const pos = fileName.lastIndexOf('.');
if (pos === -1) {
return fileName;
}
return fileName.slice(0, Math.max(0, pos));
};
const removeDuplicates = (files, preferredExtension) => {
return _lodash.default.filter(files, fileName => {
const withoutExtension = stripExtension(fileName);
const mainAlternative = withoutExtension + '.' + preferredExtension;
if (mainAlternative === fileName) {
return true;
}
return !_lodash.default.includes(files, mainAlternative);
});
};
const removeIgnoredFiles = function (files) {
let ignorePatterns = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
if (ignorePatterns.length === 0) {
return files;
}
const patterns = ignorePatterns.map(pattern => {
if (_lodash.default.startsWith(pattern, '/') && _lodash.default.endsWith(pattern, '/')) {
const patternWithoutSlashes = pattern.slice(1, -1);
return new RegExp(patternWithoutSlashes);
}
return new RegExp(pattern);
});
return _lodash.default.filter(files, fileName => {
let pattern;
for (pattern of patterns) {
if (fileName.match(pattern) !== null) {
return false;
}
}
return true;
});
};
var _default = function _default(directoryPath) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (!(0, _validateTargetDirectory.default)(directoryPath, options)) {
return false;
}
const _options$extensions = options.extensions,
extensions = _options$extensions === void 0 ? ['js'] : _options$extensions,
_options$config = options.config,
config = _options$config === void 0 ? {} : _options$config,
_options$ignoreDirect = options.ignoreDirectories,
ignoreDirectories = _options$ignoreDirect === void 0 ? false : _options$ignoreDirect;
let children;
children = _fs.default.readdirSync(directoryPath);
children = _lodash.default.filter(children, fileName => {
const absolutePath = _path.default.resolve(directoryPath, fileName);
const isDirectory = _fs.default.statSync(absolutePath).isDirectory();
if (!isSafeName(fileName)) {
return false;
}
if (hasNoExtension(fileName) && !isDirectory) {
return false;
}
if (hasMultipleExtensions(fileName)) {
return false;
}
if (_lodash.default.startsWith(fileName, options.outputFile || 'index.js')) {
return false;
}
if (!isDirectory && !extensions.some(extension => {
return _lodash.default.endsWith(fileName, '.' + extension);
})) {
return false;
}
if (isDirectory && (!(0, _hasIndex.default)(absolutePath, options) || ignoreDirectories)) {
return false;
}
return true;
});
children = removeDuplicates(children, extensions[0]);
children = removeIgnoredFiles(children, config.ignore);
return children.sort();
};
exports.default = _default;
//# sourceMappingURL=readDirectory.js.map
File diff suppressed because one or more lines are too long
+48
View File
@@ -0,0 +1,48 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _hasIndex = _interopRequireDefault(require("./hasIndex"));
var _constants = require("./constants");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = function _default(directoryPath) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (!(0, _hasIndex.default)(directoryPath, options)) {
return {};
}
const indexPath = _path.default.resolve(directoryPath, options.outputFile || 'index.js');
const indexContents = _fs.default.readFileSync(indexPath, 'utf-8');
const found = indexContents.match(_constants.CREATE_INDEX_PATTERN);
const configLine = typeof found[1] === 'string' ? found[1].trim() : '';
if (configLine.length === 0) {
return {};
}
let config;
try {
config = JSON.parse(configLine);
} catch {
throw new Error('"' + indexPath + '" contains invalid configuration object.\n' + 'Configuration object must be a valid JSON.');
}
return config;
};
exports.default = _default;
//# sourceMappingURL=readIndexConfig.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../src/utilities/readIndexConfig.js"],"names":["directoryPath","options","indexPath","path","resolve","outputFile","indexContents","fs","readFileSync","found","match","CREATE_INDEX_PATTERN","configLine","trim","length","config","JSON","parse","Error"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;;;eAEe,kBAACA,aAAD,EAAiC;AAAA,MAAjBC,OAAiB,uEAAP,EAAO;;AAC9C,MAAI,CAAC,uBAASD,aAAT,EAAwBC,OAAxB,CAAL,EAAuC;AACrC,WAAO,EAAP;AACD;;AAED,QAAMC,SAAS,GAAGC,cAAKC,OAAL,CAAaJ,aAAb,EAA4BC,OAAO,CAACI,UAAR,IAAsB,UAAlD,CAAlB;;AACA,QAAMC,aAAa,GAAGC,YAAGC,YAAH,CAAgBN,SAAhB,EAA2B,OAA3B,CAAtB;;AACA,QAAMO,KAAK,GAAGH,aAAa,CAACI,KAAd,CAAoBC,+BAApB,CAAd;AACA,QAAMC,UAAU,GAAG,OAAOH,KAAK,CAAC,CAAD,CAAZ,KAAoB,QAApB,GAA+BA,KAAK,CAAC,CAAD,CAAL,CAASI,IAAT,EAA/B,GAAiD,EAApE;;AAEA,MAAID,UAAU,CAACE,MAAX,KAAsB,CAA1B,EAA6B;AAC3B,WAAO,EAAP;AACD;;AAED,MAAIC,MAAJ;;AAEA,MAAI;AACFA,IAAAA,MAAM,GAAGC,IAAI,CAACC,KAAL,CAAWL,UAAX,CAAT;AACD,GAFD,CAEE,MAAM;AACN,UAAM,IAAIM,KAAJ,CACJ,MAAMhB,SAAN,GAAkB,4CAAlB,GACA,4CAFI,CAAN;AAID;;AAED,SAAOa,MAAP;AACD,C","sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport hasIndex from './hasIndex';\nimport {CREATE_INDEX_PATTERN} from './constants';\n\nexport default (directoryPath, options = {}) => {\n if (!hasIndex(directoryPath, options)) {\n return {};\n }\n\n const indexPath = path.resolve(directoryPath, options.outputFile || 'index.js');\n const indexContents = fs.readFileSync(indexPath, 'utf-8');\n const found = indexContents.match(CREATE_INDEX_PATTERN);\n const configLine = typeof found[1] === 'string' ? found[1].trim() : '';\n\n if (configLine.length === 0) {\n return {};\n }\n\n let config;\n\n try {\n config = JSON.parse(configLine);\n } catch {\n throw new Error(\n '\"' + indexPath + '\" contains invalid configuration object.\\n' +\n 'Configuration object must be a valid JSON.',\n );\n }\n\n return config;\n};\n"],"file":"readIndexConfig.js"}
+19
View File
@@ -0,0 +1,19 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _lodash = _interopRequireDefault(require("lodash"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = paths => {
return _lodash.default.sortBy(paths, path => {
return -path.split('/').length;
});
};
exports.default = _default;
//# sourceMappingURL=sortByDepth.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../src/utilities/sortByDepth.js"],"names":["paths","_","sortBy","path","split","length"],"mappings":";;;;;;;AAAA;;;;eAEgBA,KAAD,IAAW;AACxB,SAAOC,gBAAEC,MAAF,CAASF,KAAT,EAAiBG,IAAD,IAAU;AAC/B,WAAO,CAACA,IAAI,CAACC,KAAL,CAAW,GAAX,EAAgBC,MAAxB;AACD,GAFM,CAAP;AAGD,C","sourcesContent":["import _ from 'lodash';\n\nexport default (paths) => {\n return _.sortBy(paths, (path) => {\n return -path.split('/').length;\n });\n};\n"],"file":"sortByDepth.js"}
+61
View File
@@ -0,0 +1,61 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _constants = require("./constants");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = function _default(targetDirectory) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
const silent = options.silent;
let stats;
try {
stats = _fs.default.statSync(targetDirectory);
} catch {
if (silent) {
return false;
} else {
throw new Error('Directory "' + targetDirectory + '" does not exist.');
}
}
if (!stats.isDirectory()) {
if (silent) {
return false;
} else {
throw new Error('"' + targetDirectory + '" is not a directory.');
}
}
const indexFilePath = _path.default.resolve(targetDirectory, './' + (options.outputFile || 'index.js'));
try {
_fs.default.statSync(indexFilePath);
} catch {
return true;
}
const indexFile = _fs.default.readFileSync(indexFilePath, 'utf8');
if (!indexFile.match(_constants.CREATE_INDEX_PATTERN)) {
if (silent) {
return false;
} else {
throw new Error('"' + indexFilePath + '" unsafe index.');
}
}
return true;
};
exports.default = _default;
//# sourceMappingURL=validateTargetDirectory.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../src/utilities/validateTargetDirectory.js"],"names":["targetDirectory","options","silent","stats","fs","statSync","Error","isDirectory","indexFilePath","path","resolve","outputFile","indexFile","readFileSync","match","CREATE_INDEX_PATTERN"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;;;eAEe,kBAACA,eAAD,EAAmC;AAAA,MAAjBC,OAAiB,uEAAP,EAAO;AAChD,QAAMC,MAAM,GAAGD,OAAO,CAACC,MAAvB;AACA,MAAIC,KAAJ;;AAEA,MAAI;AACFA,IAAAA,KAAK,GAAGC,YAAGC,QAAH,CAAYL,eAAZ,CAAR;AACD,GAFD,CAEE,MAAM;AACN,QAAIE,MAAJ,EAAY;AACV,aAAO,KAAP;AACD,KAFD,MAEO;AACL,YAAM,IAAII,KAAJ,CAAU,gBAAgBN,eAAhB,GAAkC,mBAA5C,CAAN;AACD;AACF;;AAED,MAAI,CAACG,KAAK,CAACI,WAAN,EAAL,EAA0B;AACxB,QAAIL,MAAJ,EAAY;AACV,aAAO,KAAP;AACD,KAFD,MAEO;AACL,YAAM,IAAII,KAAJ,CAAU,MAAMN,eAAN,GAAwB,uBAAlC,CAAN;AACD;AACF;;AAED,QAAMQ,aAAa,GAAGC,cAAKC,OAAL,CAAaV,eAAb,EAA8B,QAAQC,OAAO,CAACU,UAAR,IAAsB,UAA9B,CAA9B,CAAtB;;AAEA,MAAI;AACFP,gBAAGC,QAAH,CAAYG,aAAZ;AACD,GAFD,CAEE,MAAM;AACN,WAAO,IAAP;AACD;;AAED,QAAMI,SAAS,GAAGR,YAAGS,YAAH,CAAgBL,aAAhB,EAA+B,MAA/B,CAAlB;;AAEA,MAAI,CAACI,SAAS,CAACE,KAAV,CAAgBC,+BAAhB,CAAL,EAA4C;AAC1C,QAAIb,MAAJ,EAAY;AACV,aAAO,KAAP;AACD,KAFD,MAEO;AACL,YAAM,IAAII,KAAJ,CAAU,MAAME,aAAN,GAAsB,iBAAhC,CAAN;AACD;AACF;;AAED,SAAO,IAAP;AACD,C","sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport {CREATE_INDEX_PATTERN} from './constants';\n\nexport default (targetDirectory, options = {}) => {\n const silent = options.silent;\n let stats;\n\n try {\n stats = fs.statSync(targetDirectory);\n } catch {\n if (silent) {\n return false;\n } else {\n throw new Error('Directory \"' + targetDirectory + '\" does not exist.');\n }\n }\n\n if (!stats.isDirectory()) {\n if (silent) {\n return false;\n } else {\n throw new Error('\"' + targetDirectory + '\" is not a directory.');\n }\n }\n\n const indexFilePath = path.resolve(targetDirectory, './' + (options.outputFile || 'index.js'));\n\n try {\n fs.statSync(indexFilePath);\n } catch {\n return true;\n }\n\n const indexFile = fs.readFileSync(indexFilePath, 'utf8');\n\n if (!indexFile.match(CREATE_INDEX_PATTERN)) {\n if (silent) {\n return false;\n } else {\n throw new Error('\"' + indexFilePath + '\" unsafe index.');\n }\n }\n\n return true;\n};\n\n"],"file":"validateTargetDirectory.js"}
+50
View File
@@ -0,0 +1,50 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _lodash = _interopRequireDefault(require("lodash"));
var _createIndexCode = _interopRequireDefault(require("./createIndexCode"));
var _validateTargetDirectory = _interopRequireDefault(require("./validateTargetDirectory"));
var _readDirectory = _interopRequireDefault(require("./readDirectory"));
var _readIndexConfig = _interopRequireDefault(require("./readIndexConfig"));
var _sortByDepth = _interopRequireDefault(require("./sortByDepth"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = function _default(directoryPaths) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
const sortedDirectoryPaths = (0, _sortByDepth.default)(directoryPaths).filter(directoryPath => {
return (0, _validateTargetDirectory.default)(directoryPath, {
outputFile: options.outputFile,
silent: options.ignoreUnsafe
});
});
_lodash.default.forEach(sortedDirectoryPaths, directoryPath => {
const config = (0, _readIndexConfig.default)(directoryPath, options);
const optionsWithConfig = Object.assign({}, options, {
config
});
const siblings = (0, _readDirectory.default)(directoryPath, optionsWithConfig);
const indexCode = (0, _createIndexCode.default)(siblings, optionsWithConfig);
const indexFilePath = _path.default.resolve(directoryPath, options.outputFile || 'index.js');
_fs.default.writeFileSync(indexFilePath, indexCode);
});
};
exports.default = _default;
//# sourceMappingURL=writeIndex.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../src/utilities/writeIndex.js"],"names":["directoryPaths","options","sortedDirectoryPaths","filter","directoryPath","outputFile","silent","ignoreUnsafe","_","forEach","config","optionsWithConfig","Object","assign","siblings","indexCode","indexFilePath","path","resolve","fs","writeFileSync"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;eAEe,kBAACA,cAAD,EAAkC;AAAA,MAAjBC,OAAiB,uEAAP,EAAO;AAC/C,QAAMC,oBAAoB,GAAG,0BAAYF,cAAZ,EAC1BG,MAD0B,CAClBC,aAAD,IAAmB;AACzB,WAAO,sCAAwBA,aAAxB,EAAuC;AAACC,MAAAA,UAAU,EAAEJ,OAAO,CAACI,UAArB;AAC5CC,MAAAA,MAAM,EAAEL,OAAO,CAACM;AAD4B,KAAvC,CAAP;AAED,GAJ0B,CAA7B;;AAMAC,kBAAEC,OAAF,CAAUP,oBAAV,EAAiCE,aAAD,IAAmB;AACjD,UAAMM,MAAM,GAAG,8BAAgBN,aAAhB,EAA+BH,OAA/B,CAAf;AACA,UAAMU,iBAAiB,GAAGC,MAAM,CAACC,MAAP,CAAc,EAAd,EAAkBZ,OAAlB,EAA2B;AAACS,MAAAA;AAAD,KAA3B,CAA1B;AACA,UAAMI,QAAQ,GAAG,4BAAcV,aAAd,EAA6BO,iBAA7B,CAAjB;AACA,UAAMI,SAAS,GAAG,8BAAgBD,QAAhB,EAA0BH,iBAA1B,CAAlB;;AACA,UAAMK,aAAa,GAAGC,cAAKC,OAAL,CAAad,aAAb,EAA4BH,OAAO,CAACI,UAAR,IAAsB,UAAlD,CAAtB;;AAEAc,gBAAGC,aAAH,CAAiBJ,aAAjB,EAAgCD,SAAhC;AACD,GARD;AASD,C","sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport _ from 'lodash';\nimport createIndexCode from './createIndexCode';\nimport validateTargetDirectory from './validateTargetDirectory';\nimport readDirectory from './readDirectory';\nimport readIndexConfig from './readIndexConfig';\nimport sortByDepth from './sortByDepth';\n\nexport default (directoryPaths, options = {}) => {\n const sortedDirectoryPaths = sortByDepth(directoryPaths)\n .filter((directoryPath) => {\n return validateTargetDirectory(directoryPath, {outputFile: options.outputFile,\n silent: options.ignoreUnsafe});\n });\n\n _.forEach(sortedDirectoryPaths, (directoryPath) => {\n const config = readIndexConfig(directoryPath, options);\n const optionsWithConfig = Object.assign({}, options, {config});\n const siblings = readDirectory(directoryPath, optionsWithConfig);\n const indexCode = createIndexCode(siblings, optionsWithConfig);\n const indexFilePath = path.resolve(directoryPath, options.outputFile || 'index.js');\n\n fs.writeFileSync(indexFilePath, indexCode);\n });\n};\n"],"file":"writeIndex.js"}
+105
View File
@@ -0,0 +1,105 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _lodash = _interopRequireDefault(require("lodash"));
var _chalk = _interopRequireDefault(require("chalk"));
var _createIndexCode = _interopRequireDefault(require("./createIndexCode"));
var _validateTargetDirectory = _interopRequireDefault(require("./validateTargetDirectory"));
var _readDirectory = _interopRequireDefault(require("./readDirectory"));
var _readIndexConfig = _interopRequireDefault(require("./readIndexConfig"));
var _sortByDepth = _interopRequireDefault(require("./sortByDepth"));
var _log = _interopRequireDefault(require("./log"));
var _findIndexFiles = _interopRequireDefault(require("./findIndexFiles"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = function _default(directoryPaths) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
let sortedDirectoryPaths;
sortedDirectoryPaths = (0, _sortByDepth.default)(directoryPaths);
(0, _log.default)('Target directories', sortedDirectoryPaths);
(0, _log.default)('Output file', options.outputFile);
if (options.updateIndex) {
(0, _log.default)('Update index:', options.updateIndex ? _chalk.default.green('true') : _chalk.default.red('false'));
} else {
(0, _log.default)('Recursive:', options.recursive ? _chalk.default.green('true') : _chalk.default.red('false'));
(0, _log.default)('Ignore unsafe:', options.ignoreUnsafe ? _chalk.default.green('true') : _chalk.default.red('false'));
(0, _log.default)('Extensions:', _chalk.default.green(options.extensions));
}
if (options.updateIndex || options.recursive) {
sortedDirectoryPaths = _lodash.default.map(sortedDirectoryPaths, directory => {
return (0, _findIndexFiles.default)(directory, {
fileName: options.updateIndex ? options.outputFile || 'index.js' : '*',
silent: options.updateIndex || options.ignoreUnsafe
});
});
sortedDirectoryPaths = _lodash.default.flatten(sortedDirectoryPaths);
sortedDirectoryPaths = _lodash.default.uniq(sortedDirectoryPaths);
sortedDirectoryPaths = (0, _sortByDepth.default)(sortedDirectoryPaths);
(0, _log.default)('Updating index files in:', sortedDirectoryPaths.reverse().join(', '));
}
sortedDirectoryPaths = sortedDirectoryPaths.filter(directoryPath => {
return (0, _validateTargetDirectory.default)(directoryPath, {
outputFile: options.outputFile,
silent: options.ignoreUnsafe
});
});
_lodash.default.forEach(sortedDirectoryPaths, directoryPath => {
let existingIndexCode;
const config = (0, _readIndexConfig.default)(directoryPath, options);
const siblings = (0, _readDirectory.default)(directoryPath, {
config,
extensions: options.extensions,
ignoreDirectories: options.ignoreDirectories,
silent: options.ignoreUnsafe
});
const indexCode = (0, _createIndexCode.default)(siblings, {
banner: options.banner,
config
});
const indexFilePath = _path.default.resolve(directoryPath, options.outputFile || 'index.js');
try {
existingIndexCode = _fs.default.readFileSync(indexFilePath, 'utf8');
/* eslint-disable no-empty */
} catch {}
/* eslint-enable no-empty */
_fs.default.writeFileSync(indexFilePath, indexCode);
if (existingIndexCode && existingIndexCode === indexCode) {
(0, _log.default)(indexFilePath, _chalk.default.yellow('[index has not changed]'));
} else if (existingIndexCode && existingIndexCode !== indexCode) {
(0, _log.default)(indexFilePath, _chalk.default.green('[updated index]'));
} else {
(0, _log.default)(indexFilePath, _chalk.default.green('[created index]'));
}
});
(0, _log.default)('Done');
};
exports.default = _default;
//# sourceMappingURL=writeIndexCli.js.map
File diff suppressed because one or more lines are too long