37 lines
1.0 KiB
JavaScript
37 lines
1.0 KiB
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(`http://${config.servers.deconz.url}:${config.servers.deconz.apiPort}/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)
|
|
}) |