UNPKG

5.93 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var Fs = require("fs-extra");
4var Minimist = require("minimist");
5var node_localstorage_1 = require("node-localstorage");
6var local_cache_1 = require("./local-cache");
7var paths_1 = require("./paths");
8var utils_1 = require("./utils");
9/**
10 A local storage whose storage dir is located under '.git/.tortilla'.
11 Mostly comes in handy when we want to share data between processes.
12 If no '.git' dir was found, an in memory storage will be used instead.
13
14 Used storage variables are listed below:
15
16 - INIT - A flag used to determine whether Tortilla is initialized or not.
17 - USE_STRICT - Tortilla's strict mode flag. If set, Tortilla will run certain
18 validations before committing changes or rebasing them.
19 - REBASE_HOOKS_DISABLED - This flag used to determine whether a step is currently
20 being rebased or not, so Tortilla will know if it should overlook certain
21 git hooks.
22 - REBASE_OLD_STEP - Used by the editor to set the old step before rebasing.
23 - REBASE_NEW_STEP - Will be set any time we run a step operation and is used by the
24 editor so it can rebase our changes correctly.
25 - HOOK_STEP - Forcibly set the step number which should be used by Tortilla's
26 git hooks.
27 - POTENTIAL_RELEASE - Used by the renderer so it will be aware of the release
28 which have just been bumped.
29 - STEP_MAP - A map of old steps and their new indexes which is being built during the
30 step editing process in order to be able to update the diffStep template helpers in
31 the manuals being rebased later on.
32 - STEP_MAP_PENDING - Indicates that this stored step map will be used in another
33 tortilla repo and not in the current repo where the process started running at.
34 */
35var cache = {};
36exports.localStorage = {
37 create: createLocalStorage,
38 assertTortilla: assertTortilla,
39};
40// Creates a new instance of local-storage
41function createLocalStorage(cwd) {
42 // LocalStorage instance creating involves FS operations which is a quiet heavy task.
43 // With the cache, we can save ourselves the extra work which will result in a visible
44 // performance boost
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 // If git dir exists use it as a local-storage dir
51 if (utils_1.Utils.exists(paths.git.resolve(), 'dir')) {
52 // If initialized a second time after the dir has been removed, LocalStorage would
53 // assume the dir exists based on cache, which is not necessarily true in some cases
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// Creates a new instance of local storage, and delegates all its method calls through the exported
63// object
64function 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// Asserts if tortilla is initialized or not
110function 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}
124delegateLocalStorage(paths_1.Paths.resolve());
125// The same instance of the exported LocalStorage module should reference a difference storage
126utils_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//# sourceMappingURL=local-storage.js.map
\No newline at end of file