fs-helpers.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import * as fs from 'original-fs';
  2. import * as cp from 'node:child_process';
  3. import * as os from 'node:os';
  4. import * as path from 'node:path';
  5. export async function copyApp (targetDir: string): Promise<string> {
  6. // On macOS we can just copy the app bundle, easier too because of symlinks
  7. if (process.platform === 'darwin') {
  8. const appBundlePath = path.resolve(process.execPath, '../../..');
  9. const newPath = path.resolve(targetDir, 'Electron.app');
  10. cp.spawnSync('cp', ['-R', appBundlePath, path.dirname(newPath)]);
  11. return newPath;
  12. }
  13. // On windows and linux we should read the zip manifest files and then copy each of those files
  14. // one by one
  15. const baseDir = path.dirname(process.execPath);
  16. const zipManifestPath = path.resolve(__dirname, '..', '..', 'script', 'zip_manifests', `dist_zip.${process.platform === 'win32' ? 'win' : 'linux'}.${process.arch}.manifest`);
  17. const filesToCopy = (fs.readFileSync(zipManifestPath, 'utf-8')).split('\n').filter(f => f !== 'LICENSE' && f !== 'LICENSES.chromium.html' && f !== 'version' && f.trim());
  18. await Promise.all(
  19. filesToCopy.map(async rel => {
  20. await fs.promises.mkdir(path.dirname(path.resolve(targetDir, rel)), { recursive: true });
  21. fs.copyFileSync(path.resolve(baseDir, rel), path.resolve(targetDir, rel));
  22. })
  23. );
  24. return path.resolve(targetDir, path.basename(process.execPath));
  25. }
  26. export async function withTempDirectory (fn: (dir: string) => Promise<void>, autoCleanUp = true) {
  27. const dir = await fs.promises.mkdtemp(path.resolve(os.tmpdir(), 'electron-update-spec-'));
  28. try {
  29. await fn(dir);
  30. } finally {
  31. if (autoCleanUp) {
  32. cp.spawnSync('rm', ['-r', dir]);
  33. }
  34. }
  35. };