Created
March 15, 2020 20:40
-
-
Save wickstargazer/a084e60f6eb55311d1e7fc2a686a5819 to your computer and use it in GitHub Desktop.
Build affected libraries within nx workspace
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
import { createProjectGraph, ProjectType, ProjectGraphNode } from '@nrwl/workspace/src/core/project-graph'; | |
import { calculateFileChanges, readEnvironment } from '@nrwl/workspace/src/core/file-utils'; | |
import { output } from '@nrwl/workspace'; | |
import { filterAffected } from '@nrwl/workspace/src/core/affected-project-graph'; | |
import { parseFiles } from '@nrwl/workspace/src/command-line/shared'; | |
import { projectHasTargetAndConfiguration } from '@nrwl/workspace/src/utils/project-has-target-and-configuration' | |
import { runMany } from '@nrwl/workspace/src/command-line/run-many' | |
import { copySync, removeSync } from 'fs-extra'; | |
import * as yargs from 'yargs'; | |
import { runCommand } from '@nrwl/workspace/src/tasks-runner/run-command'; | |
import { DefaultReporter } from '@nrwl/workspace/src/tasks-runner/default-reporter'; | |
import { splitArgsIntoNxArgsAndOverrides } from '@nrwl/workspace/src/command-line/utils'; | |
import { exec, spawnSync } from 'child_process'; | |
import { from, of, interval } from 'rxjs'; | |
import { flatMap, map, takeWhile, last, skipWhile, take } from 'rxjs/operators'; | |
async function actionAffectedLib(target = 'build', configuration = null, libsToBuild: string = null) { | |
const projectGraph = createProjectGraph(); | |
const nxArgs = { | |
base: 'master', | |
} | |
const affectedGraph = filterAffected( | |
projectGraph, | |
calculateFileChanges(parseFiles(nxArgs).files, nxArgs) | |
); | |
const projects = affectedGraph.nodes; | |
const affectedProjects = Object.values(projects) | |
const projectWithTargetAndConfig = affectedProjects.filter(p => | |
projectHasTargetAndConfiguration(p, target, configuration) | |
); | |
const libs = projectWithTargetAndConfig | |
.filter(p => p.type === ProjectType.lib) | |
.map(p => p.name); | |
if (libs.length) { | |
// output.log({ | |
// title: 'Building affected apps:', | |
// bodyLines: libs.map(lib => `${output.colors.gray('-')} ${lib}`) | |
// }); | |
let buildingLibs: string[] = []; | |
if (libsToBuild) { | |
libs.forEach(lib => { | |
if (libsToBuild.indexOf(lib) > -1) { | |
buildingLibs.push(lib); | |
} | |
}) | |
} else { | |
buildingLibs = libs | |
} | |
const args = { _: [], projects: buildingLibs.join(','), target: target, '$0': '' }; | |
const argsToRun = splitArgsIntoNxArgsAndOverrides(args); | |
const projectNodes: ProjectGraphNode[] = []; | |
const projectMap: Record<string, ProjectGraphNode> = {}; | |
buildingLibs.forEach(proj => { | |
const project = projectWithTargetAndConfig.find(p => p.name === proj); | |
projectMap[proj] = project; | |
projectNodes.push(project) | |
}); | |
const env = readEnvironment(argsToRun.nxArgs.target, projectMap); | |
runCommand( | |
projectNodes, | |
projectGraph, | |
env, | |
argsToRun.nxArgs, | |
argsToRun.overrides, | |
new DefaultReporter() | |
) | |
interval(1000).pipe(skipWhile(() => { | |
if (env.workspaceResults.hasFailure) | |
return true; | |
return !buildingLibs.every(lib => { | |
return env.workspaceResults.getResult(lib) | |
}); | |
}), | |
take(1)).subscribe(() => { | |
buildingLibs.forEach(lib => { | |
const projectData = projectWithTargetAndConfig.find(p => p.name === lib); | |
const assets = projectData.data.files.filter(f => f.file.match(/.*(src\/assets)\/.*/s)); | |
if (assets.length > 0) { | |
const path = `dist/@flowaccount/${projectData.name}`; | |
assets.forEach(a => { | |
const assetFilePath = path + a.file.match(/(\/assets)\/.*/s)[0] | |
copySync(a.file, assetFilePath) | |
}) | |
} | |
}) | |
}) | |
} | |
} | |
(async () => { | |
await actionAffectedLib('build', null, process.argv[2]); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment