Skip to content

Commit

Permalink
output folder update (#192)
Browse files Browse the repository at this point in the history
* update the output folder

* Adding the output path parameter to the yeomanui constructor

* adding the outputPath paremter to the constructor

* updating tests for the yeomanui constructor change

* Update extension.ts

* Update yeomanui.ts

Co-authored-by: Stanislav Lvovsky <[email protected]>
  • Loading branch information
saggir and slavik-lvovsky authored Mar 22, 2020
1 parent b3a0723 commit d044def
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
4 changes: 2 additions & 2 deletions backend/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ export class YeomanUIPanel {
this.rpc = new RpcExtension(this.panel.webview);
const outputChannel: YouiLog = new OutputChannelLog();
const vscodeYouiEvents: YouiEvents = new VSCodeYouiEvents(this.rpc, this.panel);

this.yeomanui = new YeomanUI(this.rpc, vscodeYouiEvents, outputChannel, this.logger, YeomanUIPanel.genFilter);
const outputFolder = _.get(vscode, "workspace.workspaceFolders[0].uri.path");
this.yeomanui = new YeomanUI(this.rpc, vscodeYouiEvents, outputChannel, this.logger, YeomanUIPanel.genFilter, outputFolder);
this.yeomanui.registerCustomQuestionEventHandler("file-browser", "getFilePath", this.showOpenFileDialog.bind(this));
this.yeomanui.registerCustomQuestionEventHandler("folder-browser", "getPath", this.showOpenFolderDialog.bind(this));

Expand Down
25 changes: 20 additions & 5 deletions backend/src/yeomanui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { GeneratorType, GeneratorFilter } from "./filter";
import { IChildLogger } from "@vscode-logging/logger";
import {IPrompt} from "@sap-devx/yeoman-ui-types";


export interface IGeneratorChoice {
name: string;
prettyName: string;
Expand All @@ -41,7 +42,7 @@ export class YeomanUI {
"Some quick example text of the generator description. This is a long text so that the example will look good.";
private static YEOMAN_PNG = "yeoman.png";
private static isWin32 = (process.platform === 'win32');
private static CWD: string = path.join(os.homedir(), 'projects');
private static cwd: string = path.join(os.homedir(), 'projects');
private static NODE_MODULES = 'node_modules';

private static funcReplacer(key: any, value: any) {
Expand All @@ -60,7 +61,7 @@ export class YeomanUI {
private genFilter: GeneratorFilter;
private customQuestionEventHandlers: Map<string, Map<string, Function>>;

constructor(rpc: IRpc, youiEvents: YouiEvents, outputChannel: YouiLog, logger: IChildLogger, genFilter?: GeneratorFilter) {
constructor(rpc: IRpc, youiEvents: YouiEvents, outputChannel: YouiLog, logger: IChildLogger, genFilter?: GeneratorFilter, outputPath?: string) {
this.rpc = rpc;
if (!this.rpc) {
throw new Error("rpc must be set");
Expand All @@ -82,6 +83,7 @@ export class YeomanUI {
this.currentQuestions = {};
this.setGenFilter(genFilter);
this.customQuestionEventHandlers = new Map();
this.setCwd(outputPath);
}

public registerCustomQuestionEventHandler(questionType: string, methodName: string, handler: Function): void {
Expand Down Expand Up @@ -131,7 +133,7 @@ export class YeomanUI {
// see issue: https://github.com/yeoman/environment/issues/55
// process.chdir() doesn't work after environment has been created
try {
await fsextra.mkdirs(YeomanUI.CWD);
await fsextra.mkdirs(YeomanUI.cwd);
const env: Environment = Environment.createEnv(undefined, {}, this.youiAdapter);
const meta: Environment.GeneratorMeta = this.getGenMetadata(generatorName);
// TODO: support sub-generators
Expand All @@ -149,7 +151,7 @@ export class YeomanUI {

this.promptCount = 0;
this.gen = (gen as Generator);
this.gen.destinationRoot(YeomanUI.CWD);
this.gen.destinationRoot(YeomanUI.cwd);
/* Generator.run() returns promise. Sending a callback is deprecated:
https://yeoman.github.io/generator/Generator.html#run
... but .d.ts hasn't been updated for a while:
Expand Down Expand Up @@ -255,7 +257,7 @@ export class YeomanUI {
// Start with the local paths derived by cwd in vscode
// (as opposed to cwd of the plugin host process which is what is used by yeoman/environment)
// Walk up the CWD and add `node_modules/` folder lookup on each level
const parts: string[] = YeomanUI.CWD.split(path.sep);
const parts: string[] = YeomanUI.cwd.split(path.sep);
const localPaths = _.map(parts, (part, index) => {
const resrpath = path.join(...parts.slice(0, index + 1), YeomanUI.NODE_MODULES);
return YeomanUI.isWin32 ? resrpath : path.join(path.sep, resrpath);
Expand Down Expand Up @@ -414,4 +416,17 @@ export class YeomanUI {
}
}
}

public setCwd(cwd: string){
if(cwd){
YeomanUI.cwd=cwd;
}
else{
YeomanUI.cwd=path.join(os.homedir(), 'projects');
}
}

public getCwd():string {
return YeomanUI.cwd;
}
}
18 changes: 17 additions & 1 deletion backend/tests/yeomanui.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { YouiEvents } from '../src/youi-events';
import { IMethod, IPromiseCallbacks, IRpc } from "@sap-devx/webview-rpc/out.ext/rpc-common";
import { GeneratorType, GeneratorFilter } from "../src/filter";
import { IChildLogger } from "@vscode-logging/logger";
import * as os from "os";

describe('yeomanui unit test', () => {
let sandbox: any;
Expand Down Expand Up @@ -450,6 +451,21 @@ describe('yeomanui unit test', () => {
expect(res).to.be.undefined;
});

it("setCwd", () => {
const yeomanUiInstance: YeomanUI = new YeomanUI(rpc, youiEvents, logger, testLogger, undefined, "testpathbefore");
expect(yeomanUiInstance.getCwd()).equal("testpathbefore");
const res = yeomanUiInstance.setCwd("testpathafter");
// tslint:disable-next-line: no-unused-expression
expect(res).to.be.undefined;
expect(yeomanUiInstance.getCwd()).equal("testpathafter");
});

it("defaultOutputPath", () => {
const yeomanUiInstance: YeomanUI = new YeomanUI(rpc, youiEvents, logger, testLogger);
const projectsPath = path.join(os.homedir(), 'projects');
expect(yeomanUiInstance.getCwd()).equal(projectsPath);
});

it("getErrorInfo", () => {
const yeomanUiInstance: YeomanUI = new YeomanUI(rpc, youiEvents, logger, testLogger);
const errorInfo: string = "Error Info";
Expand Down Expand Up @@ -498,7 +514,7 @@ describe('yeomanui unit test', () => {
};

beforeEach(() => {
YeomanUI["CWD"] = path.join("root/project/folder");
YeomanUI["cwd"] = path.join("root/project/folder");
yeomanEnvMock.expects("createEnv").returns(testEnv);
});

Expand Down

0 comments on commit d044def

Please sign in to comment.