fs-extra#move TypeScript Examples

The following examples show how to use fs-extra#move. 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: CliProgram.ts    From joplin-utils with MIT License 6 votes vote down vote up
async generate(name: string): Promise<void> {
    const destPath = path.resolve(this.config.basePath, name)
    await copy(
      path.resolve(__dirname, '../templates/joplin-plugin-template'),
      destPath,
    )
    await move(
      path.resolve(destPath, '_.gitignore'),
      path.resolve(destPath, '.gitignore'),
    )
    const pkgPath = path.resolve(destPath, 'package.json')
    await writeJson(
      pkgPath,
      {
        ...(await readJson(pkgPath)),
        name,
      },
      {
        spaces: 2,
      },
    )
  }
Example #2
Source File: clientGatewayService.ts    From flood with GNU General Public License v3.0 5 votes vote down vote up
async moveTorrents({hashes, destination, moveFiles, isBasePath, isCheckHash}: MoveTorrentsOptions): Promise<void> {
    await this.stopTorrents({hashes});

    await fs.promises.mkdir(destination, {recursive: true});

    if (moveFiles) {
      const isMultiFile = await this.clientRequestManager
        .methodCall('system.multicall', [
          hashes.map((hash) => {
            return {
              methodName: 'd.is_multi_file',
              params: [hash],
            };
          }),
        ])
        .then(this.processClientRequestSuccess, this.processRTorrentRequestError)
        .then((responses: string[][]): boolean[] =>
          responses.map((response) => (typeof response === 'number' ? response !== 0 : response?.[0] !== '0')),
        )
        .catch(() => undefined);

      if (isMultiFile == null || isMultiFile.length !== hashes.length) {
        throw new Error();
      }

      await Promise.all(
        hashes.map(async (hash, index) => {
          const {directory, name} = this.services?.torrentService.getTorrent(hash) || {};

          if (directory == null || name == null) {
            return;
          }

          const sourceDirectory = path.resolve(directory);
          const destDirectory = isMultiFile[index]
            ? path.resolve(isBasePath ? destination : path.join(destination, name))
            : path.resolve(destination);

          if (sourceDirectory !== destDirectory) {
            if (isMultiFile[index]) {
              await move(sourceDirectory, destDirectory, {overwrite: true});
            } else {
              await move(path.join(sourceDirectory, name), path.join(destDirectory, name), {overwrite: true});
            }
          }
        }),
      );
    }

    const hashesToRestart: Array<string> = [];
    const methodCalls = hashes.reduce((accumulator: MultiMethodCalls, hash) => {
      accumulator.push({
        methodName: isBasePath ? 'd.directory_base.set' : 'd.directory.set',
        params: [hash, destination],
      });

      if (!this.services?.torrentService.getTorrent(hash).status.includes('stopped')) {
        hashesToRestart.push(hash);
      }

      return accumulator;
    }, []);

    await this.clientRequestManager
      .methodCall('system.multicall', [methodCalls])
      .then(this.processClientRequestSuccess, this.processRTorrentRequestError);

    if (isCheckHash) {
      await this.checkTorrents({hashes});
    }

    return this.startTorrents({hashes: hashesToRestart});
  }
