UNPKG

react-native-template-mrf

Version:

201 lines (167 loc) 5.88 kB
const resize = require('./manipulate_image') const fs = require('fs') const g2js = require('gradle-to-js/lib/parser') const { promisify } = require('util') const androidPath = '../android' const appPath = `${androidPath}/app` const destinationPath = `${appPath}/src/main/res` const exists = promisify(fs.exists) const cp = promisify(fs.copyFile) const readFile = promisify(fs.readFile) const writeFile = promisify(fs.writeFile) const checkFiles = async () => { if (!(await exists('./asset/google-services.json'))) { throw { error: 'missing google-services.json' } } else if (!(await exists('./asset/Icon.png'))) { throw { error: 'missing Icon.png' } } else if (!(await exists('./asset/logo.png'))) { throw { error: 'missing logo.png' } } } const copyGoogleServices = async () => { const file = 'google-services.json' if (await exists(`${appPath}/${file}`)) { return } if (await exists(`./asset/${file}`)) { await cp(`./asset/${file}`, `${appPath}/${file}`) return } throw { error: `missing ${file}` } } const generateIcons = async config => { resize.setConfig({ originalFile: config.icon, destinationPath, }) const paths = [ { path: 'mipmap-hdpi', size: 72 }, { path: 'mipmap-mdpi', size: 48 }, { path: 'mipmap-xhdpi', size: 96 }, { path: 'mipmap-xxhdpi', size: 144 }, { path: 'mipmap-xxxhdpi', size: 192 }, ] for (const path of paths) { await resize.resize({ path: path.path, file: 'ic_launcher', width: path.size }) await resize.resize({ path: path.path, file: 'ic_launcher_round', width: path.size, round: true }) } } const generateSplash = async config => { const mkdir = promisify(fs.mkdir) // Splash const splashPath = `${destinationPath}/drawable` if (!(await exists(splashPath))) { await mkdir(splashPath) } let bkgSplashScreen = '@color/background_color' if (!!config.backgroundImage && (await exists(`./asset/${config.backgroundImage}`))) { await cp(`./asset/${config.backgroundImage}`, `${destinationPath}/mipmap-hdpi/${config.backgroundImage}`) bkgSplashScreen = `@mipmap/${config.backgroundImage}`.replace('.png', '') } else { // Colors const bkColor = config.backgroundColor || '#ffffff' const colorContent = await readFile('./asset/colors.xml', 'utf8').then(rs => rs.replace('__color__', bkColor)) const file = `${destinationPath}/values/colors.xml` await writeFile(file, colorContent) } const splashContent = await readFile('./asset/splash_screen.xml', 'utf8').then(rs => rs.replace('__background__', bkgSplashScreen)) const splashFile = `${splashPath}/splash_screen.xml` await writeFile(splashFile, splashContent) // Open styles.xml const styleFile = `${destinationPath}/values/styles.xml` let content = await readFile(styleFile, 'utf8') if (!content.includes('splash_screen')) { const replace = ` <item name="android:windowBackground">@drawable/splash_screen</item> </style>` content = content.replace('</style>', replace) await writeFile(styleFile, content) } } const generateSplashLogo = async config => { // Resize logo const imageFileStats = await resize.getStats('./asset/logo.png') const paths = [ { path: 'mipmap-hdpi', factor: 1.5 }, { path: 'mipmap-mdpi', factor: 1 }, { path: 'mipmap-xhdpi', factor: 2 }, { path: 'mipmap-xxhdpi', factor: 3 }, { path: 'mipmap-xxxhdpi', factor: 4 }, ] const width = 240 resize.setConfig({ originalFile: config.logo, destinationPath, }) for (const info of paths) { await resize.resize({ path: info.path, file: 'logo', width: parseInt(width * info.factor, 10), }) } } const insertContent = ({ content, keyword, toInsert, path, stop = false }) => { if (content.includes(keyword)) { return content } const elements = [] let text = content for (const key of path.split('.')) { const index = text.indexOf(key) text = text.substr(index) elements.push({ key, text, }) } if (elements.length === 0) { return content } const [item] = elements.reverse() const data = item.text.substr(0, item.text.indexOf('}')) if (stop) { console.log(item.text.match(/{/g)) console.log(data) process.exit() } const replaceText = `${data} ${toInsert} ` return content.replace(data, replaceText) } const projectBuildGradle = async () => { // google() // maven { url "https://jitpack.io" } let content = await readFile(`${androidPath}/build.gradle`, 'utf8') const keyword = 'com.google.gms:google-services' content = insertContent({ content, keyword, toInsert: `classpath '${keyword}:4.2.0'`, path: 'buildscript.dependencies', }) // content = insertContent({ // content, // keyword: 'google()', // toInsert: 'google()', // path: 'allprojects.repositories', // stop: true, // }) } const implementBuildGradle = async config => { // implementation 'com.android.support:multidex:1.0.3' // implementation 'com.facebook.android:facebook-android-sdk:[4,5)' // implementation "com.google.android.gms:play-services-base:16.1.0" // implementation "com.google.firebase:firebase-core:16.0.9" // const appBuild = } module.exports = async config => { await checkFiles() await copyGoogleServices() await generateIcons(config) await generateSplash(config) await generateSplashLogo(config) // await projectBuildGradle() }