Add cli validate command (#124)

This commit is contained in:
Piotr Rogowski 2022-10-18 11:52:40 +02:00 committed by GitHub
parent 25f10fcf2d
commit db21724bb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 1 deletions

View File

@ -1,3 +1,6 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.words": [
"hypertuner"
]
}

View File

@ -23,3 +23,11 @@ Proceed with the installation:
```bash
npm i --save @hyper-tuner/ini
```
## CLI capabilities
You can also run this package as a CLI tool, example:
```bash
npx @hyper-tuner/ini validate test/data/ini/202207.ini
```

View File

@ -9,6 +9,9 @@
},
"main": "dist/ini.js",
"types": "dist/ini.d.ts",
"bin": {
"hypertuner-ini": "dist/cli.js"
},
"files": [
"dist"
],

59
src/cli.ts Normal file
View File

@ -0,0 +1,59 @@
#!/usr/bin/env node
import { INI } from './ini';
import fs from 'fs';
enum Commands {
VALIDATE = 'validate',
}
const loadFile = (filename: string) => {
const b = fs.readFileSync(filename);
return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
};
const showUsage = () => {
console.info('\n Usage:');
console.info('hypertuner-ini validate some_ini_file.ini');
process.exit(1);
};
const validate = (filename: string) => {
const ini = new INI(loadFile(filename));
try {
ini.parse();
} catch (error) {
console.info('❌ Errors found:');
console.info((error as Error).message);
process.exit(1);
}
console.info('✅ All good!');
};
const command = process.argv[2];
const filename = process.argv[3];
if (!command) {
console.info('❗️ Please provide a command to run');
showUsage();
process.exit(1);
}
if (!filename) {
console.info('❗️ Please provide a file name');
showUsage();
process.exit(1);
}
switch (command) {
case Commands.VALIDATE:
validate(filename);
break;
default:
console.info(`❗️ Unknown command: ${command}, please use one of: [${Object.values(Commands).join(', ')}]`);
process.exit(1);
break;
}