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 fspp = _interopRequireWildcard(require("../fs-promise-proxy"));
|
11 |
|
12 | var _commonActionUtils = require("./_common-action-utils");
|
13 |
|
14 | var _commonActionInterfaceCheck = _interopRequireDefault(require("./_common-action-interface-check"));
|
15 |
|
16 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
17 |
|
18 | function _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; }
|
19 |
|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21 |
|
22 | const doAppend = function* (data, cfg, plop, fileData) {
|
23 | const stringToAppend = yield (0, _commonActionUtils.getRenderedTemplate)(data, cfg, plop);
|
24 |
|
25 |
|
26 | if (cfg.unique !== false && new RegExp(cfg.pattern).test(fileData)) {
|
27 |
|
28 | const parts = fileData.split(cfg.pattern);
|
29 | const lastPart = parts[parts.length - 1];
|
30 | const lastPartWithoutDuplicates = lastPart.replace(new RegExp(stringToAppend, 'g'), '');
|
31 | fileData = fileData.replace(lastPart, lastPartWithoutDuplicates);
|
32 | }
|
33 |
|
34 | const {
|
35 | separator = '\n'
|
36 | } = cfg;
|
37 |
|
38 |
|
39 | if (!cfg.pattern) {
|
40 |
|
41 | if (fileData.length > 0) {
|
42 | fileData += separator;
|
43 | }
|
44 |
|
45 | return fileData + stringToAppend;
|
46 | }
|
47 |
|
48 | return fileData.replace(cfg.pattern, '$&' + separator + stringToAppend);
|
49 | };
|
50 |
|
51 | var _default = _co.default.wrap(function* (data, cfg, plop) {
|
52 | const interfaceTestResult = (0, _commonActionInterfaceCheck.default)(cfg);
|
53 |
|
54 | if (interfaceTestResult !== true) {
|
55 | throw interfaceTestResult;
|
56 | }
|
57 |
|
58 | const fileDestPath = (0, _commonActionUtils.makeDestPath)(data, cfg, plop);
|
59 |
|
60 | try {
|
61 |
|
62 | const pathExists = yield fspp.fileExists(fileDestPath);
|
63 |
|
64 | if (!pathExists) {
|
65 | throw 'File does not exist';
|
66 | } else {
|
67 | let fileData = yield fspp.readFile(fileDestPath);
|
68 | fileData = yield doAppend(data, cfg, plop, fileData);
|
69 | yield fspp.writeFile(fileDestPath, fileData);
|
70 | }
|
71 |
|
72 | return (0, _commonActionUtils.getRelativeToBasePath)(fileDestPath, plop);
|
73 | } catch (err) {
|
74 | (0, _commonActionUtils.throwStringifiedError)(err);
|
75 | }
|
76 | });
|
77 |
|
78 | exports.default = _default; |
\ | No newline at end of file |