Skip to content

Instantly share code, notes, and snippets.

@isaacpalomero
Created June 25, 2019 21:28
Show Gist options
  • Save isaacpalomero/a943001b689d4911707889865fb03589 to your computer and use it in GitHub Desktop.
Save isaacpalomero/a943001b689d4911707889865fb03589 to your computer and use it in GitHub Desktop.
/**
* Copyright (c) 2014-2019 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
// - You have to make a dir named "resources" and put there all locale specific manifest data.
// Example:
//
// Resources Folders
// ---------------------
// resources
// ⌞ locales
// ⌞ en
// ⌞ privacyAndCompliance.json
// ⌞ publishingInformation.json
// ⌞ es
// ⌞ privacyAndCompliance.json
// ⌞ publishingInformation.json
// ⌞ etc
// ⌞ etc
//
//
// privacyAndCompliance.json
// ---------------------
// {
// "privacyPolicyUrl": "https://www.example.org/privacy.html",
// "termsOfUseUrl": "https://www.example.org/termsOfUseUrl.html"
// }
//
// privacyAndCompliance.json
// ---------------------
// {
// "summary": "Learn fun facts about space.",
// "examplePhrases": [
// "Alexa open space facts"
// ],
// "keywords": [
// "space"
// ],
// "name": "FactTest+",
// "description": "Ask for Space Facts...",
// "smallIconUri": "https://m.media-amazon.com/images/G/01/alexa-blueprints/icons/facts-skill-sm._CB1526682764_.png",
// "largeIconUri": "https://m.media-amazon.com/images/G/01/alexa-blueprints/icons/facts-skill-lg._CB1526682764_.png"
// }
//
// How to use it:
// [any-path]$ npm install -g "fs"
// [root-project-path]$ node ./scripts/update-skill-manifest.js
////
//TODO: update this with your values
const ASK_ENV = "development"; //Values: "development" | "production"
const RESOURCES_PATH = "../resources";
const SKILL_SCHEMA_PATH = "../skill.json";
//END-TODO
const fs = require('fs');
/**
* Locales supported list
* https://developer.amazon.com/docs/smapi/skill-manifest.html#locales
* @type {Array}
*/
const LOCALES = [
'de-DE', 'en-AU', 'en-CA', 'en-GB', 'en-IN', 'en-US', 'es-ES', 'es-MX',
'fr-CA', 'fr-FR', 'it-IT', 'ja-JP', 'pt-BR'
];
/**
* Regions supported list (for production deployment)
* https://developer.amazon.com/docs/smapi/skill-manifest.html#regions
* @type {Array}
*/
const REGIONS = [
'NA', 'EU', 'FE'
];
/**
* Load locale resources
* @param {Object} schema
*/
function loadLocaleResources(schema) {
LOCALES.forEach((locale) => {
const dirname = fs.readdirSync(`${RESOURCES_PATH}/locales`).find(
filename => filename === locale.split('-').shift());
if (dirname) {
fs.readdirSync(`${RESOURCES_PATH}/locales/${dirname}`).filter(
filename => filename.endsWith('.json')).forEach((filename) => {
const property = filename.replace(/\.json$/, '');
if (typeof schema.manifest[property] === 'object') {
try {
schema.manifest[property].locales[locale] = require(`${RESOURCES_PATH}/locales/${dirname}/${filename}`);
} catch (e) {
console.log(`Failed to load locale property file: ${RESOURCES_PATH}/locales/${dirname}/${filename}`);
throw e;
}
}
});
}
});
}
/**
* Set api regional endpoints (for production deployment)
* @param {Object} schema
*/
function setApiRegionalEndpoints(schema) {
//if (process.env.ASK_ENV === 'production') {
if (ASK_ENV === 'production') {
schema.manifest.apis.custom.regions = {};
REGIONS.forEach((region) => {
schema.manifest.apis.custom.regions[region] = { endpoint: null }
schema.manifest.apis.custom.regions[region].endpoint = schema.manifest.apis.custom.endpoint;
});
} else {
delete schema.manifest.apis.custom.regions;
}
}
/**
* Load skill schema
* @return {Object}
*/
function loadSkillSchema() {
try {
return require(SKILL_SCHEMA_PATH);
} catch (e) {
console.log(`Failed to load skill schema: ${SKILL_SCHEMA_PATH}`);
throw e;
}
}
/**
* Save skill schema
* @param {Object} schema
*/
function saveSkillSchema(schema) {
try {
fs.writeFileSync(SKILL_SCHEMA_PATH, JSON.stringify(schema, null, 2));
} catch (e) {
console.log(`Failed to save skill schema: ${SKILL_SCHEMA_PATH}`);
throw e;
}
}
if (require.main === module) {
try {
// Change working directory to script location
process.chdir(__dirname);
// Load skill schema
const schema = loadSkillSchema();
// Load locale resources into skill schema
loadLocaleResources(schema);
// Set api regional endpoints
setApiRegionalEndpoints(schema);
// Save skill schema
saveSkillSchema(schema);
} catch (error) {
console.log(`Error: ${error.message}`)
process.exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment