Skip to content

Commit

Permalink
fix(nx): replace app-root-path with a custom function to fix tslint i…
Browse files Browse the repository at this point in the history
…n vscode
  • Loading branch information
vsavkin committed Jun 25, 2019
1 parent ccd5854 commit e720dde
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 35 deletions.
1 change: 0 additions & 1 deletion packages/workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions packages/workspace/src/command-line/deps-calculator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as path from 'path';
import * as ts from 'typescript';
import * as appRoot from 'app-root-path';

import {
normalizedProjectRoot,
Expand All @@ -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[];
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions packages/workspace/src/command-line/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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('.'));
}
24 changes: 12 additions & 12 deletions packages/workspace/src/command-line/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<NxJson>(`${appRoot.path}/nx.json`);
const config = readJsonFile<NxJson>(`${appRootPath}/nx.json`);
if (!config.npmScope) {
throw new Error(`nx.json must define the npmScope property.`);
}
Expand Down Expand Up @@ -423,23 +423,23 @@ 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 [];
}

let res = [];
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 {
const s = fs.statSync(child);
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()) {
Expand All @@ -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`)
]
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/workspace/src/command-line/workspace-schematic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions packages/workspace/src/utils/app-root.ts
Original file line number Diff line number Diff line change
@@ -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));
}
}
19 changes: 10 additions & 9 deletions packages/workspace/src/utils/rules/format-files.ts
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -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
};
Expand Down

0 comments on commit e720dde

Please sign in to comment.