Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components/tool/app): Automatic optimization of the capacitor co… #637

Merged
merged 5 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
71 changes: 51 additions & 20 deletions components/tool/app/bin/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
readJSONFile,
reportError,
runCommand,
installPackage
installPackage,
saveJSONFile
} = require('../infrastructure/utils.js')

const initProject = () => {
Expand All @@ -23,62 +24,91 @@
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(

Check failure on line 50 in components/tool/app/bin/commands/init.js

View workflow job for this annotation

GitHub Actions / build

Replace `⏎····`${getCurrentDirectory()}/${PACKAGE_JSON_FILE}`⏎··` with ``${getCurrentDirectory()}/${PACKAGE_JSON_FILE}``
`${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)

Check failure on line 60 in components/tool/app/bin/commands/init.js

View workflow job for this annotation

GitHub Actions / build

Delete `⏎···`
reportError(
`\n\n🚨 Something went wrong while installing dependencies 🚨\n\n`
)
else console.log(`\n\n✅ Dependency has been successfully installed\n\n`)
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)

Check failure on line 69 in components/tool/app/bin/commands/init.js

View workflow job for this annotation

GitHub Actions / build

Delete `⏎···`
reportError(
`\n\n🚨 Something went wrong while initializing the project 🚨\n\n`
)
else console.log('\n\n✅ Project has been successfully initialized\n\n')
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)

Check failure on line 78 in components/tool/app/bin/commands/init.js

View workflow job for this annotation

GitHub Actions / build

Delete `⏎···`
reportError(`\n\n🚨 Something went wrong while configuring android 🚨\n\n`)
else console.log('\n\n✅ Android has been successfully initialized\n\n')
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 project\n')
const result = initIOS()

if (result === false)

Check failure on line 87 in components/tool/app/bin/commands/init.js

View workflow job for this annotation

GitHub Actions / build

Delete `⏎···`
reportError(`\n\n🚨 Something went wrong while configuring iOS 🚨\n\n`)
else console.log('\n\n✅ iOS has been successfully initialized\n\n')
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)

Check failure on line 96 in components/tool/app/bin/commands/init.js

View workflow job for this annotation

GitHub Actions / build

Replace `⏎····reportError(⏎······`\n🚨·Something·went·wrong·while·applying·configuration·optimizations·🚨\n`⏎····` with `·reportError(`\n🚨·Something·went·wrong·while·applying·configuration·optimizations·🚨\n``
reportError(
`\n🚨 Something went wrong while applying configuration optimizations 🚨\n`
)
else

Check failure on line 100 in components/tool/app/bin/commands/init.js

View workflow job for this annotation

GitHub Actions / build

Replace `⏎····console.log(⏎······'\n✅·Configuration·optimizations·have·been·successfully·applied\n'⏎····` with `·console.log('\n✅·Configuration·optimizations·have·been·successfully·applied\n'`
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,16 +117,16 @@
installDependency(PACKAGE_NAME)
}

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

Check failure on line 120 in components/tool/app/bin/commands/init.js

View workflow job for this annotation

GitHub Actions / build

Delete `⏎···`
installDependency('@capgo/capacitor-native-biometric')

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

Check failure on line 123 in components/tool/app/bin/commands/init.js

View workflow job for this annotation

GitHub Actions / build

Delete `⏎···`
installDependency('@capacitor/local-notifications')

// If app has already been initialized
if (hasCapacitorConfig()) {
reportError(

Check failure on line 128 in components/tool/app/bin/commands/init.js

View workflow job for this annotation

GitHub Actions / build

Replace `⏎······`\nThis·project·has·already-been·initialized.·Please·run·sui-app·remove·before·initializing·again.\n`⏎····` with ``\nThis·project·has·already-been·initialized.·Please·run·sui-app·remove·before·initializing·again.\n``
`\n\nThis project has already-been initialized. Please run sui-app remove before initializing again.\n\n`
`\nThis project has already-been initialized. Please run sui-app remove before initializing again.\n`
)
}

Expand All @@ -109,5 +139,6 @@
// Add iOS project
addIOSProject()

// Add sui-app sync to post-compile
// Apply optimizations to capacitor config file
applyConfigurationOptimizations()
}
34 changes: 17 additions & 17 deletions components/tool/app/bin/commands/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,64 +40,64 @@ 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')
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')
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')
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')
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')
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`
`\nThis project has not been initialized. sui-app cannot be removed\n`
)
}

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
Loading