@epdoc/cmdutil
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

epdoc-fs

Commander command line utilities for limited distribution.

Build

npm install
npm run build

Examples

import { CmdProps } from '@epdoc/cmdutil';
import pkg from '../package.json';
import { MainCmd, RosGenCmd, RosReadCmd } from './commands';

// import { FileRenameAll } from './commands/rename-all';
// import { FileRenameCamelcase } from './commands/rename-camelcase';
// import { FileRenameDate } from './commands/rename-date';

const mainCmd = new MainCmd(pkg as CmdProps);

mainCmd.addSubCommand(new RosGenCmd());
mainCmd.addSubCommand(new RosReadCmd());

mainCmd.parseArgs();

main-cmd.ts

import { BaseMainCmd } from '@epdoc/cmdutil';

export class MainCmd extends BaseMainCmd {
  private _isMain = true;

  static isInstance(val: any): val is MainCmd {
    return val && val._isMain === true;
  }
}

ros-gen-cmd.ts

import { CmdProps, FileBaseCmd, log } from '@epdoc/cmdutil';
import { FSItem, FolderPath } from '@epdoc/fsutil';
import { isNonEmptyArray } from '@epdoc/typeutil';
import { Argument, Option } from 'commander';
import { EnvType, isEnvType, skip } from '../lib';
import { FileOperation } from '../file-operation';

export class RosGenCmd extends FileBaseCmd {
  private _isGenRunner = true;

  static isInstance(val: any): val is RosGenCmd {
    return val && val._isGenRunner === true;
  }

  constructor() {
    super({ name: 'generate', description: 'Generate RSC files from JSON configuration' });
  }

  getProps(): CmdProps {
    return {
      aliases: ['gen'],
      options: [
        new Option('-j --json', 'Also output as JSON'),
        new Option('-t --time', 'Include time in filename of output file'),
        new Option('--env <env>', 'Set env').default(skip.env),
        new Option('--comment <text>', 'Comment to put in header of RSC file'),
        new Option('-o --output <path>', 'Output filename. Defaults to same folder as input file')
      ],
      arguments: [new Argument('<files...>', 'List of configuration files')]
    };
  }

  public async run(): Promise<void> {
    return Promise.resolve()
      .then((resp) => {
        if (this.opts.env) {
          if (isEnvType(this.opts.env)) {
            skip.setEnv(this.opts.env);
          } else {
            let envs: string[] = Object.keys(EnvType).map((key) => EnvType[key]);
            throw new Error(`Invalid env value '${this.opts.env}', must be one of ${envs.join(',')}`);
          }
        }
        return this.getFilesFromArgs(this.parent.opts.pwd as FolderPath, {
          levels: 1,
          files: true,
          folders: false
        });
      })
      .then(async (resp) => {
        if (isNonEmptyArray(resp)) {
          log.h2(`Generating output from ${resp.length} config files`).trace();
          let deviceOpts = {
            cwd: this.parent.opts.cwd,
            pwd: this.parent.opts.pwd,
            json: this.opts.json,
            time: this.opts.time,
            output: this.opts.output,
            comment: this.opts.comment
          };
          for (let fdx = 0; fdx < resp.length; ++fdx) {
            const fs: FSItem = resp[fdx];
            log.h2('Generate RSC file for device').path(fs.basename).trace();
            const device = new FileOperation(deviceOpts);
            await device
              .read(fs)
              .then((resp) => {
                device.initOutput();
                return device.generate();
              })
              .then((resp) => {
                return device.write();
              })
              .then((resp) => {
                return Promise.resolve();
              });
          }
        } else {
          log.h2('No config files specified').info();
        }
      });
  }
}

Readme

Keywords

Package Sidebar

Install

npm i @epdoc/cmdutil

Weekly Downloads

69

Version

2.0.0

License

UNLICENSED

Unpacked Size

130 kB

Total Files

50

Last publish

Collaborators

  • jpravetz