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