Skip to content

Commit

Permalink
Pass the containing URL to importers under some circumstances (#246)
Browse files Browse the repository at this point in the history
Closes #3247
  • Loading branch information
nex3 authored Sep 18, 2023
1 parent ffa9947 commit 8e7a23a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
18 changes: 14 additions & 4 deletions lib/src/importer-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
register(
importer: Importer<sync> | FileImporter<sync>
): proto.InboundMessage_CompileRequest_Importer {
const response = new proto.InboundMessage_CompileRequest_Importer();
const message = new proto.InboundMessage_CompileRequest_Importer();
if ('canonicalize' in importer) {
if ('findFileUrl' in importer) {
throw new Error(
Expand All @@ -54,14 +54,18 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
);
}

response.importer = {case: 'importerId', value: this.id};
message.importer = {case: 'importerId', value: this.id};
message.nonCanonicalScheme =
typeof importer.nonCanonicalScheme === 'string'
? [importer.nonCanonicalScheme]
: importer.nonCanonicalScheme ?? [];
this.importersById.set(this.id, importer);
} else {
response.importer = {case: 'fileImporterId', value: this.id};
message.importer = {case: 'fileImporterId', value: this.id};
this.fileImportersById.set(this.id, importer);
}
this.id += 1;
return response;
return message;
}

/** Handles a canonicalization request. */
Expand All @@ -78,6 +82,9 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
return thenOr(
importer.canonicalize(request.url, {
fromImport: request.fromImport,
containingUrl: request.containingUrl
? new URL(request.containingUrl)
: null,
}),
url =>
new proto.InboundMessage_CanonicalizeResponse({
Expand Down Expand Up @@ -157,6 +164,9 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
return thenOr(
importer.findFileUrl(request.url, {
fromImport: request.fromImport,
containingUrl: request.containingUrl
? new URL(request.containingUrl)
: null,
}),
url => {
if (!url) return new proto.InboundMessage_FileImportResponse();
Expand Down
2 changes: 1 addition & 1 deletion tool/get-language-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function getLanguageRepo(
// Workaround for https://github.com/shelljs/shelljs/issues/198
// This file is a symlink which gets messed up by `shell.cp` (called from
// `utils.link`) on Windows.
shell.rm('build/sass/spec/README.md');
if (process.platform === 'win32') shell.rm('build/sass/spec/README.md');

await utils.link('build/sass/js-api-doc', p.join(outPath, 'sass'));

Expand Down
16 changes: 12 additions & 4 deletions tool/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import {promises as fs, existsSync} from 'fs';
import {promises as fs, existsSync, lstatSync} from 'fs';
import * as p from 'path';
import * as shell from 'shelljs';

Expand All @@ -17,13 +17,21 @@ export function fetchRepo(options: {
outPath: string;
ref: string;
}): void {
if (!existsSync(p.join(options.outPath, options.repo))) {
const path = p.join(options.outPath, options.repo);
if (lstatSync(path).isSymbolicLink() && existsSync(p.join(path, '.git'))) {
throw (
`${path} is a symlink to a git repo, not overwriting.\n` +
`Run "rm ${path}" and try again.`
);
}

if (!existsSync(path)) {
console.log(`Cloning ${options.repo} into ${options.outPath}.`);
shell.exec(
`git clone \
--depth=1 \
https://github.com/sass/${options.repo} \
${p.join(options.outPath, options.repo)}`,
${path}`,
{silent: true}
);
}
Expand All @@ -35,7 +43,7 @@ export function fetchRepo(options: {
`git fetch --depth=1 origin ${options.ref} && git reset --hard FETCH_HEAD`,
{
silent: true,
cwd: p.join(options.outPath, options.repo),
cwd: path,
}
);
}
Expand Down

0 comments on commit 8e7a23a

Please sign in to comment.