UNPKG

5.97 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 native: node_localstorage_1.LocalStorage,
39 assertTortilla: assertTortilla,
40};
41// Creates a new instance of local-storage
42function createLocalStorage(cwd) {
43 // LocalStorage instance creating involves FS operations which is a quiet heavy task.
44 // With the cache, we can save ourselves the extra work which will result in a visible
45 // performance boost
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 // If git dir exists use it as a local-storage dir
52 if (utils_1.Utils.exists(paths.git.resolve(), 'dir')) {
53 // If initialized a second time after the dir has been removed, LocalStorage would
54 // assume the dir exists based on cache, which is not necessarily true in some cases
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// Creates a new instance of local storage, and delegates all its method calls through the exported
64// object
65function 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// Asserts if tortilla is initialized or not
111function 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}
125delegateLocalStorage(paths_1.Paths.resolve());
126// The same instance of the exported LocalStorage module should reference a difference storage
127utils_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//# sourceMappingURL=local-storage.js.map
\No newline at end of file