From 6baa2ceb11c3925bcd1d28f1720cf71d429aacc3 Mon Sep 17 00:00:00 2001 From: Phillip Barta Date: Sat, 28 May 2022 12:39:32 +0200 Subject: [PATCH] cleanup(repo): refactored fileutils --- e2e/utils/index.ts | 17 +------ .../src/add-nx-to-monorepo.ts | 13 ++--- .../src/utils/testing-utils/utils.ts | 27 ++-------- packages/nx/src/utils/app-root.ts | 10 +--- packages/nx/src/utils/fileutils.spec.ts | 30 ++--------- packages/nx/src/utils/fileutils.ts | 40 ++++++++------- .../nx/src/utils/target-project-locator.ts | 6 +-- packages/web/src/utils/fs.ts | 13 ++--- .../tslint/nxEnforceModuleBoundariesRule.ts | 7 ++- .../workspace/src/utilities/fileutils.spec.ts | 30 ++--------- packages/workspace/src/utilities/fileutils.ts | 48 ++++-------------- packages/workspace/src/utils/fileutils.ts | 50 ++++--------------- 12 files changed, 68 insertions(+), 223 deletions(-) diff --git a/e2e/utils/index.ts b/e2e/utils/index.ts index 7db9b89ccd4b68..c145dee6e88ee2 100644 --- a/e2e/utils/index.ts +++ b/e2e/utils/index.ts @@ -31,6 +31,7 @@ import isCI = require('is-ci'); import treeKill = require('tree-kill'); import { Workspaces } from '../../packages/nx/src/config/workspaces'; import { PackageManager } from 'nx/src/utils/package-manager'; +import { directoryExists, fileExists } from 'nx/src/utils/fileutils'; export function getPublishedVersion(): string { process.env.PUBLISHED_VERSION = @@ -735,22 +736,6 @@ export function rmDist() { removeSync(`${tmpProjPath()}/dist`); } -export function directoryExists(filePath: string): boolean { - try { - return statSync(filePath).isDirectory(); - } catch (err) { - return false; - } -} - -export function fileExists(filePath: string): boolean { - try { - return statSync(filePath).isFile(); - } catch (err) { - return false; - } -} - export function exists(filePath: string): boolean { return directoryExists(filePath) || fileExists(filePath); } diff --git a/packages/add-nx-to-monorepo/src/add-nx-to-monorepo.ts b/packages/add-nx-to-monorepo/src/add-nx-to-monorepo.ts index bcb9f05dc59830..a98d08a377d8f9 100644 --- a/packages/add-nx-to-monorepo/src/add-nx-to-monorepo.ts +++ b/packages/add-nx-to-monorepo/src/add-nx-to-monorepo.ts @@ -8,6 +8,7 @@ import * as enquirer from 'enquirer'; import * as yargsParser from 'yargs-parser'; import { output, readJsonFile, writeJsonFile } from '@nrwl/devkit'; import ignore from 'ignore'; +import { directoryExists } from 'nx/src/utils/fileutils'; const parsedArgs = yargsParser(process.argv, { boolean: ['nxCloud'], @@ -208,23 +209,15 @@ function createNxJsonFile(repoRoot: string) { } function deduceWorkspaceLayout(repoRoot: string) { - if (exists(path.join(repoRoot, 'packages'))) { + if (directoryExists(path.join(repoRoot, 'packages'))) { return undefined; - } else if (exists(path.join(repoRoot, 'projects'))) { + } else if (directoryExists(path.join(repoRoot, 'projects'))) { return { libsDir: 'projects', appsDir: 'projects' }; } else { return undefined; } } -function exists(folder: string) { - try { - return fs.statSync(folder).isDirectory(); - } catch { - return false; - } -} - function deduceDefaultBase() { try { execSync(`git rev-parse --verify main`, { diff --git a/packages/nx-plugin/src/utils/testing-utils/utils.ts b/packages/nx-plugin/src/utils/testing-utils/utils.ts index 5055b300320d2c..5d6e0022ff69fc 100644 --- a/packages/nx-plugin/src/utils/testing-utils/utils.ts +++ b/packages/nx-plugin/src/utils/testing-utils/utils.ts @@ -12,6 +12,9 @@ import { dirname, isAbsolute } from 'path'; import { tmpProjPath } from './paths'; import { parseJson } from '@nrwl/devkit'; import type { JsonParseOptions } from '@nrwl/devkit'; +import { directoryExists, fileExists } from 'nx/src/utils/fileutils'; + +export { directoryExists, fileExists }; /** * Copies module folders from the working directory to the e2e directory @@ -139,30 +142,6 @@ export function getCwd(): string { return process.cwd(); } -/** - * Check if a directory exists - * @param directoryPath Path to directory - */ -export function directoryExists(directoryPath: string): boolean { - try { - return statSync(directoryPath).isDirectory(); - } catch { - return false; - } -} - -/** - * Check if a file exists. - * @param filePath Path to file - */ -export function fileExists(filePath: string): boolean { - try { - return statSync(filePath).isFile(); - } catch { - return false; - } -} - /** * Check if a file or directory exists. * @param path Path to file or directory diff --git a/packages/nx/src/utils/app-root.ts b/packages/nx/src/utils/app-root.ts index 716fe9a345ff0d..16cc0b34e59463 100644 --- a/packages/nx/src/utils/app-root.ts +++ b/packages/nx/src/utils/app-root.ts @@ -1,5 +1,5 @@ import * as path from 'path'; -import { statSync } from 'fs'; +import { fileExists } from './fileutils'; /** * The root of the workspace. @@ -27,11 +27,3 @@ function pathInner(dir: string): string { return pathInner(path.dirname(dir)); } } - -export function fileExists(filePath: string): boolean { - try { - return statSync(filePath).isFile(); - } catch (err) { - return false; - } -} diff --git a/packages/nx/src/utils/fileutils.spec.ts b/packages/nx/src/utils/fileutils.spec.ts index d78f7c2994bb57..d3acf32602a6af 100644 --- a/packages/nx/src/utils/fileutils.spec.ts +++ b/packages/nx/src/utils/fileutils.spec.ts @@ -1,32 +1,10 @@ -import { fs, vol } from 'memfs'; -import { stripIndents } from '@angular-devkit/core/src/utils/literals'; +import { fs } from 'memfs'; import { createDirectory, isRelativePath } from './fileutils'; -jest.mock('fs', () => require('memfs').fs); +jest.mock('fs', () => fs); describe('fileutils', () => { - beforeEach(() => { - vol.fromJSON( - { - './README.md': 'hello', - './.nxignore': stripIndents` - apps/demo/tmp.txt - tmp/ - `, - './.gitignore': stripIndents` - *.js - node_modules/ - `, - './apps/demo/src/index.ts': 'console.log("hello");', - './apps/demo/tmp.txt': '...', - './apps/demo/tmp.js': 'console.log("tmp")', - './workspace.json': '{}', - }, - '/root' - ); - }); - - describe('createDirectory', () => { + describe(createDirectory.name, () => { it('should recursively create the directory', () => { createDirectory('/root/b/c'); expect(fs.statSync('/root').isDirectory()).toBe(true); @@ -35,7 +13,7 @@ describe('fileutils', () => { }); }); - describe('isRelativePath()', () => { + describe(isRelativePath.name, () => { it('should return true for deeper imports', () => { expect(isRelativePath('.')).toEqual(true); expect(isRelativePath('./file')).toEqual(true); diff --git a/packages/nx/src/utils/fileutils.ts b/packages/nx/src/utils/fileutils.ts index 9ce0ed76672033..fe17842e5c8744 100644 --- a/packages/nx/src/utils/fileutils.ts +++ b/packages/nx/src/utils/fileutils.ts @@ -3,13 +3,13 @@ import type { JsonParseOptions, JsonSerializeOptions } from './json'; import { createReadStream, createWriteStream, + PathLike, readFileSync, writeFileSync, + mkdirSync, + statSync, } from 'fs'; import { dirname } from 'path'; -import { ensureDirSync } from 'fs-extra'; -import { mkdirSync, statSync } from 'fs'; -import { resolve as pathResolve } from 'path'; import * as tar from 'tar-stream'; import { createGunzip } from 'zlib'; @@ -64,7 +64,7 @@ export function writeJsonFile( data: T, options?: JsonWriteOptions ): void { - ensureDirSync(dirname(path)); + mkdirSync(dirname(path), { recursive: true }); const serializedJson = serializeJson(data, options); const content = options?.appendNewLine ? `${serializedJson}\n` @@ -72,30 +72,32 @@ export function writeJsonFile( writeFileSync(path, content, { encoding: 'utf-8' }); } -export function directoryExists(name) { +/** + * Check if a directory exists + * @param path Path to directory + */ +export function directoryExists(path: PathLike): boolean { try { - return statSync(name).isDirectory(); - } catch (e) { + return statSync(path).isDirectory(); + } catch { return false; } } -export function fileExists(filePath: string): boolean { +/** + * Check if a file exists. + * @param path Path to file + */ +export function fileExists(path: PathLike): boolean { try { - return statSync(filePath).isFile(); - } catch (err) { + return statSync(path).isFile(); + } catch { return false; } } -export function createDirectory(directoryPath: string) { - const parentPath = pathResolve(directoryPath, '..'); - if (!directoryExists(parentPath)) { - createDirectory(parentPath); - } - if (!directoryExists(directoryPath)) { - mkdirSync(directoryPath); - } +export function createDirectory(path: PathLike) { + mkdirSync(path, { recursive: true }); } export function isRelativePath(path: string): boolean { @@ -120,7 +122,7 @@ export async function extractFileFromTarball( destinationFilePath: string ) { return new Promise((resolve, reject) => { - ensureDirSync(dirname(destinationFilePath)); + mkdirSync(dirname(destinationFilePath), { recursive: true }); var tarExtractStream = tar.extract(); const destinationFileStream = createWriteStream(destinationFilePath); diff --git a/packages/nx/src/utils/target-project-locator.ts b/packages/nx/src/utils/target-project-locator.ts index cd3345c914816a..e20d80454f49ad 100644 --- a/packages/nx/src/utils/target-project-locator.ts +++ b/packages/nx/src/utils/target-project-locator.ts @@ -1,13 +1,11 @@ import { getRootTsConfigFileName, resolveModuleByImport } from './typescript'; -import { isRelativePath } from './fileutils'; +import { isRelativePath, readJsonFile } from './fileutils'; import { dirname, join, posix } from 'path'; import { workspaceRoot } from './app-root'; -import { readFileSync } from 'fs'; import { ProjectGraphExternalNode, ProjectGraphProjectNode, } from '../config/project-graph'; -import { parseJson } from './json'; export class TargetProjectLocator { private projectRootMappings = createProjectRootMappings(this.nodes); @@ -173,7 +171,7 @@ export class TargetProjectLocator { return { absolutePath, path, - config: parseJson(readFileSync(absolutePath, 'utf8')), + config: readJsonFile(absolutePath), }; } diff --git a/packages/web/src/utils/fs.ts b/packages/web/src/utils/fs.ts index 86762162d3329e..c9b512bb29f979 100644 --- a/packages/web/src/utils/fs.ts +++ b/packages/web/src/utils/fs.ts @@ -1,5 +1,6 @@ import * as path from 'path'; -import { existsSync, removeSync, statSync } from 'fs-extra'; +import { existsSync, removeSync } from 'fs-extra'; +import { directoryExists } from 'nx/src/utils/fileutils'; export function findUp( names: string | string[], @@ -39,7 +40,7 @@ export function findAllNodeModules(from: string, root?: string) { let current = from; while (current && current !== root) { const potential = path.join(current, 'node_modules'); - if (existsSync(potential) && isDirectory(potential)) { + if (directoryExists(potential)) { nodeModules.push(potential); } @@ -64,11 +65,3 @@ export function deleteOutputDir(root: string, outputPath: string) { removeSync(resolvedOutputPath); } - -export function isDirectory(path: string) { - try { - return statSync(path).isDirectory(); - } catch (_) { - return false; - } -} diff --git a/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts b/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts index f9b9ba64f84ff6..da0a6197f85672 100644 --- a/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts +++ b/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.ts @@ -5,7 +5,11 @@ import type { NxJsonConfiguration, ProjectGraphExternalNode, } from '@nrwl/devkit'; -import { workspaceRoot } from '@nrwl/devkit'; +import { + workspaceRoot, + readNxJson, + readCachedProjectGraph, +} from '@nrwl/devkit'; import { DepConstraint, findConstraintsFor, @@ -32,7 +36,6 @@ import { findFilesInCircularPath, } from '../utils/graph-utils'; import { isRelativePath } from '../utilities/fileutils'; -import { readNxJson, readCachedProjectGraph } from '@nrwl/devkit'; export class Rule extends Lint.Rules.AbstractRule { constructor( diff --git a/packages/workspace/src/utilities/fileutils.spec.ts b/packages/workspace/src/utilities/fileutils.spec.ts index d78f7c2994bb57..d3acf32602a6af 100644 --- a/packages/workspace/src/utilities/fileutils.spec.ts +++ b/packages/workspace/src/utilities/fileutils.spec.ts @@ -1,32 +1,10 @@ -import { fs, vol } from 'memfs'; -import { stripIndents } from '@angular-devkit/core/src/utils/literals'; +import { fs } from 'memfs'; import { createDirectory, isRelativePath } from './fileutils'; -jest.mock('fs', () => require('memfs').fs); +jest.mock('fs', () => fs); describe('fileutils', () => { - beforeEach(() => { - vol.fromJSON( - { - './README.md': 'hello', - './.nxignore': stripIndents` - apps/demo/tmp.txt - tmp/ - `, - './.gitignore': stripIndents` - *.js - node_modules/ - `, - './apps/demo/src/index.ts': 'console.log("hello");', - './apps/demo/tmp.txt': '...', - './apps/demo/tmp.js': 'console.log("tmp")', - './workspace.json': '{}', - }, - '/root' - ); - }); - - describe('createDirectory', () => { + describe(createDirectory.name, () => { it('should recursively create the directory', () => { createDirectory('/root/b/c'); expect(fs.statSync('/root').isDirectory()).toBe(true); @@ -35,7 +13,7 @@ describe('fileutils', () => { }); }); - describe('isRelativePath()', () => { + describe(isRelativePath.name, () => { it('should return true for deeper imports', () => { expect(isRelativePath('.')).toEqual(true); expect(isRelativePath('./file')).toEqual(true); diff --git a/packages/workspace/src/utilities/fileutils.ts b/packages/workspace/src/utilities/fileutils.ts index 72ae2c2fb2ed2e..6ccccaeb9ef716 100644 --- a/packages/workspace/src/utilities/fileutils.ts +++ b/packages/workspace/src/utilities/fileutils.ts @@ -2,12 +2,19 @@ import { createReadStream, createWriteStream, existsSync, - mkdirSync, renameSync as fsRenameSync, - statSync, } from 'fs'; import { basename, resolve as pathResolve } from 'path'; -import { readJsonFile, writeJsonFile } from '@nrwl/devkit'; +import { + readJsonFile, + writeJsonFile, + fileExists, + directoryExists, + isRelativePath, + createDirectory, +} from 'nx/src/utils/fileutils'; + +export { fileExists, directoryExists, isRelativePath, createDirectory }; /** * This method is specifically for updating a JSON file using the filesystem @@ -31,32 +38,6 @@ export function copyFile(file: string, target: string) { source.on('error', (e) => console.error(e)); } -export function directoryExists(name) { - try { - return statSync(name).isDirectory(); - } catch (e) { - return false; - } -} - -export function fileExists(filePath: string): boolean { - try { - return statSync(filePath).isFile(); - } catch (err) { - return false; - } -} - -export function createDirectory(directoryPath: string) { - const parentPath = pathResolve(directoryPath, '..'); - if (!directoryExists(parentPath)) { - createDirectory(parentPath); - } - if (!directoryExists(directoryPath)) { - mkdirSync(directoryPath); - } -} - export function renameSync( from: string, to: string, @@ -79,12 +60,3 @@ export function renameSync( cb(e); } } - -export function isRelativePath(path: string): boolean { - return ( - path === '.' || - path === '..' || - path.startsWith('./') || - path.startsWith('../') - ); -} diff --git a/packages/workspace/src/utils/fileutils.ts b/packages/workspace/src/utils/fileutils.ts index 993447e425c163..f9a2e53f409ae2 100644 --- a/packages/workspace/src/utils/fileutils.ts +++ b/packages/workspace/src/utils/fileutils.ts @@ -5,14 +5,21 @@ import { writeFileSync, mkdirSync, renameSync as fsRenameSync, - statSync, } from 'fs'; -import { ensureDirSync } from 'fs-extra'; import { basename, dirname, resolve } from 'path'; -import { readJsonFile, writeJsonFile } from '@nrwl/devkit'; +import { + readJsonFile, + writeJsonFile, + fileExists, + directoryExists, + isRelativePath, + createDirectory, +} from 'nx/src/utils/fileutils'; + +export { fileExists, directoryExists, isRelativePath, createDirectory }; export function writeToFile(filePath: string, str: string) { - ensureDirSync(dirname(filePath)); + mkdirSync(dirname(filePath), { recursive: true }); writeFileSync(filePath, str); } @@ -38,32 +45,6 @@ export function copyFile(file: string, target: string) { source.on('error', (e) => console.error(e)); } -export function directoryExists(name) { - try { - return statSync(name).isDirectory(); - } catch (e) { - return false; - } -} - -export function fileExists(filePath: string): boolean { - try { - return statSync(filePath).isFile(); - } catch (err) { - return false; - } -} - -export function createDirectory(directoryPath: string) { - const parentPath = resolve(directoryPath, '..'); - if (!directoryExists(parentPath)) { - createDirectory(parentPath); - } - if (!directoryExists(directoryPath)) { - mkdirSync(directoryPath); - } -} - export function renameSync( from: string, to: string, @@ -86,12 +67,3 @@ export function renameSync( cb(e); } } - -export function isRelativePath(path: string): boolean { - return ( - path === '.' || - path === '..' || - path.startsWith('./') || - path.startsWith('../') - ); -}