UNPKG

2.51 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const fs = require('fs');
4const os = require('os');
5const path = require('path');
6const Analytics = require('analytics-node');
7const child_process_1 = require("child_process");
8exports.botonic_home_path = path.join(os.homedir(), '.botonic');
9exports.botonic_credentials_path = path.join(exports.botonic_home_path, 'credentials.json');
10function initializeCredentials() {
11 if (!fs.existsSync(exports.botonic_home_path))
12 fs.mkdirSync(exports.botonic_home_path);
13 let anonymous_id = Math.round(Math.random() * 100000000);
14 fs.writeFileSync(exports.botonic_credentials_path, JSON.stringify({ analytics: { anonymous_id } }));
15}
16exports.initializeCredentials = initializeCredentials;
17function readCredentials() {
18 if (!fs.existsSync(exports.botonic_credentials_path)) {
19 initializeCredentials();
20 }
21 try {
22 exports.credentials = JSON.parse(fs.readFileSync(exports.botonic_credentials_path));
23 }
24 catch (e) { }
25}
26try {
27 readCredentials();
28}
29catch (e) { }
30function analytics_enabled() {
31 return process.env.BOTONIC_DISABLE_ANALYTICS !== '1';
32}
33if (analytics_enabled()) {
34 exports.analytics = new Analytics('YD0jpJHNGW12uhLNbgB4wbdTRQ4Cy1Zu');
35}
36function track(event, properties = {}) {
37 if (analytics_enabled() && exports.analytics && exports.credentials && exports.credentials.analytics)
38 exports.analytics.track({
39 event: event,
40 anonymousId: exports.credentials.analytics.anonymous_id,
41 properties: properties
42 });
43}
44exports.track = track;
45function botonicPostInstall() {
46 if (analytics_enabled()) {
47 initializeCredentials();
48 readCredentials();
49 track('Installed Botonic CLI');
50 }
51}
52exports.botonicPostInstall = botonicPostInstall;
53function sleep(ms) {
54 return new Promise(resolve => setTimeout(resolve, ms));
55}
56exports.sleep = sleep;
57async function sh(cmd) {
58 return new Promise(function (resolve, reject) {
59 child_process_1.exec(cmd, (err, stdout, stderr) => {
60 if (err) {
61 reject(err);
62 }
63 else {
64 resolve({ stdout, stderr });
65 }
66 });
67 });
68}
69async function getGlobalNodeModulesPath() {
70 const CROSS_PLATFORM_REGEX = /\r?\n|\r/g;
71 return (await sh('npm root -g')).stdout.replace(CROSS_PLATFORM_REGEX, '');
72}
73exports.getGlobalNodeModulesPath = getGlobalNodeModulesPath;
74//# sourceMappingURL=utils.js.map
\No newline at end of file