fs-helpers.ts 1.7 KB

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