fs-extra#copy TypeScript Examples

The following examples show how to use fs-extra#copy. 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 MIT License 7 votes vote down vote up
copyFromNodeModules = async (
  moduleInfoList: ModInfo[],
  fromNodeModulesPath: string,
  targetNodeModulesPath: string
) => {
  const moduleMap = await findModuleFromNodeModules(
    moduleInfoList,
    fromNodeModulesPath,
    fromNodeModulesPath
  );
  if (!moduleMap) {
    return;
  }
  const moduleNames = Object.keys(moduleMap);
  const result = await Promise.all(
    moduleNames.map(async name => {
      const { path } = moduleMap[name];
      const target = join(targetNodeModulesPath, name);
      await copy(path, target, {
        dereference: true,
        filter: src => {
          if (src.endsWith('/node_modules')) {
            return false;
          }
          return true;
        },
      });
      return name;
    })
  );
  return result;
}
Example #2
Source File: utils.ts    From cli with MIT License 7 votes vote down vote up
docopy = async (
  sourceDir: string,
  targetDir: string,
  paths: string[],
  log?
) => {
  const limit = plimit(20);
  await Promise.all(
    paths.map((path: string) => {
      const source = join(sourceDir, path);
      const target = join(targetDir, path);
      if (existsSync(target)) {
        const sourceStat = statSync(source);
        const targetStat = statSync(target);
        // source 修改时间小于目标文件 修改时间,则不拷贝
        if (sourceStat.mtimeMs <= targetStat.mtimeMs) {
          return;
        }
      }
      if (log) {
        log(path);
      }
      return limit(() => {
        return new Promise(resolve => {
          copy(source, target)
            .then(resolve)
            .catch(e => {
              if (log) {
                log(`Error!!! From '${source}' to '${target}'`, e);
              }
              resolve(void 0);
            });
        });
      });
    })
  );
}
Example #3
Source File: TestProject.ts    From yarn-plugins with MIT License 6 votes vote down vote up
public static async setup(): Promise<TestProject> {
    const dir = await tmp.dir();
    const pluginBundles = await globby('packages/*/bundles/**/*.js', {
      cwd: PROJECT_DIR,
    });
    const plugins = pluginBundles.map((src) => ({
      src,
      name: '@yarnpkg/' + basename(src, extname(src)),
      dest: posix.join('.yarn', 'plugins', ...src.split(sep).slice(3)),
    }));

    for (const path of plugins) {
      await copy(join(PROJECT_DIR, path.src), join(dir.path, path.dest));
    }

    const yarnConfig = safeLoad(
      await readFile(join(PROJECT_DIR, '.yarnrc.yml'), 'utf8'),
    ) as Record<string, unknown>;

    // Create .yarnrc.yml
    await outputFile(
      join(dir.path, '.yarnrc.yml'),
      safeDump({
        yarnPath: join(PROJECT_DIR, yarnConfig.yarnPath as string),
        plugins: plugins.map((plugin) => ({
          path: plugin.dest,
          spec: plugin.name,
        })),
      }),
    );

    // Create package.json
    await outputJSON(join(dir.path, 'package.json'), {
      private: true,
      workspaces: ['packages/*'],
    });

    return new TestProject(dir);
  }
