generateExtracts.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict'
  2. /* eslint-disable no-global-assign */
  3. /* eslint-disable no-native-reassign */
  4. require = require('esm')(module) // ES Module loader
  5. const fs = require('fs-extra')
  6. const yargs = require('yargs')
  7. const path = require('path')
  8. const { getImage } = require('./server/routes/getImage')
  9. const { cutImage } = require('./server/routes/getImageExtracts')
  10. /**
  11. * @typedef {Object} CliObject
  12. * @property {String} sceneName Input scene. Must be in the `./images` directory
  13. * @property {String} [outputDir] Directory where to output extracts. Defaults to the extracts directory
  14. * @property {String} [extractConfigX=4] Horizontal extracting configuration. Defaults to 4
  15. * @property {String} [extractConfigY=4] Vertical extracting configuration. Defaults to 4
  16. * @property {String} $0 The script name or node command
  17. */
  18. /**
  19. * @constant
  20. * @type {CliObject}
  21. */
  22. const cli = yargs
  23. .usage('Cut a scene using provided configuration.')
  24. .usage('Usage: node generateExtracts.js --sceneName=<sceneName> --quality=<quality> [--extractConfigX=<extractConfigX=4>] [--extractConfigY=<extractConfigY=4>] [--outputDir=<outputDir>]')
  25. .example('node generateExtracts.js --sceneName=cuisine01 --quality=150 --extractConfigX=4 --extractConfigY=5 --outputDir=extracts')
  26. .option('sceneName', {
  27. demandOption: true,
  28. describe: 'Input scene. Must be in the `./images` directory',
  29. type: 'string'
  30. })
  31. .option('quality', {
  32. demandOption: true,
  33. describe: 'Scene quality to choose.',
  34. type: Number
  35. })
  36. .option('extractConfigX', {
  37. demandOption: false,
  38. describe: 'Horizontal extracting configuration. Defaults to 4.',
  39. type: Number,
  40. default: 4
  41. })
  42. .option('extractConfigY', {
  43. demandOption: false,
  44. describe: 'Vertical extracting configuration. Defaults to 4.',
  45. type: Number,
  46. default: 4
  47. })
  48. .option('outputDir', {
  49. demandOption: false,
  50. describe: 'Directory where to output extracts. Defaults to the extracts directory.',
  51. type: 'string'
  52. })
  53. .help('h')
  54. .alias('h', 'help')
  55. .wrap(yargs.terminalWidth())
  56. .argv
  57. const setup = async () => {
  58. const { sceneName, quality, extractConfigX, extractConfigY, outputDir } = cli
  59. console.log(sceneName, quality, extractConfigX, extractConfigY, outputDir)
  60. // Get the image path and link
  61. const image = await getImage(sceneName, quality)
  62. // Cut the image
  63. const extracts = await cutImage(image, extractConfigX, extractConfigY)
  64. console.log(extracts)
  65. // Move extracts images if outputDir is specified
  66. if (outputDir)
  67. await Promise.all(extracts.map(x => fs.move(x.path, path.resolve(outputDir, path.basename(x.path)))))
  68. }
  69. setup()