New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@epdoc/cmdutil

Package Overview
Dependencies
Maintainers
0
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@epdoc/cmdutil

Commander utility classes and general CLI helpers.

latest
Source
npmnpm
Version
2.6.0
Version published
Maintainers
0
Created
Source

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();
        }
      });
  }
}

Keywords

fs

FAQs

Package last updated on 10 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts