Skip to content

Commit

Permalink
Merge pull request #637 from SUI-Components/feat/sui-app-icons
Browse files Browse the repository at this point in the history
feat(components/tool/app): Automatic optimization of the capacitor co…
  • Loading branch information
oegea authored Nov 16, 2023
2 parents c680fe0 + ab6d864 commit a590f7a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 62 deletions.
15 changes: 14 additions & 1 deletion components/tool/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Before start using biometric auth, `USE_BIOMETRIC` permission needs to be added
<uses-permission android:name="android.permission.USE_BIOMETRIC">
```

### iOS configuration
### iOS prerequisites

Add something similar to the following to `App/info.plist`:

Expand All @@ -71,6 +71,8 @@ Add something similar to the following to `App/info.plist`:
<string>For an easier and faster log in.</string>
```

Note that the string will be displayed on the user interface sometimes.

### Check if biometric login is available

```
Expand Down Expand Up @@ -112,4 +114,15 @@ const isAvailable = await getBiometricLoginCredentials({

## Local notifications

### Android prerequisites

1. Create a `notification.png` icon and put it into the `res/drawable` folder, on the android project. It is also possible to edit `capacitor.config.json` file and change the `smallIcon` property contained inside the `LocalNotifications` node.
2. Add the `SCHEDULE_EXACT_ALARM` permission to the `AndroidManifest.xml`.

```
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
```

### Other features

Please refer to the following documentation: `https://capacitorjs.com/docs/apis/local-notifications`
82 changes: 49 additions & 33 deletions components/tool/app/bin/commands/init.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
// Constants
const {PACKAGE_JSON_FILE, PACKAGE_NAME} = require('../infrastructure/config.js')

Expand All @@ -8,7 +9,8 @@ const {
readJSONFile,
reportError,
runCommand,
installPackage
installPackage,
saveJSONFile
} = require('../infrastructure/utils.js')

