Skip to content

Commit

Permalink
Merge pull request #798 from OpenFn/better-compiler-exports
Browse files Browse the repository at this point in the history
compiler: don't extract common exports
  • Loading branch information
josephjclark authored Oct 22, 2024
2 parents 9186fa8 + 7fc53b4 commit 71edb8b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-parents-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openfn/compiler': minor
---

Be more accurate in calculating exports from an adaptor"
6 changes: 1 addition & 5 deletions packages/cli/src/compile/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ export const loadTransformOptions = async (
const path = await resolveSpecifierPath(adaptorInput, opts.repoDir, log);
if (path) {
try {
exports = await preloadAdaptorExports(
path,
opts.useAdaptorsMonorepo,
log
);
exports = await preloadAdaptorExports(path, log);
} catch (e) {
log.error(`Failed to load adaptor typedefs from path ${path}`);
log.error(e);
Expand Down
72 changes: 33 additions & 39 deletions packages/compiler/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'node:fs';
import { readFile, readdir } from 'node:fs/promises';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { Project, describeDts } from '@openfn/describe-package';
import type { Logger } from '@openfn/logger';
Expand All @@ -24,7 +24,6 @@ export const isRelativeSpecifier = (specifier: string) =>
// But we may relax this later.
export const preloadAdaptorExports = async (
pathToModule: string,
useMonorepo?: boolean,
log?: Logger
) => {
const project = new Project();
Expand All @@ -37,31 +36,11 @@ export const preloadAdaptorExports = async (
if (pkg.types) {
const functionDefs = {} as Record<string, true>;

// load common definitions into the project
if (pkg.name !== '@openfn/language-common') {
try {
const common = await findExports(
path.resolve(
pathToModule,
useMonorepo ? '../common' : '../language-common'
),
'types/index.d.ts',
project
);
if (common) {
common.forEach(({ name }) => {
functionDefs[name] = true;
});
}
} catch (e) {
log?.debug('Failed to load types from language common');
log?.debug(e);
}
}

const adaptor = await findExports(pathToModule, pkg.types, project);
adaptor.forEach(({ name }) => {
functionDefs[name] = true;
if (name !== 'default') {
functionDefs[name] = true;
}
});

return Object.keys(functionDefs);
Expand All @@ -82,20 +61,35 @@ const findExports = async (
types: string,
project: Project
) => {
const typesRoot = path.dirname(types);
const files = await readdir(`${moduleRoot}/${typesRoot}`);
const dtsFiles = files.filter((f) => f.endsWith('.d.ts'));
const result = [];
for (const f of dtsFiles) {
const relPath = `${typesRoot}/${f}`;
const contents = await readFile(`${moduleRoot}/${relPath}`, 'utf8');
project.createFile(contents, relPath);
const results = [];

const contents = await readFile(`${moduleRoot}/${types}`, 'utf8');
project.createFile(contents, types);

result.push(
...describeDts(project, relPath, {
includePrivate: true,
})
);
results.push(
...describeDts(project, types, {
includePrivate: true,
})
);

// Ensure that everything in adaptor.d.ts is exported
// This is kinda cheating but it's quite safe for the time being
const typesRoot = path.dirname(types);
for (const dts of ['adaptor', 'Adaptor']) {
try {
const adaptorPath = `${moduleRoot}/${typesRoot}/${dts}.d.ts`;
const contents = await readFile(adaptorPath, 'utf8');
project.createFile(contents, adaptorPath);
results.push(
...describeDts(project, adaptorPath, {
includePrivate: true,
})
);
break;
} catch (e) {
// no problem if this throws - likely the file doesn't exist
}
}
return result;

return results;
};
6 changes: 1 addition & 5 deletions packages/engine-multi/src/api/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ const compileJob = async (
for (const adaptor of adaptors) {
// TODO I probably don't want to log this stuff
const pathToAdaptor = await getModulePath(adaptor, repoDir, logger);
const exports = await preloadAdaptorExports(
pathToAdaptor!,
false,
logger
);
const exports = await preloadAdaptorExports(pathToAdaptor!, logger);
adaptorConfig.push({
name: stripVersionSpecifier(adaptor),
exports,
Expand Down

0 comments on commit 71edb8b

Please sign in to comment.