@liascript/exporter
Version:
A generic exporter for LiaScript
150 lines (131 loc) • 3.43 kB
text/typescript
import * as helper from './helper'
const path = require('path')
const fs = require('fs-extra')
const { exec } = require('child_process')
export async function exporter(
argument: {
input: string
readme: string
output: string
format: string
path: string
key?: string
'ios-appId'?: string
'ios-appName'?: string
'ios-icon'?: string
'ios-splash'?: string
'ios-splashDuration'?: number
},
json: any
) {
// make temp folder
let tmp = await helper.tmpDir()
let tmpPath = path.join(tmp, 'pro')
// copy assets to temp/dist
await fs.copy(
path.join(__dirname, './assets/capacitor'),
path.join(tmpPath, './dist')
)
// copy logo and splash
await fs.copy(
path.join(__dirname, './resources'),
path.join(tmpPath, '../resources')
)
// copy base path or readme-directory into temp
await fs.copy(argument.path, path.join(tmpPath, './dist/'), {
filter: helper.filterHidden(argument.path),
})
await helper.writeFile(
path.join(tmpPath, '../capacitor.config.json'),
`{
"appId": "${argument['ios-appId']}",
"appName": "${argument['ios-appName'] || json.lia.str_title}",
"bundledWebRuntime": true,
"webDir": "pro/dist",
"plugins": {
"SplashScreen": {
"launchShowDuration": ${argument['ios-splashDuration'] || 0}
}
}
}`
)
await helper.writeFile(
path.join(tmpPath, '../package.json'),
`{
"scripts": {
"build": "npx cap add ios"
},
"dependencies": {
"@capacitor-community/text-to-speech": "^1.1.2",
"@capacitor/cli": "^3.4.3",
"@capacitor/ios": "^3.4.1",
"capacitor-resources": "^2.0.5"
},
"engines": {
"node": ">= 12"
}
}`
)
let index = fs.readFileSync(path.join(tmpPath, 'dist/index.html'), 'utf8')
index = helper.inject(
`<script> if (!window.LIA) { window.LIA = {} } window.LIA.defaultCourseURL = "./${path.basename(
argument.readme
)}"</script>`,
index
)
try {
await helper.writeFile(path.join(tmpPath, 'dist/index.html'), index)
} catch (e) {
console.warn(e)
return
}
execute(
`cd ${tmpPath} && cd .. && npm i && npx cap add ios && npx capacitor-resources -p "ios" ${
argument['ios-icon'] ? '--icon ' + path.resolve(argument['ios-icon']) : ''
} ${
argument['ios-splash']
? '--splash ' + path.resolve(argument['ios-splash'])
: ''
}`,
async function () {
// await sdk(tmpPath, argument['android-sdk'])
execute(
`cd ${tmpPath} && cd .. && cd ios && ./gradlew assembleDebug`,
function () {
console.warn('DONE')
fs.copy(
path.join(
tmpPath,
'../android/app/build/outputs/apk/debug/app-debug.apk'
),
argument.output + '.apk'
)
}
)
}
)
}
async function sdk(tmpPath: string, uri?: string) {
if (!uri) return
try {
helper.writeFile(
path.join(tmpPath, '../android/local.properties'),
`sdk.dir=${uri}`
)
} catch (e) {
console.warn(e)
return
}
}
function execute(cmd: string, callback) {
exec(cmd, async (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
}
if (stderr) {
console.log(`stderr: ${stderr}`)
}
console.log(`stdout: ${stdout}`)
callback()
})
}