Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
42a65e79ff | |||
35435b4aa6 | |||
3db167a251 | |||
00f771d020 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
build/
|
||||
# Builds
|
||||
index-*
|
||||
# Service Files
|
||||
|
26
README.md
26
README.md
@@ -1,15 +1,33 @@
|
||||
# xml-servicefile-parser
|
||||
|
||||
Parse servicefile.xml from stock Android pieces to create a script to restore to stock
|
||||
Parse `servicefile.xml`/`flashfile.xml` from stock Android pieces to create a script to restore to stock.
|
||||
|
||||
> Remember to *inspect* the created scripts before actually running them.
|
||||
|
||||
|
||||
## NPM
|
||||
|
||||
### Usage
|
||||
|
||||
|
||||
|
||||
## Executable
|
||||
|
||||
### Building
|
||||
|
||||
1. `npm i`
|
||||
2. `npm run package`
|
||||
1. `npm i` -> This will install the dependencies
|
||||
2. Edit `settings.json` accordingly.
|
||||
3. `npm run pkg` -> This will package the application with the configured settings.json
|
||||
|
||||
### Usage
|
||||
|
||||
#### Option 1
|
||||
|
||||
1. Place alongside `servicefile.xml`.
|
||||
2. Run executable or `node test`, if using source.
|
||||
2. Run executable. On Windows, double click. On Linux, do `chmod +x <filename>` then `./<filename>`
|
||||
> Be aware that the default executable creates `serviceScript.sh` and `serviceScript.bat`, so don't have any files of that name already.
|
||||
3. Inspect the scripts and use them as required.
|
||||
|
||||
#### Option 2
|
||||
|
||||
1. Run it passing the filename as the second argument
|
||||
|
138
index.js
138
index.js
@@ -1,58 +1,110 @@
|
||||
const { parseString } = require('xml2js');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const assert = require('assert');
|
||||
|
||||
const {parseString} = require('xml2js')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
// load in settings
|
||||
console.log('Loading configuration: settings.json');
|
||||
const settings = JSON.parse(
|
||||
fs.readFileSync(path.join(__dirname, 'settings.json'))
|
||||
);
|
||||
|
||||
const settings = JSON.parse(fs.readFileSync( path.join(__dirname,"settings.json") ))
|
||||
console.log("Loaded configuration")
|
||||
//console.log(settings)
|
||||
//console.log(process.argv)
|
||||
fs.readFile(settings.fileName,(err,data)=>{
|
||||
if(err){
|
||||
throw new Error(`${err.code}: Could not read file. Try again. ${err.name}`)
|
||||
// Assert everything is alright with the settings
|
||||
assert(
|
||||
typeof settings?.scriptConfig?.general?.command === 'string',
|
||||
'Expected command'
|
||||
);
|
||||
assert(
|
||||
Array.isArray(settings?.scriptConfig?.env),
|
||||
'Expected an array of environment statuses'
|
||||
);
|
||||
assert(typeof settings?.fileName === 'string', 'Expected a filename');
|
||||
assert(
|
||||
typeof settings?.serviceScript === 'string',
|
||||
'Expected a result filename'
|
||||
);
|
||||
|
||||
/** @type {string} */
|
||||
const fastbootCommand = settings.scriptConfig.general.command;
|
||||
|
||||
/** Input File Name used for config */
|
||||
const filePathString = process.argv[2] ?? settings.fileName;
|
||||
/** Where the input file is located */
|
||||
const fileDirPath = path.dirname(filePathString);
|
||||
|
||||
console.log('Loaded configuration');
|
||||
|
||||
fs.readFile(filePathString, (err, data) => {
|
||||
if (err) {
|
||||
throw new Error(
|
||||
`${err.code}: Could not read file. Try again. ${err.name}`
|
||||
);
|
||||
}
|
||||
console.log(`Reading ${settings.fileName}`)
|
||||
parseString(data,(err,res)=>{
|
||||
if(err){
|
||||
throw new Error(`${err.code}: Error parsing file. :${err.name}`)
|
||||
console.log(`Reading ${filePathString}`);
|
||||
parseString(data, (err, res) => {
|
||||
if (err) {
|
||||
throw new Error(`${err.code}: Error parsing file. :${err.name}`);
|
||||
}
|
||||
|
||||
console.log(`Parsing ${settings.fileName}`)
|
||||
console.log("Model: ",res.flashing.header[0].phone_model[0].$.model)
|
||||
console.log(`Parsed ${filePathString}`);
|
||||
assert(
|
||||
typeof res?.flashing?.header?.[0]?.phone_model[0]?.$?.model ===
|
||||
'string',
|
||||
'Expected a model number'
|
||||
);
|
||||
console.log('Model: ', res.flashing.header[0].phone_model[0].$.model);
|
||||
|
||||
assert(
|
||||
Array.isArray(res?.flashing?.steps),
|
||||
'Expected steps to be an array!'
|
||||
);
|
||||
// Store steps
|
||||
const steps = res.flashing.steps[0]
|
||||
const steps = res.flashing.steps[0];
|
||||
//console.log(steps.$.interface)
|
||||
if(steps.$.interface!=="AP") {
|
||||
throw new Error("Does not look like servicefile.xml. Aborting.")
|
||||
}
|
||||
let sScript = ''//settings.scriptConfig.env[settings.defaultMode].preconfig + '\n'
|
||||
assert(steps?.$?.interface === 'AP', 'Expected an Interface of AP');
|
||||
// if (steps.$.interface !== "AP") {
|
||||
// throw new Error("Does not look like servicefile.xml. Aborting.");
|
||||
// }
|
||||
//settings.scriptConfig.env[settings.defaultMode].preconfig + '\n'
|
||||
//sScript+= `${settings.scriptConfig.env[settings.defaultMode].commentPre} Generated for ${res.flashing.header[0].phone_model[0].$.model} \n`
|
||||
steps.step.forEach(e=>{
|
||||
switch(e.$.operation){
|
||||
case "oem":
|
||||
case "getvar":
|
||||
sScript += (`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.var}`) +'\n'
|
||||
const sScript = steps.step.reduce((t, e) => {
|
||||
switch (e.$.operation) {
|
||||
case 'oem':
|
||||
case 'getvar':
|
||||
t += `${fastbootCommand} ${e.$.operation} ${e.$.var}\n`;
|
||||
break;
|
||||
case "flash":
|
||||
sScript += (`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.partition} ${e.$.filename}`) +'\n'
|
||||
case 'flash':
|
||||
t += `${fastbootCommand} ${e.$.operation} ${e.$.partition} ${e.$.filename}\n`;
|
||||
break;
|
||||
case "erase":
|
||||
sScript += (`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.partition}`) +'\n'
|
||||
case 'erase':
|
||||
t += `${fastbootCommand} ${e.$.operation} ${e.$.partition} \n`;
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unkown: ${e.$.operation}`)
|
||||
throw new Error(`Unkown: ${e.$.operation}`);
|
||||
}
|
||||
})
|
||||
settings.scriptConfig.env.forEach(e=>{
|
||||
const data = e.preConfig+'\n'+ e.commentPre + ` Generated for ${res.flashing.header[0].phone_model[0].$.model}` +'\n'+sScript
|
||||
fs.writeFile(settings.serviceScript+e.extension,data,{mode:0o765},(err)=>{
|
||||
if(err){
|
||||
throw new Error(`${err.errno}: Error Writing Script: ${err.name}`)
|
||||
return t;
|
||||
}, '');
|
||||
|
||||
//Write the data down
|
||||
settings.scriptConfig.env.forEach((e) => {
|
||||
// add some spice to the script that is required
|
||||
const data =
|
||||
e.preConfig +
|
||||
`\n${e.commentPre} Generated for ${res.flashing.header[0].phone_model[0].$.model} \n` +
|
||||
sScript;
|
||||
|
||||
const outputFileName = settings.serviceScript + e.extension;
|
||||
/** Output file full path */
|
||||
const resFileLocation = path.join(fileDirPath, outputFileName);
|
||||
// write it down
|
||||
fs.writeFile(resFileLocation, data, { mode: 0o765 }, (err) => {
|
||||
if (err) {
|
||||
throw new Error(
|
||||
`${err.errno}: Error Writing Script: ${err.name}`
|
||||
);
|
||||
}
|
||||
console.log(`Done: ${settings.serviceScript+e.extension}`)
|
||||
})
|
||||
})
|
||||
//console.log(sScript)
|
||||
})
|
||||
})
|
||||
console.log(`Done: ${settings.serviceScript + e.extension}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
3106
package-lock.json
generated
3106
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
23
package.json
23
package.json
@@ -1,17 +1,23 @@
|
||||
{
|
||||
"name": "xml-servicefile-parser",
|
||||
"version": "1.0.0",
|
||||
"description": "Noob script to create scripts onn servicefile.xml",
|
||||
"main": "index.js",
|
||||
"version": "1.1.1",
|
||||
"description": "Create scripts on servicefile.xml for flashing images on Android Phones",
|
||||
"bin": {
|
||||
"main": "index.js"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node .",
|
||||
"package":"pkg index.js --out-path build/"
|
||||
"pkg": "pkg . --out-path build/"
|
||||
},
|
||||
"prettier": {
|
||||
"semi": true,
|
||||
"tabWidth": 4,
|
||||
"singleQuote": true
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/chrisvrose/xml-servicefile-parser.git"
|
||||
},
|
||||
"author": "Christopher Rose",
|
||||
"author": "Atreya Bain",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/chrisvrose/xml-servicefile-parser/issues"
|
||||
@@ -20,8 +26,11 @@
|
||||
"dependencies": {
|
||||
"xml2js": "^0.4.19"
|
||||
},
|
||||
"pkg":{
|
||||
"pkg": {
|
||||
"scripts": "index.js",
|
||||
"assets": "settings.json"
|
||||
},
|
||||
"devDependencies": {
|
||||
"pkg": "^5.5.2"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user