-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: Add command line interface to combine/split uhex files.
- Loading branch information
1 parent
0898e2f
commit d147dc7
Showing
6 changed files
with
216 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import * as fs from 'fs'; | ||
import { sep } from 'path'; | ||
import * as process from 'process'; | ||
import { Command } from 'commander'; | ||
import * as microbitUh from './universal-hex'; | ||
|
||
function combine( | ||
v1IntelHexPath: string, | ||
v2IntelHexPath: string, | ||
universalHexPath: string | undefined, | ||
overwrite: boolean | undefined | ||
) { | ||
console.log('Combining Intel Hex files into Universal Hex'); | ||
console.log(`V1 Intel hex file: ${fs.realpathSync(v1IntelHexPath)}`); | ||
console.log(`V2 Intel hex file: ${fs.realpathSync(v2IntelHexPath)}`); | ||
|
||
if (!universalHexPath) { | ||
// If the output path is not specified, save it in the current working directory | ||
universalHexPath = `${process.cwd()}${sep}universal.hex`; | ||
} | ||
if (!overwrite && fs.existsSync(universalHexPath)) { | ||
throw new Error( | ||
`Output file already exists: ${fs.realpathSync(universalHexPath)}\n` + | ||
'\tUse "--overwrite" flag to replace it.' | ||
); | ||
} | ||
|
||
const v1IntelHexStr = fs.readFileSync(v1IntelHexPath, 'ascii'); | ||
const v2IntelHexStr = fs.readFileSync(v2IntelHexPath, 'ascii'); | ||
const universalHexStr = microbitUh.createUniversalHex([ | ||
{ | ||
hex: v1IntelHexStr, | ||
boardId: microbitUh.microbitBoardId.V1, | ||
}, | ||
{ | ||
hex: v2IntelHexStr, | ||
boardId: microbitUh.microbitBoardId.V2, | ||
}, | ||
]); | ||
fs.writeFileSync(universalHexPath, universalHexStr, { encoding: 'ascii' }); | ||
|
||
console.log(`Universal Hex saved to: ${fs.realpathSync(universalHexPath)}`); | ||
} | ||
|
||
function separate( | ||
universalHexPath: string, | ||
v1IntelHexPath: string | undefined, | ||
v2IntelHexPath: string | undefined, | ||
overwrite: boolean | undefined | ||
) { | ||
console.log( | ||
`Splitting Universal Hex file: ${fs.realpathSync(universalHexPath)}` | ||
); | ||
if (!v1IntelHexPath) { | ||
v1IntelHexPath = `${process.cwd()}${sep}v1-intel.hex`; | ||
} | ||
if (!v2IntelHexPath) { | ||
v2IntelHexPath = `${process.cwd()}${sep}v2-intel.hex`; | ||
} | ||
if (!overwrite && fs.existsSync(v1IntelHexPath)) { | ||
throw new Error( | ||
`Output V1 file already exists: ${fs.realpathSync(v1IntelHexPath)}\n` + | ||
'\tUse "--overwrite" flag to replace it.' | ||
); | ||
} | ||
if (!overwrite && fs.existsSync(v2IntelHexPath)) { | ||
throw new Error( | ||
`Output V2 file already exists: ${fs.realpathSync(v2IntelHexPath)}\n` + | ||
'\tUse "--overwrite" flag to replace it.' | ||
); | ||
} | ||
|
||
const universalHexStr = fs.readFileSync(universalHexPath, 'ascii'); | ||
const separatedHexes = microbitUh.separateUniversalHex(universalHexStr); | ||
if (separatedHexes.length !== 2) { | ||
const boardIds = separatedHexes.map((hexObj) => hexObj.boardId); | ||
const errorMsg = | ||
'Universal Hex should contain only two micro:bit Intel Hexes.\n' + | ||
`Found ${separatedHexes.length}: ${boardIds.join(', ')}`; | ||
throw new Error(errorMsg); | ||
} | ||
|
||
let intelHexV1Str = ''; | ||
let intelHexV2Str = ''; | ||
separatedHexes.forEach((hexObj) => { | ||
if (microbitUh.V1_BOARD_IDS.includes(hexObj.boardId)) { | ||
intelHexV1Str = hexObj.hex; | ||
} else if (microbitUh.V2_BOARD_IDS.includes(hexObj.boardId)) { | ||
intelHexV2Str = hexObj.hex; | ||
} | ||
}); | ||
if (!intelHexV1Str || !intelHexV2Str) { | ||
const boardIds = separatedHexes.map((hexObj) => hexObj.boardId); | ||
const errorMsg = | ||
'Universal Hex does not contain both micro:bit Intel Hexes.\n' + | ||
`Found hexes for following board IDs: ${boardIds.join(', ')}`; | ||
throw new Error(errorMsg); | ||
} | ||
fs.writeFileSync(v1IntelHexPath, intelHexV1Str, { encoding: 'ascii' }); | ||
fs.writeFileSync(v2IntelHexPath, intelHexV2Str, { encoding: 'ascii' }); | ||
|
||
console.log(`V1 Intel Hex saved to: ${fs.realpathSync(v1IntelHexPath)}`); | ||
console.log(`V2 Intel Hex saved to: ${fs.realpathSync(v2IntelHexPath)}`); | ||
} | ||
|
||
function cli(args: string[]): number { | ||
const uHexCli = new Command(); | ||
|
||
uHexCli | ||
.command('combine') | ||
.requiredOption('-v1, --v1 <path>', 'Path to micro:bit V1 input Intel Hex') | ||
.requiredOption('-v2, --v2 <path>', 'Path to micro:bit V2 input Intel Hex') | ||
.option('-u, --universal <path>', 'Path to output Universal Hex') | ||
.option('-o, --overwrite', 'Overwrite output file if it exists', false) | ||
.action( | ||
(options: { | ||
v1: string; | ||
v2: string; | ||
universal?: string; | ||
overwrite: boolean; | ||
}) => { | ||
try { | ||
combine(options.v1, options.v2, options.universal, options.overwrite); | ||
} catch (e) { | ||
console.error('Error:', e.message); | ||
process.exit(1); | ||
} | ||
} | ||
); | ||
|
||
uHexCli | ||
.command('split') | ||
.requiredOption('-u, --universal <path>', 'Path to input Universal Hex') | ||
.option('-v1, --v1 <path>', 'Path to micro:bit V1 output Intel Hex') | ||
.option('-v2, --v2 <path>', 'Path to micro:bit V2 output Intel Hex') | ||
.option('-o, --overwrite', 'Overwrite output files if they exist', false) | ||
.action( | ||
(options: { | ||
v1?: string; | ||
v2?: string; | ||
universal: string; | ||
overwrite: boolean; | ||
}) => { | ||
try { | ||
separate( | ||
options.universal, | ||
options.v1, | ||
options.v2, | ||
options.overwrite | ||
); | ||
} catch (e) { | ||
console.error('Error:', e.message); | ||
process.exit(1); | ||
} | ||
} | ||
); | ||
|
||
uHexCli.parse(args); | ||
|
||
return 0; | ||
} | ||
|
||
process.exit(cli(process.argv)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters