UNPKG

3.26 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = addFile;
7
8var _path = _interopRequireDefault(require("path"));
9
10var _del = _interopRequireDefault(require("del"));
11
12var _commonActionUtils = require("./_common-action-utils");
13
14var _isbinaryfile = require("isbinaryfile");
15
16var fspp = _interopRequireWildcard(require("../fs-promise-proxy"));
17
18function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
19
20function _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
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24function* 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 // check path
33 let destExists = yield fspp.fileExists(fileDestPath); // if we are forcing and the file already exists, delete the file
34
35 if (force === true && destExists) {
36 yield (0, _del.default)([fileDestPath], {
37 force
38 });
39 destExists = false;
40 } // we can't create files where one already exists
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 } // keep the executable flags
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 } // return the added file path (relative to the destination path)
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