Skip to content

Commit 30749da

Browse files
authored
fix(cli): correctly resolve file with fake extension (#3924)
## Changes Fixes https://linear.app/nango/issue/NAN-3090/cli-issue-when-detecting-lib - CLI: correctly resolve file with fake extension It was buggy when importing stuff like `schema.zod`. It was resolved to `schema.ts` instead of `schema.zod.ts`.
1 parent 5d78116 commit 30749da

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

packages/cli/lib/services/compile.service.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -170,26 +170,33 @@ function compileImportedFile({
170170

171171
for (const importedFile of importedFiles) {
172172
const importedFilePath = path.resolve(path.dirname(filePath), importedFile);
173-
const importedFilePathWithoutExtension = path.join(path.dirname(importedFilePath), path.basename(importedFilePath, path.extname(importedFilePath)));
174-
const importedFilePathWithExtension = importedFilePathWithoutExtension + '.ts';
173+
let importedFilePathResolved: string;
174+
if (importedFilePath.endsWith('.ts')) {
175+
importedFilePathResolved = importedFilePath;
176+
} else if (importedFilePath.endsWith('.js')) {
177+
importedFilePathResolved = `${path.join(path.dirname(importedFilePath), path.basename(importedFilePath, path.extname(importedFilePath)))}.ts`;
178+
} else {
179+
importedFilePathResolved = `${importedFilePath}.ts`;
180+
}
175181

176-
/// if it is a library import then we can skip it
177-
if (!fs.existsSync(importedFilePathWithExtension)) {
182+
if (!fs.existsSync(importedFilePathResolved)) {
178183
// if the library is not allowed then we should let the user know
179184
// that it is not allowed and won't work early on
180185
if (!ALLOWED_IMPORTS.includes(importedFile)) {
181186
console.log(chalk.red(`Importing libraries is not allowed. Please remove the import "${importedFile}" from "${path.basename(filePath)}"`));
182187
return false;
183188
}
189+
190+
// if it is a library import then we can skip it
184191
continue;
185192
}
186193

187194
// if the file is not in the nango-integrations directory
188195
// then we should not compile it
189196
// if the parts of the path are shorter than the current that means it is higher
190197
// than the nango-integrations directory
191-
if (importedFilePathWithExtension.split(path.sep).length < fullPath.split(path.sep).length) {
192-
const importedFileName = path.basename(importedFilePathWithExtension);
198+
if (importedFilePathResolved.split(path.sep).length < fullPath.split(path.sep).length) {
199+
const importedFileName = path.basename(importedFilePathResolved);
193200

194201
console.log(
195202
chalk.red(
@@ -199,14 +206,14 @@ function compileImportedFile({
199206
return false;
200207
}
201208

202-
if (importedFilePathWithExtension.includes('models.ts')) {
209+
if (importedFilePathResolved.includes('models.ts')) {
203210
continue;
204211
}
205212

206-
compiler.compile(fs.readFileSync(importedFilePathWithExtension, 'utf8'), importedFilePathWithExtension);
207-
console.log(chalk.green(`Compiled "${importedFilePathWithExtension}" successfully`));
213+
compiler.compile(fs.readFileSync(importedFilePathResolved, 'utf8'), importedFilePathResolved);
214+
console.log(chalk.green(`Compiled "${importedFilePathResolved}" successfully`));
208215

209-
finalResult = compileImportedFile({ fullPath, filePath: importedFilePathWithExtension, compiler, type, parsed });
216+
finalResult = compileImportedFile({ fullPath, filePath: importedFilePathResolved, compiler, type, parsed });
210217
}
211218

212219
return finalResult;

0 commit comments

Comments
 (0)