Skip to content
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

[dev-tool] Added support for deep imports in samples #18511

Merged
Changes from all commits
Commits
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
21 changes: 18 additions & 3 deletions common/tools/dev-tool/src/commands/samples/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function isDependency(moduleSpecifier: string): boolean {

// This seems like a reasonable test for "is a relative path" as long as
// absolute path imports are forbidden.
const isRelativePath = /^\.\.?\//.test(moduleSpecifier);
const isRelativePath = /^\.\.?[\/\\]/.test(moduleSpecifier);
return !isRelativePath;
}

Expand Down Expand Up @@ -280,6 +280,21 @@ async function collect<T>(i: AsyncIterableIterator<T>): Promise<T[]> {
return out;
}

/**
* Processes a segmented module path to return the first segment. This is useful for packages that have nested imports
* such as "dayjs/plugin/duration".
*
* @param specifier - the module specifier to resolve to a package name
* @returns a package name
*/
function resolveModule(specifier: string): string {
const parts = specifier.split("/", 2);

// The first part could be a namespace, in which case we need to join them
if (parts.length > 1 && parts[0].startsWith("@")) return parts[0] + "/" + parts[1];
else return parts[0];
}

/**
* Extracts the sample generation metainformation from the sample sources and
* configuration in package.json.
Expand Down Expand Up @@ -346,7 +361,7 @@ async function makeSampleGenerationInfo(
.slice(-1)[0]
.replace("\\", "/"),
// This'll be good enough most of the time, but products like Azure Form Recognizer will have
// too adjust using the sample configuration.
// to adjust using the sample configuration.
productName:
sampleConfiguration.productName ??
fail(`The sample configuration does not specify a "productName".`),
Expand Down Expand Up @@ -377,7 +392,7 @@ async function makeSampleGenerationInfo(
return {
dependencies: moduleInfos.reduce((prev, source) => {
const current: Record<string, string> = {};
for (const dependency of source.importedModules) {
for (const dependency of source.importedModules.map(resolveModule)) {
if (prev[dependency] === undefined) {
const dependencyVersion =
sampleConfiguration.dependencyOverrides?.[dependency] ??
Expand Down