-
Notifications
You must be signed in to change notification settings - Fork 367
/
compile.service.ts
93 lines (76 loc) · 3.45 KB
/
compile.service.ts
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
import fs from 'fs';
import * as tsNode from 'ts-node';
import glob from 'glob';
import chalk from 'chalk';
import path from 'path';
import { SyncConfigType } from '@nangohq/shared';
import configService from './config.service.js';
import { getNangoRootPath, printDebug } from '../utils.js';
import { TYPES_FILE_NAME } from '../constants.js';
import modelService from './model.service.js';
import parserService from './parser.service.js';
class CompileService {
public async run(debug = false, syncName?: string): Promise<boolean> {
const tsconfig = fs.readFileSync(`${getNangoRootPath()}/tsconfig.dev.json`, 'utf8');
const distDir = './dist';
if (!fs.existsSync(distDir)) {
if (debug) {
printDebug(`Creating ${distDir} directory`);
}
fs.mkdirSync(distDir);
}
if (!fs.existsSync(`./${TYPES_FILE_NAME}`)) {
if (debug) {
printDebug(`Creating ${TYPES_FILE_NAME} file`);
}
await modelService.createModelFile();
}
const compilerOptions = (JSON.parse(tsconfig) as { compilerOptions: Record<string, any> }).compilerOptions;
const compiler = tsNode.create({
skipProject: true, // when installed locally we don't want ts-node to pick up the package tsconfig.json file
compilerOptions
});
if (debug) {
printDebug(`Compiler options: ${JSON.stringify(compilerOptions, null, 2)}`);
}
const integrationFiles = syncName ? [`./${syncName}.ts`] : glob.sync(`./*.ts`);
let success = true;
const { success: loadSuccess, error, response: config } = await configService.load('', debug);
if (!loadSuccess || !config) {
console.log(chalk.red(error?.message));
throw new Error('Error loading config');
}
const modelNames = configService.getModelNames(config);
for (const filePath of integrationFiles) {
try {
const providerConfiguration = config.find((config) =>
[...config.syncs, ...config.actions].find((sync) => sync.name === path.basename(filePath, '.ts'))
);
if (!providerConfiguration) {
continue;
}
const syncConfig = [...(providerConfiguration?.syncs || []), ...(providerConfiguration?.actions || [])].find(
(sync) => sync.name === path.basename(filePath, '.ts')
);
const type = syncConfig?.type || SyncConfigType.SYNC;
if (!parserService.callsAreUsedCorrectly(filePath, type, modelNames)) {
if (syncName && filePath.includes(syncName)) {
success = false;
}
continue;
}
const result = compiler.compile(fs.readFileSync(filePath, 'utf8'), filePath);
const jsFilePath = filePath.replace(/\/[^/]*$/, `/dist/${path.basename(filePath.replace('.ts', '.js'))}`);
fs.writeFileSync(jsFilePath, result);
console.log(chalk.green(`Compiled "${filePath}" successfully`));
} catch (error) {
console.error(`Error compiling "${filePath}":`);
console.error(error);
success = false;
}
}
return success;
}
}
const compileService = new CompileService();
export default compileService;