UNPKG

3.01 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getRenderedTemplatePath = getRenderedTemplatePath;
7exports.getTemplate = getTemplate;
8exports.getRenderedTemplate = getRenderedTemplate;
9exports.throwStringifiedError = exports.getRelativeToBasePath = exports.makeDestPath = exports.normalizePath = void 0;
10
11var _path = _interopRequireDefault(require("path"));
12
13var fspp = _interopRequireWildcard(require("../fs-promise-proxy"));
14
15function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
16
17function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21const getFullData = (data, cfg) => Object.assign({}, cfg.data, data);
22
23const normalizePath = path => {
24 return !path.sep || path.sep === '\\' ? path.replace(/\\/g, '/') : path;
25};
26
27exports.normalizePath = normalizePath;
28
29const makeDestPath = (data, cfg, plop) => {
30 return _path.default.resolve(plop.getDestBasePath(), plop.renderString(normalizePath(cfg.path) || '', getFullData(data, cfg)));
31};
32
33exports.makeDestPath = makeDestPath;
34
35function getRenderedTemplatePath(data, cfg, plop) {
36 if (cfg.templateFile) {
37 const absTemplatePath = _path.default.resolve(plop.getPlopfilePath(), cfg.templateFile);
38
39 return plop.renderString(normalizePath(absTemplatePath), getFullData(data, cfg));
40 }
41
42 return null;
43}
44
45function* getTemplate(data, cfg, plop) {
46 const makeTmplPath = p => _path.default.resolve(plop.getPlopfilePath(), p);
47
48 let {
49 template
50 } = cfg;
51
52 if (cfg.templateFile) {
53 template = yield fspp.readFile(makeTmplPath(cfg.templateFile));
54 }
55
56 if (template == null) {
57 template = '';
58 }
59
60 return template;
61}
62
63function* getRenderedTemplate(data, cfg, plop) {
64 const template = yield getTemplate(data, cfg, plop);
65 return plop.renderString(template, getFullData(data, cfg));
66}
67
68const getRelativeToBasePath = (filePath, plop) => filePath.replace(_path.default.resolve(plop.getDestBasePath()), '');
69
70exports.getRelativeToBasePath = getRelativeToBasePath;
71
72const throwStringifiedError = err => {
73 if (typeof err === 'string') {
74 throw err;
75 } else {
76 throw err.message || JSON.stringify(err);
77 }
78};
79
80exports.throwStringifiedError = throwStringifiedError;
\No newline at end of file