[update] add assertions
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
|
build/
|
||||||
# Builds
|
# Builds
|
||||||
index-*
|
index-*
|
||||||
# Service Files
|
# Service Files
|
||||||
|
14
README.md
14
README.md
@@ -1,15 +1,21 @@
|
|||||||
# xml-servicefile-parser
|
# xml-servicefile-parser
|
||||||
|
|
||||||
Parse servicefile.xml from stock Android pieces to create a script to restore to stock
|
Parse `servicefile.xml` from stock Android pieces to create a script to restore to stock.
|
||||||
|
|
||||||
|
> Remember to *inspect* the created scripts before actually running them.
|
||||||
|
|
||||||
|
|
||||||
## Executable
|
## Executable
|
||||||
|
|
||||||
### Building
|
### Building
|
||||||
|
|
||||||
1. `npm i`
|
1. `npm i` -> This will install the dependencies
|
||||||
2. `npm run package`
|
2. Edit `settings.json` accordingly.
|
||||||
|
3. `npm run pkg` -> This will package the application with the configured settings.json
|
||||||
|
|
||||||
### Usage
|
### Usage
|
||||||
|
|
||||||
1. Place alongside `servicefile.xml`.
|
1. Place alongside `servicefile.xml`.
|
||||||
2. Run executable or `node test`, if using source.
|
2. Run executable.
|
||||||
|
> 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.
|
||||||
|
126
index.js
126
index.js
@@ -1,58 +1,104 @@
|
|||||||
|
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')
|
|
||||||
|
|
||||||
const settings = JSON.parse(fs.readFileSync( path.join(__dirname,"settings.json") ))
|
|
||||||
console.log("Loaded configuration")
|
console.log('Loading configuration: settings.json')
|
||||||
|
const settings = JSON.parse(
|
||||||
|
fs.readFileSync(path.join(__dirname, 'settings.json'))
|
||||||
|
);
|
||||||
|
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')
|
||||||
|
|
||||||
|
|
||||||
|
console.log('Loaded configuration');
|
||||||
//console.log(settings)
|
//console.log(settings)
|
||||||
//console.log(process.argv)
|
//console.log(process.argv)
|
||||||
fs.readFile(settings.fileName,(err,data)=>{
|
fs.readFile(settings.fileName, (err, data) => {
|
||||||
if(err){
|
if (err) {
|
||||||
throw new Error(`${err.code}: Could not read file. Try again. ${err.name}`)
|
throw new Error(
|
||||||
|
`${err.code}: Could not read file. Try again. ${err.name}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
console.log(`Reading ${settings.fileName}`)
|
console.log(`Reading ${settings.fileName}`);
|
||||||
parseString(data,(err,res)=>{
|
parseString(data, (err, res) => {
|
||||||
if(err){
|
if (err) {
|
||||||
throw new Error(`${err.code}: Error parsing file. :${err.name}`)
|
throw new Error(`${err.code}: Error parsing file. :${err.name}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Parsing ${settings.fileName}`)
|
console.log(`Parsing ${settings.fileName}`);
|
||||||
console.log("Model: ",res.flashing.header[0].phone_model[0].$.model)
|
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
|
// Store steps
|
||||||
const steps = res.flashing.steps[0]
|
const steps = res.flashing.steps[0];
|
||||||
//console.log(steps.$.interface)
|
//console.log(steps.$.interface)
|
||||||
if(steps.$.interface!=="AP") {
|
assert(steps?.$?.interface === 'AP', 'Expected an Interface of AP');
|
||||||
throw new Error("Does not look like servicefile.xml. Aborting.")
|
// if (steps.$.interface !== "AP") {
|
||||||
}
|
// throw new Error("Does not look like servicefile.xml. Aborting.");
|
||||||
let sScript = ''//settings.scriptConfig.env[settings.defaultMode].preconfig + '\n'
|
// }
|
||||||
|
//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`
|
//sScript+= `${settings.scriptConfig.env[settings.defaultMode].commentPre} Generated for ${res.flashing.header[0].phone_model[0].$.model} \n`
|
||||||
steps.step.forEach(e=>{
|
const sScript = steps.step.reduce((t, e) => {
|
||||||
switch(e.$.operation){
|
switch (e.$.operation) {
|
||||||
case "oem":
|
case 'oem':
|
||||||
case "getvar":
|
case 'getvar':
|
||||||
sScript += (`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.var}`) +'\n'
|
t +=
|
||||||
|
`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.var}` +
|
||||||
|
'\n';
|
||||||
break;
|
break;
|
||||||
case "flash":
|
case 'flash':
|
||||||
sScript += (`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.partition} ${e.$.filename}`) +'\n'
|
t +=
|
||||||
|
`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.partition} ${e.$.filename}` +
|
||||||
|
'\n';
|
||||||
break;
|
break;
|
||||||
case "erase":
|
case 'erase':
|
||||||
sScript += (`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.partition}`) +'\n'
|
t +=
|
||||||
|
`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.partition}` +
|
||||||
|
'\n';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unkown: ${e.$.operation}`)
|
throw new Error(`Unkown: ${e.$.operation}`);
|
||||||
}
|
}
|
||||||
})
|
return t;
|
||||||
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)=>{
|
//Write the data down
|
||||||
if(err){
|
settings.scriptConfig.env.forEach((e) => {
|
||||||
throw new Error(`${err.errno}: Error Writing Script: ${err.name}`)
|
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}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
console.log(
|
||||||
|
`Done: ${settings.serviceScript + e.extension}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
console.log(`Done: ${settings.serviceScript+e.extension}`)
|
);
|
||||||
})
|
});
|
||||||
})
|
|
||||||
//console.log(sScript)
|
//console.log(sScript)
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
|
2983
package-lock.json
generated
2983
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
@@ -1,11 +1,18 @@
|
|||||||
{
|
{
|
||||||
"name": "xml-servicefile-parser",
|
"name": "xml-servicefile-parser",
|
||||||
"version": "1.0.0",
|
"version": "1.1.0",
|
||||||
"description": "Noob script to create scripts onn servicefile.xml",
|
"description": "Noob script to create scripts onn servicefile.xml",
|
||||||
"main": "index.js",
|
"bin": {
|
||||||
|
"main": "index.js"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node .",
|
"test": "node .",
|
||||||
"package":"pkg index.js --out-path build/"
|
"pkg": "pkg . --out-path build/"
|
||||||
|
},
|
||||||
|
"prettier": {
|
||||||
|
"semi": true,
|
||||||
|
"tabWidth": 4,
|
||||||
|
"singleQuote": true
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -20,8 +27,11 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"xml2js": "^0.4.19"
|
"xml2js": "^0.4.19"
|
||||||
},
|
},
|
||||||
"pkg":{
|
"pkg": {
|
||||||
"scripts": "index.js",
|
"scripts": "index.js",
|
||||||
"assets": "settings.json"
|
"assets": "settings.json"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"pkg": "^5.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user