๐Ÿ“ฆ Kong / insomnia

๐Ÿ“„ index.ts ยท 114 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114import { stat } from 'node:fs/promises';

import type { CaCertificate } from 'insomnia/src/models/ca-certificate';
import type { ClientCertificate } from 'insomnia/src/models/client-certificate';
import type { CloudProviderCredential } from 'insomnia/src/models/cloud-credential';
import type { CookieJar } from 'insomnia/src/models/cookie-jar';

import { logger } from '../logger';
import gitAdapter from './adapters/git-adapter';
import insomniaExportAdapter from './adapters/insomnia-adapter';
import neDbAdapter from './adapters/ne-db-adapter';
import type {
  ApiSpec,
  BaseModel,
  Environment,
  UnitTest,
  UnitTestSuite,
  Workspace,
  WorkspaceMeta,
} from './models/types';

export interface Database {
  ApiSpec: ApiSpec[];
  Environment: Environment[];
  Request: BaseModel[];
  RequestGroup: BaseModel[];
  Workspace: Workspace[];
  WorkspaceMeta: WorkspaceMeta[];
  UnitTestSuite: UnitTestSuite[];
  UnitTest: UnitTest[];
  ClientCertificate: ClientCertificate[];
  CaCertificate: CaCertificate[];
  CookieJar: CookieJar[];
  CloudCredential: CloudProviderCredential[];
}

export const emptyDb = (): Database => ({
  ApiSpec: [],
  Environment: [],
  Request: [],
  RequestGroup: [],
  Workspace: [],
  WorkspaceMeta: [],
  UnitTest: [],
  UnitTestSuite: [],
  ClientCertificate: [],
  CaCertificate: [],
  CookieJar: [],
  CloudCredential: [],
});

export type DbAdapter = (dir: string, filterTypes?: (keyof Database)[]) => Promise<Database | null>;

interface Options {
  pathToSearch: string;
  filterTypes?: (keyof Database)[];
}

export const isFile = async (path: string) => {
  try {
    return (await stat(path)).isFile();
  } catch {
    return false;
  }
};
export const loadDb = async ({ pathToSearch, filterTypes }: Options) => {
  // if path to file is provided try to it is an insomnia export file
  const isFilePath = await isFile(pathToSearch);
  if (isFilePath) {
    const exportDb = await insomniaExportAdapter(pathToSearch, filterTypes);
    if (exportDb) {
      logger.debug(`Data store configured from Insomnia export at \`${pathToSearch}\``);
      return exportDb;
    }
  }

  // try load from git
  const git = await gitAdapter(pathToSearch, filterTypes);
  git && logger.debug(`Data store configured from git repository at \`${pathToSearch}\``);
  if (git) {
    logger.debug(`Data store configured from git repository at \`${pathToSearch}\``);
    return git;
  }

  // try load from nedb
  const nedb = await neDbAdapter(pathToSearch, filterTypes);
  if (nedb) {
    logger.debug(`Data store configured from app data directory  at \`${pathToSearch}\``);
    return nedb;
  }

  logger.warn(
    `Error: No data source found at path "${pathToSearch}".
  TIP: Use "--workingDir/-w" to specify one of the following:
    - A Git repository root
    - An Insomnia export file
    - A directory containing Insomnia data
  
  Examples:
    1. Using a (legacy) Git repository:
       $ inso run collection --workingDir /path/to/git-repo
  
    2. Using an Insomnia export file or inside a Git project:
       $ inso run collection --workingDir /path/to/insomnia-file.yaml
  
    3. Using a directory with Insomnia app data:
       $ inso run collection --workingDir /path/to/insomnia-data
  
  Re-run with "--verbose" for more details.`,
  );

  return emptyDb();
};