-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathfirebaseTools.ts
51 lines (49 loc) · 2.08 KB
/
firebaseTools.ts
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
import { FirebaseTools } from './interfaces';
import { spawn, execSync } from 'child_process';
import ora from 'ora';
import * as semver from 'semver';
declare global {
var firebaseTools: FirebaseTools|undefined;
}
export const getFirebaseTools = () => globalThis.firebaseTools ?
Promise.resolve(globalThis.firebaseTools) :
new Promise<FirebaseTools>((resolve, reject) => {
process.env.FIREBASE_CLI_EXPERIMENTS ||= 'webframeworks';
try {
resolve(require('firebase-tools'));
} catch (e) {
try {
const root = execSync('npm root --location=global').toString().trim();
resolve(require(`${root}/firebase-tools`));
} catch (e) {
const spinner = ora({
text: `Installing firebase-tools...`,
// Workaround for https://github.com/sindresorhus/ora/issues/136.
discardStdin: process.platform !== 'win32',
}).start();
spawn('npm', ['i', '--location=global', 'firebase-tools'], {
stdio: 'pipe',
shell: true,
}).on('close', (code) => {
if (code === 0) {
spinner.succeed('firebase-tools installed globally.');
spinner.stop();
const root = execSync('npm root -g').toString().trim();
resolve(require(`${root}/firebase-tools`));
} else {
spinner.fail('Package install failed.');
reject();
}
});
}
}
}).then(firebaseTools => {
globalThis.firebaseTools = firebaseTools;
const version = firebaseTools.cli.version();
console.log(`Using firebase-tools version ${version}`);
if (semver.compare(version, '9.9.0') === -1) {
console.error('firebase-tools version 9.9+ is required, please upgrade and run again');
return Promise.reject();
}
return firebaseTools;
});