Skip to content

Commit 75c708c

Browse files
committed
[application-package] resolve modules in the application context
Signed-off-by: Anton Kosiakov <anton.kosyakov@typefox.io>
1 parent 8c1d924 commit 75c708c

File tree

7 files changed

+46
-19
lines changed

7 files changed

+46
-19
lines changed

dev-packages/application-package/src/application-package.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66
*/
77

8+
import * as fs from 'fs-extra';
89
import * as paths from 'path';
910
import { readJsonFile, writeJsonFile } from './json-file';
1011
import { NpmRegistry, NodePackage, PublishedNodePackage, sortByKey } from './npm-registry';
1112
import { Extension, ExtensionPackage, RawExtensionPackage } from './extension-package';
1213
import { ExtensionPackageCollector } from './extension-package-collector';
1314

1415
export type ApplicationLog = (message?: any, ...optionalParams: any[]) => void;
16+
export type ApplicationModuleLoader = (modulePath: string) => string;
1517
export type ApplicationPackageTarget = 'browser' | 'electron';
1618
export class ApplicationPackageOptions {
1719
readonly projectPath: string;
@@ -55,7 +57,10 @@ export class ApplicationPackage extends ApplicationPackageOptions {
5557
*/
5658
get extensionPackages(): ReadonlyArray<ExtensionPackage> {
5759
if (!this._extensionPackages) {
58-
const collector = new ExtensionPackageCollector(raw => this.newExtensionPackage(raw));
60+
const collector = new ExtensionPackageCollector(
61+
raw => this.newExtensionPackage(raw),
62+
this.loadModule
63+
);
5964
this._extensionPackages = collector.collect(this.pck);
6065
}
6166
return this._extensionPackages;
@@ -202,4 +207,19 @@ export class ApplicationPackage extends ApplicationPackageOptions {
202207
});
203208
}
204209

210+
protected _moduleLoader: undefined | ApplicationModuleLoader;
211+
get loadModule(): ApplicationModuleLoader {
212+
if (!this._moduleLoader) {
213+
const loaderPath = this.path('.application-module-loader.js');
214+
fs.writeFileSync(loaderPath, 'module.exports = modulePath => require.resolve(modulePath);');
215+
this._moduleLoader = require(loaderPath) as ApplicationModuleLoader;
216+
fs.removeSync(loaderPath);
217+
}
218+
return this._moduleLoader!;
219+
}
220+
221+
resolveModulePath(moduleName: string, ...segments: string[]): string {
222+
return paths.resolve(this.loadModule(moduleName + '/package.json'), '..', ...segments);
223+
}
224+
205225
}

dev-packages/application-package/src/extension-package-collector.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export class ExtensionPackageCollector {
1515
protected readonly visited = new Map<string, boolean>();
1616

1717
constructor(
18-
protected readonly extensionPackageFactory: (raw: PublishedNodePackage) => ExtensionPackage
18+
protected readonly extensionPackageFactory: (raw: PublishedNodePackage) => ExtensionPackage,
19+
protected readonly loadModule: (modulePath: string) => string
1920
) { }
2021

2122
collect(pck: NodePackage): ReadonlyArray<ExtensionPackage> {
@@ -40,7 +41,7 @@ export class ExtensionPackageCollector {
4041
}
4142
this.visited.set(name, true);
4243

43-
const packagePath = require.resolve(name + '/package.json');
44+
const packagePath = this.loadModule(name + '/package.json');
4445
const pck: NodePackage = readJsonFile(packagePath);
4546
if (RawExtensionPackage.is(pck)) {
4647
const version = pck.version;

dev-packages/application-package/src/generator/webpack-generator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class WebpackGenerator extends AbstractGenerator {
1919
}
2020

2121
protected resolve(moduleName: string, path: string): string {
22-
return this.pck.relative(paths.resolve(require.resolve(moduleName + '/package.json'), '../' + path)).split(paths.sep).join('/');
22+
return this.pck.resolveModulePath(moduleName, path).split(paths.sep).join('/');
2323
}
2424

2525
protected compileWebpackConfig(): string {

packages/extension-manager/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
testproject_temp
1+
*_test_temp

packages/extension-manager/src/node/application-project.spec.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import * as path from 'path';
9+
import * as temp from 'temp';
910
import * as fs from 'fs-extra';
1011
import * as assert from 'assert';
1112
import { DidStopInstallationParam } from "../common/extension-protocol";
@@ -16,8 +17,8 @@ process.on('unhandledRejection', (reason, promise) => {
1617
throw reason;
1718
});
1819

20+
let appProjectPath: string;
1921
let appProject: ApplicationProject;
20-
const appProjectPath = path.resolve(__dirname, '..', '..', 'test-resources', 'testproject_temp');
2122

2223
export async function assertInstallation(expectation: {
2324
installed?: string[],
@@ -51,8 +52,10 @@ describe("application-project", function () {
5152

5253
beforeEach(function () {
5354
this.timeout(50000);
54-
fs.removeSync(appProjectPath);
55-
fs.ensureDirSync(appProjectPath);
55+
appProjectPath = temp.mkdirSync({
56+
suffix: '_project_test_temp',
57+
dir: path.resolve(__dirname, '..', '..')
58+
});
5659
appProject = extensionNodeTestContainer({
5760
projectPath: appProjectPath,
5861
target: 'browser',

packages/extension-manager/src/node/node-extension-server.spec.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66
*/
77

8+
import * as temp from 'temp';
89
import * as path from 'path';
910
import * as fs from 'fs-extra';
1011
import * as assert from 'assert';
@@ -16,10 +17,9 @@ process.on('unhandledRejection', (reason, promise) => {
1617
throw reason;
1718
});
1819

20+
let appProjectPath: string;
1921
let appProject: ApplicationProject;
2022
let server: ExtensionServer;
21-
const testProjectPath = path.resolve(__dirname, '..', '..', 'test-resources', 'testproject');
22-
const appProjectPath = path.resolve(__dirname, '..', '..', 'test-resources', 'testproject_temp');
2323

2424
export function waitForDidChange(): Promise<void> {
2525
return new Promise(resolve => {
@@ -33,8 +33,18 @@ describe("node-extension-server", function () {
3333

3434
beforeEach(function () {
3535
this.timeout(50000);
36-
fs.removeSync(appProjectPath);
37-
fs.copySync(testProjectPath, appProjectPath);
36+
37+
appProjectPath = temp.mkdirSync({
38+
suffix: '_server_test_temp',
39+
dir: path.resolve(__dirname, '..', '..')
40+
});
41+
fs.writeJsonSync(path.resolve(appProjectPath, 'package.json'), {
42+
"dependencies": {
43+
"@theia/core": "0.1.0",
44+
"@theia/extension-manager": "0.1.0"
45+
}
46+
});
47+
3848
const container = extensionNodeTestContainer({
3949
projectPath: appProjectPath,
4050
target: 'browser',

packages/extension-manager/test-resources/testproject/package.json

-7
This file was deleted.

0 commit comments

Comments
 (0)