UNPKG

3.27 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 fspp = _interopRequireWildcard(require("../fs-promise-proxy"));
11
12var _commonActionUtils = require("./_common-action-utils");
13
14var _commonActionInterfaceCheck = _interopRequireDefault(require("./_common-action-interface-check"));
15
16function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
17
18function _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
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22const doAppend = function* (data, cfg, plop, fileData) {
23 const stringToAppend = yield (0, _commonActionUtils.getRenderedTemplate)(data, cfg, plop); // if the appended string should be unique (default),
24 // remove any occurence of it (but only if pattern would match)
25
26 if (cfg.unique !== false && new RegExp(cfg.pattern).test(fileData)) {
27 // only remove after "pattern", so that we remove not too much accidentally
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; // add the appended string to the end of the "fileData" if "pattern"
37 // was not provided, i.e. null or false
38
39 if (!cfg.pattern) {
40 // make sure to add a "separator" if "fileData" is not empty
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
51var _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 // check path
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
78exports.default = _default;
\No newline at end of file