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
78 lines (77 loc) • 3.63 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.directoryIsTpsInitialized = exports.findTemplate = exports.getTemplateLocations = void 0;
const TPS = __importStar(require("../utilities/constants"));
const fileSystem_1 = require("../utilities/fileSystem");
const helpers_1 = require("../utilities/helpers");
const path_1 = __importDefault(require("path"));
/**
* Get all locations a template can be
*
* Templates can be in be:
* - any `.tps/` directory from the callers cwd and any directory above it
* - Any `node_module` directory from the callers cwd and any directory above it
*/
const getTemplateLocations = (cwd = TPS.CWD) => {
const tpsDirectoryLocations = (0, helpers_1.getAllDirectoriesAndUp)(cwd).map((dir) => {
return path_1.default.join(dir, TPS.TPS_FOLDER);
});
// TODO: Sort this by directory
return [
...tpsDirectoryLocations,
path_1.default.join(TPS.MAIN_DIR, TPS.TPS_FOLDER),
...(0, helpers_1.getNpmPaths)(cwd),
];
};
exports.getTemplateLocations = getTemplateLocations;
/**
* Get the path to a template or null if template doesnt exist
*/
const findTemplate = (templateName, cwd = TPS.CWD) => {
const homeDirectory = (0, exports.getTemplateLocations)(cwd).find((tpsDir) => {
return (0, fileSystem_1.isDir)(path_1.default.join(tpsDir, templateName));
});
if (!homeDirectory)
return null;
return path_1.default.join(homeDirectory, templateName);
};
exports.findTemplate = findTemplate;
const directoryIsTpsInitialized = (location) => __awaiter(void 0, void 0, void 0, function* () {
return (0, fileSystem_1.isDirAsync)(path_1.default.join(location, TPS.TPS_FOLDER));
});
exports.directoryIsTpsInitialized = directoryIsTpsInitialized;