templates-mo
Version:
Templates is a scaffolding framework that makes code generation simple, dynamic, and reusable. Generate files, parts of your app, or whole project structures—without the repetitive copy-pasting
129 lines (128 loc) • 4.51 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDir = isDir;
exports.isDirAsync = isDirAsync;
exports.isFileAsync = isFileAsync;
exports.json = json;
exports.findUp = findUp;
exports.cosmiconfigAllExampleSync = cosmiconfigAllExampleSync;
const fs_1 = __importDefault(require("fs"));
const findFileUp = __importStar(require("find-up"));
const path_1 = __importDefault(require("path"));
/**
* Check to see if the `path` is a valid directory
*/
function isDir(filePath) {
let dir;
try {
dir = fs_1.default.lstatSync(filePath);
}
catch (e) {
return false;
}
return dir.isDirectory();
}
/**
* Check to see if the `path` is a valid directory
*/
function isDirAsync(filePath) {
return __awaiter(this, void 0, void 0, function* () {
try {
const dir = yield fs_1.default.promises.lstat(filePath);
return dir.isDirectory();
}
catch (e) {
return false;
}
});
}
/**
* Check to see if the `path` is a valid file
*/
function isFileAsync(filePath) {
return __awaiter(this, void 0, void 0, function* () {
try {
const file = yield fs_1.default.promises.lstat(filePath);
return file.isFile();
}
catch (e) {
return false;
}
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function json(jsonFile) {
try {
const jsonContents = fs_1.default.readFileSync(jsonFile).toString();
return JSON.parse(jsonContents);
}
catch (err) {
return {};
}
}
function findUp(folder, cwd = process.cwd()) {
return findFileUp.sync(folder, {
cwd,
});
}
function countDirectories(filePath) {
return filePath.split(path_1.default.sep).length - 1;
}
function cosmiconfigAllExampleSync(searchPath, explorer, searchPlaces) {
const results = [];
const sortedSearchPlaces = searchPlaces.sort((a, b) => {
return countDirectories(b) - countDirectories(a);
});
function getNext(currentPath) {
const currentResult = explorer.search(currentPath);
// if no result found, end search
if (!currentResult) {
return;
}
// add current result to end of array
results.push(currentResult);
const dir = sortedSearchPlaces.reduce((acc, next) => {
return acc.replace(next, '');
}, currentResult.filepath);
const nextPath = path_1.default.dirname(dir);
// eslint-disable-next-line consistent-return
return getNext(nextPath);
}
getNext(searchPath);
return results;
}