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
174 lines (173 loc) • 5.29 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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stripPrefix = exports.unique = exports.flatten = exports.getAllDirectoriesAndUp = exports.getNpmPaths = void 0;
exports.hasProp = hasProp;
exports.eachObj = eachObj;
exports.couldMatchObj = couldMatchObj;
exports.defaults = defaults;
exports.cliLog = cliLog;
/* eslint-disable @typescript-eslint/no-explicit-any */
const is = __importStar(require("is"));
const path_1 = __importDefault(require("path"));
const npm_paths_1 = __importDefault(require("npm-paths"));
const constants_1 = require("./constants");
function hasProp(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
/**
* Loop through a object property. Will break out of loop if `cb` returns false
* @param obj - object to loop through
* @param cb - Function to call on every property
*/
function eachObj(obj, cb) {
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (!is.undef(obj[key])) {
const val = obj[key];
if (cb(val, key) === false) {
break;
}
}
}
}
/**
* Check to see if `obj` matches `matcher`
* @param matcher
* @param obj - object to match against `matcher`
*/
function couldMatchObj(matcher, obj) {
let matched = true;
eachObj(matcher, (val, key) => {
switch (typeof val) {
case 'function':
matched = val(obj[key]);
break;
case 'object':
if (val.not) {
matched = val.not !== obj[key];
break;
}
// eslint-disable-next-line no-fallthrough
default:
matched = val === obj[key];
}
// matched = val === obj[key];
return matched;
});
return matched;
}
/**
* Makes `options` inherit all properties it doesnt have from `default`
* @param options
* @param defaultObj - default properties that you want `options` to have
* @returns - options with all default properties
*/
function defaults(options, defaultObj) {
const newObj = Object.assign({}, options);
eachObj(defaultObj, (val, key) => {
if (!hasProp(options, key) || is.undefined(options[key])) {
newObj[key] = val;
}
});
return newObj;
}
function cliLog(str) {
const string = str
.split(/\n/)
.map((s) => s.trim())
.join('\n')
.trim();
// eslint-disable-next-line
console.log(string);
}
/**
* Get all npm paths
*/
const getNpmPaths = (cwd = constants_1.CWD) => {
return (0, npm_paths_1.default)(cwd);
};
exports.getNpmPaths = getNpmPaths;
/**
* Get a list of all parent directories from a directory
*
* @example
* getAllDirectoriesAndUp("/User/marcellinoornelas/Desktop/random")
* // returns
* [
* "/User/marcellinoornelas/Desktop/random",
* "/User/marcellinoornelas/Desktop",
* "/User/marcellinoornelas",
* "/User",
* "/",
* ]
*/
const getAllDirectoriesAndUp = (dir) => {
const parent = path_1.default.dirname(dir);
if (dir === parent)
return [dir];
return [dir, ...(0, exports.getAllDirectoriesAndUp)(parent)];
};
exports.getAllDirectoriesAndUp = getAllDirectoriesAndUp;
/**
* Unflatten an array
*/
const flatten = (arr) => {
return arr.reduce((unflattened, subArr) => {
unflattened.push(...subArr);
return unflattened;
}, []);
};
exports.flatten = flatten;
const unique = (array) => {
const tracker = {};
const uniqueArray = [];
array.forEach((item) => {
const string = item.toString();
if (tracker[string])
return;
tracker[string] = true;
uniqueArray.push(item);
});
return uniqueArray;
};
exports.unique = unique;
/**
* strip the prefix from the provided string
*
* @example
* stripPrefix("tps-app", "tps-"); // "app"
*/
const stripPrefix = (str, prefix) => {
if (str.startsWith(prefix)) {
return str.slice(prefix.length);
}
return str;
};
exports.stripPrefix = stripPrefix;