Skip to content

Commit

Permalink
feat: Replace mock-fs with fs-bridge to prepare node update (#221)
Browse files Browse the repository at this point in the history
* feat: Replace mock-fs with fs-bridge to prepare node update

---------

Signed-off-by: Dennis Meister <[email protected]>
  • Loading branch information
dennismeister93 authored Mar 19, 2024
1 parent 1812ceb commit e5f62ec
Show file tree
Hide file tree
Showing 31 changed files with 700 additions and 360 deletions.
2 changes: 0 additions & 2 deletions NOTICE-3RD-PARTY-CONTENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
|@types/decompress|4.2.7|MIT|
|@types/fs-extra|11.0.4|MIT|
|@types/mocha|10.0.6|MIT|
|@types/mock-fs|4.13.4|MIT|
|@types/node|18.19.22|MIT|
|@typescript-eslint/eslint-plugin|7.1.1|MIT|
|@typescript-eslint/parser|7.1.1|unknown|
Expand All @@ -23,7 +22,6 @@
|eslint|8.57.0|ISC<br/>MIT|
|inquirer|8.2.6|MIT|
|mocha|10.3.0|ISC<br/>MIT|
|mock-fs|5.2.0|MIT|
|node-pty|1.0.0|MIT|
|nyc|15.1.0|ISC<br/>MIT|
|oclif|3.17.2|MIT|
Expand Down
358 changes: 285 additions & 73 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@
"@types/decompress": "4.2.7",
"@types/fs-extra": "11.0.4",
"@types/mocha": "10.0.6",
"@types/mock-fs": "4.13.4",
"@types/node": "18.19.22",
"@typescript-eslint/eslint-plugin": "7.1.1",
"@typescript-eslint/parser": "7.1.1",
"archiver": "5.3.2",
"chai": "4.4.1",
"eslint": "8.57.0",
"mocha": "10.3.0",
"mock-fs": "5.2.0",
"nyc": "15.1.0",
"oclif": "3.17.2",
"pkg": "5.8.1",
Expand Down
12 changes: 1 addition & 11 deletions src/commands/create/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,15 @@

import { Command, Flags } from '@oclif/core';
import { ProjectConfig } from '../../modules/project-config';
import { packageDownloader } from '../../modules/package-downloader';
// eslint-disable-next-line @typescript-eslint/naming-convention
import Exec from '../exec';
// eslint-disable-next-line @typescript-eslint/naming-convention
import Init from '../init';
// eslint-disable-next-line @typescript-eslint/naming-convention
import Sync from '../sync';
import {
PackageIndex,
CoreComponent,
ExtensionComponent,
CoreOptions,
DescribedId,
Parameter,
PackageAttributes,
} from '../../modules/package-index';
import { PackageIndex, CoreComponent, ExtensionComponent, CoreOptions, DescribedId, Parameter } from '../../modules/package-index';
import { AppManifest, AppManifestInterfaceAttributes } from '../../modules/app-manifest';
import { InteractiveMode } from '../../modules/create-interactive';
import { PackageConfig } from '../../modules/package';

// inquirer >= v9 is an ESM package.
// We are not using ESM in our CLI,
Expand Down
10 changes: 4 additions & 6 deletions src/modules/app-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
//
// SPDX-License-Identifier: Apache-2.0

import { existsSync, readFileSync } from 'fs';
import { resolve } from 'path';
import { cwd } from 'process';
import { DEFAULT_BUFFER_ENCODING } from './constants';
import { outputFileSync } from 'fs-extra';
import { CliFileSystem } from '../utils/fs-bridge';

export const DEFAULT_APP_MANIFEST_PATH = './app/AppManifest.json';
export const APP_MANIFEST_PATH_VARIABLE = 'appManifestPath';
Expand Down Expand Up @@ -95,12 +93,12 @@ export class AppManifest implements AppManifestAttributes {
* @throws {Error} Throws an error if there is an issue reading the AppManifest file.
*/
static read(appManifestPath: string = APP_MANIFEST_PATH): AppManifest | undefined {
if (!existsSync(appManifestPath)) {
if (!CliFileSystem.existsSync(appManifestPath)) {
console.info('*** Info ***: No AppManifest found');
return undefined;
}
try {
const file = readFileSync(appManifestPath, DEFAULT_BUFFER_ENCODING);
const file = CliFileSystem.readFileSync(appManifestPath);
const manifest = JSON.parse(file);
if (Array.isArray(manifest)) {
// for backwards compatibility, use the first entry from the array
Expand All @@ -114,6 +112,6 @@ export class AppManifest implements AppManifestAttributes {
}

write() {
outputFileSync(DEFAULT_APP_MANIFEST_PATH, JSON.stringify(this, null, 4));
CliFileSystem.outputFileSync(DEFAULT_APP_MANIFEST_PATH, JSON.stringify(this, null, 4));
}
}
4 changes: 2 additions & 2 deletions src/modules/package-downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
//
// SPDX-License-Identifier: Apache-2.0

import { existsSync } from 'fs-extra';
import { posix as pathPosix } from 'path';
import { CheckRepoActions, SimpleGit, simpleGit } from 'simple-git';
import { PackageConfig } from './package';
import { getLatestVersion } from './semver';
import { CliFileSystem } from '../utils/fs-bridge';

export class PackageDownloader {
packageConfig: PackageConfig;
Expand Down Expand Up @@ -62,7 +62,7 @@ export class PackageDownloader {
checkRepoAction = CheckRepoActions.IS_REPO_ROOT;
}

if (!existsSync(packageDir)) {
if (!CliFileSystem.existsSync(packageDir)) {
await this.cloneRepository(packageDir, cloneOpts);
}

Expand Down
5 changes: 2 additions & 3 deletions src/modules/package-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
//
// SPDX-License-Identifier: Apache-2.0

import { readFileSync } from 'fs-extra';
import { DEFAULT_BUFFER_ENCODING } from './constants';
import { CliFileSystem } from '../utils/fs-bridge';

/**
* Additional argument for exposed interface
Expand Down Expand Up @@ -148,7 +147,7 @@ export class PackageIndex {
*/
static read(path: string = './package-index.json'): PackageIndex {
try {
const packageIndexFile = readFileSync(path, DEFAULT_BUFFER_ENCODING);
const packageIndexFile = CliFileSystem.readFileSync(path);
const packageIndex: PackageAttributes[] = JSON.parse(packageIndexFile);
return new PackageIndex(packageIndex);
} catch (error) {
Expand Down
7 changes: 3 additions & 4 deletions src/modules/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
//
// SPDX-License-Identifier: Apache-2.0

import { existsSync, readFileSync } from 'fs-extra';
import { homedir } from 'os';
import { join } from 'path';
import { ComponentManifest } from './component';
import { DEFAULT_BUFFER_ENCODING } from './constants';
import { packageDownloader } from './package-downloader';
import { CliFileSystem } from '../utils/fs-bridge';

export const MANIFEST_FILE_NAME = 'manifest.json';

Expand Down Expand Up @@ -116,7 +115,7 @@ export class PackageConfig {
}

isPackageInstalled(): boolean {
if (!existsSync(this.getPackageDirectoryWithVersion())) {
if (!CliFileSystem.existsSync(this.getPackageDirectoryWithVersion())) {
return false;
}
return true;
Expand All @@ -125,7 +124,7 @@ export class PackageConfig {
readPackageManifest(): PackageManifest {
try {
const path = this.getManifestFilePath();
const config: PackageManifest = deserializePackageJSON(readFileSync(path, DEFAULT_BUFFER_ENCODING));
const config: PackageManifest = deserializePackageJSON(CliFileSystem.readFileSync(path));
return config;
} catch (error) {
console.log(`Cannot find package ${this.getPackageName()}:${this.version}. Please upgrade or init first!`);
Expand Down
12 changes: 6 additions & 6 deletions src/modules/project-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// SPDX-License-Identifier: Apache-2.0

import { createHash } from 'node:crypto';
import { existsSync, mkdirSync, PathLike, readFileSync, writeFileSync } from 'node:fs';
import { PathLike } from 'node:fs';
import { join, parse } from 'node:path';
import { DEFAULT_BUFFER_ENCODING } from './constants';
import { mapReplacer } from './helpers';
import { getVelocitasRoot } from './package';
import { CliFileSystem } from '../utils/fs-bridge';

const FILE_NAME = 'cache.json';

Expand Down Expand Up @@ -47,7 +47,7 @@ export class ProjectCache {
static read(path: string = join(ProjectCache.getCacheDir(), FILE_NAME)): ProjectCache {
const cache = new ProjectCache();
try {
const data: any = JSON.parse(readFileSync(path, DEFAULT_BUFFER_ENCODING));
const data: any = JSON.parse(CliFileSystem.readFileSync(path));
cache._data = new Map(Object.entries(data));
} catch (e: any) {
if (e.code !== 'ENOENT') {
Expand All @@ -64,9 +64,9 @@ export class ProjectCache {

write(path: string = join(ProjectCache.getCacheDir(), FILE_NAME)) {
const parsedPath = parse(path);
if (!existsSync(parsedPath.base)) {
mkdirSync(parsedPath.dir, { recursive: true });
if (!CliFileSystem.existsSync(parsedPath.base)) {
CliFileSystem.mkdirSync(parsedPath.dir);
}
writeFileSync(path, JSON.stringify(this._data, mapReplacer));
CliFileSystem.writeFileSync(path, JSON.stringify(this._data, mapReplacer));
}
}
11 changes: 5 additions & 6 deletions src/modules/project-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
//
// SPDX-License-Identifier: Apache-2.0

import { existsSync, PathLike, readFileSync, writeFileSync } from 'fs';
import { PathLike } from 'node:fs';
import { resolve } from 'path';
import { cwd } from 'process';
import { DEFAULT_BUFFER_ENCODING } from './constants';
import { mapReplacer } from './helpers';
import { PackageConfig } from './package';
import { getLatestVersion } from './semver';
import { PackageIndex } from './package-index';
import { DEFAULT_APP_MANIFEST_PATH } from './app-manifest';
import { ComponentConfig, ComponentContext } from './component';
import { VariableCollection } from './variables';
import { CliFileSystem } from '../utils/fs-bridge';

export const DEFAULT_CONFIG_FILE_NAME = '.velocitas.json';
export const DEFAULT_CONFIG_FILE_PATH = resolve(cwd(), DEFAULT_CONFIG_FILE_NAME);
Expand Down Expand Up @@ -66,9 +66,8 @@ export class ProjectConfig {

static read(cliVersion: string, path: PathLike = DEFAULT_CONFIG_FILE_PATH): ProjectConfig {
let config: ProjectConfig;

try {
config = new ProjectConfig(cliVersion, JSON.parse(readFileSync(path, DEFAULT_BUFFER_ENCODING)));
config = new ProjectConfig(cliVersion, JSON.parse(CliFileSystem.readFileSync(path as string)));
} catch (error) {
throw new Error(`Error in parsing ${DEFAULT_CONFIG_FILE_NAME}: ${(error as Error).message}`);
}
Expand All @@ -94,7 +93,7 @@ export class ProjectConfig {
return config;
}

static isAvailable = (path: PathLike = DEFAULT_CONFIG_FILE_PATH) => existsSync(path);
static isAvailable = (path: PathLike = DEFAULT_CONFIG_FILE_PATH) => CliFileSystem.existsSync(path);

static async create(usedComponents: Set<string>, packageIndex: PackageIndex, cliVersion: string) {
const projectConfig = new ProjectConfig(`v${cliVersion}`);
Expand Down Expand Up @@ -140,7 +139,7 @@ export class ProjectConfig {
cliVersion: this.cliVersion,
};
const configString = `${JSON.stringify(projectConfigOptions, mapReplacer, 4)}\n`;
writeFileSync(path, configString, DEFAULT_BUFFER_ENCODING);
CliFileSystem.writeFileSync(path, configString);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/modules/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
// SPDX-License-Identifier: Apache-2.0

import { existsSync, Stats } from 'fs';
import { Stats } from 'node:fs';
import { Transform } from 'node:stream';
import path, { join } from 'path';
import { cwd } from 'process';
Expand All @@ -21,6 +21,7 @@ import { TransformCallback, TransformOptions } from 'stream';
import { PackageConfig } from './package';
import { VariableCollection } from './variables';
import { ComponentManifest } from './component';
import { CliFileSystem } from '../utils/fs-bridge';

const SUPPORTED_TEXT_FILES_ARRAY = ['.md', '.yaml', '.yml', '.txt', '.json', '.sh', '.html', '.htm', '.xml', '.tpl'];

Expand Down Expand Up @@ -91,7 +92,7 @@ export function installComponent(packageConfig: PackageConfig, component: Compon
const sourceFileOrDir = join(packageConfig.getPackageDirectory(), packageConfig.version, src);
const destFileOrDir = join(cwd(), dst);
try {
if (existsSync(sourceFileOrDir)) {
if (CliFileSystem.existsSync(sourceFileOrDir)) {
copy(sourceFileOrDir, destFileOrDir, {
dot: true,
overwrite: true,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
//
// SPDX-License-Identifier: Apache-2.0

import { realpathSync } from 'node:fs';
import { cwd } from 'node:process';
import { ComponentContext } from './component';
import { mapReplacer } from './helpers';
import { ProjectCache } from './project-cache';
import { CliFileSystem } from '../utils/fs-bridge';

export enum ScopeIdentifier {
package = 'package',
Expand Down Expand Up @@ -263,7 +263,7 @@ export function createEnvVars(packagePath: string, variables: VariableCollection
const envVars = Object.assign({}, process.env, {
VELOCITAS_WORKSPACE_DIR: cwd(),
VELOCITAS_CACHE_DATA: JSON.stringify(projectCache.raw(), mapReplacer),
VELOCITAS_CACHE_DIR: ProjectCache.getCacheDir(realpathSync(process.cwd())),
VELOCITAS_CACHE_DIR: ProjectCache.getCacheDir(CliFileSystem.realpathSync(process.cwd())),
VELOCITAS_PACKAGE_DIR: packagePath,
});

Expand Down
Loading

0 comments on commit e5f62ec

Please sign in to comment.