fs-extra#copyFileSync TypeScript Examples

The following examples show how to use fs-extra#copyFileSync. 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: utils.ts    From cli with Apache License 2.0 6 votes vote down vote up
export function restoreCliConfig() {
  mkdirSync(dirname(getConfigFilePath()), {recursive: true});
  copyFileSync('decrypted', getConfigFilePath());
}
Example #2
Source File: tsc.ts    From multi-downloader-nx with MIT License 5 votes vote down vote up
(async () => {

  const waitForProcess = async (proc: ChildProcess) => {
    return new Promise((resolve, reject) => {
      proc.stdout?.on('data', console.log);
      proc.stderr?.on('data', console.error);
      proc.on('close', resolve);
      proc.on('error', reject);
    });
  };

  process.stdout.write('Removing lib dir... ');
  removeSync('lib');
  process.stdout.write('✓\nRunning tsc... ');
  const tsc = exec('npx tsc');

  await waitForProcess(tsc);
  
  if (!isGUI) {
    fs.emptyDirSync(path.join('lib', 'gui'));
    fs.rmdirSync(path.join('lib', 'gui'));
  }

  if (!isTest && isGUI) {
    process.stdout.write('✓\nBuilding react... ');

    const installReactDependencies = exec('npm install', {
      cwd: path.join(__dirname, 'gui', 'react'),
    });

    await waitForProcess(installReactDependencies);
  
    const react = exec('npm run build', {
      cwd: path.join(__dirname, 'gui', 'react'),
    });
  
    await waitForProcess(react);
  }

  process.stdout.write('✓\nCopying files... ');
  if (!isTest && isGUI) {
    copyDir(path.join(__dirname, 'gui', 'react', 'build'), path.join(__dirname, 'lib', 'gui', 'electron', 'build'));
  }

  const files = readDir(__dirname);
  files.forEach(item => {
    const itemPath = path.join(__dirname, 'lib', item.path.replace(__dirname, ''));
    if (item.stats.isDirectory()) {
      if (!fs.existsSync(itemPath))
        fs.mkdirSync(itemPath);
    } else {
      copyFileSync(item.path, itemPath);
    }
  });

  process.stdout.write('✓\nInstalling dependencies... ');
  if (!isTest && !isGUI) {
    alterJSON();
  }
  if (!isTest) {
    const dependencies = exec(`npm install ${isGUI ? '' : '--production'}`, {
      cwd: path.join(__dirname, 'lib')
    });
    await waitForProcess(dependencies);
  }

  process.stdout.write('✓\n');
})();
Example #3
Source File: image-compressor.ts    From image-optimizer with MIT License 4 votes vote down vote up
#processFile = (file: DroppedFile, output: string) => {
    const originalSize = getFileSize(file.path)

    return new Promise<void>((resolve, reject) => {
      switch (file.type) {
        case MIME_TYPE_ENUM.jpg: {
          const { quality } = store.app.get('mozjpeg')

          let originalFile: string
          const isAddTempFile =
            !store.app.get('addToSubfolder') && !store.app.get('addMinSuffix')

          if (isAddTempFile) {
            originalFile = output + '.tmp'
            copyFileSync(file.path, originalFile)
          } else {
            originalFile = file.path
          }

          execFile(
            mozjpeg,
            ['-quality', `${quality}`, '-outfile', output, originalFile],
            err => {
              if (err) {
                console.log(err)
                reject(err)
              }

              const compressedSize = getFileSize(output)
              this.#sendToRenderer(file, originalSize, compressedSize)
              if (isAddTempFile) unlinkSync(originalFile)
              resolve()
            }
          )
          break
        }

        case MIME_TYPE_ENUM.png: {
          const { qualityMin, qualityMax } = store.app.get('pngquant')

          execFile(
            pngquant,
            [
              '--quality',
              `${qualityMin}-${qualityMax}`,
              '-fo',
              output,
              file.path
            ],
            err => {
              if (err) {
                console.log(err)
                reject(err)
              }

              const compressedSize = getFileSize(output)
              this.#sendToRenderer(file, originalSize, compressedSize)
              resolve()
            }
          )
          break
        }

        case MIME_TYPE_ENUM.gif: {
          execFile(gifsicle, ['-o', output, file.path], err => {
            if (err) {
              console.log(err)
              reject(err)
            }

            const compressedSize = getFileSize(output)
            this.#sendToRenderer(file, originalSize, compressedSize)
            resolve()
          })
          break
        }

        case MIME_TYPE_ENUM.svg: {
          readFile(file.path, (err, buffer) => {
            if (err) {
              console.log(err)
              reject(err)
            }

            const { data } = svg.optimize(buffer)
            writeFile(output, data, err => {
              if (err) console.log(err)

              const compressedSize = getFileSize(output)
              this.#sendToRenderer(file, originalSize, compressedSize)
              resolve()
            })
          })
          break
        }
      }
    })
  }