Skip to content

Commit

Permalink
refactor(nx): change workspace-schematic output format to be consiste…
Browse files Browse the repository at this point in the history
…nt with Angular CLI

Any schematic that modifies the Virtual Tree using the `workspace-schematic` command will output
changes to the Virtual Tree in a consistent format with the Angular CLI `generate` command.
  • Loading branch information
Coly010 authored and Doginal committed Oct 30, 2019
1 parent d14e6f6 commit 074996a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
10 changes: 5 additions & 5 deletions e2e/command-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ forEachCli(() => {
`npm run workspace-schematic ${custom} ${workspace} -- --no-interactive --directory=dir -d`
);
expect(exists(`libs/dir/${workspace}/src/index.ts`)).toEqual(false);
expect(dryRunOutput).toContain(`update ${workspaceConfigName()}`);
expect(dryRunOutput).toContain('update nx.json');
expect(dryRunOutput).toContain(`UPDATE ${workspaceConfigName()}`);
expect(dryRunOutput).toContain('UPDATE nx.json');

const output = runCommand(
`npm run workspace-schematic ${custom} ${workspace} -- --no-interactive --directory=dir`
);
checkFilesExist(`libs/dir/${workspace}/src/index.ts`);
expect(output).toContain(`update ${workspaceConfigName()}`);
expect(output).toContain('update nx.json');
expect(output).toContain(`UPDATE ${workspaceConfigName()}`);
expect(output).toContain('UPDATE nx.json');

const another = uniq('another');
runCLI(`g workspace-schematic ${another} --no-interactive`);
Expand All @@ -220,7 +220,7 @@ forEachCli(() => {
const promptOutput = runCommand(
`npm run workspace-schematic ${custom} mylib2 --dry-run`
);
expect(promptOutput).toContain('update nx.json');
expect(promptOutput).toContain('UPDATE nx.json');
}, 1000000);

describe('dep-graph', () => {
Expand Down
46 changes: 41 additions & 5 deletions packages/workspace/src/command-line/workspace-schematic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
JsonObject,
logging,
normalize,
terminal,
tags,
schema,
virtualFs
} from '@angular-devkit/core';
Expand Down Expand Up @@ -219,37 +221,67 @@ async function executeSchematic(
);

let nothingDone = true;
let loggingQueue: string[] = [];
let hasError = false;

workflow.reporter.subscribe((event: any) => {
nothingDone = false;
const eventPath = event.path.startsWith('/')
? event.path.substr(1)
: event.path;
switch (event.kind) {
case 'error':
hasError = true;

const desc =
event.description == 'alreadyExist'
? 'already exists'
: 'does not exist.';
console.error(`error! ${eventPath} ${desc}.`);
logger.warn(`ERROR! ${eventPath} ${desc}.`);
break;
case 'update':
console.log(`update ${eventPath} (${event.content.length} bytes)`);
loggingQueue.push(
tags.oneLine`${terminal.white('UPDATE')} ${eventPath} (${
event.content.length
} bytes)`
);
break;
case 'create':
console.log(`create ${eventPath} (${event.content.length} bytes)`);
loggingQueue.push(
tags.oneLine`${terminal.green('CREATE')} ${eventPath} (${
event.content.length
} bytes)`
);
break;
case 'delete':
console.log(`delete ${eventPath}`);
loggingQueue.push(
tags.oneLine`${terminal.yellow('DELETE')} ${eventPath}`
);
break;
case 'rename':
const eventToPath = event.to.startsWith('/')
? event.to.substr(1)
: event.to;
console.log(`rename ${eventPath} => ${eventToPath}`);
loggingQueue.push(
tags.oneLine`${terminal.blue(
'RENAME'
)} ${eventPath} => ${eventToPath}`
);
break;
}
});

workflow.lifeCycle.subscribe(event => {
if (event.kind === 'workflow-end' || event.kind === 'post-tasks-start') {
if (!hasError) {
loggingQueue.forEach(log => logger.info(log));
}

loggingQueue = [];
hasError = false;
}
});

const args = options._.slice(1);
workflow.registry.addSmartDefaultProvider('argv', (schema: JsonObject) => {
if ('index' in schema) {
Expand Down Expand Up @@ -288,6 +320,10 @@ async function executeSchematic(
if (nothingDone) {
logger.info('Nothing to be done.');
}

if (options.dryRun) {
logger.warn(`\nNOTE: The "dryRun" flag means no changes were made.`);
}
} catch (err) {
if (err instanceof UnsuccessfulWorkflowExecution) {
// "See above" because we already printed the error.
Expand Down

0 comments on commit 074996a

Please sign in to comment.