async function add()

in packages/plugin-block/src/index.ts [20:106]


  async function add(args: minimist.ParsedArgs) {
    const spinner = ora();
    const url = args._[1];
    if (!url) {
      error(`${chalk.cyan.underline('breezr add <path>')}, path can't be empty`);
      return;
    }
    spinner.start('Parse url and args');
    const useYarn = existsSync(join(api.getCwd(), 'yarn.lock'));
    const defaultNpmClient = useYarn ? 'yarn' : 'npm';

    debug(DEBUG_TAG, `defaultNpmClient: ${defaultNpmClient}`);
    debug(DEBUG_TAG, `args: ${JSON.stringify(args)}`);

    const {
      path: argPath,
      dryRun,
      npmClient = defaultNpmClient,
      skipDependencies
    } = args;

    let path = '';
    if (config.prefix) {
      path = join(config.prefix, argPath);
    } else {
      path = argPath;
    }

    const ctx = getCtx(url, args);
    spinner.succeed();

    // 2. clone git repo
    if (!ctx.isLocal && !ctx.repoExists) {
      await gitClone(ctx, spinner);
    }

    // 3. update git repo
    if (!ctx.isLocal && ctx.repoExists) {
      await gitUpdate(ctx, spinner);
    }

    const pkgPath = join(ctx.sourcePath, 'package.json');
    if (!existsSync(pkgPath)) {
      error(`not find package.json in ${ctx.sourcePath}`);
      exit(-1);
    } else {
      ctx.pkg = require(pkgPath);
    }

    // 4. update depenencies
    if (!skipDependencies) {
      await installBlockDeps({
        ctx,
        npmClient,
        projectPkg: api.service.pkg,
        args,
        cwd: api.getCwd(),
        spinner
      });
    } else {
      debug(DEBUG_TAG,'skip dependencies');
    }

    // 5. Generate files
    spinner.start(`Generate files`);
    spinner.stopAndPersist();
    const BlockGenerator = require('./generator').BlockGenerator;
    try {
      const generator = new BlockGenerator(args._.slice(2), {
        sourcePath: ctx.sourcePath,
        path,
        dryRun,
        api,
        cwd: api.getCwd(),
        env: {
          cwd: api.getCwd(),
        },
        resolved: __dirname,
      });
      await generator.run();
    } catch (e) {
      spinner.fail();
      error(e.toString());
      return;
    }
    spinner.succeed('Generate files');
  }