@applicaster/zapplicaster-cli
Version:
CLI Tool for the zapp app and Quick Brick project
94 lines (81 loc) • 2.66 kB
JavaScript
const R = require("ramda");
const logger = require("../logger");
const DEVICES = {
APPLE_TV: "apple_tv",
ANDROID_TV: "android_tv",
AMAZON: "amazon",
SAMSUNG_TV: "samsung_tv",
TV: "tv",
UNIVERSAL: "universal",
MOBILE: "mobile",
TABLET: "tablet",
IPHONE: "iphone",
SMARTPHONE: "smartphone",
VIZIO: "vizio",
};
const DEVICE_SPECIFIC_TEMPLATES = [
DEVICES.APPLE_TV,
DEVICES.SAMSUNG_TV,
DEVICES.ANDROID_TV,
DEVICES.AMAZON,
];
const templatesMap = {
ios: "@applicaster/zapp-react-native-cli-template",
android: "@applicaster/zapp-react-native-cli-template",
samsung_tv: "@applicaster/zapp-react-dom-cli-template",
lg_tv: "@applicaster/zapp-react-dom-cli-template",
vizio: "@applicaster/zapp-react-dom-cli-template",
apple_tv: "@applicaster/zapp-react-native-tvos-cli-template",
android_tv: "@applicaster/zapp-react-native-android-tv-cli-template",
amazon: "@applicaster/zapp-react-native-android-tv-cli-template",
};
/**
* convenience function created to throw an error
* will only be called if the platform requested is not in the map above
* @param {string} platform to look up for
* @returns {Function} which throws an error with an explicit message
*/
function throwTemplateError(platform) {
return function () {
throw new Error(
`could not find template for ${platform} - available templates: ${R.keys(
templatesMap
)}`
);
};
}
/**
* returns the module for the template on a given platform
* if the platform is unknown, will throw an error
* @param {string} platform to look up template for
* @returns {string} name of the template module
*/
function getAppTemplate(platform) {
return R.ifElse(
R.has(platform),
R.prop(platform),
throwTemplateError(platform)
)(templatesMap);
}
/**
* returns the app template module
* @param {String?} templatePath to use. if null, will try to resolve the template from the platform
* @param {String} platform: will resolve template for the platform if templatePath is null
* @param {String} deviceTarget: device target of the app (universal, TV...)
* @returns {Object} template module
*/
function resolveAppTemplate(templatePath, platform, deviceTarget) {
if (templatePath) {
logger.log(`\u{1f4e6} using provided template: ${templatePath}`);
return require(templatePath);
}
if (DEVICE_SPECIFIC_TEMPLATES.includes(deviceTarget)) {
logger.log(`\u{1f4e6} using ${deviceTarget} template`);
return require(templatesMap[deviceTarget]);
}
logger.log(`\u{1f4e6} using ${platform} template`);
return require(getAppTemplate(platform));
}
module.exports = {
resolveAppTemplate,
};