@appshuttle.io/turing
Version:
Code Generation Library used in Shuttle
133 lines (118 loc) • 6.2 kB
JavaScript
const TRFileManagement = require('./Mixins/FileManagement/FileManagement')
var iOSBuilder = require('./iOS/iOSBuilder')
var AndroidBuilder = require('./Android/AndroidBuilder')
class SHTuring {
constructor(path, config) {
if (!path) throw new Error('No shuttle project path is specified')
this.path = path
if (typeof config != 'undefined') {
this.config = config
if (typeof this.config.exportPath != 'undefined') {
this.exportPath = this.config.exportPath
} else {
this.exportPath = this.path
}
} else {
this.exportPath = this.path
}
this.FileManagement = new TRFileManagement()
this.generatePlatforms = function (success, errorBlock) {
const thisRef = this
this.loadProjectFromDisk(this.path, function (projectInformation, screenPointers, screens) {
thisRef.project = projectInformation
thisRef.screenPointers = screenPointers
thisRef.screens = screens
thisRef.startGenerators(function () {
console.log('Projects created at: ' + thisRef.exportPath)
success()
}, function (err) {
errorBlock(err)
})
}, function (err) {
errorBlock(err)
})
}
this.startGenerators = function (success, errorBlock) {
if (this.project.appInformation.appPlatforms === 'IOS' || (this.config !== undefined && this.config.appPlatforms === 'IOS')) {
this.iosBuilder = new iOSBuilder(this.exportPath, this.project, this.screenPointers, this.screens)
this.iosBuilder.generateiOSProject(function () {
success()
}, function (err) {
errorBlock(err)
})
} else if (this.project.appInformation.appPlatforms === 'ANDROID' || (this.config !== undefined && this.config.appPlatforms === 'ANDROID')) {
this.androidBuilder = new AndroidBuilder(this.exportPath, this.project, this.screenPointers, this.screens)
this.androidBuilder.generateAndroidProject(function () {
success()
}, function (err) {
errorBlock(err)
})
} else if (this.project.appInformation.appPlatforms === 'BOTH' || (this.config !== undefined && this.config.appPlatforms === 'BOTH')) {
this.iosBuilder = new iOSBuilder(this.exportPath, this.project, this.screenPointers, this.screens)
this.androidBuilder = new AndroidBuilder(this.exportPath, this.project, this.screenPointers, this.screens)
this.iosBuilder.generateiOSProject(function () {
success()
}, function (err) {
errorBlock(err)
})
this.androidBuilder.generateAndroidProject(function () {
success()
}, function (err) {
errorBlock(err)
})
}
}
this.buildPlatforms = function () {
if (this.project.appInformation.appPlatforms === 'IOS') {
this.iosBuilder = new iOSBuilder(this.path, this.project, this.screenPointers, this.screens)
return {'IOS': this.iosBuilder.buildiOS()}
} else if (this.project.appInformation.appPlatforms === 'ANDROID') {
this.androidBuilder = new AndroidBuilder(this.path, this.project, this.screenPointers, this.screens)
return {'ANDROID': this.androidBuilder.buildAndroidEnv()}
} else if (this.project.appInformation.appPlatforms === 'BOTH') {
this.iosBuilder = new iOSBuilder(this.path, this.project, this.screenPointers, this.screens)
this.androidBuilder = new AndroidBuilder(this.path, this.project, this.screenPointers, this.screens)
//buildPromises.push(this.androidBuilder.buildAndroidEnv())
return {'IOS': this.iosBuilder.buildiOS()}
}
}
this.loadProjectFromDisk = function (path, successBlock, errorBlock) {
const thisRef = this
var screenPointersDir = path + '/screens/screens.js'
var promiseInformation = this.FileManagement.methods.readFileSync(path + '/main.json')
var promiseScreenPointers = this.FileManagement.methods.readFileSync(screenPointersDir)
Promise.all([promiseInformation, promiseScreenPointers]).then(function (values) {
var projectInformation = JSON.parse(values[0])
var objectScreenPointers = JSON.parse(values[1])
if (objectScreenPointers.length > 0) {
var screensPromisesArray = []
for (var screenPointer of objectScreenPointers) {
screensPromisesArray.push(thisRef.loadScreenFromDisk(screenPointer))
}
Promise.all(screensPromisesArray).then(function (valuesScreens) {
successBlock(projectInformation, objectScreenPointers, valuesScreens)
}).catch(function (errorScreens) {
errorBlock(errorScreens)
})
} else {
successBlock(projectInformation, objectScreenPointers, [])
}
}).catch(function (error) {
errorBlock(error)
})
}
this.loadScreenFromDisk = function (screenPointer) {
var screenId = screenPointer.id
var pathView = path + '/screens/' + screenId + '/' + screenId + '.json'
const thisRef = this
return new Promise((resolve, reject) => {
thisRef.FileManagement.methods.readFileSync(pathView).then(function (object) {
resolve(JSON.parse(object + ''))
}).catch(function (error) {
reject(error)
})
})
}
}
}
module.exports = SHTuring