[fix] unstringify
This commit is contained in:
84
index.js
84
index.js
@@ -3,34 +3,50 @@ const path = require('path');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
|
||||||
|
// load in settings
|
||||||
|
console.log('Loading configuration: settings.json');
|
||||||
console.log('Loading configuration: settings.json')
|
|
||||||
const settings = JSON.parse(
|
const settings = JSON.parse(
|
||||||
fs.readFileSync(path.join(__dirname, 'settings.json'))
|
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')
|
|
||||||
|
|
||||||
|
// 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');
|
console.log('Loaded configuration');
|
||||||
//console.log(settings)
|
|
||||||
//console.log(process.argv)
|
fs.readFile(filePathString, (err, data) => {
|
||||||
fs.readFile(settings.fileName, (err, data) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`${err.code}: Could not read file. Try again. ${err.name}`
|
`${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}`);
|
||||||
assert(
|
assert(
|
||||||
typeof res?.flashing?.header?.[0]?.phone_model[0]?.$?.model ===
|
typeof res?.flashing?.header?.[0]?.phone_model[0]?.$?.model ===
|
||||||
'string',
|
'string',
|
||||||
@@ -55,50 +71,40 @@ fs.readFile(settings.fileName, (err, data) => {
|
|||||||
switch (e.$.operation) {
|
switch (e.$.operation) {
|
||||||
case 'oem':
|
case 'oem':
|
||||||
case 'getvar':
|
case 'getvar':
|
||||||
t +=
|
t += `${fastbootCommand} ${e.$.operation} ${e.$.var}\n`;
|
||||||
`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.var}` +
|
|
||||||
'\n';
|
|
||||||
break;
|
break;
|
||||||
case 'flash':
|
case 'flash':
|
||||||
t +=
|
t += `${fastbootCommand} ${e.$.operation} ${e.$.partition} ${e.$.filename}\n`;
|
||||||
`${settings.scriptConfig.general.command} ${e.$.operation} ${e.$.partition} ${e.$.filename}` +
|
|
||||||
'\n';
|
|
||||||
break;
|
break;
|
||||||
case 'erase':
|
case 'erase':
|
||||||
t +=
|
t += `${fastbootCommand} ${e.$.operation} ${e.$.partition} \n`;
|
||||||
`${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;
|
return t;
|
||||||
});
|
}, '');
|
||||||
|
|
||||||
//Write the data down
|
//Write the data down
|
||||||
settings.scriptConfig.env.forEach((e) => {
|
settings.scriptConfig.env.forEach((e) => {
|
||||||
|
// add some spice to the script that is required
|
||||||
const data =
|
const data =
|
||||||
e.preConfig +
|
e.preConfig +
|
||||||
'\n' +
|
`\n${e.commentPre} Generated for ${res.flashing.header[0].phone_model[0].$.model} \n` +
|
||||||
`${e.commentPre} Generated for ${res.flashing.header[0].phone_model[0].$.model}` +
|
|
||||||
'\n' +
|
|
||||||
sScript;
|
sScript;
|
||||||
fs.writeFile(
|
|
||||||
settings.serviceScript + e.extension,
|
const outputFileName = settings.serviceScript + e.extension;
|
||||||
data,
|
/** Output file full path */
|
||||||
{ mode: 0o765 },
|
const resFileLocation = path.join(fileDirPath, outputFileName);
|
||||||
(err) => {
|
// write it down
|
||||||
if (err) {
|
fs.writeFile(resFileLocation, data, { mode: 0o765 }, (err) => {
|
||||||
throw new Error(
|
if (err) {
|
||||||
`${err.errno}: Error Writing Script: ${err.name}`
|
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)
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user