anephenix/mcg

View on GitHub
bin/mcg

Summary

Maintainability
Test Coverage
#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const util = require('util');
const program = require('commander');
const main = require('../index');
const packageJson = require('../package.json');

// Helper functions
const exists = util.promisify(fs.exists);

const loadConfigFileIfExists = async () => {
    const expectedConfigFilePath = path.join(process.cwd(), 'mcg.config.js');
    const configFileExists = await exists(expectedConfigFilePath);
    if (!configFileExists) return {};
    console.log(
        `Using configuration settings found at ${expectedConfigFilePath}`
    );
    const configFile = require(expectedConfigFilePath);
    return configFile;
};

const mainAction = async (modelName) => {
    const options = program.opts();
    const configFileOptions = await loadConfigFileIfExists();
    const testFolder = options.testFolder || configFileOptions.testFolder;
    const mainDir =
    options.mainDir || configFileOptions.mainDir || process.cwd();
    const tableName = options.tableName || configFileOptions.tableName;
    const filesCreated = await main(modelName, mainDir, testFolder, tableName);
    // const { attributes } = program;
    // TODO - have a way to turn the attributes into a JS object
    // TODO - use that js object to:
    // - create the table columns in the migration file
    // - define the properties in the model file's JSON schema
    // - define the properties for the test seed data file
    // - define the things to test in the model test file (validations)
    // console.log({ attributes });
    console.log('Created files:');
    filesCreated.forEach((fc) => {
        console.log(fc);
    });
};

program
    .version(packageJson.version)
    .option(
        '-t,--testFolder <testFolder>',
        'custom folder for the test files to be put in'
    )
    .option(
        '-m,--mainDir <mainDir>',
        'the main folder for the files to be generated in'
    )
    .option(
        '-a, --attributes <attributes...>',
        'the attributes to insert into the model'
    )
    .option('-d, --tableName <tableName>', 'the name of the database table')
    .arguments('<modelName>')
    .action(mainAction);

// Run the command
program.parse();