1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _co = _interopRequireDefault(require("co"));
|
9 |
|
10 | var _path = _interopRequireDefault(require("path"));
|
11 |
|
12 | var _fs = _interopRequireDefault(require("fs"));
|
13 |
|
14 | var _globby = _interopRequireDefault(require("globby"));
|
15 |
|
16 | var _commonActionInterfaceCheck = _interopRequireDefault(require("./_common-action-interface-check"));
|
17 |
|
18 | var _commonActionAddFile = _interopRequireDefault(require("./_common-action-add-file"));
|
19 |
|
20 | var _commonActionUtils = require("./_common-action-utils");
|
21 |
|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23 |
|
24 | const defaultConfig = {
|
25 | verbose: true,
|
26 | stripExtensions: ['hbs']
|
27 | };
|
28 |
|
29 | var _default = _co.default.wrap(function* (data, userConfig, plop) {
|
30 |
|
31 | const cfg = Object.assign({}, defaultConfig, userConfig);
|
32 |
|
33 | const interfaceTestResult = (0, _commonActionInterfaceCheck.default)(cfg, {
|
34 | checkPath: false
|
35 | });
|
36 |
|
37 | if (interfaceTestResult !== true) {
|
38 | throw interfaceTestResult;
|
39 | }
|
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)
|
57 | .map(file => plop.renderString(file, data));
|
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 |
|
77 | exports.default = _default;
|
78 |
|
79 | function 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 |
|
88 | function 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 |
|
94 | function isUnder(basePath = '') {
|
95 | return path => path.startsWith(basePath);
|
96 | }
|
97 |
|
98 | function resolvePath(destination, file, rootPath) {
|
99 | return (0, _commonActionUtils.normalizePath)(_path.default.join(destination, dropFileRootPath(file, rootPath)));
|
100 | }
|
101 |
|
102 | function dropFileRootPath(file, rootPath) {
|
103 | return rootPath ? file.replace(rootPath, '') : dropFileRootFolder(file);
|
104 | }
|
105 |
|
106 | function 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 |
|
113 | function 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 |