Skip to content

Support explicit glint-environment package entrypoints #55

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 2 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
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
65 changes: 65 additions & 0 deletions packages/config/__tests__/environment.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from 'fs';
import os from 'os';
import { GlintEnvironment } from '../src';

describe('Environments', () => {
Expand Down Expand Up @@ -71,4 +73,67 @@ describe('Environments', () => {
expect(env.getPossibleScriptPaths('hello.hbs')).toEqual(['hello.ts', 'hello.js']);
});
});

describe('loading an environment', () => {
const testDir = `${os.tmpdir()}/glint-env-test-${process.pid}`;

beforeEach(() => {
fs.mkdirSync(testDir);
fs.writeFileSync(`${testDir}/package.json`, JSON.stringify({ name: 'test-pkg' }));
});

afterEach(() => {
fs.rmdirSync(testDir, { recursive: true });
});

test('loading an environment via @glint/environment-* shorthand', () => {
const envDir = `${testDir}/node_modules/@glint/environment-test-env`;

fs.mkdirSync(envDir, { recursive: true });
fs.writeFileSync(`${envDir}/env.js`, 'module.exports = () => ({ tags: "hello" });');
fs.writeFileSync(
`${envDir}/package.json`,
JSON.stringify({
name: '@glint/environment-test-env',
'glint-environment': 'env',
})
);

let env = GlintEnvironment.load('test-env', { rootDir: testDir });

expect(env.getConfiguredTemplateTags()).toEqual('hello');
});

test('loading an environment from some other package', () => {
const envDir = `${testDir}/node_modules/some-other-environment`;

fs.mkdirSync(envDir, { recursive: true });
fs.writeFileSync(`${envDir}/third-party-env.js`, 'module.exports = () => ({ tags: "hi" });');
fs.writeFileSync(
`${envDir}/package.json`,
JSON.stringify({
name: 'some-other-environment',
'glint-environment': 'third-party-env',
})
);

let env = GlintEnvironment.load('some-other-environment', { rootDir: testDir });

expect(env.getConfiguredTemplateTags()).toEqual('hi');
});

test('loading an environment from an explicit path', () => {
const envDir = `${testDir}/lib`;

fs.mkdirSync(envDir, { recursive: true });
fs.writeFileSync(
`${envDir}/my-internal-env.js`,
'module.exports = () => ({ tags: "internal" });'
);

let env = GlintEnvironment.load('./lib/my-internal-env', { rootDir: testDir });

expect(env.getConfiguredTemplateTags()).toEqual('internal');
});
});
});
32 changes: 24 additions & 8 deletions packages/config/src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import resolve from 'resolve';
import path from 'path';
import escapeStringRegexp from 'escape-string-regexp';

export type GlintEnvironmentConfig = {
Expand Down Expand Up @@ -90,17 +91,32 @@ export class GlintEnvironment {
}

function locateEnvironment(name: string, basedir: string): string {
// Resolve a package name, either shorthand or explicit
for (let candidate of [`@glint/environment-${name}`, name]) {
try {
return resolve.sync(candidate, { basedir });
} catch (error) {
if (error?.code === 'MODULE_NOT_FOUND') {
continue;
}

throw error;
let pkg = tryResolve(`${candidate}/package.json`, basedir);
if (pkg) {
let relativePath = require(pkg)['glint-environment'] ?? '.';
return path.resolve(path.dirname(pkg), relativePath);
}
}

// Resolve a path to an explicit file
let literalPath = tryResolve(name, basedir);
if (literalPath) {
return literalPath;
}

throw new Error(`Unable to resolve environment '${name}' from ${basedir}`);
}

function tryResolve(name: string, basedir: string): string | null {
try {
return resolve.sync(name, { basedir });
} catch (error) {
if (error?.code === 'MODULE_NOT_FOUND') {
return null;
}

throw error;
}
}
1 change: 1 addition & 0 deletions packages/environment-ember-loose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"author": "Dan Freeman (https://github.com/dfreeman)",
"main": "lib/index.js",
"glint-environment": "lib/environment.js",
"keywords": [
"glint-environment"
],
Expand Down
1 change: 1 addition & 0 deletions packages/environment-glimmerx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"author": "Dan Freeman (https://github.com/dfreeman)",
"main": "lib/index.js",
"glint-environment": "lib/environment.js",
"keywords": [
"glint-environment"
],
Expand Down