UNPKG

4 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _co = _interopRequireDefault(require("co"));
9
10var _path = _interopRequireDefault(require("path"));
11
12var _fs = _interopRequireDefault(require("fs"));
13
14var _globby = _interopRequireDefault(require("globby"));
15
16var _commonActionInterfaceCheck = _interopRequireDefault(require("./_common-action-interface-check"));
17
18var _commonActionAddFile = _interopRequireDefault(require("./_common-action-add-file"));
19
20var _commonActionUtils = require("./_common-action-utils");
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24const defaultConfig = {
25 verbose: true,
26 stripExtensions: ['hbs']
27};
28
29var _default = _co.default.wrap(function* (data, userConfig, plop) {
30 // shallow-merge default config and input config
31 const cfg = Object.assign({}, defaultConfig, userConfig); // check the common action interface attributes. skip path check because it's NA
32
33 const interfaceTestResult = (0, _commonActionInterfaceCheck.default)(cfg, {
34 checkPath: false
35 });
36
37 if (interfaceTestResult !== true) {
38 throw interfaceTestResult;
39 } // check that destination (instead of path) is a string value
40
41
42 const dest = cfg.destination;
43
44 if (typeof dest !== 'string' || dest.length === 0) {
45 throw `Invalid destination "${dest}"`;
46 }
47
48 if (cfg.base) {
49 cfg.base = plop.renderString(cfg.base, data);
50 }
51
52 if (typeof cfg.templateFiles === 'function') {
53 cfg.templateFiles = cfg.templateFiles();
54 }
55
56 cfg.templateFiles = [].concat(cfg.templateFiles) // Ensure `cfg.templateFiles` is an array, even if a string is passed.
57 .map(file => plop.renderString(file, data)); // render the paths as hbs templates
58
59 const templateFiles = resolveTemplateFiles(cfg.templateFiles, cfg.base, cfg.globOptions, plop);
60 const filesAdded = [];
61
62 for (let templateFile of templateFiles) {
63 const absTemplatePath = _path.default.resolve(plop.getPlopfilePath(), templateFile);
64
65 const fileCfg = Object.assign({}, cfg, {
66 path: stripExtensions(cfg.stripExtensions, resolvePath(cfg.destination, templateFile, cfg.base)),
67 templateFile: absTemplatePath
68 });
69 const addedPath = yield (0, _commonActionAddFile.default)(data, fileCfg, plop);
70 filesAdded.push(addedPath);
71 }
72
73 const summary = `${filesAdded.length} files added`;
74 if (!cfg.verbose) return summary;else return `${summary}\n -> ${filesAdded.join('\n -> ')}`;
75});
76
77exports.default = _default;
78
79function resolveTemplateFiles(templateFilesGlob, basePath, globOptions, plop) {
80 globOptions = Object.assign({
81 cwd: plop.getPlopfilePath()
82 }, globOptions);
83 return _globby.default.sync(templateFilesGlob, Object.assign({
84 braceExpansion: false
85 }, globOptions)).filter(isUnder(basePath)).filter(isAbsoluteOrRelativeFileTo(plop.getPlopfilePath()));
86}
87
88function isAbsoluteOrRelativeFileTo(relativePath) {
89 const isFile = file => _fs.default.existsSync(file) && _fs.default.lstatSync(file).isFile();
90
91 return file => isFile(file) || isFile(_path.default.join(relativePath, file));
92}
93
94function isUnder(basePath = '') {
95 return path => path.startsWith(basePath);
96}
97
98function resolvePath(destination, file, rootPath) {
99 return (0, _commonActionUtils.normalizePath)(_path.default.join(destination, dropFileRootPath(file, rootPath)));
100}
101
102function dropFileRootPath(file, rootPath) {
103 return rootPath ? file.replace(rootPath, '') : dropFileRootFolder(file);
104}
105
106function dropFileRootFolder(file) {
107 const fileParts = _path.default.normalize(file).split(_path.default.sep);
108
109 fileParts.shift();
110 return fileParts.join(_path.default.sep);
111}
112
113function stripExtensions(shouldStrip, fileName) {
114 const maybeFile = _path.default.parse(fileName);
115
116 if (Array.isArray(shouldStrip) && !shouldStrip.map(item => `.${item}`).includes(maybeFile.ext)) return fileName;
117 return _path.default.parse(maybeFile.name).ext !== '' ? _path.default.join(maybeFile.dir, maybeFile.name) : fileName;
118}
\No newline at end of file