-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
115 lines (99 loc) · 3.08 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
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
/**
* 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 { siteId as getSiteId, token as getToken } from '../index.js';
import { handleErrorMessage, handleSuccessMessage } from '../../utils/index.js';
import { Command } from 'commander';
import { Ora } from 'ora';
import { execa } from 'execa';
interface INetlifyDeployArgs {
dir: string;
production?: boolean;
token?: string;
siteId?: string;
message?: string;
spinner?: Ora;
}
type RETVAL = {
url: string;
logUrl: string;
};
/**
* Execute the `netlify-deploy` command.
*
* @param {string} args.dir Folder to deploy.
* @param {boolean} [args.production] Determine whether this is a production deploy.
* @param {string} [args.message] Deploy message.
* @param {string} [args.token] Netlify personal access token.
* @param {string} [args.siteId] Netlify site API ID.
* @param {Ora} [args.spinner] Terminal spinner.
*
* @returns {object} The Netlify deployment and log URLs.
*/
export const execute = async (args: INetlifyDeployArgs): Promise<RETVAL | undefined> => {
let retVal: RETVAL | undefined;
try {
const siteId = args.siteId || (await getSiteId(args.spinner));
const token = args.token || (await getToken(args.spinner));
const deployArgs = [
'deploy',
`--site=${siteId}`,
`--auth=${token}`,
`--cwd=${process.cwd()}`,
`--dir=${args.dir}`,
'--json'
];
if (args.production) {
deployArgs.push('--prod');
}
if (args.message) {
deployArgs.push(`--message=${args.message}`);
}
/* https://cli.netlify.com/commands/deploy requires netlify-cli */
const deploy = await execa('netlify', deployArgs, { preferLocal: true });
const response = JSON.parse(deploy.stdout);
const url = args.production ? response.url : response.deploy_url;
retVal = { url, logUrl: response.logs };
} catch (error: unknown) {
handleErrorMessage(error, 'netlify-deploy', args.spinner);
throw error;
}
return retVal;
};
export default (spinner: Ora): Command => {
const command = new Command('netlify-deploy');
return command
.description('deploy to a Netlify site')
.argument('<dir>', 'directory of content to deploy')
.option('-p, --production', 'production deploy')
.option('-i, --id <id>', 'site API ID')
.option('-t, --token <token>', 'access token')
.option('-m, --message <message>', 'deploy message')
.action(async dir => {
try {
spinner.start();
const options = command.opts();
const result = await execute({
dir,
production: options.production,
token: options.token,
siteId: options.id,
message: options.message,
spinner
});
if (result) {
handleSuccessMessage(result.url, spinner);
} else {
throw new Error();
}
} catch {
spinner.fail(`Unable to deploy ${dir}`);
process.exitCode = 1;
} finally {
spinner.stop();
}
});
};