#!/usr/bin/env node
/* eslint-disable no-var */
/* eslint-disable prefer-arrow-callback */
/* eslint-disable @typescript-eslint/no-require-imports */

var pkg = require('../package.json');

var program = require('commander');

program
  .version(pkg.version, '-v, --version', 'display version information');

program
  .command('generate', { isDefault: true })
  .description('Generate code from appthen')
  .requiredOption('-p, --id <id>', 'project id')
  .requiredOption('-a, --auth <auth>', 'auth token')
  .option('-cc, --clear <clear>', 'clear cache')
  // .option('-i, --input <input>', 'specify the input schema file')
  .option('-o, --output <output>', 'specify the output directory', 'generated')
  .option('-fn, --filter <filter>', 'filter docments')
  .option('-fd, --folder <folder>', 'filter docments by folder')
  .option('-fr, --frame <frame>', 'code frame web or app')
  .option('-env, --env <env>', 'lowcode env')
  .option('-c, --cwd <cwd>', 'specify the working directory', '.')
  .option(
    '-q, --quiet',
    'be quiet, do not output anything unless get error',
    false
  )
  .option('-vb, --verbose', 'be verbose, output more information', false)
  .arguments('[input-schema] ali lowcode schema JSON file')
  .action(function doGenerate(inputSchema, command) {
    var options = command.opts();
    if (options.cwd) {
      process.chdir(options.cwd);
    }

    require('../dist/index.js')
      .run(inputSchema ? [inputSchema] : [], options)
      .then((retCode) => {
        process.exit(retCode);
      });
  });

program
  .command('watch')
  .description('start watch service')
  .requiredOption('-p, --id <id>', 'project id')
  .requiredOption('-a, --auth <auth>', 'auth token')
  .option('-cc, --clear <clear>', 'clear cache')
  // .option('-i, --input <input>', 'specify the input schema file')
  .option('-o, --output <output>', 'specify the output directory', 'generated')
  .option('-fn, --filter <filter>', 'filter docments')
  .option('-fd, --folder <folder>', 'filter docments by folder')
  .option('-fr, --frame <frame>', 'code frame web or app')
  .option('-env, --env <env>', 'lowcode env')
  .option('-c, --cwd <cwd>', 'specify the working directory', '.')
  .option('-m, --mode <mode>', 'watching mode', '.')
  .option('-a, --account <account>', 'user id', '.')
  .option(
    '-q, --quiet',
    'be quiet, do not output anything unless get error',
    false
  )
  .option('-vb, --verbose', 'be verbose, output more information', false)
  .arguments('[input-schema] ali lowcode schema JSON file')
  .action(function doGenerate(inputSchema, command) {
    var options = command.opts();
    if (options.cwd) {
      process.chdir(options.cwd);
    }

    require('../dist/index.js')
      .startWatching(options)
      .then((retCode) => {
        // process.exit(retCode);
      });
  });

program
  .command('version', { isDefault: false })
  .description('show cli version')
  .action(function doGenerate(inputSchema, command) {
    console.log('version: ', pkg.version);
    process.exit();
  });

program
  .command('release', { isDefault: false })
  .description('release website and upload to cdn / ftp / oss / cloud')
  .requiredOption('-c, --cloud <cloud>', 'cloud type')
  .action(function doGenerate(command) {
    var options = command?.opts?.() || {};
    if (options.cwd) {
      process.chdir(options.cwd);
    }

    require('../dist/index.js')
      .releaseMaterial(options)
      .then((retCode) => {
        process.exit(retCode);
      });
  });

program
  .command('release-material', { isDefault: false })
  .description('release material and upload to cdn')
  .requiredOption('-a, --auth <auth>', 'auth token')
  .action(function doGenerate(command) {
    var options = command?.opts?.() || {};
    if (options.cwd) {
      process.chdir(options.cwd);
    }

    require('../dist/index.js')
      .releaseMaterial(options)
      .then((retCode) => {
        process.exit(retCode);
      });
  });

program
  .command('create', { isDefault: false })
  .description('Create a new project from template')
  .requiredOption('-t, --template <template>', 'template name')
  .requiredOption('-f, --folder <folder>', 'folder name')
  .action(async function doInit(command) {
    var options = command?.opts?.() || {};
    if (options.cwd) {
      process.chdir(options.cwd);
    }
    require('../dist/index.js')
      .create(options)
      .then((retCode) => {
        process.exit(retCode);
      });
  });

program.parse(process.argv);
