-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
61 lines (51 loc) · 1.51 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/
import { handleErrorMessage, handleSuccessMessage } from '../../utils/index.js';
import { Command } from 'commander';
import { Ora } from 'ora';
import { execa } from 'execa';
/**
* Execute the `netlify-site-id` command.
*
* @param {Ora} [spinner] Terminal spinner.
*
* @returns {Promise<string>} The value provided by the `NETLIFY_SITE_ID`
* environment variable or the value of the `netlify.siteid` git configuration
* option.
*/
export const execute = async (spinner?: Ora): Promise<string | undefined> => {
let retVal = process.env.NETLIFY_SITE_ID;
if (!retVal) {
try {
const siteId = await execa('git', ['config', '--get', 'netlify.siteid']);
retVal = siteId.stdout.toString();
} catch (error: unknown) {
handleErrorMessage(error, 'netlify-site-id', spinner);
throw error;
}
}
return retVal;
};
export default (spinner: Ora): Command => {
const command = new Command('netlify-site-id');
return command.description('output Netlify site API ID').action(async () => {
try {
spinner.start();
const siteId = await execute(spinner);
if (siteId) {
handleSuccessMessage(siteId, spinner);
} else {
throw new Error();
}
} catch {
spinner.fail('Netlify site ID not found');
process.exitCode = 1;
} finally {
spinner.stop();
}
});
};