Skip to content

Commit 463be21

Browse files
committed
fix(core): add a default provider setting project name
1 parent 00ece92 commit 463be21

6 files changed

Lines changed: 163 additions & 74 deletions

File tree

packages/tao/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export async function invokeCommand(
3333
case 'generate':
3434
case 'g':
3535
return (await import('./src/commands/generate')).generate(
36+
process.cwd(),
3637
root,
3738
commandArgs,
3839
isVerbose

packages/tao/src/commands/generate.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ export async function taoNew(cwd: string, args: string[], isVerbose = false) {
174174
normalizedGeneratorName,
175175
null,
176176
schema,
177-
opts.interactive
177+
opts.interactive,
178+
null
178179
);
179180
return (await import('./ngcli-adapter')).invokeNew(
180181
cwd,
@@ -188,6 +189,7 @@ export async function taoNew(cwd: string, args: string[], isVerbose = false) {
188189
}
189190

190191
export async function generate(
192+
cwd: string,
191193
root: string,
192194
args: string[],
193195
isVerbose = false
@@ -218,7 +220,8 @@ export async function generate(
218220
normalizedGeneratorName,
219221
workspaceDefinition,
220222
schema,
221-
opts.interactive
223+
opts.interactive,
224+
ws.calculateDefaultProjectName(cwd, workspaceDefinition)
222225
);
223226

224227
if (ws.isNxGenerator(opts.collectionName, normalizedGeneratorName)) {

packages/tao/src/commands/run.ts

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,10 @@ function throwInvalidInvocation() {
3030
);
3131
}
3232

33-
function calculateDefaultProjectName(
34-
cwd: string,
35-
root: string,
36-
wc: WorkspaceConfiguration
37-
) {
38-
let relativeCwd = cwd.split(root)[1];
39-
if (relativeCwd) {
40-
relativeCwd = relativeCwd.startsWith('/')
41-
? relativeCwd.substring(1)
42-
: relativeCwd;
43-
const matchingProject = Object.keys(wc.projects).find((p) => {
44-
const projectRoot = wc.projects[p].root;
45-
return (
46-
relativeCwd == projectRoot || relativeCwd.startsWith(`${projectRoot}/`)
47-
);
48-
});
49-
if (matchingProject) return matchingProject;
50-
}
51-
return wc.defaultProject;
52-
}
53-
5433
function parseRunOpts(
5534
cwd: string,
56-
root: string,
5735
args: string[],
58-
wc: WorkspaceConfiguration
36+
defaultProjectName: string | null
5937
): RunOptions {
6038
const runOptions = convertToCamelCase(
6139
minimist(args, {
@@ -70,7 +48,6 @@ function parseRunOpts(
7048
if (!runOptions._ || !runOptions._[0]) {
7149
throwInvalidInvocation();
7250
}
73-
const defaultProjectName = calculateDefaultProjectName(cwd, root, wc);
7451
// eslint-disable-next-line prefer-const
7552
let [project, target, configuration]: [
7653
string,
@@ -168,7 +145,8 @@ export async function run(
168145

169146
return handleErrors(isVerbose, async () => {
170147
const workspace = ws.readWorkspaceConfiguration();
171-
const opts = parseRunOpts(cwd, root, args, workspace);
148+
const defaultProjectName = ws.calculateDefaultProjectName(cwd, workspace);
149+
const opts = parseRunOpts(cwd, args, defaultProjectName);
172150
validateTargetAndConfiguration(workspace, opts);
173151

174152
const target = workspace.projects[opts.project].targets[opts.target];
@@ -178,7 +156,8 @@ export async function run(
178156
opts.runOptions,
179157
opts.configuration,
180158
target,
181-
schema
159+
schema,
160+
defaultProjectName
182161
);
183162
if (opts.help) {
184163
printRunHelp(opts, schema);

packages/tao/src/shared/params.spec.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ParsedArgs } from 'minimist';
22
import {
33
coerceTypesInOptions,
44
convertAliases,
5-
convertPositionParamsIntoNamedParams,
5+
convertSmartDefaultsIntoNamedParams,
66
convertToCamelCase,
77
lookupUnmatched,
88
Schema,
@@ -330,10 +330,10 @@ describe('params', () => {
330330
});
331331
});
332332

333-
describe('convertPositionParamsIntoNamedParams', () => {
334-
it('should set defaults from argv', () => {
333+
describe('convertSmartDefaultsIntoNamedParams', () => {
334+
it('should use argv', () => {
335335
const params = {};
336-
convertPositionParamsIntoNamedParams(
336+
convertSmartDefaultsIntoNamedParams(
337337
params,
338338
{
339339
properties: {
@@ -346,11 +346,33 @@ describe('params', () => {
346346
},
347347
},
348348
},
349-
['argv-value']
349+
['argv-value'],
350+
null
350351
);
351352

352353
expect(params).toEqual({ a: 'argv-value' });
353354
});
355+
356+
it('should use projectName', () => {
357+
const params = {};
358+
convertSmartDefaultsIntoNamedParams(
359+
params,
360+
{
361+
properties: {
362+
a: {
363+
type: 'string',
364+
$default: {
365+
$source: 'projectName',
366+
},
367+
},
368+
},
369+
},
370+
[],
371+
'myProject'
372+
);
373+
374+
expect(params).toEqual({ a: 'myProject' });
375+
});
354376
});
355377

356378
describe('validateOptsAgainstSchema', () => {

packages/tao/src/shared/params.ts

Lines changed: 80 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type PropertyDescription = {
1212
description?: string;
1313
default?: string | number | boolean | string[];
1414
$ref?: string;
15-
$default?: { $source: 'argv'; index: number };
15+
$default?: { $source: 'argv'; index: number } | { $source: 'projectName' };
1616
'x-prompt'?: string | { message: string; type: string; items: any[] };
1717
};
1818

@@ -261,6 +261,7 @@ function setPropertyDefault(
261261
if (schema.$ref) {
262262
schema = resolveDefinition(schema.$ref, definitions);
263263
}
264+
264265
if (schema.type !== 'object' && schema.type !== 'array') {
265266
if (opts[propName] === undefined && schema.default !== undefined) {
266267
opts[propName] = schema.default;
@@ -297,28 +298,12 @@ function resolveDefinition(ref: string, definitions: Properties) {
297298
return definitions[definition];
298299
}
299300

300-
export function convertPositionParamsIntoNamedParams(
301-
opts: { [k: string]: any },
302-
schema: Schema,
303-
argv: string[]
304-
) {
305-
Object.entries(schema.properties).forEach(([k, v]) => {
306-
if (
307-
opts[k] === undefined &&
308-
v.$default !== undefined &&
309-
argv[v.$default.index]
310-
) {
311-
opts[k] = coerceType(v, argv[v.$default.index]);
312-
}
313-
});
314-
delete opts['_'];
315-
}
316-
317301
export function combineOptionsForExecutor(
318302
commandLineOpts: Options,
319303
config: string,
320304
target: TargetConfiguration,
321-
schema: Schema
305+
schema: Schema,
306+
defaultProjectName: string | null
322307
) {
323308
const r = convertAliases(
324309
coerceTypesInOptions(commandLineOpts, schema),
@@ -328,10 +313,11 @@ export function combineOptionsForExecutor(
328313
const configOpts =
329314
config && target.configurations ? target.configurations[config] || {} : {};
330315
const combined = { ...target.options, ...configOpts, ...r };
331-
convertPositionParamsIntoNamedParams(
316+
convertSmartDefaultsIntoNamedParams(
332317
combined,
333318
schema,
334-
(commandLineOpts['_'] as string[]) || []
319+
(commandLineOpts['_'] as string[]) || [],
320+
defaultProjectName
335321
);
336322
setDefaults(combined, schema);
337323
validateOptsAgainstSchema(combined, schema);
@@ -342,50 +328,105 @@ export async function combineOptionsForGenerator(
342328
commandLineOpts: Options,
343329
collectionName: string,
344330
generatorName: string,
345-
ws: WorkspaceConfiguration | null,
331+
wc: WorkspaceConfiguration | null,
346332
schema: Schema,
347-
isInteractive: boolean
333+
isInteractive: boolean,
334+
defaultProjectName: string | null
348335
) {
349-
const generatorDefaults = ws
350-
? getGeneratorDefaults(ws, collectionName, generatorName)
336+
const generatorDefaults = wc
337+
? getGeneratorDefaults(
338+
defaultProjectName,
339+
wc,
340+
collectionName,
341+
generatorName
342+
)
351343
: {};
352344
let combined = convertAliases(
353345
coerceTypesInOptions({ ...generatorDefaults, ...commandLineOpts }, schema),
354346
schema,
355347
false
356348
);
357-
convertPositionParamsIntoNamedParams(
349+
convertSmartDefaultsIntoNamedParams(
358350
combined,
359351
schema,
360-
(commandLineOpts['_'] as string[]) || []
352+
(commandLineOpts['_'] as string[]) || [],
353+
defaultProjectName
361354
);
355+
362356
if (isInteractive && isTTY()) {
363357
combined = await promptForValues(combined, schema);
364358
}
359+
365360
setDefaults(combined, schema);
361+
366362
validateOptsAgainstSchema(combined, schema);
367363
return combined;
368364
}
369365

366+
export function convertSmartDefaultsIntoNamedParams(
367+
opts: { [k: string]: any },
368+
schema: Schema,
369+
argv: string[],
370+
defaultProjectName: string | null
371+
) {
372+
Object.entries(schema.properties).forEach(([k, v]) => {
373+
if (
374+
opts[k] === undefined &&
375+
v.$default !== undefined &&
376+
v.$default.$source === 'argv' &&
377+
argv[v.$default.index]
378+
) {
379+
opts[k] = coerceType(v, argv[v.$default.index]);
380+
} else if (
381+
opts[k] === undefined &&
382+
v.$default !== undefined &&
383+
v.$default.$source === 'projectName' &&
384+
defaultProjectName
385+
) {
386+
opts[k] = defaultProjectName;
387+
}
388+
});
389+
}
390+
370391
function getGeneratorDefaults(
371-
ws: WorkspaceConfiguration,
392+
projectName: string | null,
393+
wc: WorkspaceConfiguration,
372394
collectionName: string,
373395
generatorName: string
374396
) {
375-
if (!ws.generators) return {};
376-
377397
let defaults = {};
398+
if (wc.generators) {
399+
if (
400+
wc.generators[collectionName] &&
401+
wc.generators[collectionName][generatorName]
402+
) {
403+
defaults = {
404+
...defaults,
405+
...wc.generators[collectionName][generatorName],
406+
};
407+
}
408+
if (wc.generators[`${collectionName}:${generatorName}`]) {
409+
defaults = {
410+
...defaults,
411+
...wc.generators[`${collectionName}:${generatorName}`],
412+
};
413+
}
414+
}
378415
if (
379-
ws.generators[collectionName] &&
380-
ws.generators[collectionName][generatorName]
416+
projectName &&
417+
wc.projects[projectName] &&
418+
wc.projects[projectName].generators
381419
) {
382-
defaults = { ...defaults, ...ws.generators[collectionName][generatorName] };
383-
}
384-
if (ws.generators[`${collectionName}:${generatorName}`]) {
385-
defaults = {
386-
...defaults,
387-
...ws.generators[`${collectionName}:${generatorName}`],
388-
};
420+
const g = wc.projects[projectName].generators;
421+
if (g[collectionName] && g[collectionName][generatorName]) {
422+
defaults = { ...defaults, ...g[collectionName][generatorName] };
423+
}
424+
if (g[`${collectionName}:${generatorName}`]) {
425+
defaults = {
426+
...defaults,
427+
...g[`${collectionName}:${generatorName}`],
428+
};
429+
}
389430
}
390431
return defaults;
391432
}

0 commit comments

Comments
 (0)