Sandra Schlichting
Sandra Schlichting

Reputation: 26026

How to call and reuse function from file?

In the below script would I like to read the content of config.toml, and have marked where I would like to do that. What confuses me is how to include the toml.js file which contains the toml reader function.

Throughout the script will I need to read 3 different toml files.

Question

How should I include the toml.js function and how to reuse the function to read 3 different files?

Disclaimer: I am sorry for this super noob question, but this is my first project, and with 6 different ways to write a function, I find it very confusing.

index.js

'use strict'
var minimist = require('minimist')
const toml = require('./src/toml')

module.exports = () => {
var argv = minimist(process.argv.slice(2), {
  string: 'input',
  string: 'project',
  boolean: ['validate'],
  boolean: ['help'],
  alias: { i: 'input', v: 'validate', h: 'help', p: 'project' },
  unknown: function () { console.log('Unkown argument') }
})

  if (argv.help || argv.h) {
    // help output goes here
  }

  // read config.toml

}

src/toml.js

module.exports = (filename) => {
  const TOML = require('@iarna/toml')
  const fs = require('fs');

  return TOML.parse(fs.readFileSync(filename, 'utf-8'));
}

Upvotes: 0

Views: 325

Answers (2)

AJ_
AJ_

Reputation: 1485

It looks like you have everything set up correctly. Inside index.js you should be able to use toml('filename.toml'). This function returns the contents of filename.toml as an object.

src/toml.js exports a function that parses a .toml file. When you use const toml = require('./src/toml') inside index.js, you are assigning toml to the exports of ./src/toml.js (which is the parsing function). This means that in index.js, toml represents the function in ./src/toml.js.

You can then use the toml('filename.toml') as many times as you want in index.js.

Here is your index.js code modified to read the config.toml file and store the object in config...

'use strict'
var minimist = require('minimist')
const toml = require('./src/toml')

module.exports = () => {
  var argv = minimist(process.argv.slice(2), {
    string: 'input',
    string: 'project',
    boolean: ['validate'],
    boolean: ['help'],
    alias: { i: 'input', v: 'validate', h: 'help', p: 'project' },
    unknown: function () { console.log('Unkown argument') }
  })

  if (argv.help || argv.h) {
    // help output goes here
  }

  // read config.toml
  const config = toml('config.toml')

}

Upvotes: 1

McLisak
McLisak

Reputation: 61

You should be able to call toml('path/to/config/that/you/want/to/read.toml')

You have required a module src/toml.js. This module exports a function - doesn't really matter how this function is declared in this case. Whenever you import that module - you are given this function.

So:

const iCanCallThisReferenceHoweverIWant = require('./src/toml');
iCanCallThisReferenceHoweverIWant('path/to/a/toml/file.toml');

I think you need to require your dependencies in the toml.js outside of the function definition - I assume it may yell at you about that, but I'm not super-duper-confident about it :)

Proposition + some refactoring:

src/toml.js

const TOML = require('@iarna/toml')
const fs = require('fs');

const readTOML = (filename) => TOML.parse(fs.readFileSync(filename, 'utf-8'));
module.exports = readTOML;

Upvotes: 1

Related Questions