index.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env node
  2. /**
  3. * Copyright (c) 2017-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  4. *
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  7. */
  8. import * as argparse from 'argparse';
  9. import * as util from 'util';
  10. import * as fs from 'fs';
  11. import * as zlib from 'zlib';
  12. import { convert } from './converter';
  13. require('util.promisify').shim();
  14. async function process(srcPath: string, outPath: string, configPath?: string, filterPath?: string) {
  15. const config = configPath ? JSON.parse(fs.readFileSync(configPath, 'utf8')) : void 0;
  16. const filter = filterPath ? fs.readFileSync(filterPath, 'utf8') : void 0;
  17. const res = await convert(srcPath, false, config, filter);
  18. await write(outPath, res);
  19. }
  20. const zipAsync = util.promisify<zlib.InputType, Buffer>(zlib.gzip);
  21. async function write(outPath: string, res: Uint8Array) {
  22. const isGz = /\.gz$/i.test(outPath);
  23. if (isGz) {
  24. res = await zipAsync(res);
  25. }
  26. fs.writeFileSync(outPath, res);
  27. }
  28. function run(args: Args) {
  29. process(args.src, args.out, args.config, args.filter);
  30. }
  31. const parser = new argparse.ArgumentParser({
  32. addHelp: true,
  33. description: 'Convert any CIF file to a BCIF file'
  34. });
  35. parser.addArgument([ 'src' ], {
  36. help: 'Source CIF path'
  37. });
  38. parser.addArgument([ 'out' ], {
  39. help: 'Output BCIF path'
  40. });
  41. parser.addArgument([ '-c', '--config' ], {
  42. help: 'Optional encoding strategy/precision config path',
  43. required: false
  44. });
  45. parser.addArgument([ '-f', '--filter' ], {
  46. help: 'Optional filter whitelist/blacklist path',
  47. required: false
  48. });
  49. interface Args {
  50. src: string
  51. out: string
  52. config?: string
  53. filter?: string
  54. }
  55. const args: Args = parser.parseArgs();
  56. if (args) {
  57. run(args);
  58. }