Skip to content

Commit

Permalink
Update node version to v18
Browse files Browse the repository at this point in the history
- Add possibility to output to stdout, instead of just file
- Update webpack
- Add `--stdout` option
  • Loading branch information
w1kman committed Oct 12, 2023
1 parent 2cdc429 commit d39bfc3
Show file tree
Hide file tree
Showing 8 changed files with 1,568 additions and 7,171 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16
v18
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16-slim
FROM node:18-slim

WORKDIR /converter

Expand All @@ -7,4 +7,4 @@ COPY . .
RUN npm install
RUN npm run bundle

ENTRYPOINT ["node", "bin/har-to-k6.js"]
ENTRYPOINT ["node", "bin/har-to-k6.js", "--stdout"]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ $ ./node_modules/.bin/har-to-k6 archive.har -o my-k6-script.js
$ har-to-k6 archive.har -o my-k6-script.js
```

#### Docker

```shell
$ docker run grafana/har-to-k6:latest archive.har > my-k6-script.js
```

### Programmatic Usage

#### Converting
Expand Down
63 changes: 53 additions & 10 deletions bin/har-to-k6.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { DEFAULT_CLI_OPTIONS } = require('../src/constants')
const { VError } = require('verror')

class CommandLineError extends VError {}

const BOM_REGEX = /^\uFEFF/

pkginfo(module, 'version')
Expand All @@ -21,13 +22,19 @@ io.version(version)
'-o, --output <output>',
'Output file',
output,
DEFAULT_CLI_OPTIONS.output
/** @see {normalizeOptions} */
''
)
.option(
'--add-sleep',
'Add automatic sleep() based on startDateTime',
DEFAULT_CLI_OPTIONS.addSleep
)
.option(
'-s, --stdout',
'Write to stdout (ignored when running with -o, --output)',
DEFAULT_CLI_OPTIONS.stdout
)
.argument('<archive>', 'LI-HAR archive to convert')
.action(run)

Expand All @@ -40,21 +47,51 @@ function output(value) {
return value
}

/**
* Returns logger that either logs to stdout or stderr
* When writing to stdout, we want to log to stderr
*
* @param {import('caporal').LoggerInstance} log
* @param {{stdout: boolean}} opt
*/
function getLogger(log, opt) {
return opt.stdout ? log.warn : log.info
}

async function run(arg, opt, log) {
normalizeOptions(opt)
const logger = getLogger(log, opt)

try {
start(arg.archive, log)
start(arg.archive, logger)
const json = read(arg.archive)
const archive = parse(json)
const { main } = await transform(archive, opt)
write(main, opt.output)
success(opt.output, log)
write(main, opt)
success(opt, logger)
} catch (error) {
inform(error, log)
}
}

function start(file, log) {
log.info(chalk.green(`Converting '${file}'`))
/**
* Normalize options so that they are sane
* @param {{output: string, addSleep: boolean, stdout: boolean}} opt
*/
function normalizeOptions(opt) {
// If output is empty, and stdout is not set, set output to default value
if (opt.output === '' && !opt.stdout) {
opt.output = DEFAULT_CLI_OPTIONS.output
}

// If output is not empty, and stdout is set, set stdout to FALSE
if (opt.output !== '' && opt.stdout) {
opt.stdout = false
}
}

function start(file, logger) {
logger(chalk.green(`Converting '${file}'`))
}

function read(file) {
Expand All @@ -81,16 +118,22 @@ async function transform(archive, options) {
}
}

function write(main, output) {
function write(main, opt) {
try {
fs.writeFileSync(output, main)
// Write to stdout if requested, AND if no output file was specified
if (opt.stdout) {
process.stdout.write(main)
} else {
fs.writeFileSync(opt.output, main)
}
} catch (error) {
throw new CommandLineError({ name: 'WriteError', cause: error })
}
}

function success(output, log) {
log.info(chalk.green(`Wrote k6 script to '${output}'`))
function success(opt, logger) {
const target = opt.stdout ? 'STDOUT' : `'${opt.output}'`
logger(chalk.green(`Wrote k6 script to ${target}`))
}

function inform(error, log) {
Expand Down
Loading

0 comments on commit d39bfc3

Please sign in to comment.