The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
#!/usr/bin/env node
/* eslint-env node */
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");
const glob = require("glob");
const SDK_DIST_DIR_PATH = path.resolve("./resources/embedding-sdk/dist");
const API_EXTRACTOR_BIN_PATH = path.resolve(
"./node_modules/@microsoft/api-extractor/bin/api-extractor",
);
/*
* This script replaces all custom aliases in Embedding SDK generated ".d.ts" files so that this imports could be resolved
* in the host app
* */
// this map should be synced with "tsconfig.sdk.json"
const REPLACES_MAP = {
"metabase-enterprise": "enterprise/frontend/src/metabase-enterprise",
"metabase-lib": "frontend/src/metabase-lib",
"metabase-types": "frontend/src/metabase-types",
metabase: "frontend/src/metabase",
"embedding-sdk-package": "enterprise/frontend/src/embedding-sdk-package",
"embedding-sdk-bundle": "frontend/src/embedding-sdk-bundle",
"embedding-sdk-shared": "frontend/src/embedding-sdk-shared",
"embedding-sdk-ee": "enterprise/frontend/src/embedding-sdk-ee",
cljs: "target/cljs_release",
};
const DTS_ROLLUPS = [
{
name: "main",
configPath: path.join(
__dirname,
"../../enterprise/frontend/src/embedding-sdk-package/embedding-sdk-api-extractor.json",
),
entryPointPath: path.join(
__dirname,
"../../resources/embedding-sdk/dist/enterprise/frontend/src/embedding-sdk-package/index.d.ts",
),
},
{
name: "data-app",
configPath: path.join(
__dirname,
"../../enterprise/frontend/src/embedding-sdk-package/embedding-sdk-data-app-api-extractor.json",
),
entryPointPath: path.join(
__dirname,
"../../resources/embedding-sdk/dist/enterprise/frontend/src/embedding-sdk-package/data-app.d.ts",
),
},
{
name: "testing",
configPath: path.join(
__dirname,
"../../enterprise/frontend/src/embedding-sdk-package/embedding-sdk-testing-api-extractor.json",
),
entryPointPath: path.join(
__dirname,
"../../resources/embedding-sdk/dist/enterprise/frontend/src/embedding-sdk-package/testing.d.ts",
),
},
{
name: "data-app-dev",
configPath: path.join(
__dirname,
"../../enterprise/frontend/src/embedding-sdk-package/embedding-sdk-data-app-dev-api-extractor.json",
),
entryPointPath: path.join(
__dirname,
"../../resources/embedding-sdk/dist/enterprise/frontend/src/embedding-sdk-package/data-app-dev.d.ts",
),
},
{
name: "data-app-dev-config",
configPath: path.join(
__dirname,
"../../enterprise/frontend/src/embedding-sdk-package/embedding-sdk-data-app-dev-config-api-extractor.json",
),
entryPointPath: path.join(
__dirname,
"../../resources/embedding-sdk/dist/enterprise/frontend/src/embedding-sdk-package/data-app-dev.config.d.ts",
),
},
];
const getLogger = (prefix) => {
const verbose = process.env.SDK_FIXUP_VERBOSE_LOGS === "true";
return {
verbose: (message) => verbose && console.log(`[${prefix}] ${message}`),
log: (message) => console.log(`[${prefix}] ${message}`),
error: (message) => console.error(`[${prefix}] ${message}`),
};
};
const { log, verbose } = getLogger("dts fixup");
const getRelativePath = (fromPath, toPath) => {
const relativePath = path.relative(path.dirname(fromPath), toPath);
return relativePath.startsWith(".") ? relativePath : "./" + relativePath;
};
const replaceAliasedImports = (filePath) => {
let fileContent = fs.readFileSync(filePath, { encoding: "utf8" });
Object.entries(REPLACES_MAP).forEach(([alias, targetPath]) => {
const relativePath = getRelativePath(
filePath,
path.join(SDK_DIST_DIR_PATH, targetPath),
);
fileContent = fileContent
.replaceAll(`from "${alias}/`, `from "${relativePath}/`)
.replaceAll(`from "${alias}"`, `from "${relativePath}"`)
.replaceAll(`import("${alias}`, `import("${relativePath}`)
.replace(
new RegExp(
`import\\("(\\.\\.\/)*(frontend\/src\/)*${alias.replace("/", "\\/")}`,
"gi",
),
`import("${relativePath}`,
);
});
fs.writeFileSync(filePath, fileContent, { encoding: "utf-8" });
verbose(`Edited file: ${filePath}`);
};
const removeUnresolvedReexports = (filePath) => {
if (!filePath.endsWith("index.d.ts")) {
return;
}
const fileContent = fs.readFileSync(filePath, { encoding: "utf8" });
const lines = fileContent.split(/\r?\n/);
let isModified = false;
const updatedContent = lines
.filter((line) => {
if (!line.includes("export * from")) {
return true;
}
const target = line.match(/export \* from "(.*)"/)[1];
const dirPath = path.dirname(filePath);
const isUnresolved =
!fs.existsSync(path.resolve(dirPath, target)) &&
!fs.existsSync(`${path.resolve(dirPath, target)}.d.ts`);
if (isUnresolved) {
verbose(`Removing unresolved re-export: ${line}`);
isModified = true;
}
return !isUnresolved;
})
.join("\n");
if (isModified) {
fs.writeFileSync(filePath, updatedContent, { encoding: "utf-8" });
}
};
const fixupTypesAfterCompilation = async ({ isWatchMode }) => {
log("Fixing SDK d.ts files...");
const dtsFilePaths = glob.sync(`${SDK_DIST_DIR_PATH}/**/*.d.ts`);
dtsFilePaths.forEach((filePath) => {
replaceAliasedImports(filePath);
removeUnresolvedReexports(filePath);
});
console.log("[dts fixup] Done!");
if (!isWatchMode) {
await generateDtsRollup();
}
};
const generateDtsRollup = async () => {
// Dts rollup logger
const { log, error } = getLogger("dts rollup");
log("Generate dts rollup...");
const existingEntryPointCount = DTS_ROLLUPS.filter(({ entryPointPath }) =>
fs.existsSync(path.resolve(entryPointPath)),
).length;
if (existingEntryPointCount === 0) {
log("It looks like dts rollup is already generated, skipping...");
return;
}
if (existingEntryPointCount !== DTS_ROLLUPS.length) {
DTS_ROLLUPS.forEach(({ entryPointPath, name }) => {
const resolvedEntryPointPath = path.resolve(entryPointPath);
if (!fs.existsSync(resolvedEntryPointPath)) {
error(
`Dts rollup entry point for ${name} does not exist: ${resolvedEntryPointPath}`,
);
}
});
process.exitCode = 1;
return;
}
let hasExtractorFailure = false;
// The extractors are independent (each reads the shared compiled d.ts tree
// and writes its own rollup), so run them concurrently. Output is buffered
// per extractor and printed on completion so parallel runs don't interleave.
const runApiExtractor = ({ configPath, name }) =>
new Promise((resolve) => {
const child = spawn(
process.execPath,
[
API_EXTRACTOR_BIN_PATH,
"run",
"--local",
"--verbose",
"--config",
path.resolve(configPath),
],
{ stdio: ["ignore", "pipe", "pipe"] },
);
let output = "";
child.stdout.on("data", (chunk) => (output += chunk));
child.stderr.on("data", (chunk) => (output += chunk));
child.on("close", (status, signal) =>
resolve({ name, status, signal, output }),
);
});
const results = await Promise.all(
DTS_ROLLUPS.filter(({ entryPointPath, name }) => {
if (fs.existsSync(path.resolve(entryPointPath))) {
return true;
}
error(
`Dts rollup entry point for ${name} does not exist: ${path.resolve(entryPointPath)}`,
);
hasExtractorFailure = true;
return false;
}).map(runApiExtractor),
);
for (const { name, status, signal, output } of results) {
log(`--- api-extractor: ${name} ---`);
process.stdout.write(output);
if (status !== 0 || signal) {
error(
`API Extractor failed for ${name}` +
(signal ? ` with signal ${signal}` : ""),
);
hasExtractorFailure = true;
}
}
if (hasExtractorFailure) {
process.exitCode = 1;
return;
}
log("API Extractor completed successfully");
log("Removing intermediate dts files...");
[
`${SDK_DIST_DIR_PATH}/enterprise`,
`${SDK_DIST_DIR_PATH}/frontend`,
`${SDK_DIST_DIR_PATH}/target`,
].forEach((path) => {
fs.rmSync(path, {
force: true,
recursive: true,
});
});
console.log("[dts rollup] Dts rollup done!");
};
const watchFilesAndFixThem = () => {
log("Watching for changes in the SDK d.ts files...");
// we need to keep track of the files that just edited
// as they trigger a file save event otherwise we'd end up in a loop
// NOTE: if this solution ends up being flaky for some reason, we could
// just check if the file includes "@metabase/embedding-sdk-react", if it does
// it means we can skip it
const dirty = new Map();
fs.watch(
SDK_DIST_DIR_PATH,
{ recursive: true },
async (eventType, filename) => {
if (filename && filename.endsWith(".d.ts")) {
if (dirty.get(filename)) {
return dirty.set(filename, false);
}
log(`File ${filename} changed, fixing the imports`);
dirty.set(filename, true);
replaceAliasedImports(path.resolve(SDK_DIST_DIR_PATH, filename));
}
},
);
};
const waitForFolder = async (folderPath) => {
while (!fs.existsSync(folderPath)) {
log(`Waiting for ${folderPath} to be created...`);
await new Promise((resolve) => setTimeout(resolve, 1000));
}
};
const isWatchMode = process.argv.includes("--watch");
const run = async () => {
// when running on a clean state and with --watch, the folder might not exist yet
await waitForFolder(SDK_DIST_DIR_PATH);
await fixupTypesAfterCompilation({ isWatchMode });
if (isWatchMode) {
console.log("\n\n\n");
watchFilesAndFixThem();
}
};
run();