async importScript()

in packages/costa/src/index.ts [122:172]


  async importScript<T>(script: PipcookScript): Promise<T> {
    const existEntry = this.entriesCache[script.path];
    if (existEntry) {
      return existEntry;
    }
    const scriptExports: any = await importFrom(script.path);
    let entry: any;
    if (typeof scriptExports === 'function') {
      entry = scriptExports;
    } else if (typeof scriptExports.default === 'function') {
      entry = scriptExports.default;
    }
    if (!entry && script.type === ScriptType.DataSource) {
      const { datasource } = scriptExports as any;
      if (typeof datasource === 'function') {
        entry = datasource;
      }
    } else if (!entry && script.type === ScriptType.Dataflow) {
      const { dataflow } = scriptExports as any;
      if (typeof dataflow === 'function') {
        entry = dataflow;
      }
    } else if (script.type === ScriptType.Model) {
      if (entry) {
        entry = {
          train: entry,
          predict: null
        };
      } else {
        const { model } = scriptExports as any;
        const tmpEntry = model ? model : scriptExports;
        if (typeof tmpEntry === 'function') {
          entry = {
            train: tmpEntry,
            predict: null
          };
        } else if (
          typeof tmpEntry === 'object'
          && typeof tmpEntry.train === 'function'
          && typeof tmpEntry.predict === 'function'
        ) {
          entry = tmpEntry;
        }
      }
    }
    if (!entry) {
      throw new TypeError(`no entry found in ${script.name}(${script.path})`);
    }
    this.entriesCache[script.path] = entry;
    return entry;
  }