Skip to content

Commit

Permalink
application-package: show warning if user set unknown target and expl…
Browse files Browse the repository at this point in the history
…icitly set it as browser

Fixes #7559

Signed-off-by: Maksim Ryzhikov <[email protected]>
  • Loading branch information
maksimr authored and akosyakov committed Apr 16, 2020
1 parent 05ea8fd commit 442e981
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
62 changes: 62 additions & 0 deletions dev-packages/application-package/src/application-package.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/********************************************************************************
* Copyright (C) 2020 Maksim Ryzhikov and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import * as assert from 'assert';
import * as temp from 'temp';
import * as fs from 'fs-extra';
import * as path from 'path';
import { ApplicationPackage } from './application-package';
import { ApplicationProps } from './application-props';
import * as sinon from 'sinon';

const track = temp.track();
const sandbox = sinon.createSandbox();

describe('application-package', function (): void {
after((): void => {
sandbox.restore();
track.cleanupSync();
});

it('should print warning if user set unknown target in package.json and use browser as a default value', function (): void {
const warn = sandbox.stub(console, 'warn');
const root = createProjectWithTarget('foo');
const applicationPackage = new ApplicationPackage({ projectPath: root });
assert.equal(applicationPackage.target, ApplicationProps.ApplicationTarget.browser);
assert.equal(warn.called, true);
});

it('should set target from package.json', function (): void {
const target = 'electron';
const root = createProjectWithTarget(target);
const applicationPackage = new ApplicationPackage({ projectPath: root });
assert.equal(applicationPackage.target, target);
});

it('should prefer target from passed options over target from package.json', function (): void {
const pckTarget = 'electron';
const optTarget = 'browser';
const root = createProjectWithTarget(pckTarget);
const applicationPackage = new ApplicationPackage({ projectPath: root, appTarget: optTarget });
assert.equal(applicationPackage.target, optTarget);
});

function createProjectWithTarget(target: string): string {
const root = track.mkdirSync('foo-project');
fs.writeFileSync(path.join(root, 'package.json'), `{"theia": {"target": "${target}"}}`);
return root;
}
});
10 changes: 8 additions & 2 deletions dev-packages/application-package/src/application-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ export class ApplicationPackage {
theia.target = this.options.appTarget;
}

if (theia.target && !(theia.target in ApplicationProps.ApplicationTarget)) {
const defaultTarget = ApplicationProps.ApplicationTarget.browser;
console.warn(`Unknown application target '${theia.target}', '${defaultTarget}' to be used instead`);
theia.target = defaultTarget;
}

return this._props = { ...ApplicationProps.DEFAULT, ...theia };
}

Expand Down Expand Up @@ -205,11 +211,11 @@ export class ApplicationPackage {
}

isBrowser(): boolean {
return this.target === 'browser';
return this.target === ApplicationProps.ApplicationTarget.browser;
}

isElectron(): boolean {
return this.target === 'electron';
return this.target === ApplicationProps.ApplicationTarget.electron;
}

ifBrowser<T>(value: T): T | undefined;
Expand Down
6 changes: 5 additions & 1 deletion dev-packages/application-package/src/application-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ export interface ApplicationProps extends NpmRegistryProps {
readonly generator: Readonly<{ config: GeneratorConfig }>;
}
export namespace ApplicationProps {
export enum ApplicationTarget {
browser = 'browser',
electron = 'electron'
};

export type Target = 'browser' | 'electron';
export type Target = keyof typeof ApplicationTarget;

export const DEFAULT: ApplicationProps = {
...NpmRegistryProps.DEFAULT,
Expand Down

0 comments on commit 442e981

Please sign in to comment.