37 lines
1005 B
JavaScript
37 lines
1005 B
JavaScript
|
import axios from 'axios'
|
||
|
import fs from 'fs'
|
||
|
import meow from 'meow'
|
||
|
let config = JSON.parse(fs.readFileSync('config.json', 'utf8'))
|
||
|
|
||
|
const cli = meow(`
|
||
|
Usage
|
||
|
$ node getKey.js
|
||
|
|
||
|
Options
|
||
|
--force, -f Overwrite existing config.json file
|
||
|
`, {
|
||
|
importMeta: import.meta,
|
||
|
flags: {
|
||
|
force: {
|
||
|
type: 'boolean',
|
||
|
shortFlag: 'f'
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
if (config.servers.deconz.apiKey.length > 0 && !cli.flags.force) {
|
||
|
console.log("API Key already exists in config.json. Use --force to overwrite.")
|
||
|
process.exit(1)
|
||
|
}
|
||
|
|
||
|
axios.post(config.servers.deconz.url + '/api', {
|
||
|
devicetype: config.servers.deconz.clientName +'l',
|
||
|
})
|
||
|
.then(response => {
|
||
|
console.log("Got API Key, saving to config.json")
|
||
|
config.servers.deconz.apiKey = response.data[0].success.username
|
||
|
fs.writeFileSync('config.json', JSON.stringify(config, null, 2))
|
||
|
}).catch(error => {
|
||
|
console.log("Did you enable API linking in Phoscon?")
|
||
|
console.log(error.status)
|
||
|
})
|