UNPKG

4.38 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};
13Object.defineProperty(exports, "__esModule", { value: true });
14var child_process = require("child_process");
15var dump_1 = require("./dump");
16var git_1 = require("./git");
17/*
18 lib.ts module provides a runtime access to Tortilla's API through a cluster
19 to distribute the work to another core and not to block the main thread
20**/
21// Will call the function through the worker
22var callTortillaFn = function (fnName) {
23 var args = [];
24 for (var _i = 1; _i < arguments.length; _i++) {
25 args[_i - 1] = arguments[_i];
26 }
27 return new Promise(function (resolve, reject) {
28 // Note that we use a child_process here and not a cluster since we don't wanna
29 // fork the main module
30 var worker = child_process.fork(__filename, [], {
31 env: __assign({}, process.env, { TORTILLA_WORKER: 1 })
32 });
33 worker.send({
34 op: 'exec',
35 fn: fnName,
36 args: args,
37 });
38 // On result
39 worker.on('message', function (msg) {
40 if (typeof msg !== 'object') {
41 return;
42 }
43 if (msg.op !== 'result') {
44 return;
45 }
46 if (msg.fn !== fnName) {
47 return;
48 }
49 resolve(msg.payload);
50 });
51 // On error
52 worker.on('message', function (msg) {
53 if (typeof msg !== 'object') {
54 return;
55 }
56 if (msg.op !== 'error') {
57 return;
58 }
59 if (msg.fn !== fnName) {
60 return;
61 }
62 reject(msg.payload);
63 });
64 });
65};
66// Helper function that will simulate a registered user in the git.config
67var withUser = function (fn) { return function () {
68 var args = [];
69 for (var _i = 0; _i < arguments.length; _i++) {
70 args[_i] = arguments[_i];
71 }
72 var result = '';
73 var nameExists = false;
74 var emailExists = false;
75 // Upcoming method requires user.name and user.email to be set
76 try {
77 // If invocation was successful it means name exists
78 try {
79 git_1.Git(['config', '--global', 'user.name']);
80 nameExists = true;
81 }
82 catch (e) {
83 git_1.Git(['config', '--global', 'user.name', 'tortilla']);
84 }
85 // If invocation was successful it means email exists
86 try {
87 git_1.Git(['config', '--global', 'user.email']);
88 emailExists = true;
89 }
90 catch (e) {
91 git_1.Git(['config', '--global', 'user.email', 'tortilla@tortilla.academy']);
92 }
93 result = fn.apply(void 0, args);
94 }
95 finally {
96 if (!nameExists) {
97 git_1.Git(['config', '--global', '--unset', 'user.name']);
98 }
99 if (!emailExists) {
100 git_1.Git(['config', '--global', '--unset', 'user.email']);
101 }
102 }
103 return result;
104}; };
105exports.diffReleases = withUser(function (dump, srcRelease, destRelease) {
106 return dump_1.Dump.diffReleases(dump, srcRelease, destRelease);
107});
108// In worker
109if (!process.env.TORTILLA_WORKER) {
110 // Listen for signals
111 process.on('message', function (msg) {
112 if (msg.op !== 'exec') {
113 return;
114 }
115 var handler;
116 switch (msg.fn) {
117 case 'diffReleases': {
118 handler = exports.diffReleases;
119 break;
120 }
121 // Escape if function name is not supported
122 default: return;
123 }
124 try {
125 var result = handler.apply(void 0, msg.args);
126 process.send({
127 op: 'result',
128 fn: msg.fn,
129 id: msg.id,
130 payload: result,
131 });
132 }
133 catch (e) {
134 process.send({
135 op: 'error',
136 fn: msg.fn,
137 id: msg.id,
138 payload: e,
139 });
140 }
141 });
142}
143//# sourceMappingURL=lib.js.map
\No newline at end of file