fs-extra#rename TypeScript Examples

The following examples show how to use fs-extra#rename. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AppUpdater.ts    From electron-differential-updater with MIT License 4 votes vote down vote up
protected async executeDownload(
    taskOptions: DownloadExecutorTask
  ): Promise<Array<string>> {
    const fileInfo = taskOptions.fileInfo;
    const downloadOptions: DownloadOptions = {
      headers: taskOptions.downloadUpdateOptions.requestHeaders,
      cancellationToken: taskOptions.downloadUpdateOptions.cancellationToken,
      sha2: (fileInfo.info as any).sha2,
      sha512: fileInfo.info.sha512
    };

    if (this.listenerCount(DOWNLOAD_PROGRESS) > 0) {
      downloadOptions.onProgress = it => this.emit(DOWNLOAD_PROGRESS, it);
    }

    const updateInfo =
      taskOptions.downloadUpdateOptions.updateInfoAndProvider.info;
    const version = updateInfo.version;
    const packageInfo = fileInfo.packageInfo;
    const {
      configuration
    } = taskOptions.downloadUpdateOptions.updateInfoAndProvider.provider;
    function getCacheUpdateFileName(): string {
      // NodeJS URL doesn't decode automatically
      const urlPath = decodeURIComponent(taskOptions.fileInfo.url.pathname);
      if (urlPath.endsWith(`.${taskOptions.fileExtension}`)) {
        return path.posix.basename(urlPath);
      } else {
        // url like /latest, generate name
        return `update.${taskOptions.fileExtension}`;
      }
    }
    const useAppSupportCache = configuration ? configuration.useAppSupportCache : false;
    const downloadedUpdateHelper = await this.getOrCreateDownloadHelper(
      useAppSupportCache
    );
    const cacheDir = downloadedUpdateHelper.cacheDirForPendingUpdate;
    await ensureDir(cacheDir);
    const updateFileName = getCacheUpdateFileName();
    let updateFile = path.join(cacheDir, updateFileName);
    const packageFile =
      packageInfo == null
        ? null
        : path.join(
            cacheDir,
            `package-${version}${path.extname(packageInfo.path) || ".7z"}`
          );

    const done = async (isSaveCache: boolean) => {
      await downloadedUpdateHelper.setDownloadedFile(
        updateFile,
        packageFile,
        updateInfo,
        fileInfo,
        updateFileName,
        isSaveCache
      );
      await taskOptions.done!!({
        ...updateInfo,
        downloadedFile: updateFile
      });
      return packageFile == null ? [updateFile] : [updateFile, packageFile];
    };

    const log = this._logger;
    const cachedUpdateFile = await downloadedUpdateHelper.validateDownloadedPath(
      updateFile,
      updateInfo,
      fileInfo,
      log
    );
    if (cachedUpdateFile != null) {
      updateFile = cachedUpdateFile;
      return await done(false);
    }

    const removeFileIfAny = async () => {
      await downloadedUpdateHelper.clear().catch(() => {
        // ignore
      });
      return await unlink(updateFile).catch(() => {
        // ignore
      });
    };

    const tempUpdateFile = await createTempUpdateFile(
      `temp-${updateFileName}`,
      cacheDir,
      log
    );
    try {
      await taskOptions.task(
        tempUpdateFile,
        downloadOptions,
        packageFile,
        removeFileIfAny
      );
      await rename(tempUpdateFile, updateFile);
    } catch (e) {
      await removeFileIfAny();

      if (e instanceof CancellationError) {
        log.info("cancelled");
        this.emit("update-cancelled", updateInfo);
      }
      throw e;
    }

    log.info(`New version ${version} has been downloaded to ${updateFile}`);
    return await done(true);
  }