-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
102 lines (93 loc) · 2.97 KB
/
index.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
#!/usr/bin/env node
const fs = require("fs");
const inquirer = require("inquirer");
const googleAuth = require("./src/google-auth");
const { getAccounts, getWebProperties, getProfiles, getData } = require("./src/google-analytics");
const { parse } = require("./src/caniuse-parser");
inquirer.registerPrompt("datetime", require("inquirer-datepicker-prompt"));
const outputFilename = "browserslist-stats.json";
googleAuth(oauth2Client => {
let selectedProfile;
getAccounts(oauth2Client)
.then((accounts) => {
if (accounts.length === 0) {
throw new Error('No Google Analytics accounts.')
}
return accounts;
})
.then(accounts =>
inquirer.prompt([
{
type: "list",
name: "account",
message: "Please select an account:",
choices: accounts.map(account => ({
value: account,
name: `${account.name} (#${account.id})`,
})),
},
])
)
.then(({ account }) => getWebProperties(oauth2Client, account.id))
.then(webProperties =>
inquirer.prompt([
{
type: "list",
name: "webProperty",
message: "Please select a property:",
choices: webProperties.map(webProperty => ({
value: webProperty,
name: `${webProperty.name} (#${webProperty.id})`,
})),
},
])
)
.then(({ webProperty }) => getProfiles(oauth2Client, webProperty.accountId, webProperty.id))
.then(profiles =>
inquirer.prompt([
{
type: "list",
name: "profile",
message: "Please select a profile:",
choices: profiles.map(profile => ({
value: profile,
name: `${profile.name} (#${profile.id})`,
})),
},
])
)
.then(({ profile }) => {
const defaultStartDate = new Date();
const defaultEndDate = new Date();
selectedProfile = profile;
// End date defaults to today, start date defaults to 90 days ago
defaultStartDate.setDate(defaultEndDate.getDate() - 90);
return inquirer.prompt([
{
type: "datetime",
name: "startDate",
message: 'Specify a start date (format is "YYYY-MM-DD", defaults to 90 days ago):',
format: ["yyyy", "-", "mm", "-", "dd"],
initial: defaultStartDate,
},
{
type: "datetime",
name: "endDate",
message: 'Specify an end date (format is "YYYY-MM-DD", defaults to today):',
format: ["yyyy", "-", "mm", "-", "dd"],
initial: defaultEndDate,
},
]);
})
.then(({ startDate, endDate }) => getData(oauth2Client, selectedProfile.id, startDate, endDate))
.then(parse)
.then(stats => {
fs.writeFileSync(outputFilename, JSON.stringify(stats, null, 2));
console.log(`Success! Stats saved to '${outputFilename}'`);
process.exit();
})
.catch(err => {
console.error(err);
process.exit(1);
});
});