Skip to content

Commit

Permalink
minimal support for addon v2 (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart authored Nov 1, 2021
1 parent 0ede2ab commit 290be29
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 52 deletions.
46 changes: 46 additions & 0 deletions src/utils/layout-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface PackageInfo {
paths?: string[];
before?: string | string[];
after?: string | string[];
'app-js'?: string | Record<string, string>;
};
}

Expand Down Expand Up @@ -420,6 +421,51 @@ export async function getProjectAddonsInfo(root: string): Promise<void> {
listServices(localProject),
listModifiers(localProject),
]);
} else if (version === 2) {
const appExports = info?.['ember-addon']?.['app-js'] ?? {};

// https://github.com/embroider-build/embroider/blob/fe30c4c5a942e608c81082433940b530bfd6a7b2/SPEC.md#app-javascript
if (typeof appExports !== 'string') {
const localProject = new BaseProject(packagePath);

localProject['classicMatcher'].setIgnores([]);
localProject['podMatcher'].setIgnores([]);

const appExportedPaths = Object.keys(appExports);

appExportedPaths.forEach((el) => {
const entry = path.join(localProject.root, 'app', el);
const meta = localProject.matchPathToType(entry);

if (meta) {
const normalizedItem = normalizeMatchNaming(meta);

addToRegistry(normalizedItem.name, normalizedItem.type, [path.join(localProject.root, appExports[el])]);
}
});
} else {
const entry = path.join(packagePath, appExports);
const files = await safeWalkAsync(entry, {
directories: false,
globs: ['**/*.js', '**/*.ts', '**/*.hbs'],
});

const localProject = new BaseProject(entry);

localProject['classicMatcher'].setIgnores([]);
localProject['podMatcher'].setIgnores([]);

files.forEach((el) => {
const entry = path.join(localProject.root, el);
const meta = localProject.matchPathToType(entry);

if (meta) {
const normalizedItem = normalizeMatchNaming(meta);

addToRegistry(normalizedItem.name, normalizedItem.type, [entry]);
}
});
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/utils/path-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export class ClassicPathMatcher {
util: ['/utils/'],
};
ignores = ['/tmp/', '/dist/', '/.git/'];
setIgnores(ignores: string[]) {
this.ignores = ignores;
}
matchKey(key: string, str: string) {
const isIgnored = this.ignores.find((el) => str.includes(el));

Expand Down
61 changes: 9 additions & 52 deletions test/bultin-addons/core/intl-completion-provider-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as cp from 'child_process';
import { Readable, Writable } from 'stream';
import { createMessageConnection, Disposable, Logger, MessageConnection } from 'vscode-jsonrpc';
import { StreamMessageReader, StreamMessageWriter } from 'vscode-jsonrpc/node';
import { MessageConnection } from 'vscode-jsonrpc';
import { CompletionRequest } from 'vscode-languageserver-protocol';
import { asyncFSProvider, getResult, initServer, makeProject, registerCommandExecutor, startServer } from '../../test_helpers/integration-helpers';
import { createServer, ServerBucket, getResult, makeProject } from '../../test_helpers/public-integration-helpers';

const testCaseAsyncFsOptions = [false, true];
const translations = {
Expand All @@ -22,56 +19,16 @@ const translations = {

for (const asyncFsEnabled of testCaseAsyncFsOptions) {
describe(`Intl - async fs enabled: ${asyncFsEnabled.toString()}`, function () {
let connection: MessageConnection;
let serverProcess: cp.ChildProcess;
let asyncFSProviderInstance!: any;
const disposables: Disposable[] = [];
let instance!: ServerBucket;
let connection!: MessageConnection;

beforeAll(async () => {
serverProcess = startServer(asyncFsEnabled);
connection = createMessageConnection(
new StreamMessageReader(serverProcess.stdout as Readable),
new StreamMessageWriter(serverProcess.stdin as Writable),
<Logger>{
error(msg) {
console.log('error', msg);
},
log(msg) {
console.log('log', msg);
},
info(msg) {
console.log('info', msg);
},
warn(msg) {
console.log('warn', msg);
},
}
);
connection.listen();

if (asyncFsEnabled) {
asyncFSProviderInstance = asyncFSProvider();
disposables.push(await registerCommandExecutor(connection, asyncFSProviderInstance));
}

await initServer(connection, 'full-project');

await new Promise((resolve) => setTimeout(resolve, 1000));
instance = await createServer({ asyncFsEnabled: asyncFsEnabled });
connection = instance.connection;
});

afterAll(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));

for (const item of disposables) {
await item.dispose();
}

if (asyncFsEnabled) {
asyncFSProviderInstance = null;
}

await connection.dispose();
await serverProcess.kill();
await instance.destroy();
});

describe('empty autocomplete', () => {
Expand Down Expand Up @@ -378,7 +335,7 @@ for (const asyncFsEnabled of testCaseAsyncFsOptions) {
translations: {
'en-us.yaml': `rootFileTranslation: text 1`,
'sub-folder': {
'en-us.yaml': `subFolderTranslation:
'en-us.yaml': `subFolderTranslation:
subTranslation: text 2
anotherTranslation: another text
`,
Expand Down Expand Up @@ -426,7 +383,7 @@ for (const asyncFsEnabled of testCaseAsyncFsOptions) {
translations: {
'en-us.yaml': `rootFileTranslation: text 1`,
'sub-folder': {
'en-us.yaml': `subFolderTranslation:
'en-us.yaml': `subFolderTranslation:
subTranslation: text 2
anotherTranslation: another text
`,
Expand Down
177 changes: 177 additions & 0 deletions test/embroider-integration-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import { createServer, ServerBucket, makeProject, createProject } from './test_helpers/public-integration-helpers';

describe('it has minimal embroider v2 packages support', function () {
let instance!: ServerBucket;

describe('package.json [ember-addon][app-js]: object', () => {
beforeAll(async () => {
instance = await createServer({ asyncFsEnabled: false });
});

afterAll(async () => {
await instance.destroy();
});

it('able to resolve components with classic structure', async () => {
const projectStructure = makeProject(
{
app: {
components: {
'my-component.hbs': '',
},
},
},
{
'basic-v2-addon': {
'bundle/app/components': {
'foo.hbs': '',
'bar.hbs': '',
},
'package.json': JSON.stringify({
name: 'basic-v2-addon',
keywords: ['ember-addon'],
'ember-addon': {
version: 2,
type: 'addon',
main: 'addon-main.js',
'app-js': {
'./components/foo.hbs': './bundle/app/components/foo.hbs',
'./components/bar.hbs': './bundle/app/components/bar.hbs',
},
},
}),
},
}
);

const project = await createProject(projectStructure, instance.connection);

expect(project.result.addonsMeta.length).toBe(1);
expect(Object.keys(project.result.registry.component).length).toBe(3);
await project.destroy();
});

it('able to resolve components inside node_modules addon dist folder', async () => {
const projectStructure = makeProject(
{
app: {
components: {
'my-component.hbs': '',
},
},
},
{
'basic-v2-addon': {
'dist/app/components': {
'foo.hbs': '',
'bar.hbs': '',
},
'package.json': JSON.stringify({
name: 'basic-v2-addon',
keywords: ['ember-addon'],
'ember-addon': {
version: 2,
type: 'addon',
main: 'addon-main.js',
'app-js': {
'./components/foo.hbs': './dist/app/components/foo.hbs',
'./components/bar.hbs': './dist/app/components/bar.hbs',
},
},
}),
},
}
);

const project = await createProject(projectStructure, instance.connection);

expect(project.result.addonsMeta.length).toBe(1);
expect(Object.keys(project.result.registry.component).length).toBe(3);
await project.destroy();
});
});

describe('package.json [ember-addon][app-js]: string', () => {
beforeAll(async () => {
instance = await createServer({ asyncFsEnabled: false });
});

afterAll(async () => {
await instance.destroy();
});

it('able to resolve components with classic structure', async () => {
const projectStructure = makeProject(
{
app: {
components: {
'my-component.hbs': '',
},
},
},
{
'basic-v2-addon': {
'bundle/app/components': {
'foo.hbs': '',
'bar.hbs': '',
},
'package.json': JSON.stringify({
name: 'basic-v2-addon',
keywords: ['ember-addon'],
'ember-addon': {
version: 2,
type: 'addon',
main: 'addon-main.js',
'app-js': './bundle/app',
},
}),
},
}
);

const project = await createProject(projectStructure, instance.connection);

expect(project.result.addonsMeta.length).toBe(1);
expect(Object.keys(project.result.registry.component).length).toBe(3);

await project.destroy();
});

it('able to resolve components inside node_modules addon dist folder', async () => {
const projectStructure = makeProject(
{
app: {
components: {
'my-component.hbs': '',
},
},
},
{
'basic-v2-addon': {
'dist/app/components': {
'foo.hbs': '',
'bar.hbs': '',
},
'package.json': JSON.stringify({
name: 'basic-v2-addon',
keywords: ['ember-addon'],
'ember-addon': {
version: 2,
type: 'addon',
main: 'addon-main.js',
'app-js': './dist/app',
},
}),
},
}
);

const project = await createProject(projectStructure, instance.connection);

expect(project.result.addonsMeta.length).toBe(1);
expect(Object.keys(project.result.registry.component).length).toBe(3);

await project.destroy();
});
});
});
1 change: 1 addition & 0 deletions test/test_helpers/integration-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function startServer(asyncFs = false) {
export type UnknownResult = Record<string, unknown> & {
registry: IRegistry;
initIssues: string[];
addonsMeta: { name: string; root: string }[];
};

export type Registry = {
Expand Down
Loading

0 comments on commit 290be29

Please sign in to comment.