1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = addFile;
|
7 |
|
8 | var _path = _interopRequireDefault(require("path"));
|
9 |
|
10 | var _del = _interopRequireDefault(require("del"));
|
11 |
|
12 | var _commonActionUtils = require("./_common-action-utils");
|
13 |
|
14 | var _isbinaryfile = require("isbinaryfile");
|
15 |
|
16 | var fspp = _interopRequireWildcard(require("../fs-promise-proxy"));
|
17 |
|
18 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
19 |
|
20 | 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; }
|
21 |
|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23 |
|
24 | function* addFile(data, cfg, plop) {
|
25 | const fileDestPath = (0, _commonActionUtils.makeDestPath)(data, cfg, plop);
|
26 | const {
|
27 | force,
|
28 | skipIfExists = false
|
29 | } = cfg;
|
30 |
|
31 | try {
|
32 |
|
33 | let destExists = yield fspp.fileExists(fileDestPath);
|
34 |
|
35 | if (force === true && destExists) {
|
36 | yield (0, _del.default)([fileDestPath], {
|
37 | force
|
38 | });
|
39 | destExists = false;
|
40 | }
|
41 |
|
42 |
|
43 | if (destExists) {
|
44 | if (skipIfExists) {
|
45 | return `[SKIPPED] ${fileDestPath} (exists)`;
|
46 | }
|
47 |
|
48 | throw `File already exists\n -> ${fileDestPath}`;
|
49 | } else {
|
50 | yield fspp.makeDir(_path.default.dirname(fileDestPath));
|
51 | const absTemplatePath = cfg.templateFile && _path.default.resolve(plop.getPlopfilePath(), cfg.templateFile) || null;
|
52 |
|
53 | if (absTemplatePath != null && (0, _isbinaryfile.isBinaryFileSync)(absTemplatePath)) {
|
54 | const rawTemplate = yield fspp.readFileRaw(cfg.templateFile);
|
55 | yield fspp.writeFileRaw(fileDestPath, rawTemplate);
|
56 | } else {
|
57 | const renderedTemplate = yield (0, _commonActionUtils.getRenderedTemplate)(data, cfg, plop);
|
58 | yield fspp.writeFile(fileDestPath, renderedTemplate);
|
59 | }
|
60 |
|
61 |
|
62 | if (absTemplatePath != null) {
|
63 | const sourceStats = yield fspp.stat(absTemplatePath);
|
64 | const destStats = yield fspp.stat(fileDestPath);
|
65 | const executableFlags = sourceStats.mode & (fspp.constants.S_IXUSR | fspp.constants.S_IXGRP | fspp.constants.S_IXOTH);
|
66 | yield fspp.chmod(fileDestPath, destStats.mode | executableFlags);
|
67 | }
|
68 | }
|
69 |
|
70 |
|
71 | return (0, _commonActionUtils.getRelativeToBasePath)(fileDestPath, plop);
|
72 | } catch (err) {
|
73 | (0, _commonActionUtils.throwStringifiedError)(err);
|
74 | }
|
75 | } |
\ | No newline at end of file |