-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
134 lines (114 loc) · 2.9 KB
/
cli.js
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
#!/usr/bin/env node
const exec = require('child_process').exec;
const ora = require('ora');
const meow = require('meow');
const stripAnsi = require('strip-ansi');
const { prompt } = require('enquirer');
const cli = meow(`
Usage:
$ teardown surge
By default teardown will fetch all surge.sh projects.
Options:
surge Get the list of all surge.sh projects to delete.
`,
{
flags: {
surge: {
type: 'boolean'
}
}
}
);
const input = cli.input.length ? cli.input : 'surge';
const spinner = ora('Getting the list of surge.sh projects, pls wait!').start();
const services = {
surge: {
whoami: 'surge whoami',
list: 'surge list',
teardown: 'surge teardown'
}
};
const asyncExec = command =>
new Promise((resolve, reject) => {
exec(`${command}`, (err, stdout, stderr) => {
if (err) {
reject(err);
return;
}
resolve(stdout);
});
});
const isAuthenticated = () => asyncExec(services[input].whoami);
isAuthenticated()
.then(stdout => {
const result = stripAnsi(stdout)
.split('\n')
.map(s => s.trim())
.filter(Boolean);
spinner.stop();
if (result.length && result[0] === 'Not Authenticated') {
console.log('\n Make sure you are logged into surge.sh and try again.\n');
return;
}
})
.catch(err => {
spinner.stop();
console.log('\n Error occurred while getting the list of projects.\n', err);
process.exit();
});
const getListOfProjects = () => asyncExec(services[input].list);
getListOfProjects()
.then(async projects => {
const listOfProjects = stripAnsi(projects)
.split('\n')
.map(s => s.trim())
.filter(Boolean)
.map(list => list.split(' ')[1]);
const userResponse = await prompt([
{
type: 'select',
name: 'projects',
message: 'Select the surge.sh projects to delete (Space to select or Esc to cancel)',
choices: listOfProjects,
highlight: true,
multiple: true
}
]);
spinner.stop();
spinner.text = `Tearing down the selected projects, pls wait..`;
spinner.start();
if (userResponse.projects.length) {
const done = Promise.all([
new Promise((resolve, reject) => {
userResponse.projects.map(project => teardown(project, resolve, reject));
})
]);
done
.then(() => {
spinner.stop();
console.log(`\n ✅ Selected projects deleted successfully! \n`);
})
.catch(err => {
spinner.stop();
console.log(`\n Error occurred while deleting some of the surge.sh projects! \n`, err);
});
} else {
spinner.stop();
console.log(`\n Pls select the projects to deleted. \n`);
}
})
.catch(err => {
spinner.stop();
if (!err) {
console.log('\n 🥑 You canceled it.\n', err);
} else console.log('Error occurred while getting list of surge.sh projects', err);
});
const teardown = (project, resolve, reject) => {
exec(`${services[input].teardown} ${String(project)}`, err => {
if (err) {
reject(err);
return;
}
resolve(project);
});
};