1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var Fs = require("fs-extra");
|
4 | var Minimist = require("minimist");
|
5 | var node_localstorage_1 = require("node-localstorage");
|
6 | var local_cache_1 = require("./local-cache");
|
7 | var paths_1 = require("./paths");
|
8 | var utils_1 = require("./utils");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | var cache = {};
|
36 | exports.localStorage = {
|
37 | create: createLocalStorage,
|
38 | assertTortilla: assertTortilla,
|
39 | };
|
40 |
|
41 | function createLocalStorage(cwd) {
|
42 |
|
43 |
|
44 |
|
45 | if (!process.env.TORTILLA_CACHE_DISABLED && cache[cwd]) {
|
46 | return cache[cwd];
|
47 | }
|
48 | var paths = cwd.resolve ? cwd : paths_1.Paths.resolveProject(cwd);
|
49 | var l;
|
50 |
|
51 | if (utils_1.Utils.exists(paths.git.resolve(), 'dir')) {
|
52 |
|
53 |
|
54 | Fs.ensureDirSync(paths.storage);
|
55 | l = new node_localstorage_1.LocalStorage(paths.storage);
|
56 | }
|
57 | else {
|
58 | l = new local_cache_1.LocalCache();
|
59 | }
|
60 | return cache[cwd] = l;
|
61 | }
|
62 |
|
63 |
|
64 | function delegateLocalStorage(cwd) {
|
65 | var l = createLocalStorage(cwd);
|
66 | var descriptors = Object.getOwnPropertyDescriptors(l.__proto__);
|
67 | Object.entries(descriptors).forEach(function (_a) {
|
68 | var key = _a[0], descriptor = _a[1];
|
69 | var delegator = {};
|
70 | if ('configurable' in descriptor) {
|
71 | delegator.configurable = descriptor.configurable;
|
72 | }
|
73 | if ('enumerable' in descriptor) {
|
74 | delegator.enumerable = descriptor.configurable;
|
75 | }
|
76 | if ('writable' in descriptor) {
|
77 | delegator.writable = descriptor.configurable;
|
78 | }
|
79 | if (descriptor.get) {
|
80 | delegator.get = function () {
|
81 | var args = [];
|
82 | for (var _i = 0; _i < arguments.length; _i++) {
|
83 | args[_i] = arguments[_i];
|
84 | }
|
85 | return descriptor.get.apply(l, args);
|
86 | };
|
87 | }
|
88 | if (descriptor.set) {
|
89 | delegator.set = function () {
|
90 | var args = [];
|
91 | for (var _i = 0; _i < arguments.length; _i++) {
|
92 | args[_i] = arguments[_i];
|
93 | }
|
94 | return descriptor.set.apply(l, args);
|
95 | };
|
96 | }
|
97 | if (typeof descriptor.value === 'function') {
|
98 | delegator.value = function () {
|
99 | var args = [];
|
100 | for (var _i = 0; _i < arguments.length; _i++) {
|
101 | args[_i] = arguments[_i];
|
102 | }
|
103 | return descriptor.value.apply(l, args);
|
104 | };
|
105 | }
|
106 | Object.defineProperty(exports.localStorage, key, delegator);
|
107 | });
|
108 | }
|
109 |
|
110 | function assertTortilla(exists) {
|
111 | var isInit = this.getItem('INIT');
|
112 | if (exists && !isInit) {
|
113 | throw Error([
|
114 | 'Tortilla essentials must be initialized!',
|
115 | 'Please run `$ tortilla init` before proceeding.',
|
116 | ].join('\n'));
|
117 | }
|
118 | if (!exists && isInit) {
|
119 | throw Error([
|
120 | 'Tortilla essentials are already initialized!',
|
121 | ].join('\n'));
|
122 | }
|
123 | }
|
124 | delegateLocalStorage(paths_1.Paths.resolve());
|
125 |
|
126 | utils_1.Utils.on('cwdChange', delegateLocalStorage);
|
127 | (function () {
|
128 | if (require.main !== module) {
|
129 | return;
|
130 | }
|
131 | var argv = Minimist(process.argv.slice(2), {
|
132 | string: ['_'],
|
133 | });
|
134 | var method = argv._[0];
|
135 | var args = argv._.slice(1);
|
136 | switch (method) {
|
137 | case 'set':
|
138 | for (var i = 0; i < args.length; i += 2) {
|
139 | exports.localStorage.setItem(args[i], args[i + 1]);
|
140 | }
|
141 | break;
|
142 | case 'remove':
|
143 | for (var i = 0; i < args.length; i++) {
|
144 | exports.localStorage.removeItem(args[i]);
|
145 | }
|
146 | break;
|
147 | default:
|
148 | break;
|
149 | }
|
150 | })();
|
151 |
|
\ | No newline at end of file |