Example #4
Source File: not-commonjs.test.ts    From cli with MIT License 6 votes vote down vote up
describe('test/not-commonjs.test.ts', () => {
  it('dev', async () => {
    const tsConfigFile = join(cwd, 'tsconfig.json');
    if (existsSync(tsConfigFile)) {
      await remove(tsConfigFile);
    }
    await copy(join(cwd, 'tsconfig.json.origin'), tsConfigFile);
    const { close, port, getData } = await run(cwd, {
      silent: true,
      fast: false,
    });
    const response = await fetch(`http://127.0.0.1:${port}/hello?name=midway`);
    const body = await response.text();
    const functions = await getData('functions');
    await close();
    const tsconfig = JSON.parse(readFileSync(tsConfigFile).toString());
    await remove(tsConfigFile);
    assert(functions.http);
    assert(body === 'hello world,midway');
    assert(tsconfig['ts-node'].compilerOptions.module === 'commonjs');
  });
});
Example #5
Source File: index.ts    From cli with MIT License 6 votes vote down vote up
// 生成默认入口
  async defaultGenerateEntry() {
    const functions = this.core.service.functions || {};
    for (const func in functions) {
      const handlerConf = functions[func];
      const [handlerFileName] = handlerConf.handler.split('.');
      const othEnterFile = [
        join(this.defaultTmpFaaSOut, handlerFileName + '.js'),
        join(this.core.config.servicePath, handlerFileName + '.js'),
      ].find(file => existsSync(file));
      if (othEnterFile) {
        const fileName = join(this.midwayBuildPath, `${handlerFileName}.js`);
        await copy(othEnterFile, fileName);
        this.core.debug('Use user entry', othEnterFile);
      }
    }
    if (existsSync(this.defaultTmpFaaSOut)) {
      this.core.debug('Tmp Out Dir Removed');
      await remove(this.defaultTmpFaaSOut);
    }
  }
Example #6
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 #7
Source File: init-manager.ts    From malagu with MIT License 5 votes vote down vote up
protected async outputLocalTemplate(): Promise<void> {
        await copy(this.realLocation, this.outputDir);
    }
Example #8
Source File: init-manager.ts    From malagu with MIT License 5 votes vote down vote up
protected async doOutput() {
        await copy(this.realLocation, this.outputDir);
        console.log(chalk`{bold.green Success!}`);
    }
Example #9
Source File: install-manager.ts    From malagu with MIT License 5 votes vote down vote up
protected async outputLocalRuntime(): Promise<void> {
        await copy(this.realLocation, this.outputDir);
    }
