UNPKG

6.05 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __importDefault = (this && this.__importDefault) || function (mod) {
14 return (mod && mod.__esModule) ? mod : { "default": mod };
15};
16var fs = require("fs-extra");
17var walk_sync_1 = __importDefault(require("walk-sync"));
18var fixturify;
19(function (fixturify) {
20 ;
21 // merge walkSync.Options + Options for now
22 function readSync(dir, options, _relativeRoot) {
23 if (options === void 0) { options = {}; }
24 if (_relativeRoot === void 0) { _relativeRoot = ''; }
25 if ('include' in options) {
26 if ('globs' in options) {
27 throw new TypeError('fixturify.readSync does not support both options.include and options.globs, please only use options.globs.');
28 }
29 console.log('fixturify.readSync no longer supports options.include, please use options.globs instead.');
30 options.globs = options.include;
31 }
32 if ('exclude' in options) {
33 if ('ignore' in options) {
34 throw new TypeError('fixturify.readSync does not support both options.exclude and options.ignore, please only use options.ignore.');
35 }
36 console.log('fixturify.readSync no longer supports options.exclude, please use options.ignore instead.');
37 options.ignore = options.exclude;
38 }
39 var ignoreEmptyDirs = options.ignoreEmptyDirs;
40 var obj = {};
41 var entriesOptions = __assign(__assign({}, options), { directories: !ignoreEmptyDirs });
42 for (var _i = 0, _a = walk_sync_1.default.entries(dir, entriesOptions); _i < _a.length; _i++) {
43 var entry = _a[_i];
44 if (entry.isDirectory() === false) {
45 addFile(obj, entry.relativePath, fs.readFileSync(entry.fullPath, 'utf8'));
46 }
47 else {
48 addFolder(obj, entry.relativePath);
49 }
50 }
51 return obj;
52 }
53 fixturify.readSync = readSync;
54 function writeSync(dir, obj) {
55 if ('string' !== typeof dir || dir === '') {
56 throw new TypeError('writeSync first argument must be a non-empty string');
57 }
58 if ('object' !== typeof obj && obj !== null) {
59 throw new TypeError('writeSync second argument must be an object');
60 }
61 fs.mkdirpSync(dir);
62 for (var entry in obj) {
63 if (obj.hasOwnProperty(entry)) {
64 if ('string' !== typeof entry || entry === '') {
65 throw new Error('Directory entry must be a non-empty string');
66 }
67 if (entry === '.' || entry === '..') {
68 throw new Error('Directory entry must not be "." or ".."');
69 }
70 if (entry.indexOf('/') !== -1 || entry.indexOf('\\') !== -1) {
71 throw new Error('Directory entry must not contain "/" or "\\"');
72 }
73 var fullPath = "".concat(dir, "/").concat(entry);
74 var value = obj[entry];
75 var stat = void 0;
76 try {
77 stat = fs.statSync(fullPath);
78 }
79 catch (e) {
80 stat = undefined;
81 }
82 if (typeof value === 'string') {
83 if (stat && stat.isDirectory()) {
84 fs.removeSync(fullPath);
85 }
86 fs.writeFileSync(fullPath, value, 'utf8');
87 }
88 else if (typeof value === 'object') {
89 if (value === null) {
90 fs.removeSync(fullPath);
91 }
92 else {
93 try {
94 if (stat && stat.isFile()) {
95 fs.unlinkSync(fullPath);
96 }
97 fs.mkdirSync(fullPath);
98 }
99 catch (e) {
100 // if the directory already exists, carry on.
101 // This is to support, re-applying (append-only) of fixtures
102 if (!(typeof e === 'object' && e !== null && e.code === 'EEXIST')) {
103 throw e;
104 }
105 }
106 writeSync(fullPath, value);
107 }
108 }
109 else {
110 throw new Error("".concat(entry, " in ").concat(dir, " : Expected string or object, got ").concat(value));
111 }
112 }
113 }
114 }
115 fixturify.writeSync = writeSync;
116})(fixturify || (fixturify = {}));
117function addFile(obj, path, content) {
118 var segments = path.split('/');
119 var file = segments.pop();
120 if (typeof file !== 'string') {
121 throw new Error("invalid file path: '".concat(path, "'"));
122 }
123 addFolder(obj, segments)[file] = content;
124}
125function addFolder(obj, path) {
126 var segments = Array.isArray(path) ? path : path.split('/');
127 for (var _i = 0, segments_1 = segments; _i < segments_1.length; _i++) {
128 var segment = segments_1[_i];
129 if (segment === '') {
130 break;
131 }
132 var entry = obj[segment];
133 if (entry === undefined) {
134 obj = obj[segment] = {};
135 }
136 else {
137 if (typeof entry === 'object' && entry !== null) {
138 obj = entry;
139 }
140 else {
141 throw new Error("expected no existing directory entry for '".concat(path, "' but got '").concat(entry, "'"));
142 }
143 }
144 }
145 return obj;
146}
147module.exports = fixturify;
148//# sourceMappingURL=index.js.map
\No newline at end of file