const initProject = () => {
Expand All @@ -23,62 +25,79 @@ const initIOS = () => {
return runCommand(`npx cap add ios`)
}

const optimizeConfigurations = () => {
const config = readJSONFile('./capacitor.config.json')
config.plugins = {
CapacitorCookies: {
enabled: true
},
LocalNotifications: {
smallIcon: 'notification',
iconColor: '#488AF',
sound: 'beep.wav'
},
CapacitorHttp: {
enabled: true
}
}

saveJSONFile(config, './capacitor.config.json')
}

// Business logic
const {hasPackageJson, hasCapacitorConfig} = require('../domain/utils.js')

const hasDependency = dependency => {
const packageData = readJSONFile(
`${getCurrentDirectory()}/${PACKAGE_JSON_FILE}`
)
const packageData = readJSONFile(`${getCurrentDirectory()}/${PACKAGE_JSON_FILE}`)
return packageData.dependencies.hasOwnProperty(dependency)
}

const installDependency = dependency => {
console.log(`\n\n🚚 Installing required dependency 👉 ${dependency}\n\n`)
console.log(`\n🚚 Installing required dependency 👉 ${dependency}\n`)
const result = installPackage(dependency)

if (result === false)
reportError(
`\n\n🚨 Something went wrong while installing dependencies 🚨\n\n`
)
else console.log(`\n\n✅ Dependency has been successfully installed\n\n`)
if (result === false) reportError(`\n🚨 Something went wrong while installing dependencies 🚨\n`)
else console.log(`\n✅ Dependency has been successfully installed\n`)
}

const initAppProject = () => {
console.log('\n\n🚚 Initializing the project\n\n')
console.log('\n🚚 Initializing the project.\n')
const result = initProject()

if (result === false)
reportError(
`\n\n🚨 Something went wrong while initializing the project 🚨\n\n`
)
else console.log('\n\n✅ Project has been successfully initialized\n\n')
if (result === false) reportError(`\n🚨 Something went wrong while initializing the project 🚨\n`)
else console.log('\n✅ Project has been successfully initialized\n')
}

const addAndroidProject = () => {
console.log('\n\n🤖 Adding android project\n\n')
console.log('\n🤖 Adding android project\n')
const result = initAndroid()

if (result === false)
reportError(`\n\n🚨 Something went wrong while configuring android 🚨\n\n`)
else console.log('\n\n✅ Android has been successfully initialized\n\n')
if (result === false) reportError(`\n🚨 Something went wrong while configuring android 🚨\n`)
else console.log('\n✅ Android has been successfully initialized\n')
}

const addIOSProject = () => {
console.log('\n\n🍏 Adding iOS project\n\n')
console.log('\n🍏 Adding iOS projectt\n')
const result = initIOS()

if (result === false)
reportError(`\n\n🚨 Something went wrong while configuring iOS 🚨\n\n`)
else console.log('\n\n✅ iOS has been successfully initialized\n\n')
if (result === false) reportError(`\n🚨 Something went wrong while configuring iOS 🚨\n`)
else console.log('\n✅ iOS has been successfully initialized\n')
}

const applyConfigurationOptimizations = () => {
console.log('\n Applying configuration optimizations\n')
const result = optimizeConfigurations()

if (result === false) reportError(`\n🚨 Something went wrong while applying configuration optimizations 🚨\n`)
else console.log('\n✅ Configuration optimizations have been successfully applied\n')
}

// Command
module.exports = () => {
// If we are not placed on a webapp, we cannot continue
if (!hasPackageJson()) {
reportError(
'\n\nsui-app should be executed from a web-app project.\nPlease be sure that there is a package.json file in your current directory.\n\n'
'\nsui-app should be executed from a web-app project.\nPlease be sure that there is a package.json file in your current directory.\n'
)
return
}
Expand All @@ -87,17 +106,13 @@ module.exports = () => {
installDependency(PACKAGE_NAME)
}

if (!hasDependency('@capgo/capacitor-native-biometric'))
installDependency('@capgo/capacitor-native-biometric')
if (!hasDependency('@capgo/capacitor-native-biometric')) installDependency('@capgo/capacitor-native-biometric')

if (!hasDependency('@capacitor/local-notifications'))
installDependency('@capacitor/local-notifications')
if (!hasDependency('@capacitor/local-notifications')) installDependency('@capacitor/local-notifications')

// If app has already been initialized
if (hasCapacitorConfig()) {
reportError(
`\n\nThis project has already-been initialized. Please run sui-app remove before initializing again.\n\n`
)
reportError(`\nThis project has already-been initialized. Please run sui-app remove before initializing again.\n`)
}

// Init the project
Expand All @@ -109,5 +124,6 @@ module.exports = () => {
// Add iOS project
addIOSProject()

// Add sui-app sync to post-compile
// Apply optimizations to capacitor config file
applyConfigurationOptimizations()
}
47 changes: 19 additions & 28 deletions components/tool/app/bin/commands/remove.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable no-console */
// Constants
const {
PACKAGE_NAME,
PROJECT_CONFIG_FILE
} = require('../infrastructure/config.js')
const {PACKAGE_NAME, PROJECT_CONFIG_FILE} = require('../infrastructure/config.js')

// Infra
// -> files
Expand Down Expand Up @@ -40,65 +38,58 @@ const uninstallPlugins = () => {
const {hasPackageJson, hasCapacitorConfig} = require('../domain/utils.js')

const removeConfiguration = () => {
console.log('\n\n🚚 Removing the project\n\n')
console.log('\n🚚 Removing the project\n')
const result = removeProject()

if (result === false)
reportError(`\n\n🚨 Something went wrong while removing the project 🚨\n\n`)
else console.log('\n\n✅ Project has been successfully removed\n\n')
if (result === false) reportError(`\n🚨 Something went wrong while removing the project 🚨\n`)
else console.log('\n✅ Project has been successfully removed\n')
}

const removeAndroidProject = () => {
console.log('\n\n🚚 Removing android\n\n')
console.log('\n🚚 Removing android\n')
const result = removeAndroid()

if (result === false)
reportError(`\n\n🚨 Something went wrong while removing android 🚨\n\n`)
else console.log('\n\n✅ Android has been successfully removed\n\n')
if (result === false) reportError(`\n🚨 Something went wrong while removing android 🚨\n`)
else console.log('\n✅ Android has been successfully removed\n')
}

const removeIOSProject = () => {
console.log('\n\n🚚 Removing iOS\n\n')
console.log('\n🚚 Removing iOS\n')
const result = removeIOS()

if (result === false)
reportError(`\n\n🚨 Something went wrong while removing iOS 🚨\n\n`)
else console.log('\n\n✅ iOS has been successfully removed\n\n')
if (result === false) reportError(`\n🚨 Something went wrong while removing iOS 🚨\n`)
else console.log('\n✅ iOS has been successfully removed\n')
}

const removeSuiApp = () => {
console.log('\n\n🚚 Uninstalling sui-app\n\n')
console.log('\n🚚 Uninstalling sui-app\n')
const result = uninstallSuiApp()

if (result === false)
reportError(`\n\n🚨 Something went wrong while uninstalling sui-app 🚨\n\n`)
else console.log('\n\n✅ sui-app has been successfully uninstalled\n\n')
if (result === false) reportError(`\n🚨 Something went wrong while uninstalling sui-app 🚨\n`)
else console.log('\n✅ sui-app has been successfully uninstalled\n')
}

const removePlugins = () => {
console.log('\n\n🚚 Uninstalling plugins\n\n')
console.log('\n🚚 Uninstalling plugins\n')
const result = uninstallPlugins()

if (result === false)
reportError(`\n\n🚨 Something went wrong while uninstalling plugins 🚨\n\n`)
else console.log('\n\n✅ plugins have been successfully uninstalled\n\n')
if (result === false) reportError(`\n🚨 Something went wrong while uninstalling plugins 🚨\n`)
else console.log('\n✅ plugins have been successfully uninstalled\n')
}

// Command
module.exports = () => {
// If we are not placed on a webapp, we cannot continue
if (!hasPackageJson()) {
reportError(
'\n\nsui-app should be executed from a web-app project.\nPlease be sure that there is a package.json file in your current directory.\n\n'
'\nsui-app should be executed from a web-app project.\nPlease be sure that there is a package.json file in your current directory.\n'
)
return
}

// If app has already been initialized
if (!hasCapacitorConfig()) {
reportError(
`\n\nThis project has not been initialized. sui-app cannot be removed\n\n`
)
reportError(`\nThis project has not been initialized. sui-app cannot be removed\n`)
}

// Remove configuration
Expand Down
7 changes: 7 additions & 0 deletions components/tool/app/bin/infrastructure/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const readJSONFile = file => {
return JSON.parse(data)
}

const saveJSONFile = (json, file) => {
const {writeFileSync} = require('fs')
const stringified = JSON.stringify(json, null, 2)
writeFileSync(file, stringified)
}

const removeFile = file => {
const {rmSync} = require('fs')
rmSync(file)
Expand Down Expand Up @@ -54,6 +60,7 @@ module.exports = {
getCurrentDirectory,
fileExists,
readJSONFile,
saveJSONFile,
reportError,
runCommand,
installPackage,
Expand Down

0 comments on commit a590f7a

Please sign in to comment.