Example #10
Source File: index.test.ts    From cli with MIT License 5 votes vote down vote up
describe('test/index.test.ts', () => {
  it('dev', async () => {
    const dist = join(cwd, 'dist');
    if (existsSync(dist)) {
      await remove(dist);
    }
    const { close, port } = await run(cwd, {
      fast: true,
      watchFile: 'package.json',
    });
    if (existsSync(api)) {
      await remove(api);
    }
    await copy(api1, api);
    await wait();

    const response = await fetch(`http://127.0.0.1:${port}/?name=midway`);
    const body = await response.text();
    assert(body === 'hello world,midway');
    await remove(api);
    await copy(api2, api);
    await wait();
    const response2 = await fetch(`http://127.0.0.1:${port}/?name=midway`);
    const body2 = await response2.text();
    assert(body2 === 'hello world2,midway');
    await wait();
    await close();
  });
  it('dev multi', async () => {
    const dist = join(cwd, 'dist');
    if (existsSync(dist)) {
      await remove(dist);
    }
    if (existsSync(api)) {
      await remove(api);
    }
    await copy(api1, api);
    const { close, port } = await run(cwd);
    const { close: close2, port: port2 } = await run(cwd, { silent: true });
    const response = await fetch(`http://127.0.0.1:${port}/?name=midway`);
    const body = await response.text();
    assert(body === 'hello world,midway');
    const response2 = await fetch(`http://127.0.0.1:${port2}/?name=midway`);
    const body2 = await response2.text();
    assert(body2 === 'hello world,midway');
    await close();
    await close2();
  });
  it('dev error', async () => {
    const cwd = join(__dirname, 'fixtures/error-app');
    const dist = join(cwd, 'dist');
    if (existsSync(dist)) {
      await remove(dist);
    }
    const { close } = await run(cwd, { port: 12336 });
    await close();
  });
});
Example #11
Source File: index.ts    From cli with MIT License 5 votes vote down vote up
async copyFile() {
    this.core.cli.log('Copy Files to build directory...');
    // copy packages config files
    const packageObj: any = this.core.service.package || {};
    if (this.core.config.specFile.path) {
      // backup original yml file
      await copy(
        this.core.config.specFile.path,
        resolve(this.midwayBuildPath, './f.origin.yml')
      );
      this.core.cli.log(
        `   - Copy ${this.core.config.specFile.path.replace(
          `${this.servicePath}/`,
          ''
        )} to ${'f.origin.yml'}`
      );
    }
    const exclude = [].concat(packageObj.exclude || []);
    if (!packageObj.lockFile) {
      exclude.push('yarn.lock');
      exclude.push('package-lock.json');
      exclude.push('pnpm-lock.yaml');
    }
    await copyFiles({
      sourceDir: this.servicePath,
      targetDir: this.midwayBuildPath,
      include: this.options.skipBuild
        ? [].concat(packageObj.include || [])
        : [this.options.sourceDir || 'src'].concat(packageObj.include || []),
      exclude,
      log: path => {
        this.core.cli.log(`   - Copy ${path}`);
      },
    });

    if (this.options.skipBuild) {
      // 跳过编译时也不处理package.json
      return;
    }
    if (this.codeAnalyzeResult.integrationProject) {
      let originPkgJson = {};
      try {
        const pkgJsonPath = join(this.servicePath, 'package.json');
        if (existsSync(pkgJsonPath)) {
          originPkgJson = JSON.parse(readFileSync(pkgJsonPath).toString());
        }
      } catch {
        //
      }
      await writeJSON(join(this.midwayBuildPath, 'package.json'), {
        ...originPkgJson,
        name: this.codeAnalyzeResult.projectType,
        version: '1.0.0',
        dependencies: this.codeAnalyzeResult.usingDependenciesVersion.valid,
      });
    }
    if (this.options.sharedDir) {
      this.options.sharedTargetDir = this.options.sharedTargetDir || 'static';
      this.core.cli.log(
        ` - Copy Shared Files to build directory(${this.options.sharedTargetDir})...`
      );
      this.options.sharedDir = this.transformToAbsolute(
        this.servicePath,
        this.options.sharedDir
      );
      this.options.sharedTargetDir = this.transformToAbsolute(
        this.midwayBuildPath,
        this.options.sharedTargetDir
      );
      console.log(this.options.sharedTargetDir);
      await copy(this.options.sharedDir, this.options.sharedTargetDir);
    }
    this.core.cli.log(' - File copy complete');
  }
Example #12
Source File: faas_tmp_out.ts    From cli with MIT License 5 votes vote down vote up
hooks = {
    'after:package:cleanup': async () => {
      const tmpDir = this.getStore('defaultTmpFaaSOut', 'PackagePlugin');
      console.log('tmpDIr', tmpDir);
      await copy(resolve(__dirname, 'test.ts'), resolve(tmpDir, 'src/test.ts'));
      await copy(resolve(__dirname, 'test.js'), resolve(tmpDir, 'test.js'));
    }
  }
Example #13
Source File: properties-file.ts    From relate with GNU General Public License v3.0 5 votes vote down vote up
public async backupPropertiesFile(backupPath: string): Promise<void> {
        await copy(this.path, backupPath);
    }
Example #14
Source File: CliProgram.ts    From joplin-utils with MIT License 5 votes vote down vote up
private async copyManifest() {
    await copy(
      path.resolve(this.config.basePath, 'src/manifest.json'),
      path.resolve(this.config.basePath, 'dist/manifest.json'),
    )
  }