Example #3
Source File: index.ts    From cli with MIT License 5 votes vote down vote up
async bundle() {
    if (!this.options.bundle) {
      return;
    }
    const nccPkgJsonFile = require.resolve('@vercel/ncc/package');
    const nccPkgJson = JSON.parse(readFileSync(nccPkgJsonFile).toString());
    const nccCli = join(nccPkgJsonFile, '../', nccPkgJson.bin.ncc);
    const outDir = join(this.core.cwd, this.getOutDir());

    let preloadCode = '// midway bundle';
    const preloadFile = 'midway_bundle_entry.js';
    const requireList = await globby(['**/*.js'], {
      cwd: outDir,
    });

    preloadCode += requireList
      .map((file, index) => {
        return `require('./${file}');`;
      })
      .join('\n');

    const configurationFilePath = join(outDir, 'configuration.js');
    if (existsSync(configurationFilePath)) {
      preloadCode += `
      const configuration = require('./configuration.js');
      if (typeof configuration === 'object') {
        const configurationKey = Object.keys(configuration).find(key => typeof configuration[key] === 'function');
        if (configurationKey) {
          exports.configuration = configuration[configurationKey];
        }
      } else {
        exports.configuration = configuration;
      }
      `;
    }
    writeFileSync(join(outDir, preloadFile), preloadCode);

    this.core.cli.log('Build bundle...');
    await forkNode(
      nccCli,
      ['build', preloadFile, '-o', 'ncc_build_tmp', '-m'],
      {
        cwd: outDir,
      }
    );
    const tmpFile = join(tmpdir(), `midway_bundle_${Date.now()}.js`);
    await move(join(outDir, 'ncc_build_tmp/index.js'), tmpFile);
    await remove(outDir);
    await move(tmpFile, join(outDir, 'bundle.js'));
    await remove(tmpFile);
    this.core.cli.log(
      `Success compile to ${relative(
        process.cwd(),
        join(outDir, 'bundle.js')
      )}.`
    );
    this.core.cli.log(
      'You can use it through the configurationModule parameter in the bootstrap file.'
    );
  }
Example #4
Source File: index.ts    From cli with MIT License 5 votes vote down vote up
async package() {
    this.core.cli.log('Package artifact...');
    // 跳过打包
    if (this.options.skipZip) {
      this.core.cli.log(' - Zip artifact skip');
      if (this.core.service?.experimentalFeatures?.removeUselessFiles) {
        this.core.cli.log(' - Experimental Feature RemoveUselessFiles');
        await removeUselessFiles(this.midwayBuildPath);
      }
      return;
    }
    // 构建打包
    const packageObj: any = this.core.service.package || {};

    let file = join(this.servicePath, this.zipCodeDefaultName);

    if (packageObj.artifact) {
      if (isAbsolute(packageObj.artifact)) {
        file = packageObj.artifact;
      } else {
        file = join(this.servicePath, packageObj.artifact);
      }
    }

    this.setStore('artifactFile', file, true);
    this.core.cli.log(` - Artifact file ${relative(this.servicePath, file)}`);

    // 保证文件存在,然后删了文件,只留目录
    await ensureFile(file);
    await remove(file);

    await this.makeZip(this.midwayBuildPath, file);
    const stat = statSync(file);
    this.setStore('zipSize', stat.size, true);
    this.core.cli.log(
      ` - Zip size ${Number(stat.size / (1024 * 1024)).toFixed(2)}MB`
    );
    if (this.options.package) {
      const to = resolve(this.servicePath, this.options.package);
      await move(file, to);
    }
  }
Example #5
Source File: index.ts    From cli with MIT License 5 votes vote down vote up
async bundle() {
    if (!this.options.bundle) {
      return;
    }
    const nccPkgJsonFile = require.resolve('@vercel/ncc/package');
    const nccPkgJson = JSON.parse(readFileSync(nccPkgJsonFile).toString());
    const nccCli = join(nccPkgJsonFile, '../', nccPkgJson.bin.ncc);

    const entryList = await globby(['*.js'], {
      cwd: this.midwayBuildPath,
    });

    if (!entryList.length) {
      return;
    }

    this.core.cli.log('Build bundle...');

    await Promise.all(
      entryList.map(async entry => {
        const entryName = entry.slice(0, -3);
        await forkNode(
          nccCli,
          ['build', entry, '-o', 'ncc_build_tmp/' + entryName, '-m'],
          {
            cwd: this.midwayBuildPath,
          }
        );
        await remove(join(this.midwayBuildPath, entry));
        await move(
          join(
            this.midwayBuildPath,
            'ncc_build_tmp/' + entryName + '/index.js'
          ),
          join(this.midwayBuildPath, entry)
        );
      })
    );

    await remove(join(this.midwayBuildPath, 'node_modules'));
    await remove(join(this.midwayBuildPath, 'dist'));
    await remove(join(this.midwayBuildPath, 'ncc_build_tmp'));
    await remove(join(this.midwayBuildPath, 'src'));
  }