Skip to content

feat(cli): add message when all files compile successfully in nango dev #3908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
c421e08
feat(cli): add message when all files compile successfully in `nango …
kaposke Apr 15, 2025
acfd2bd
Merge remote-tracking branch 'origin/master' into gui/NAN-2985/feat-s…
kaposke Apr 15, 2025
cb0b18f
fix remaining usages of `compileAllFiles`
kaposke Apr 15, 2025
aec281d
Merge remote-tracking branch 'origin/master' into gui/NAN-2985/feat-s…
kaposke Apr 15, 2025
d3c3464
fix new tests
kaposke Apr 15, 2025
b870c14
Add missing failedFiles push
kaposke Apr 16, 2025
ae2334f
Add missing `chalk.red`
kaposke Apr 16, 2025
3ea6d9e
refactor .then
kaposke Apr 16, 2025
d6370fa
Change success message
kaposke Apr 16, 2025
b4846a1
Merge branch 'master' into gui/NAN-2985/feat-successful-compilation-m…
kaposke Apr 16, 2025
6f97a66
Don't revalidate yaml file on single file compilation
kaposke Apr 22, 2025
5dc4254
Handle file deletion
kaposke Apr 22, 2025
042f8b5
Merge branch 'master' into gui/NAN-2985/feat-successful-compilation-m…
kaposke Apr 22, 2025
182b737
Merge remote-tracking branch 'origin/master' into gui/NAN-2985/feat-s…
kaposke Apr 23, 2025
8c638bd
fix deploy service usage of compilaAllFiles
kaposke Apr 23, 2025
664dddf
Don't consider skipped files are failed
kaposke Apr 23, 2025
1aebd12
Merge branch 'master' into gui/NAN-2985/feat-successful-compilation-m…
kaposke Apr 23, 2025
8f3a088
Merge branch 'master' into gui/NAN-2985/feat-successful-compilation-m…
kaposke Apr 24, 2025
fc60d91
Merge branch 'master' into gui/NAN-2985/feat-successful-compilation-m…
kaposke Apr 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"test:unit": "vitest",
"test:integration": "vitest --config ./vite.integration.config.ts",
"test:cli": "vitest --config ./vite.cli.config.ts",
"test:cli:update-snapshots": "vitest --config ./vite.cli.config.ts --update",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note you can just press u when running tests to update snapshots

"test:openapi": "npx @apidevtools/swagger-cli validate docs-v2/spec.yaml",
"test:providers": "npx tsx scripts/validation/providers/validate.ts",
"docs": "tsx scripts/docs-gen-snippets.ts && cd ./docs-v2 && npx mintlify dev --port 3033",
Expand Down
117 changes: 85 additions & 32 deletions packages/cli/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { getLayoutMode } from './utils/layoutMode.js';
import { getNangoRootPath, printDebug } from './utils.js';
import { NANGO_VERSION } from './version.js';

import type { NangoYamlParsed } from '@nangohq/types';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

Expand Down Expand Up @@ -127,14 +129,19 @@ export function generate({ fullPath, debug = false }: { fullPath: string; debug?
}
}

export function tscWatch({ fullPath, debug = false }: { fullPath: string; debug?: boolean }) {
const tsconfig = fs.readFileSync(path.resolve(getNangoRootPath(), 'tsconfig.dev.json'), 'utf8');
const parsed = loadYamlAndGenerate({ fullPath, debug });
if (!parsed) {
return;
function showCompilationMessage(failedFiles: Set<string>) {
if (failedFiles.size === 0) {
console.log(chalk.green('Compilation success! Watching files…'));
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure you need a function for that. Inlining the if would be good enough imho

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's a NIT, I prefer having it. I generally like to avoid duplicating user-facing messages. It should be updatable in a single place when needed.


const watchPath = ['./**/*.ts', `./${nangoConfigFile}`];
export function tscWatch({ fullPath, debug = false, watchConfigFile }: { fullPath: string; debug?: boolean; watchConfigFile: boolean }) {
const tsconfig = fs.readFileSync(path.resolve(getNangoRootPath(), 'tsconfig.dev.json'), 'utf8');

const watchPath = ['./**/*.ts'];
if (watchConfigFile) {
watchPath.push(`./${nangoConfigFile}`);
}

if (debug) {
printDebug(`Watching ${watchPath.join(', ')}`);
Expand All @@ -144,7 +151,7 @@ export function tscWatch({ fullPath, debug = false }: { fullPath: string; debug?
ignoreInitial: false,
ignored: (filePath: string) => {
const relativePath = path.relative(__dirname, filePath);
return relativePath.includes('node_modules') || path.basename(filePath) === TYPES_FILE_NAME;
return relativePath.includes('node_modules') || path.basename(filePath) === TYPES_FILE_NAME || relativePath.includes('.nango');
}
});

Expand All @@ -157,46 +164,92 @@ export function tscWatch({ fullPath, debug = false }: { fullPath: string; debug?
fs.mkdirSync(distDir);
}

watcher.on('add', async (filePath: string) => {
if (filePath === nangoConfigFile) {
return;
// First parsing of the config file
let parsed: NangoYamlParsed | null = loadYamlAndGenerate({ fullPath, debug });

const failedFiles = new Set<string>();

watcher.on('add', (filePath: string) => {
async function onAdd() {
if (debug) {
printDebug(`Added ${filePath}`);
}
if (filePath === nangoConfigFile || !parsed) {
return;
}
const success = await compileSingleFile({
fullPath,
file: getFileToCompile({ fullPath, filePath }),
tsconfig,
parsed,
debug
});
if (success) {
failedFiles.delete(filePath);
} else {
failedFiles.add(filePath);
}
showCompilationMessage(failedFiles);
}

void onAdd();
});

watcher.on('change', (filePath: string) => {
async function onChange() {
if (debug) {
printDebug(`Changed ${filePath}`);
}
if (filePath === nangoConfigFile) {
parsed = loadYamlAndGenerate({ fullPath, debug });

if (!parsed) {
return;
}

const { failedFiles: newFailedFiles } = await compileAllFiles({ fullPath, debug });
failedFiles.clear();
for (const file of newFailedFiles) {
failedFiles.add(file);
}
showCompilationMessage(failedFiles);
return;
}

if (!parsed) {
return;
}

const success = await compileSingleFile({ fullPath, file: getFileToCompile({ fullPath, filePath }), parsed, debug });
if (success) {
failedFiles.delete(filePath);
} else {
failedFiles.add(filePath);
}
showCompilationMessage(failedFiles);
}
await compileSingleFile({ fullPath, file: getFileToCompile({ fullPath, filePath }), tsconfig, parsed, debug });

void onChange();
});

watcher.on('unlink', (filePath: string) => {
if (filePath === nangoConfigFile) {
if (debug) {
printDebug(`Unlinked ${filePath}`);
}
if (filePath === nangoConfigFile || !parsed) {
return;
}
const providerConfiguration = getProviderConfigurationFromPath({ filePath, parsed });
const baseName = path.basename(filePath, '.ts');
const fileName = providerConfiguration ? `${baseName}-${providerConfiguration.providerConfigKey}.js` : `${baseName}.js`;
const jsFilePath = `./dist/${fileName}`;

failedFiles.delete(filePath);

try {
fs.unlinkSync(jsFilePath);
} catch {
console.log(chalk.red(`Error deleting ${jsFilePath}`));
}
});

watcher.on('change', async (filePath: string) => {
if (filePath === nangoConfigFile) {
await compileAllFiles({ fullPath, debug });
return;
}
await compileSingleFile({ fullPath, file: getFileToCompile({ fullPath, filePath }), parsed, debug });
});
}

export function configWatch({ fullPath, debug = false }: { fullPath: string; debug?: boolean }) {
const watchPath = path.join(fullPath, nangoConfigFile);
if (debug) {
printDebug(`Watching ${watchPath}`);
}
const watcher = chokidar.watch(watchPath, { ignoreInitial: true });

watcher.on('change', () => {
loadYamlAndGenerate({ fullPath, debug });
});
}
10 changes: 3 additions & 7 deletions packages/cli/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import figlet from 'figlet';

import { nangoConfigFile } from '@nangohq/nango-yaml';

import { configWatch, generate, getVersionOutput, tscWatch } from './cli.js';
import { generate, getVersionOutput, tscWatch } from './cli.js';
import { compileAllFiles } from './services/compile.service.js';
import { parse } from './services/config.service.js';
import deployService from './services/deploy.service.js';
Expand Down Expand Up @@ -168,11 +168,7 @@ program
const fullPath = process.cwd();
await verificationService.necessaryFilesExist({ fullPath, autoConfirm, debug, checkDist: false });

if (compileInterfaces) {
configWatch({ fullPath, debug });
}

tscWatch({ fullPath, debug });
tscWatch({ fullPath, debug, watchConfigFile: compileInterfaces });
});

program
Expand Down Expand Up @@ -267,7 +263,7 @@ program
return;
}

const success = await compileAllFiles({ fullPath, debug });
const { success } = await compileAllFiles({ fullPath, debug });
if (!success) {
console.log(chalk.red('Compilation was not fully successful. Please make sure all files compile before deploying'));
process.exitCode = 1;
Expand Down
21 changes: 15 additions & 6 deletions packages/cli/lib/services/compile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ function getCachedParser({ fullPath, debug }: { fullPath: string; debug: boolean
};
}

interface CompileAllFilesResult {
success: boolean;
failedFiles: string[];
}

export async function compileAllFiles({
debug,
fullPath,
Expand All @@ -45,7 +50,7 @@ export async function compileAllFiles({
scriptName?: string;
providerConfigKey?: string;
type?: ScriptFileType;
}): Promise<boolean> {
}): Promise<CompileAllFilesResult> {
const tsconfig = fs.readFileSync(path.join(getNangoRootPath(), 'tsconfig.dev.json'), 'utf8');

const distDir = path.join(fullPath, 'dist');
Expand All @@ -59,7 +64,7 @@ export async function compileAllFiles({
const cachedParser = getCachedParser({ fullPath, debug });
const parsed = cachedParser();
if (!parsed) {
return false;
return { success: false, failedFiles: [] };
}

const compilerOptions = (JSON.parse(tsconfig) as { compilerOptions: Record<string, any> }).compilerOptions;
Expand All @@ -80,20 +85,23 @@ export async function compileAllFiles({

const integrationFiles = listFilesToCompile({ scriptName, fullPath, scriptDirectory, parsed, debug, providerConfigKey });
let allSuccess = true;
const failedFiles: string[] = [];
const compilationErrors: string[] = [];

for (const file of integrationFiles) {
try {
const completed = await compile({ fullPath, file, compiler, debug, cachedParser });
if (completed === false) {
allSuccess = false;
failedFiles.push(file.inputPath);
compilationErrors.push(`Failed to compile ${file.inputPath}`);
continue;
}
} catch (err) {
console.log(chalk.red(`Error compiling "${file.inputPath}":`));
console.error(err);
allSuccess = false;
failedFiles.push(file.inputPath);
compilationErrors.push(`Error compiling ${file.inputPath}: ${err instanceof Error ? err.message : String(err)}`);
}
}
Expand All @@ -107,13 +115,14 @@ export async function compileAllFiles({
console.log(chalk.green('Successfully compiled all files present in the Nango YAML config file.'));
}

return allSuccess;
return { success: allSuccess, failedFiles };
}

export async function compileSingleFile({
fullPath,
file,
tsconfig,
parsed,
debug = false
}: {
fullPath: string;
Expand All @@ -124,7 +133,7 @@ export async function compileSingleFile({
}) {
const resolvedTsconfig = tsconfig ?? fs.readFileSync(path.join(getNangoRootPath(), 'tsconfig.dev.json'), 'utf8');

const cachedParser = getCachedParser({ fullPath, debug });
const cachedParser = parsed ? () => parsed : getCachedParser({ fullPath, debug });

try {
const compiler = tsNode.create({
Expand All @@ -140,9 +149,9 @@ export async function compileSingleFile({
debug
});

return result === true;
return result === true || result === null;
} catch (err) {
console.error(`Error compiling ${file.inputPath}:`);
console.error(chalk.red(`Error compiling ${file.inputPath}:`));
console.error(err);
return false;
}
Expand Down
16 changes: 10 additions & 6 deletions packages/cli/lib/services/deploy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class DeployService {
printDebug(`Environment is set to ${environmentName}`);
}

const successfulCompile = await compileAllFiles({ fullPath, debug });
const { success } = await compileAllFiles({ fullPath, debug });

if (!successfulCompile) {
if (!success) {
console.log(chalk.red('Compilation was not fully successful. Please make sure all files compile before deploying'));
process.exit(1);
}
Expand Down Expand Up @@ -199,14 +199,16 @@ class DeployService {
}
} else if (integrationIdMode) {
// Only compile files for the specified integration
successfulCompile = await compileAllFiles({
const { success } = await compileAllFiles({
fullPath,
debug,
providerConfigKey: integrationId!
});
successfulCompile = success;
} else {
// Compile all files
successfulCompile = await compileAllFiles({ fullPath, debug });
const { success } = await compileAllFiles({ fullPath, debug });
successfulCompile = success;
}

if (!successfulCompile) {
Expand Down Expand Up @@ -408,14 +410,16 @@ class DeployService {
let successfulCompile: boolean = false;
if (integration) {
// Only compile files for the specified integration
successfulCompile = await compileAllFiles({
const { success } = await compileAllFiles({
fullPath,
debug,
providerConfigKey: integration
});
successfulCompile = success;
} else {
// Compile all files
successfulCompile = await compileAllFiles({ fullPath, debug });
const { success } = await compileAllFiles({ fullPath, debug });
successfulCompile = success;
}

if (!successfulCompile) {
Expand Down
13 changes: 9 additions & 4 deletions packages/cli/lib/services/deploy.service.unit.cli-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import fs from 'node:fs';
import { describe, expect, it, beforeAll } from 'vitest';

import { beforeAll, describe, expect, it } from 'vitest';

import { compileAllFiles } from './compile.service';
import { parse } from './config.service.js';
import deployService from './deploy.service';
import { copyDirectoryAndContents, fixturesPath, getTestDirectory, removeVersion } from '../tests/helpers.js';
import { compileAllFiles } from './compile.service';

describe('package', () => {
let dir: string;
Expand All @@ -19,8 +21,11 @@ describe('package', () => {
await fs.promises.copyFile(`${fixturesPath}/nango-yaml/v2/nested-integrations/nango.yaml`, `${dir}/nango.yaml`);

// Compile only once
const success = await compileAllFiles({ fullPath: dir, debug: false });
expect(success).toBe(true);
const result = await compileAllFiles({ fullPath: dir, debug: false });
expect(result).toEqual({
success: true,
failedFiles: []
});
});

it('should package correctly', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/lib/services/dryrun.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ export class DryRunService {
type = 'on-events';
}

const result = await compileAllFiles({ fullPath: process.cwd(), debug, scriptName: syncName, providerConfigKey, type });
const { success } = await compileAllFiles({ fullPath: process.cwd(), debug, scriptName: syncName, providerConfigKey, type });

if (!result) {
if (!success) {
console.log(chalk.red('The sync/action did not compile successfully. Exiting'));
return;
}
Expand Down
Loading
Loading