Last active
June 12, 2020 19:51
-
-
Save goldzulu/b2f80676bed088242228417aab71f715 to your computer and use it in GitHub Desktop.
Get Alexa Skill Skill ID from command line suitable for both Ask Cli > 2.x and Ask Cli 1.x originally by Justin Jeffress @SleepyDeveloper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const CONFIG_FILE = '.ask/ask-states.json'; | |
const CONFIG_FILE_V1 = '.ask/config'; | |
// Break the path into an array of folders and filter out | |
// the empty entry for the leading '/' | |
const folders = process.env.PWD.split('/').filter(Boolean); | |
// findAskConfigFile is a recursive function that searches | |
// from the Parent Working Directory to one folder below root for | |
// the .ask/config file or .ask/ask-states.json (v2) | |
// For example, if PWD is /Work/amazon/skills/coffee_shop/lambda | |
// it will look for .ask/config or .ask/ask-states.json in the following folders: | |
// /Work/amazon/skills/coffee_shop/lambda | |
// /Work/amazon/skills/coffee_shop | |
// /Work/amazon/skills | |
// /Work/amazon | |
// /Work | |
const findAskConfigFile = function (folders) { | |
// The ask cli downloads skills into a folder and | |
// writes the .ask/config or ..ask/ask-states.json (v2) there. There should | |
// never be one at your root. | |
if (folders.length <= 0) throw 'No config file found!'; | |
const directory = folders.join('/'); | |
const askConfigFile = '/' + path.join(directory, CONFIG_FILE); | |
const askConfigFileV1 = '/' + path.join(directory, CONFIG_FILE_V1); | |
if (fs.existsSync(askConfigFile)) { | |
return askConfigFile; | |
} else { | |
if (fs.existsSync(askConfigFileV1)) { | |
return askConfigFileV1; | |
} | |
} | |
// if .ask/config or .ask/ask-states.json (v2) doesn't exist in the current directory | |
// look in the one above by removing the last item from | |
// folders and call findAskConfigFile again. | |
folders.pop(); | |
return findAskConfigFile(folders); | |
}; | |
const configFile = findAskConfigFile(folders); | |
if (configFile) { | |
fs.readFile(configFile, 'utf8', function(err, data) { | |
if (err) throw err; | |
// prase the JSON | |
obj = JSON.parse(data); | |
// print the skill id if v2 | |
if (obj.profiles) { | |
console.log(obj.profiles.default.skillId); | |
} else { | |
console.log(obj.deploy_settings.default.skill_id); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment