|
1 | 1 | #!/usr/bin/env node |
2 | | -const inquirer = require('inquirer'); |
3 | | -const fs = require('fs'); |
4 | | -const argv = require('yargs').argv; |
5 | | -const stravaConfig = './data/strava_config'; |
6 | | -const stravaConfigTemplate = './strava_config'; |
7 | | -const stravaApiUrl = 'https://www.strava.com/settings/api#_=_'; |
| 2 | +const inquirer = require('inquirer') |
| 3 | +const fs = require('fs') |
| 4 | +const argv = require('yargs').argv |
| 5 | +const stravaConfig = './data/strava_config' |
| 6 | +const stravaConfigTemplate = './strava_config' |
| 7 | +const stravaApiUrl = 'https://www.strava.com/settings/api#_=_' |
8 | 8 |
|
9 | | -const strava = require('../index.js'); |
| 9 | +const strava = require('../index.js') |
10 | 10 |
|
11 | 11 | /** |
12 | 12 | * Generates the token to access the strava application |
13 | 13 | */ |
14 | | -console.log('Before processing, you shall fill your strava config with client id and secret provided by Strava:\n' + stravaApiUrl); |
| 14 | +console.log('Before processing, you shall fill your strava config with client id and secret provided by Strava:\n' + stravaApiUrl) |
15 | 15 |
|
| 16 | +inquirer |
| 17 | + .prompt( |
| 18 | + [ |
| 19 | + { |
| 20 | + type: 'input', |
| 21 | + name: 'clientId', |
| 22 | + message: 'What is your strava client id?', |
| 23 | + default: argv['client-id'] |
| 24 | + }, |
| 25 | + { |
| 26 | + type: 'input', |
| 27 | + name: 'clientSecret', |
| 28 | + message: 'What is your strava client secret?', |
| 29 | + default: argv['client-secret'] |
| 30 | + } |
| 31 | + ]) |
| 32 | + .then(function (answers) { |
| 33 | + // We copy the strava config file |
| 34 | + try { |
| 35 | + fs.mkdirSync('data') |
| 36 | + } catch (e) { |
| 37 | + // nothing |
| 38 | + } |
16 | 39 |
|
17 | | - inquirer |
18 | | - .prompt( |
19 | | - [ |
20 | | - { |
21 | | - type: 'input', |
22 | | - name: 'clientId', |
23 | | - message: 'What is your strava client id?', |
24 | | - default: argv['client-id'], |
25 | | - }, |
26 | | - { |
27 | | - type: 'input', |
28 | | - name: 'clientSecret', |
29 | | - message: 'What is your strava client secret?', |
30 | | - default: argv['client-secret'], |
31 | | - } |
32 | | - ]) |
33 | | - .then(function (answers) { |
| 40 | + var content = fs.readFileSync(stravaConfigTemplate) |
| 41 | + fs.writeFileSync(stravaConfig, content) |
34 | 42 |
|
35 | | - // We copy the strava config file |
36 | | - try { |
37 | | - fs.mkdirSync('data'); |
38 | | - } catch (e) { |
39 | | - // nothing |
40 | | - } |
| 43 | + // We open the default config file and inject the client_id and client secret |
| 44 | + // Without these informations in the config file the getRequestAccessURL would fail |
| 45 | + content = fs.readFileSync(stravaConfig) |
| 46 | + var config = JSON.parse(content) |
| 47 | + config.client_id = answers.clientId |
| 48 | + config.client_secret = answers.clientSecret |
| 49 | + config.access_token = 'to define' |
| 50 | + // You may need to make your callback URL |
| 51 | + // at Strava /settings/api temporarily match this |
| 52 | + config.redirect_uri = 'http://localhost' |
41 | 53 |
|
42 | | - var content = fs.readFileSync(stravaConfigTemplate); |
43 | | - fs.writeFileSync(stravaConfig, content); |
| 54 | + // We update the config file |
| 55 | + fs.writeFileSync(stravaConfig, JSON.stringify(config)) |
44 | 56 |
|
45 | | - // We open the default config file and inject the client_id and client secret |
46 | | - // Without these informations in the config file the getRequestAccessURL would fail |
47 | | - content = fs.readFileSync(stravaConfig); |
48 | | - var config = JSON.parse(content); |
49 | | - config.client_id = answers.clientId; |
50 | | - config.client_secret = answers.clientSecret; |
51 | | - config.access_token = 'to define'; |
52 | | - // You may need to make your callback URL |
53 | | - // at Strava /settings/api temporarily match this |
54 | | - config.redirect_uri = 'http://localhost'; |
55 | | - |
56 | | - // We update the config file |
57 | | - fs.writeFileSync(stravaConfig, JSON.stringify(config)); |
58 | | - |
59 | | - // Generates the url to have full access |
60 | | - var url = strava.oauth.getRequestAccessURL({ |
61 | | - scope:"activity:write,profile:write,read_all,profile:read_all,activity:read_all" |
62 | | - }); |
63 | | - // We have to grab the code manually in the browser and then copy/paste it into strava_config as "access_token" |
64 | | - console.log('Connect to the following url and copy the code: ' + url); |
65 | | - |
66 | | - inquirer.prompt( |
67 | | - [ |
68 | | - { |
69 | | - type: 'input', |
70 | | - name: 'code', |
71 | | - message: 'Enter the code obtained from previous strava url (the code parameter in redirection url)' |
72 | | - } |
73 | | - ]) |
74 | | - .then(function (answers2) { |
75 | | - if (!answers2.code) { |
76 | | - console.log("no code provided") |
77 | | - process.exit() |
78 | | - } |
79 | | - strava.oauth.getToken(answers2.code).then( result => { |
80 | | - // We update the access token in strava conf file |
81 | | - if (result.access_token === undefined) throw 'Problem with provided code: ' + JSON.stringify(result); |
82 | | - config.access_token = result.access_token; |
83 | | - fs.writeFileSync(stravaConfig, JSON.stringify(config)); |
84 | | - }); |
85 | | - }) |
86 | | - .then(done => console.log("Done. Details written to data/strava_config.")) |
87 | | - }); |
| 57 | + // Generates the url to have full access |
| 58 | + var url = strava.oauth.getRequestAccessURL({ |
| 59 | + scope: 'activity:write,profile:write,read_all,profile:read_all,activity:read_all' |
| 60 | + }) |
| 61 | + // We have to grab the code manually in the browser and then copy/paste it into strava_config as "access_token" |
| 62 | + console.log('Connect to the following url and copy the code: ' + url) |
88 | 63 |
|
| 64 | + inquirer.prompt( |
| 65 | + [ |
| 66 | + { |
| 67 | + type: 'input', |
| 68 | + name: 'code', |
| 69 | + message: 'Enter the code obtained from previous strava url (the code parameter in redirection url)' |
| 70 | + } |
| 71 | + ]) |
| 72 | + .then(function (answers2) { |
| 73 | + if (!answers2.code) { |
| 74 | + console.log('no code provided') |
| 75 | + process.exit() |
| 76 | + } |
| 77 | + strava.oauth.getToken(answers2.code).then(result => { |
| 78 | + // We update the access token in strava conf file |
| 79 | + if (result.access_token === undefined) throw new Error('Problem with provided code: ' + JSON.stringify(result)) |
| 80 | + config.access_token = result.access_token |
| 81 | + fs.writeFileSync(stravaConfig, JSON.stringify(config)) |
| 82 | + }) |
| 83 | + }) |
| 84 | + .then(done => console.log('Done. Details written to data/strava_config.')) |
| 85 | + }) |
0 commit comments