Skip to content

Commit

Permalink
feat(core): set path property based on cwd when running generators
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Dec 30, 2020
1 parent 67fde2d commit 1cb152e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
4 changes: 3 additions & 1 deletion packages/tao/src/commands/generate.ts
Expand Up @@ -176,6 +176,7 @@ export async function taoNew(cwd: string, args: string[], isVerbose = false) {
null,
schema,
opts.interactive,
null,
null
);
return (await import('./ngcli-adapter')).invokeNew(
Expand Down Expand Up @@ -222,7 +223,8 @@ export async function generate(
workspaceDefinition,
schema,
opts.interactive,
ws.calculateDefaultProjectName(cwd, workspaceDefinition)
ws.calculateDefaultProjectName(cwd, workspaceDefinition),
ws.relativeCwd(cwd)
);

if (ws.isNxGenerator(opts.collectionName, normalizedGeneratorName)) {
Expand Down
3 changes: 2 additions & 1 deletion packages/tao/src/commands/run.ts
Expand Up @@ -157,7 +157,8 @@ export async function run(
opts.configuration,
target,
schema,
defaultProjectName
defaultProjectName,
ws.relativeCwd(cwd)
);
if (opts.help) {
printRunHelp(opts, schema);
Expand Down
25 changes: 24 additions & 1 deletion packages/tao/src/shared/params.spec.ts
Expand Up @@ -347,6 +347,7 @@ describe('params', () => {
},
},
['argv-value'],
null,
null
);

Expand All @@ -368,11 +369,33 @@ describe('params', () => {
},
},
[],
'myProject'
'myProject',
null
);

expect(params).toEqual({ a: 'myProject' });
});

it('should use relativeCwd to set path', () => {
const params = {};
convertSmartDefaultsIntoNamedParams(
params,
{
properties: {
a: {
type: 'string',
format: 'path',
visible: false,
},
},
},
[],
null,
'./somepath'
);

expect(params).toEqual({ a: './somepath' });
});
});

describe('validateOptsAgainstSchema', () => {
Expand Down
24 changes: 19 additions & 5 deletions packages/tao/src/shared/params.ts
Expand Up @@ -10,6 +10,8 @@ type PropertyDescription = {
items?: any;
alias?: string;
description?: string;
format?: string;
visible?: boolean;
default?: string | number | boolean | string[];
$ref?: string;
$default?: { $source: 'argv'; index: number } | { $source: 'projectName' };
Expand Down Expand Up @@ -324,7 +326,8 @@ export function combineOptionsForExecutor(
config: string,
target: TargetConfiguration,
schema: Schema,
defaultProjectName: string | null
defaultProjectName: string | null,
relativeCwd: string | null
) {
const r = convertAliases(
coerceTypesInOptions(commandLineOpts, schema),
Expand All @@ -338,7 +341,8 @@ export function combineOptionsForExecutor(
combined,
schema,
(commandLineOpts['_'] as string[]) || [],
defaultProjectName
defaultProjectName,
relativeCwd
);
setDefaults(combined, schema);
validateOptsAgainstSchema(combined, schema);
Expand All @@ -352,7 +356,8 @@ export async function combineOptionsForGenerator(
wc: WorkspaceConfiguration | null,
schema: Schema,
isInteractive: boolean,
defaultProjectName: string | null
defaultProjectName: string | null,
relativeCwd: string | null
) {
const generatorDefaults = wc
? getGeneratorDefaults(
Expand All @@ -371,7 +376,8 @@ export async function combineOptionsForGenerator(
combined,
schema,
(commandLineOpts['_'] as string[]) || [],
defaultProjectName
defaultProjectName,
relativeCwd
);

if (isInteractive && isTTY()) {
Expand All @@ -388,7 +394,8 @@ export function convertSmartDefaultsIntoNamedParams(
opts: { [k: string]: any },
schema: Schema,
argv: string[],
defaultProjectName: string | null
defaultProjectName: string | null,
relativeCwd: string | null
) {
Object.entries(schema.properties).forEach(([k, v]) => {
if (
Expand All @@ -405,6 +412,13 @@ export function convertSmartDefaultsIntoNamedParams(
defaultProjectName
) {
opts[k] = defaultProjectName;
} else if (
opts[k] === undefined &&
v.format === 'path' &&
v.visible === false &&
relativeCwd
) {
opts[k] = relativeCwd;
}
});
delete opts['_'];
Expand Down
12 changes: 10 additions & 2 deletions packages/tao/src/shared/workspace.ts
Expand Up @@ -127,12 +127,20 @@ export function workspaceConfigName(root: string) {
export class Workspaces {
constructor(private root: string) {}

calculateDefaultProjectName(cwd: string, wc: WorkspaceConfiguration) {
relativeCwd(cwd: string) {
let relativeCwd = cwd.split(this.root)[1];
if (relativeCwd) {
relativeCwd = relativeCwd.startsWith('/')
return relativeCwd.startsWith('/')
? relativeCwd.substring(1)
: relativeCwd;
} else {
return null;
}
}

calculateDefaultProjectName(cwd: string, wc: WorkspaceConfiguration) {
const relativeCwd = this.relativeCwd(cwd);
if (relativeCwd) {
const matchingProject = Object.keys(wc.projects).find((p) => {
const projectRoot = wc.projects[p].root;
return (
Expand Down

0 comments on commit 1cb152e

Please sign in to comment.