Example #15
Source File: init.ts    From engine with MIT License 4 votes vote down vote up
init: producer = async ({
  _webpack = webpack,
  _copy = copy,
  _HtmlWebpackPlugin = HtmlWebpackPlugin,
  _BundleAnalyzerPlugin = BundleAnalyzerPlugin,
  _MiniCssExtractPlugin = MiniCssExtractPlugin,
  trigger = observe.build.triggers.init,
  entryPath = get.config.entryPath,
  distPath = get.config.distPath,
  publicIndexPath = get.config.publicIndexPath,
  publicPath = get.config.publicPath,
  commandPath = get.config.commandPath,
  packagePath = get.config.packagePath,
  nodeModulesPath = get.config.nodeModulesPath,
  overrideModulesPath = get.config.overrideModulesPath,
  replacerPath = get.config.replacerPath,
  packageNodeModulesPath = get.config.packageNodeModulesPath,
  configPath = get.config.configPath,
  name = get.config.name,
}: props) => {
  if (!trigger) {
    return;
  }

  let webpackConfig: EngineConfig["webpack"];
  try {
    if (existsSync(configPath.value())) {
      webpackConfig = require(configPath.value())?.webpack;
    }
  } catch (error) {
    console.error("Could not extend the webpack config", error);
  }

  if (!webpackConfig) {
    webpackConfig = {};
  }

  if (!webpackConfig.publicPath) {
    //TODO: have a place to store defaults
    webpackConfig.publicPath = "/";
  }

  const plugins = {
    htmlWebpackPlugin: {
      title: name.value(),
      template: publicIndexPath.value(),
      templateParameters: {
        PUBLIC_URL: webpackConfig.publicPath,
      },
    } as HtmlWebpackPlugin.Options,
    miniCssExtractPlugin: {
      filename: "[name].css",
      chunkFilename: "[id].css",
    },
    definePlugin: {
      "process.env.NODE_ENV": JSON.stringify("production"),
      "process.env.DEBUG": JSON.stringify(false),
    } as ConstructorParameters<typeof webpack.DefinePlugin>[0],
  };

  if (webpackConfig.production?.plugins?.htmlWebpackPlugin) {
    plugins.htmlWebpackPlugin =
      webpackConfig.production?.plugins?.htmlWebpackPlugin(
        plugins.htmlWebpackPlugin,
        require.resolve
      );
  }

  if (webpackConfig.production?.plugins?.definePlugin) {
    plugins.definePlugin = webpackConfig.production.plugins.definePlugin(
      plugins.definePlugin,
      require.resolve
    );
  }

  let config = {
    mode: "production",
    devtool: "source-map",
    entry: entryPath.value(),
    output: {
      path: distPath.value(),
      filename: "[name].[contenthash:8].js",
      publicPath: webpackConfig.publicPath,
    },
    resolve: {
      modules: ["node_modules", commandPath.value()],
      extensions: [".js", ".jsx", ".ts", ".tsx"],
    },
    resolveLoader: {
      modules: [nodeModulesPath.value(), packageNodeModulesPath.value()],
      extensions: [".js", ".jsx", ".ts", ".tsx"],
    },
    module: {
      rules: [
        {
          test: /\.svg$/,
          use: [
            require.resolve("@svgr/webpack"),
            require.resolve("url-loader"),
          ],
        },
        {
          test: /\.(png|jpg|gif|webp)$/,
          use: [
            {
              loader: require.resolve("file-loader"),
              options: {
                name: "[name].[ext]?[hash]",
                outputPath: "assets",
              },
            },
          ],
        },
        {
          test: /\.(woff(2)?|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
          use: [
            {
              loader: require.resolve("file-loader"),
              options: {
                name: "[name].[ext]",
                outputPath: "fonts/",
              },
            },
          ],
        },
        {
          test: /\.(js|jsx)$/,
          exclude: /node_modules/,
          // exclude: fileIsES5(FILE_ENCODING),
          use: [
            {
              loader: require.resolve("babel-loader"),
              options: {
                cacheDirectory: true,
                comments: false,
                minified: true,
                presets: [
                  require.resolve("@babel/preset-env"),
                  require.resolve("@babel/preset-react"),
                ],
                plugins: [
                  require.resolve("babel-plugin-react-require"),
                  require.resolve("@babel/plugin-proposal-class-properties"),
                  require.resolve("@babel/plugin-transform-runtime"),
                  // [
                  //   "babel-plugin-module-rewrite",
                  //   {
                  //     replaceFunc: replacerPath.value(),
                  //     replaceHandlerName: "replacer",
                  //     overrideModulesPath: overrideModulesPath.value(),
                  //     nodeModulesPath: nodeModulesPath.value(),
                  //   },
                  // ],
                ],
              },
            },
          ],
        },
        {
          test: /\.(ts|tsx)$/,
          // exclude: fileIsES5(FILE_ENCODING),
          use: [
            {
              loader: require.resolve("babel-loader"),
              options: {
                cacheDirectory: true,
                comments: false,
                minified: true,
                presets: [
                  {
                    plugins: [
                      [
                        require.resolve("@c11/engine.babel-plugin-syntax"),
                        {
                          viewLibrary: "@c11/engine.react",
                        },
                      ],
                    ],
                  },
                  require.resolve("@babel/preset-typescript"),
                  require.resolve("@babel/preset-env"),
                  require.resolve("@babel/preset-react"),
                ],
                plugins: [
                  require.resolve("babel-plugin-react-require"),
                  require.resolve("@babel/plugin-proposal-class-properties"),
                  require.resolve("@babel/plugin-transform-runtime"),
                  // [
                  //   "babel-plugin-module-rewrite",
                  //   {
                  //     replaceFunc: replacerPath.value(),
                  //     replaceHandlerName: "replacer",
                  //     overrideModulesPath: overrideModulesPath.value(),
                  //     nodeModulesPath: nodeModulesPath.value(),
                  //   },
                  // ],
                ],
              },
            },
          ],
        },
        {
          test: /\.css$/,
          exclude: /\.module\.css$/,
          use: [
            {
              loader: _MiniCssExtractPlugin.loader,
              options: {
                publicPath: webpackConfig.publicPath,
              },
            },
            {
              loader: require.resolve("css-loader"),
              options: { modules: false, importLoaders: 1 },
            },
            {
              loader: require.resolve("postcss-loader"),
              options: {
                postcssOptions: {
                  config: false,
                  plugins: [
                    require.resolve("postcss-import"),
                    [require.resolve("postcss-preset-env"), { stage: 1 }],
                  ],
                },
              },
            },
          ],
        },
        {
          test: /\.module.css$/,
          use: [
            {
              loader: _MiniCssExtractPlugin.loader,
              options: {
                publicPath: webpackConfig.publicPath,
              },
            },
            {
              loader: require.resolve("css-loader"),
              options: {
                importLoaders: 1,
                modules: true,
                sourceMap: true,
              },
            },
            {
              loader: require.resolve("postcss-loader"),
              options: {
                postcssOptions: {
                  config: false,
                  plugins: [
                    require.resolve("postcss-import"),
                    [require.resolve("postcss-preset-env"), { stage: 1 }],
                  ],
                },
              },
            },
          ],
        },
      ],
    },
    plugins: [
      // new _BundleAnalyzerPlugin(),
      new _webpack.ids.HashedModuleIdsPlugin(),
      new _webpack.DefinePlugin(plugins.definePlugin),
      new _HtmlWebpackPlugin(plugins.htmlWebpackPlugin),
      new _MiniCssExtractPlugin(plugins.miniCssExtractPlugin),
    ],
    optimization: {
      runtimeChunk: "single",
      splitChunks: {
        chunks: "all",
        maxInitialRequests: Infinity,
        minSize: 0,
        cacheGroups: {
          vendor: {
            test: /[\\/]node_modules[\\/]/,
            name(module) {
              // get the name. E.g. node_modules/packageName/not/this/part.js
              // or node_modules/packageName
              const parts = module.context.match(
                /[\\/]node_modules[\\/](.*?)([\\/]|$)/
              );
              const packageName = parts && parts[1] ? parts[1] : randomId();

              // npm package names are URL-safe, but some servers don't like @ symbols
              return `npm.${packageName.replace("@", "")}`;
            },
          },
        },
      },
    },
  } as Configuration;

  if (webpackConfig.production && webpackConfig.production.config) {
    config = webpackConfig.production.config(config, require.resolve);
  }

  await _copy(publicPath.value(), distPath.value(), {
    dereference: true,
    filter: (file) => file !== publicIndexPath.value(),
  });

  _webpack(config, (err, stats) => {
    if (err) {
      console.error(err.stack || err);
      return;
    }
    if (stats) {
      const info = stats.toJson();
      if (stats.hasErrors()) {
        console.error(info.errors);
      }
      if (stats.hasWarnings()) {
        console.warn(info.warnings);
      }
      console.log(stats.toString());
    }
    console.log(`Build complete`);
  });
}
Example #16
Source File: index.ts    From cli with MIT License 4 votes vote down vote up
hooks = {
    'before:package:cleanup': async () => {
      // 跳过zip打包
      this.options.skipZip = true;
      this.options.skipInstallDep = true;
      if (!this.core.service.package) {
        this.core.service.package = {};
      }
      this.core.service.package.include = (
        this.core.service?.package?.include || []
      ).concat(['config.json', 'sitemap.json']);
    },
    'package:generateEntry': async () => {
      this.core.cli.log('Generate entry file...');
      this.setGlobalDependencies('@midwayjs/serverless-scf-starter');
      writeWrapper({
        baseDir: this.servicePath,
        service: this.core.service,
        distDir: this.midwayBuildPath,
        starter: '@midwayjs/serverless-scf-starter',
      });
    },
    'package:package': async () => {
      // 拷贝到 cloudfunctions 目录
      let projectPkgJson: any = {};
      try {
        const json: string = readFileSync(
          join(this.servicePath, 'package.json')
        ).toString();
        projectPkgJson = JSON.parse(json);
      } catch {
        // ignore
      }
      const functions = this.core.service.functions || {};
      if (existsSync(this.wechatFunctionBuildPath)) {
        this.core.cli.log('Clear old cloud functions directory');
        await remove(this.wechatFunctionBuildPath);
      }
      await ensureDir(this.wechatFunctionBuildPath);
      for (const func in functions) {
        const handlerConf = functions[func];
        const [originFileName, handlerName] = handlerConf.handler.split('.');
        const cloudFunctionName = func;
        this.core.cli.log('Create function: ' + cloudFunctionName);

        const functionDir = join(
          this.wechatFunctionBuildPath,
          cloudFunctionName
        );
        await copy(this.midwayBuildPath, functionDir);

        await this.safeRemove(join(functionDir, 'src'));
        await this.safeRemove(join(functionDir, 'f.yml'));
        await this.safeRemove(join(functionDir, 'f.origin.yml'));
        await this.safeRemove(join(functionDir, 'tsconfig.json'));
        await this.safeRemove(join(functionDir, 'dist/midway.build.json'));
        await this.safeRemove(join(functionDir, 'dist/.mwcc-cache'));

        if (originFileName !== 'index') {
          const main = 'index.js';
          const originFile = originFileName + '.js';
          writeFileSync(
            join(functionDir, main),
            `exports.main = require('./${originFile}').${handlerName};`
          );
        }

        const pkgJsonFile = join(functionDir, 'package.json');
        let pkgJson: any = {};

        if (existsSync(pkgJsonFile)) {
          pkgJson = JSON.parse(readFileSync(pkgJsonFile).toString());
        }

        pkgJson.name = `${cloudFunctionName}`;
        pkgJson.version = projectPkgJson.version || '1.0.0';
        pkgJson.main = 'index.js';
        delete pkgJson.devDependencies;
        writeFileSync(pkgJsonFile, JSON.stringify(pkgJson, null, 2));
      }
    },
  };