main_cli.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Phoenix Shell.
  5. *
  6. * Phoenix Shell is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import { Context } from 'contextlink';
  20. import { launchPuterShell } from './puter-shell/main.js';
  21. import { NodeStdioPTT } from './pty/NodeStdioPTT.js';
  22. import { CreateFilesystemProvider } from './platform/node/filesystem.js';
  23. import { CreateEnvProvider } from './platform/node/env.js';
  24. import { parseArgs } from '@pkgjs/parseargs';
  25. import capcon from 'capture-console';
  26. import fs from 'fs';
  27. const { values } = parseArgs({
  28. options: {
  29. 'log': {
  30. type: 'string',
  31. }
  32. },
  33. args: process.argv.slice(2),
  34. });
  35. const logFile = await (async () => {
  36. if (!values.log)
  37. return;
  38. return await fs.promises.open(values.log, 'w');
  39. })();
  40. // Capture console.foo() output and either send it to the log file, or to nowhere.
  41. for (const [name, oldMethod] of Object.entries(console)) {
  42. console[name] = async (...args) => {
  43. let result;
  44. const stdio = capcon.interceptStdio(() => {
  45. result = oldMethod(...args);
  46. });
  47. if (logFile) {
  48. await logFile.write(stdio.stdout);
  49. await logFile.write(stdio.stderr);
  50. }
  51. return result;
  52. };
  53. }
  54. const ctx = new Context({
  55. ptt: new NodeStdioPTT(),
  56. config: {},
  57. platform: new Context({
  58. name: 'node',
  59. filesystem: CreateFilesystemProvider(),
  60. env: CreateEnvProvider(),
  61. }),
  62. });
  63. await launchPuterShell(ctx);