visitImports()

in lib/generator.js [206:303]


  visitImports(imports, usedTypes, innerModule) {
    const teafilePath = fs.existsSync(path.join(this.config.pkgDir, 'Teafile')) ?
      path.join(this.config.pkgDir, 'Teafile') : path.join(this.config.pkgDir, 'Darafile');
    const teaFile = JSON.parse(fs.readFileSync(teafilePath, 'utf8'));
    const release = teaFile.releases && teaFile.releases.go || '';
    const strs = release.split(':');
    this.module = {
      importPackages: {},
      moduleName: strs[0].substring(0, strs[0].lastIndexOf('/')) || 'client'
    };
    if(!this.structName) {
      this.structName = this.config.go ? this.config.go.clientName || 'Client' : 'Client';
    }
    this.clientName = {};
    this.constructFunc = {};
    if (imports.length > 0) {
      const lockPath = path.join(this.config.pkgDir, '.libraries.json');
      const lock = fs.existsSync(lockPath) ? JSON.parse(fs.readFileSync(lockPath, 'utf8')) : {};
      for (let i = 0; i < imports.length; i++) {
        const item = imports[i];
        const aliasId = _name(item);
        const main = item.mainModule;
        const inner = item.module;
        const moduleDir = main ? this.config.libraries[main] : this.config.libraries[aliasId];
        const innerPath = item.innerPath;
        
        if (innerPath) {
          const filepath = innerPath.replace(/(\.tea)$|(\.spec)$|(\.dara)$/gi, '');
          const pkgName = this.getPkgName(filepath);
          innerModule.set(aliasId, filepath);
          this.module.importPackages[aliasId] = {
            pkgName: pkgName,
            name: aliasId,
            inner: true
          };
          this.constructFunc[aliasId] = this.getInnerClient(aliasId);
          this.clientNameDefine(aliasId);
          continue;
        }
        let targetPath = '';
        if (moduleDir.startsWith('./') || moduleDir.startsWith('../')) {
          targetPath = path.join(this.config.pkgDir, moduleDir);
        } else if (moduleDir.startsWith('/')) {
          targetPath = moduleDir;
        } else {
          targetPath = path.join(this.config.pkgDir, lock[moduleDir]);
        }
        const pkgPath = fs.existsSync(path.join(targetPath, 'Teafile')) ? path.join(targetPath, 'Teafile') : path.join(targetPath, 'Darafile');
        const importTeaFile = JSON.parse(fs.readFileSync(pkgPath));
        const goPkg = importTeaFile.releases && importTeaFile.releases.go;
        if (importTeaFile.go && importTeaFile.go.typedef) {
          this.importsTypedef[aliasId] = {};
          const moduleTypedef = importTeaFile.go && importTeaFile.go.typedef;
          Object.keys(moduleTypedef).forEach((types) => {
            if (!this.importsTypedef[aliasId][types]) {
              this.importsTypedef[aliasId][types] = {};
            }
            this.importsTypedef[aliasId][types].import = moduleTypedef[types].import;
            this.importsTypedef[aliasId][types].type = moduleTypedef[types].type;
          });
        }
        if (importTeaFile.go && importTeaFile.go.clientName) {
          if (importTeaFile.go.clientName.indexOf('*') === 0) {
            this.constructFunc[aliasId] = importTeaFile.go.clientName.substring(1);
            this.clientNameDefine(aliasId);
          } else {
            this.constructFunc[aliasId] = importTeaFile.go.clientName;
            this.clientNameDefine(aliasId, false);
          }
        } else {
          this.constructFunc[aliasId] = 'Client';
          this.clientNameDefine(aliasId);
        }
        if (!goPkg) {
          throw new Error(`The '${aliasId}' has no Go supported.`);
        }

        const [pkgName, version] = goPkg.split(':'); 
        this.module.importPackages[aliasId] = {
          path: pkgName.substring(0, pkgName.lastIndexOf('/')),
          pkgName: pkgName,
          version: version,
          name: aliasId,
          goPkg: importTeaFile.go
        };
        if(inner) {
          const mainPath = pkgName.substring(0, pkgName.lastIndexOf('/'));
          const innerPath = importTeaFile.exports[inner].replace(/(\.tea)$|(\.spec)$|(\.dara)$/gi, '');
          const pkgPath = path.join(mainPath, innerPath);
          this.module.importPackages[aliasId] = {
            pkgName: pkgPath,
            name: aliasId,
            inner: true,
          };
        }
      }
    }
  }