|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const replace = require('replace'); |
| 5 | +const execSync = require('child_process').execSync; |
| 6 | +const readline = require('readline'); |
| 7 | +const fse = require('fs-extra'); |
| 8 | + |
| 9 | +const PROJECT_NAME = "XX_PROJECT_NAME_XX"; |
| 10 | +const APP_ID = "XX_APPLICATION_ID_XX"; |
| 11 | +const ANDROID_SRC_PATH = 'android/app/src/main/java/'; |
| 12 | +const BOILERPLATE_DIR = path.join(__dirname, '..', 'boilerplate'); |
| 13 | + |
| 14 | +const replaceInProject = (oldStr, newStr, appDir) => { |
| 15 | + replace({ |
| 16 | + regex: oldStr, |
| 17 | + replacement: newStr, |
| 18 | + paths: [appDir], |
| 19 | + recursive: true, |
| 20 | + silent: true, |
| 21 | + exclude: 'generator.js' |
| 22 | + }); |
| 23 | +}; |
| 24 | + |
| 25 | +const generate = function (appName, appId) { |
| 26 | + const appDir = `${process.cwd()}/${appName}/`; |
| 27 | + fse.mkdirsSync(appDir); |
| 28 | + fse.copySync(BOILERPLATE_DIR, appDir, { |
| 29 | + filter: (src, dest) => { |
| 30 | + return src.indexOf('node_modules') === -1; |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + console.log('Setting Project Name(MyAwesomeApp): ' + appName); |
| 35 | + replaceInProject(PROJECT_NAME, appName, appDir); |
| 36 | + |
| 37 | + console.log('Setting App Identifier: ' + appId); |
| 38 | + replaceInProject(APP_ID, appId, appDir); |
| 39 | + |
| 40 | + console.log('Setting Project Folder:' + appName); |
| 41 | + const renamerCli = path.join(__dirname, '..', 'node_modules/renamer/bin/cli.js'); |
| 42 | + const command = `${renamerCli} --find '${PROJECT_NAME}' --replace '${appName}' '${appDir}**'`; |
| 43 | + execSync(command); |
| 44 | + |
| 45 | + console.log('Copying Android Source files'); |
| 46 | + const fullPath = appDir + ANDROID_SRC_PATH + appId.split('.').join('/'); |
| 47 | + fse.mkdirsSync(fullPath); |
| 48 | + fse.moveSync(`${appDir}${ANDROID_SRC_PATH}${APP_ID}`, fullPath, {overwrite: true}); |
| 49 | + |
| 50 | + console.log('Created Project at ' + appDir); |
| 51 | +}; |
| 52 | + |
| 53 | + |
| 54 | +const rl = readline.createInterface({ |
| 55 | + input: process.stdin, |
| 56 | + output: process.stdout |
| 57 | +}); |
| 58 | + |
| 59 | +rl.question('\nApp Name: ', (appName) => { |
| 60 | + rl.question('\nApp Identifier: ', (appId) => { |
| 61 | + generate(appName, appId); |
| 62 | + |
| 63 | + rl.close(); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
0 commit comments