8 Commits

Author SHA1 Message Date
da5c679900 Merge pull request #5 from chrisvrose/dependabot/npm_and_yarn/minimist-1.2.8
Bump minimist from 1.2.5 to 1.2.8
2024-10-31 11:13:27 +00:00
c257c6be32 Merge pull request #6 from chrisvrose/dependabot/npm_and_yarn/xml2js-0.5.0
Bump xml2js from 0.4.23 to 0.5.0
2024-10-31 11:13:16 +00:00
dependabot[bot]
6a555c7fbc Bump xml2js from 0.4.23 to 0.5.0
Bumps [xml2js](https://github.com/Leonidas-from-XIV/node-xml2js) from 0.4.23 to 0.5.0.
- [Release notes](https://github.com/Leonidas-from-XIV/node-xml2js/releases)
- [Commits](https://github.com/Leonidas-from-XIV/node-xml2js/commits/0.5.0)

---
updated-dependencies:
- dependency-name: xml2js
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-04-11 21:10:37 +00:00
dependabot[bot]
42ce1dbacb Bump minimist from 1.2.5 to 1.2.8
Bumps [minimist](https://github.com/minimistjs/minimist) from 1.2.5 to 1.2.8.
- [Release notes](https://github.com/minimistjs/minimist/releases)
- [Changelog](https://github.com/minimistjs/minimist/blob/main/CHANGELOG.md)
- [Commits](https://github.com/minimistjs/minimist/compare/v1.2.5...v1.2.8)

---
updated-dependencies:
- dependency-name: minimist
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-03-06 02:28:39 +00:00
42a65e79ff [update] pkg 2022-01-30 13:54:34 +05:30
35435b4aa6 [fix] fix some readme and descriptions 2022-01-30 13:51:34 +05:30
3db167a251 [fix] unstringify 2022-01-30 13:45:12 +05:30
00f771d020 [update] add assertions 2021-05-06 12:36:25 +05:30
5 changed files with 3235 additions and 64 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
build/
# Builds # Builds
index-* index-*
# Service Files # Service Files

View File

@@ -1,15 +1,33 @@
# xml-servicefile-parser # 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 ## 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
#### Option 1
1. Place alongside `servicefile.xml`. 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
View File

@@ -1,58 +1,110 @@
const { parseString } = require('xml2js');
const path = require('path');
const fs = require('fs');
const assert = require('assert');
const {parseString} = require('xml2js') // load in settings
const path = require('path') console.log('Loading configuration: settings.json');
const fs = require('fs') const settings = JSON.parse(
fs.readFileSync(path.join(__dirname, 'settings.json'))
);
const settings = JSON.parse(fs.readFileSync( path.join(__dirname,"settings.json") )) // Assert everything is alright with the settings
console.log("Loaded configuration") assert(
//console.log(settings) typeof settings?.scriptConfig?.general?.command === 'string',
//console.log(process.argv) 'Expected command'
fs.readFile(settings.fileName,(err,data)=>{ );
if(err){ assert(
throw new Error(`${err.code}: Could not read file. Try again. ${err.name}`) 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}`) console.log(`Reading ${filePathString}`);
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(`Parsed ${filePathString}`);
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 += `${fastbootCommand} ${e.$.operation} ${e.$.var}\n`;
break; break;
case "flash": case 'flash':
sScript += (`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.partition} ${e.$.filename}`) +'\n' t += `${fastbootCommand} ${e.$.operation} ${e.$.partition} ${e.$.filename}\n`;
break; break;
case "erase": case 'erase':
sScript += (`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.partition}`) +'\n' t += `${fastbootCommand} ${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}`) // 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(`Done: ${settings.serviceScript + e.extension}`);
}) });
}) });
//console.log(sScript) });
}) });
})

3109
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,27 +1,36 @@
{ {
"name": "xml-servicefile-parser", "name": "xml-servicefile-parser",
"version": "1.0.0", "version": "1.1.1",
"description": "Noob script to create scripts onn servicefile.xml", "description": "Create scripts on servicefile.xml for flashing images on Android Phones",
"main": "index.js", "bin": {
"main": "index.js"
},
"scripts": { "scripts": {
"test": "node .", "pkg": "pkg . --out-path build/"
"package":"pkg index.js --out-path build/" },
"prettier": {
"semi": true,
"tabWidth": 4,
"singleQuote": true
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/chrisvrose/xml-servicefile-parser.git" "url": "git+https://github.com/chrisvrose/xml-servicefile-parser.git"
}, },
"author": "Christopher Rose", "author": "Atreya Bain",
"license": "MIT", "license": "MIT",
"bugs": { "bugs": {
"url": "https://github.com/chrisvrose/xml-servicefile-parser/issues" "url": "https://github.com/chrisvrose/xml-servicefile-parser/issues"
}, },
"homepage": "https://github.com/chrisvrose/xml-servicefile-parser#readme", "homepage": "https://github.com/chrisvrose/xml-servicefile-parser#readme",
"dependencies": { "dependencies": {
"xml2js": "^0.4.19" "xml2js": "^0.5.0"
}, },
"pkg":{ "pkg": {
"scripts": "index.js", "scripts": "index.js",
"assets": "settings.json" "assets": "settings.json"
},
"devDependencies": {
"pkg": "^5.5.2"
} }
} }