Skip to content

Commit

Permalink
feat: use vscode settings for shell execution (#1134)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli authored Sep 21, 2021
1 parent 12a4515 commit fc4caea
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 113 deletions.
2 changes: 0 additions & 2 deletions libs/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export * from './lib/utils/read-projects';
export * from './lib/utils/read-generator-collections';
export {
fileExistsSync,
findClosestNg,
findClosestNx,
readAndParseJson,
readAndCacheJsonFile,
cacheJson,
Expand Down
57 changes: 8 additions & 49 deletions libs/server/src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { schema } from '@angular-devkit/core';
import { standardFormats } from '@angular-devkit/schematics/src/formats';
import { Option as CliOption } from '@angular/cli/models/interface';
import { parseJsonSchemaToOptions } from '@angular/cli/utilities/json-schema';
import type { WorkspaceJsonConfiguration } from '@nrwl/devkit';
import { existsSync, readdirSync, readFileSync, statSync } from 'fs';
import { platform } from 'os';
import * as path from 'path';
import {
Option,
XPrompt,
LongFormXPrompt,
ItemsWithEnum,
ItemTooltips,
LongFormXPrompt,
Option,
OptionItemLabelValue,
ItemsWithEnum,
XPrompt,
} from '@nx-console/schema';
import { Option as CliOption } from '@angular/cli/models/interface';
import { existsSync, readdirSync, readFileSync, statSync } from 'fs';
import {
parse as parseJson,
printParseErrorCode,
ParseError,
printParseErrorCode,
} from 'jsonc-parser';
import * as path from 'path';
import { getOutputChannel } from './output-channel';

export interface GeneratorDefaults {
Expand All @@ -39,46 +38,6 @@ const IMPORTANT_FIELD_NAMES = [
];
const IMPORTANT_FIELDS_SET = new Set(IMPORTANT_FIELD_NAMES);

export function findClosestNg(dir: string): string {
if (directoryExists(path.join(dir, 'node_modules'))) {
if (platform() === 'win32') {
if (fileExistsSync(path.join(dir, 'ng.cmd'))) {
return path.join(dir, 'ng.cmd');
} else {
return path.join(dir, 'node_modules', '.bin', 'ng.cmd');
}
} else {
if (fileExistsSync(path.join(dir, 'node_modules', '.bin', 'ng'))) {
return path.join(dir, 'node_modules', '.bin', 'ng');
} else {
return path.join(dir, 'node_modules', '@angular', 'cli', 'bin', 'ng');
}
}
} else {
return findClosestNg(path.dirname(dir));
}
}

export function findClosestNx(dir: string): string {
if (directoryExists(path.join(dir, 'node_modules'))) {
if (platform() === 'win32') {
if (fileExistsSync(path.join(dir, 'nx.cmd'))) {
return path.join(dir, 'nx.cmd');
} else {
return path.join(dir, 'node_modules', '.bin', 'nx.cmd');
}
} else {
if (fileExistsSync(path.join(dir, 'node_modules', '.bin', 'nx'))) {
return path.join(dir, 'node_modules', '.bin', 'nx');
} else {
return path.join(dir, 'node_modules', '@nrwl', 'cli', 'bin', 'nx.js');
}
}
} else {
return findClosestNx(path.dirname(dir));
}
}

export function listOfUnnestedNpmPackages(nodeModulesDir: string): string[] {
const res: string[] = [];
if (!existsSync(nodeModulesDir)) {
Expand Down
13 changes: 3 additions & 10 deletions libs/vscode/tasks/src/lib/cli-task.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { CliTaskDefinition } from './cli-task-definition';
import { join } from 'path';
import { Task, TaskGroup, TaskScope } from 'vscode';
import { CliTaskDefinition } from './cli-task-definition';
import { getShellExecutionForConfig } from './shell-execution';
import { findClosestNg, findClosestNx } from '@nx-console/server';
import { join } from 'path';

export class CliTask extends Task {
static create(
Expand All @@ -17,9 +16,6 @@ export class CliTask extends Task {
const args = getArgs(definition);

const useNxCli = workspaceJsonPath.endsWith('workspace.json');
const program = useNxCli
? findClosestNx(workspacePath)
: findClosestNg(workspacePath);

const displayCommand = useNxCli
? `nx ${args.join(' ')}`
Expand All @@ -29,14 +25,11 @@ export class CliTask extends Task {
{ ...definition, type: useNxCli ? 'nx' : 'ng' }, // definition
TaskScope.Workspace, // scope
displayCommand, // name
'nx-console', // source
useNxCli ? 'nx' : 'ng',
// execution
getShellExecutionForConfig({
displayCommand,
args,
cwd: workspacePath,
name: displayCommand,
program,
})
);

Expand Down
7 changes: 1 addition & 6 deletions libs/vscode/tasks/src/lib/nx-task.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Task, TaskScope } from 'vscode';
import { getShellExecutionForConfig } from './shell-execution';
import { findClosestNx } from '@nx-console/server';

export interface NxTaskDefinition {
positional?: string;
Expand All @@ -23,17 +22,13 @@ export class NxTask extends Task {
{ ...definition, type: 'nx' }, // definition
TaskScope.Workspace, // scope
displayCommand, // name
'nx-console', // source
'nx', // source
// execution
getShellExecutionForConfig({
displayCommand,
args,
cwd: workspacePath,
name: displayCommand,
program: findClosestNx(workspacePath),
})
);

return task;
}
}
54 changes: 8 additions & 46 deletions libs/vscode/tasks/src/lib/shell-execution.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,21 @@
import { detectPackageManager, getPackageManagerCommand } from '@nrwl/devkit';
import { ShellExecution } from 'vscode';
import { platform } from 'os';
import { execSync } from 'child_process';

export interface ShellConfig {
/** Human-readable string which will be used to represent the terminal in the UI. */
name: string;
program: string;
args: string[];
cwd: string;
displayCommand: string;
}

export function getShellExecutionForConfig(
config: ShellConfig
): ShellExecution {
let execution: ShellExecution;
if (platform() === 'win32') {
execution = getWin32ShellExecution(config);
} else {
execution = getUnixShellExecution(config);
}
const packageManager = detectPackageManager(config.cwd);
const packageManagerCommand = getPackageManagerCommand(packageManager);

return execution;
}

function getWin32ShellExecution(config: ShellConfig): ShellExecution {
return new ShellExecution(config.displayCommand, {
cwd: config.cwd,
executable:
'C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe',
shellArgs: [
`-Sta -NoLogo -NonInteractive -C "& {${config.program.replace(
/ /g,
'` ' // NOTE: In powershell ` is the escape key.
)} ${config.args.join(' ')}}"`,
],
});
}

let bashPath: string;
function getUnixShellExecution(config: ShellConfig): ShellExecution {
if (!bashPath) {
try {
bashPath = execSync('which bash').toString().trim() || '/bin/bash';
} catch {
bashPath = '/bin/bash'; // Default to where bash is usually installed.
return new ShellExecution(
`${packageManagerCommand.exec} ${config.displayCommand}`,
{
cwd: config.cwd,
}
}
return new ShellExecution(config.displayCommand, {
cwd: config.cwd,
executable: bashPath,
shellArgs: [
'-l',
'-c',
`${config.program.replace(/ /g, '\\ ')} ${config.args.join(' ')}`,
],
});
);
}

0 comments on commit fc4caea

Please sign in to comment.