diff --git a/packages/workspace/package.json b/packages/workspace/package.json index ea97ea3f142e3..5568f0e840e80 100644 --- a/packages/workspace/package.json +++ b/packages/workspace/package.json @@ -44,7 +44,6 @@ "dependencies": { "@angular-devkit/core": "8.0.1", "@angular-devkit/schematics": "8.0.1", - "app-root-path": "^2.0.1", "cosmiconfig": "4.0.0", "fs-extra": "6.0.0", "graphviz": "0.0.8", diff --git a/packages/workspace/src/command-line/deps-calculator.ts b/packages/workspace/src/command-line/deps-calculator.ts index e17b25b501e79..038d3b312fb73 100644 --- a/packages/workspace/src/command-line/deps-calculator.ts +++ b/packages/workspace/src/command-line/deps-calculator.ts @@ -1,6 +1,5 @@ import * as path from 'path'; import * as ts from 'typescript'; -import * as appRoot from 'app-root-path'; import { normalizedProjectRoot, @@ -17,6 +16,7 @@ import { writeToFile } from '../utils/fileutils'; import { stripSourceCode } from '../utils/strip-source-code'; +import { appRootPath } from '../utils/app-root'; export type DepGraph = { projects: ProjectNode[]; @@ -44,15 +44,15 @@ export function readDependencies( npmScope: string, projectNodes: ProjectNode[] ): Deps { - const nxDepsPath = `${appRoot.path}/dist/nxdeps.json`; + const nxDepsPath = `${appRootPath}/dist/nxdeps.json`; const m = lastModifiedAmongProjectFiles(projectNodes); - if (!directoryExists(`${appRoot.path}/dist`)) { - mkdirSync(`${appRoot.path}/dist`); + if (!directoryExists(`${appRootPath}/dist`)) { + mkdirSync(`${appRootPath}/dist`); } const existingDeps = fileExists(nxDepsPath) ? readJsonFile(nxDepsPath) : null; if (!existingDeps || m > mtime(nxDepsPath)) { return dependencies(npmScope, projectNodes, existingDeps, (f: string) => - readFileSync(`${appRoot.path}/${f}`, 'UTF-8') + readFileSync(`${appRootPath}/${f}`, 'UTF-8') ); } else { return existingDeps.dependencies; @@ -71,7 +71,7 @@ export function dependencies( existingDependencies: NxDepsJson | null, fileRead: (s: string) => string ): Deps { - const nxDepsPath = `${appRoot.path}/dist/nxdeps.json`; + const nxDepsPath = `${appRootPath}/dist/nxdeps.json`; const nxDepsExists = fileExists(nxDepsPath); const nxDepsMTime = nxDepsExists ? mtime(nxDepsPath) : -Infinity; const calculator = new DepsCalculator( diff --git a/packages/workspace/src/command-line/lint.ts b/packages/workspace/src/command-line/lint.ts index eb9ec9b6add49..b2f05e30c7b0c 100644 --- a/packages/workspace/src/command-line/lint.ts +++ b/packages/workspace/src/command-line/lint.ts @@ -5,8 +5,8 @@ import { readNxJson } from './shared'; import { WorkspaceIntegrityChecks } from './workspace-integrity-checks'; -import * as appRoot from 'app-root-path'; import * as path from 'path'; +import { appRootPath } from '../utils/app-root'; export function lint() { const nodes = getProjectNodes(readAngularJson(), readNxJson()); @@ -27,7 +27,7 @@ export function lint() { function readAllFilesFromAppsAndLibs() { return [ - ...allFilesInDir(`${appRoot.path}/apps`).map(f => f.file), - ...allFilesInDir(`${appRoot.path}/libs`).map(f => f.file) + ...allFilesInDir(`${appRootPath}/apps`).map(f => f.file), + ...allFilesInDir(`${appRootPath}/libs`).map(f => f.file) ].filter(f => !path.basename(f).startsWith('.')); } diff --git a/packages/workspace/src/command-line/shared.ts b/packages/workspace/src/command-line/shared.ts index 442823b9933f9..cd63380bf6543 100644 --- a/packages/workspace/src/command-line/shared.ts +++ b/packages/workspace/src/command-line/shared.ts @@ -10,11 +10,11 @@ import { affectedProjectNamesWithTarget } from './affected-apps'; import * as fs from 'fs'; -import * as appRoot from 'app-root-path'; import { readJsonFile } from '../utils/fileutils'; import { YargsAffectedOptions } from './affected'; import { readDependencies, DepGraph, Deps } from './deps-calculator'; import { touchedProjects } from './touched'; +import { appRootPath } from '../utils/app-root'; const ignore = require('ignore'); @@ -43,7 +43,7 @@ function readFileIfExisting(path: string) { } const ig = ignore(); -ig.add(readFileIfExisting(`${appRoot.path}/.gitignore`)); +ig.add(readFileIfExisting(`${appRootPath}/.gitignore`)); export function printArgsWarning(options: YargsAffectedOptions) { const { files, uncommitted, untracked, base, head, all } = options; @@ -320,7 +320,7 @@ export function getProjectNodes( implicitDependencies = [key.replace(/-e2e$/, '')]; } - const filesWithMTimes = allFilesInDir(`${appRoot.path}/${p.root}`); + const filesWithMTimes = allFilesInDir(`${appRootPath}/${p.root}`); const fileMTimes = {}; filesWithMTimes.forEach(f => { fileMTimes[f.file] = f.mtime; @@ -350,11 +350,11 @@ function minus(a: string[], b: string[]): string[] { } export function readAngularJson(): any { - return readJsonFile(`${appRoot.path}/angular.json`); + return readJsonFile(`${appRootPath}/angular.json`); } export function readNxJson(): NxJson { - const config = readJsonFile(`${appRoot.path}/nx.json`); + const config = readJsonFile(`${appRootPath}/nx.json`); if (!config.npmScope) { throw new Error(`nx.json must define the npmScope property.`); } @@ -423,7 +423,7 @@ export function allFilesInDir( dirName: string ): { file: string; mtime: number }[] { // Ignore .gitignored files - if (ig.ignores(path.relative(appRoot.path, dirName))) { + if (ig.ignores(path.relative(appRootPath, dirName))) { return []; } @@ -431,7 +431,7 @@ export function allFilesInDir( try { fs.readdirSync(dirName).forEach(c => { const child = path.join(dirName, c); - if (ig.ignores(path.relative(appRoot.path, child))) { + if (ig.ignores(path.relative(appRootPath, child))) { return; } try { @@ -439,7 +439,7 @@ export function allFilesInDir( if (!s.isDirectory()) { // add starting with "apps/myapp/..." or "libs/mylib/..." res.push({ - file: normalizePath(path.relative(appRoot.path, child)), + file: normalizePath(path.relative(appRootPath, child)), mtime: s.mtimeMs }); } else if (s.isDirectory()) { @@ -455,10 +455,10 @@ export function lastModifiedAmongProjectFiles(projects: ProjectNode[]) { return Math.max( ...[ ...projects.map(project => getProjectMTime(project)), - mtime(`${appRoot.path}/angular.json`), - mtime(`${appRoot.path}/nx.json`), - mtime(`${appRoot.path}/tslint.json`), - mtime(`${appRoot.path}/package.json`) + mtime(`${appRootPath}/angular.json`), + mtime(`${appRootPath}/nx.json`), + mtime(`${appRootPath}/tslint.json`), + mtime(`${appRootPath}/package.json`) ] ); } diff --git a/packages/workspace/src/command-line/workspace-schematic.ts b/packages/workspace/src/command-line/workspace-schematic.ts index 746c30e4ef102..15c264dd36986 100644 --- a/packages/workspace/src/command-line/workspace-schematic.ts +++ b/packages/workspace/src/command-line/workspace-schematic.ts @@ -14,7 +14,6 @@ import { NodeModulesEngineHost, NodeWorkflow } from '@angular-devkit/schematics/tools'; -import * as appRoot from 'app-root-path'; import { execSync } from 'child_process'; import * as fs from 'fs'; import { readFileSync, writeFileSync } from 'fs'; @@ -23,8 +22,9 @@ import * as inquirer from 'inquirer'; import * as path from 'path'; import * as yargsParser from 'yargs-parser'; import { fileExists } from '../utils/fileutils'; +import { appRootPath } from '../utils/app-root'; -const rootDirectory = appRoot.path; +const rootDirectory = appRootPath; export function workspaceSchematic(args: string[]) { const parsedArgs = parseOptions(args); diff --git a/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts b/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts index e0bee53a9815a..c80bbeafd1bd1 100644 --- a/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts +++ b/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts @@ -2,7 +2,6 @@ import * as path from 'path'; import * as Lint from 'tslint'; import { IOptions } from 'tslint'; import * as ts from 'typescript'; -import * as appRoot from 'app-root-path'; import { getProjectNodes, normalizedProjectRoot, @@ -15,6 +14,7 @@ import { DependencyType, readDependencies } from '../command-line/deps-calculator'; +import { appRootPath } from '../utils/app-root'; export class Rule extends Lint.Rules.AbstractRule { constructor( @@ -26,7 +26,7 @@ export class Rule extends Lint.Rules.AbstractRule { ) { super(options); if (!projectPath) { - this.projectPath = appRoot.path; + this.projectPath = appRootPath; if (!(global as any).projectNodes) { const angularJson = readAngularJson(); const nxJson = readNxJson(); diff --git a/packages/workspace/src/utils/app-root.ts b/packages/workspace/src/utils/app-root.ts new file mode 100644 index 0000000000000..e51f8677849ee --- /dev/null +++ b/packages/workspace/src/utils/app-root.ts @@ -0,0 +1,13 @@ +import { fileExists } from './fileutils'; +import * as path from 'path'; + +export const appRootPath = pathInner(__dirname); + +function pathInner(dir: string): string { + if (path.dirname(dir) === dir) return process.cwd(); + if (fileExists(path.join(dir, 'angular.json'))) { + return dir; + } else { + return pathInner(path.dirname(dir)); + } +} diff --git a/packages/workspace/src/utils/rules/format-files.ts b/packages/workspace/src/utils/rules/format-files.ts index 4e7e8e04d34ad..35c370ad895f2 100644 --- a/packages/workspace/src/utils/rules/format-files.ts +++ b/packages/workspace/src/utils/rules/format-files.ts @@ -1,15 +1,16 @@ import { - Tree, - SchematicContext, - Rule, + CreateFileAction, noop, OverwriteFileAction, - CreateFileAction + Rule, + SchematicContext, + Tree } from '@angular-devkit/schematics'; -import { format, resolveConfig, getFileInfo } from 'prettier'; -import * as appRoot from 'app-root-path'; -import { from, Observable } from 'rxjs'; -import { concatMap, delay, filter, map, mergeMap } from 'rxjs/operators'; +import { format, getFileInfo, resolveConfig } from 'prettier'; +import { from } from 'rxjs'; +import { filter, map, mergeMap } from 'rxjs/operators'; +import * as path from 'path'; +import { appRootPath } from '../app-root'; export function formatFiles( options: { skipFormat: boolean } = { skipFormat: false } @@ -32,7 +33,7 @@ export function formatFiles( return from(files).pipe( filter(file => host.exists(file.path)), mergeMap(async file => { - const systemPath = appRoot.resolve(file.path); + const systemPath = path.join(appRootPath, file.path); let options: any = { filepath: systemPath };