forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add a command to run tasks imperatively
- Loading branch information
Showing
10 changed files
with
539 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
checkFilesExist, | ||
cleanupProject, | ||
fileExists, | ||
isWindows, | ||
newProject, | ||
readFile, | ||
readJson, | ||
readProjectConfig, | ||
removeFile, | ||
runCLI, | ||
runCLIAsync, | ||
runCommand, | ||
tmpProjPath, | ||
uniq, | ||
updateFile, | ||
updateJson, | ||
updateProjectConfig, | ||
} from '@nrwl/e2e/utils'; | ||
import { PackageJson } from 'nx/src/utils/package-json'; | ||
import * as path from 'path'; | ||
|
||
describe('Invoke Runner', () => { | ||
let proj: string; | ||
beforeAll(() => (proj = newProject())); | ||
afterAll(() => cleanupProject()); | ||
|
||
it('should invoke runner imperatively ', async () => { | ||
const mylib = uniq('mylib'); | ||
runCLI(`generate @nrwl/workspace:lib ${mylib}`); | ||
updateProjectConfig(mylib, (c) => { | ||
c.targets['prebuild'] = { | ||
command: 'echo prebuild', | ||
}; | ||
c.targets['build'] = { | ||
command: 'echo build', | ||
}; | ||
return c; | ||
}); | ||
|
||
updateFile( | ||
'runner.js', | ||
` | ||
const { initTasksRunner } = require('nx/src/index'); | ||
async function main(){ | ||
const r = await initTasksRunner({}); | ||
await r.invoke([{id: '${mylib}:prebuild', target: {project: '${mylib}', target: 'prebuild'}, overrides: {__overrides_unparsed__: ''}}]); | ||
await r.invoke([{id: '${mylib}:build', target: {project: '${mylib}', target: 'build'}, overrides: {__overrides_unparsed__: ''}}]); | ||
} | ||
main().then(q => { | ||
console.log("DONE") | ||
process.exit(0) | ||
}) | ||
` | ||
); | ||
|
||
const q = runCommand('node runner.js'); | ||
expect(q).toContain(`Task ${mylib}:prebuild`); | ||
expect(q).toContain(`Task ${mylib}:build`); | ||
expect(q).toContain(`Successfully ran 1 tasks`); | ||
expect(q).toContain(`DONE`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { initTasksRunner } from './tasks-runner/init-tasks-runner'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { workspaceConfigurationCheck } from '../utils/workspace-configuration-check'; | ||
import { readNxJson } from '../config/configuration'; | ||
import { NxArgs } from '../utils/command-line-utils'; | ||
import { createProjectGraphAsync } from '../project-graph/project-graph'; | ||
import { Task, TaskGraph } from '../config/task-graph'; | ||
import { invokeTasksRunner } from './run-command'; | ||
import { InvokeRunnerTerminalOutputLifeCycle } from './life-cycles/invoke-runner-terminal-output-life-cycle'; | ||
import { performance } from 'perf_hooks'; | ||
|
||
export async function initTasksRunner(nxArgs: NxArgs) { | ||
performance.mark('init-local'); | ||
workspaceConfigurationCheck(); | ||
const nxJson = readNxJson(); | ||
if (nxArgs.verbose) { | ||
process.env.NX_VERBOSE_LOGGING = 'true'; | ||
} | ||
const projectGraph = await createProjectGraphAsync({ exitOnError: true }); | ||
return { | ||
invoke: async ( | ||
tasks: Task[] | ||
): Promise<{ status: number; taskGraph: TaskGraph }> => { | ||
performance.mark('command-execution-begins'); | ||
const lifeCycle = new InvokeRunnerTerminalOutputLifeCycle(tasks); | ||
|
||
const taskGraph = { | ||
roots: tasks.map((task) => task.id), | ||
tasks: tasks.reduce((acc, task) => { | ||
acc[task.id] = task; | ||
return acc; | ||
}, {} as any), | ||
dependencies: tasks.reduce((acc, task) => { | ||
acc[task.id] = []; | ||
return acc; | ||
}, {} as any), | ||
}; | ||
|
||
const status = await invokeTasksRunner({ | ||
tasks, | ||
projectGraph, | ||
taskGraph, | ||
lifeCycle, | ||
nxJson, | ||
nxArgs, | ||
loadDotEnvFiles: true, | ||
initiatingProject: null, | ||
}); | ||
|
||
return { | ||
status, | ||
taskGraph, | ||
}; | ||
}, | ||
}; | ||
} |
83 changes: 83 additions & 0 deletions
83
packages/nx/src/tasks-runner/life-cycles/invoke-runner-terminal-output-life-cycle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { output } from '../../utils/output'; | ||
import { TaskStatus } from '../tasks-runner'; | ||
import { getPrintableCommandArgsForTask } from '../utils'; | ||
import type { LifeCycle } from '../life-cycle'; | ||
import { Task } from '../../config/task-graph'; | ||
import { formatFlags, formatTargetsAndProjects } from './formatting-utils'; | ||
|
||
export class InvokeRunnerTerminalOutputLifeCycle implements LifeCycle { | ||
failedTasks = [] as Task[]; | ||
cachedTasks = [] as Task[]; | ||
|
||
constructor(private readonly tasks: Task[]) {} | ||
|
||
startCommand(): void { | ||
output.log({ | ||
color: 'cyan', | ||
title: `Running ${this.tasks.length} tasks:`, | ||
bodyLines: this.tasks.map( | ||
(task) => | ||
`- Task ${task.id} ${ | ||
task.overrides.__overrides_unparsed__ | ||
? `Overrides: ${task.overrides.__overrides_unparsed__}` | ||
: '' | ||
}` | ||
), | ||
}); | ||
|
||
output.addVerticalSeparatorWithoutNewLines('cyan'); | ||
} | ||
|
||
endCommand(): void { | ||
output.addNewline(); | ||
const taskIds = this.tasks.map((task) => { | ||
const cached = this.cachedTasks.indexOf(task) !== -1; | ||
const failed = this.failedTasks.indexOf(task) !== -1; | ||
return `- Task ${task.id} ${ | ||
task.overrides.__overrides_unparsed__ | ||
? `Overrides: ${task.overrides.__overrides_unparsed__}` | ||
: '' | ||
} ${cached ? 'CACHED' : ''} ${failed ? 'FAILED' : ''}`; | ||
}); | ||
if (this.failedTasks.length === 0) { | ||
output.addVerticalSeparatorWithoutNewLines('green'); | ||
output.success({ | ||
title: `Successfully ran ${this.tasks.length} tasks:`, | ||
bodyLines: taskIds, | ||
}); | ||
} else { | ||
output.addVerticalSeparatorWithoutNewLines('red'); | ||
output.error({ | ||
title: `Ran ${this.tasks.length} tasks:`, | ||
bodyLines: taskIds, | ||
}); | ||
} | ||
} | ||
|
||
endTasks( | ||
taskResults: { task: Task; status: TaskStatus; code: number }[] | ||
): void { | ||
for (let t of taskResults) { | ||
if (t.status === 'failure') { | ||
this.failedTasks.push(t.task); | ||
} else if (t.status === 'local-cache') { | ||
this.cachedTasks.push(t.task); | ||
} else if (t.status === 'local-cache-kept-existing') { | ||
this.cachedTasks.push(t.task); | ||
} else if (t.status === 'remote-cache') { | ||
this.cachedTasks.push(t.task); | ||
} | ||
} | ||
} | ||
|
||
printTaskTerminalOutput( | ||
task: Task, | ||
cacheStatus: TaskStatus, | ||
terminalOutput: string | ||
) { | ||
const args = getPrintableCommandArgsForTask(task); | ||
output.logCommand(args.join(' '), cacheStatus); | ||
output.addNewline(); | ||
process.stdout.write(terminalOutput); | ||
} | ||
} |
Oops, something went wrong.