forked from teambit/bit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.ts
More file actions
140 lines (126 loc) · 4.33 KB
/
bootstrap.ts
File metadata and controls
140 lines (126 loc) · 4.33 KB
1
2
3
4
5
6
7
8
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import Bluebird from 'bluebird';
import path from 'path';
import chalk from 'chalk';
import fs from 'fs-extra';
import semver from 'semver';
import { Analytics } from './analytics/analytics';
import { handleUnhandledRejection } from './cli/handle-errors';
import { BIT_VERSION, GLOBAL_CONFIG, GLOBAL_LOGS } from './constants';
import HooksManager from './hooks';
import { printWarning } from './logger/logger';
import loader from './cli/loader';
const SUPPORTED_NODE_VERSIONS = '>=14.19.3 <17.0.0';
process.env.MEMFS_DONT_WARN = 'true'; // suppress fs experimental warnings from memfs
require('events').EventEmitter.defaultMaxListeners = 100; // set max listeners to a more appropriate numbers
require('regenerator-runtime/runtime');
// eslint-disable-next-line @typescript-eslint/no-misused-promises
process.on('unhandledRejection', async (err) => handleUnhandledRejection(err));
// by default Bluebird enables the longStackTraces when env is `development`, or when
// BLUEBIRD_DEBUG is set.
// the drawback of enabling it all the time is a performance hit. (see http://bluebirdjs.com/docs/api/promise.longstacktraces.html)
// some commands are slower by 20% with this enabled.
Bluebird.config({
longStackTraces: Boolean(process.env.BLUEBIRD_DEBUG || process.env.BIT_LOG),
});
export async function bootstrap() {
enableLoaderIfPossible();
printBitVersionIfAsked();
warnIfRunningAsRoot();
verifyNodeVersionCompatibility();
await ensureDirectories();
await Analytics.promptAnalyticsIfNeeded();
HooksManager.init();
}
async function ensureDirectories() {
await fs.ensureDir(GLOBAL_CONFIG);
await fs.ensureDir(GLOBAL_LOGS);
}
function verifyNodeVersionCompatibility() {
const nodeVersion = process.versions.node.split('-')[0];
const isCompatible = semver.satisfies(nodeVersion, SUPPORTED_NODE_VERSIONS);
if (!isCompatible) {
// eslint-disable-next-line no-console
console.log(
chalk.red(
`Node version ${nodeVersion} is not supported, please use Node.js ${SUPPORTED_NODE_VERSIONS}.
If you must use legacy versions of Node.js, please use our binary installation methods. https://docs.bit.dev/docs/installation`
)
);
process.exit(1);
}
}
function warnIfRunningAsRoot() {
const isRoot = process.getuid && process.getuid() === 0;
if (isRoot) {
printWarning('running bit as root might cause permission issues later');
}
}
function printBitVersionIfAsked() {
if (process.argv[2]) {
if (['-V', '-v', '--version'].includes(process.argv[2])) {
const harmonyVersion = getHarmonyVersion();
if (harmonyVersion) {
console.log(harmonyVersion); // eslint-disable-line no-console
} else {
console.log(BIT_VERSION); // eslint-disable-line no-console
}
process.exit();
}
}
}
/**
* once Yargs and Harmony are fully loaded we have all commands instances and we are able to
* determine whether or not the loader should be loaded.
* in this phase, all we have are the args from the cli, so we can only guess when it's ok to start
* the loader. the reason we start it here is to have the loader report the progress of bit
* bootstrap process, which can slow at times.
*/
function enableLoaderIfPossible() {
const safeCommandsForLoader = [
'status',
's', // status alias
'compile',
'start',
'add',
'show',
'tag',
'build',
'create',
'test',
'install',
'update',
'link',
'import',
'log',
'checkout',
'merge',
'diff',
'env',
'envs',
];
if (
safeCommandsForLoader.includes(process.argv[2]) &&
!process.argv.includes('--json') &&
!process.argv.includes('-j')
) {
loader.on();
// loader.start('loading bit...');
}
}
export function getHarmonyVersion(showValidSemver = false) {
try {
const teambitBit = require.resolve('@teambit/bit');
// eslint-disable-next-line
const packageJson = require(path.join(teambitBit, '../..', 'package.json'));
if (packageJson.version) return packageJson.version;
// this is running locally
if (packageJson.componentId && packageJson.componentId.version) {
return showValidSemver ? packageJson.componentId.version : `last-tag ${packageJson.componentId.version}`;
}
if (showValidSemver) throw new Error(`unable to find Bit version`);
return null;
} catch (err: any) {
if (showValidSemver) throw err;
return null;
}
}