single-file 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env node
  2. /*
  3. * Copyright 2010-2020 Gildas Lormeau
  4. * contact : gildas.lormeau <at> gmail.com
  5. *
  6. * This file is part of SingleFile.
  7. *
  8. * The code in this file is free software: you can redistribute it and/or
  9. * modify it under the terms of the GNU Affero General Public License
  10. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  11. * of the License, or (at your option) any later version.
  12. *
  13. * The code in this file is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  16. * General Public License for more details.
  17. *
  18. * As additional permission under GNU AGPL version 3 section 7, you may
  19. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  20. * AGPL normally required by section 4, provided you include this license
  21. * notice and a URL through which recipients can access the Corresponding
  22. * Source.
  23. */
  24. /* global require */
  25. const fileUrl = require("file-url");
  26. const fs = require("fs");
  27. run(require("./args"));
  28. async function run(options) {
  29. Object.keys(options).filter(optionName => optionName.includes("-"))
  30. .forEach(optionName => delete options[optionName]);
  31. const singlefile = await require("./single-file-cli-api")(options);
  32. let urls;
  33. if (options.url && !singlefile.VALID_URL_TEST.test(options.url)) {
  34. options.url = fileUrl(options.url);
  35. }
  36. if (options.urlsFile) {
  37. urls = fs.readFileSync(options.urlsFile).toString().split("\n");
  38. } else {
  39. urls = [options.url];
  40. }
  41. options.retrieveLinks = true;
  42. options.browserScripts = options.browserScripts.map(path => require.resolve(path));
  43. await singlefile.capture(urls);
  44. await singlefile.finish();
  45. }