diff --git a/package-lock.json b/package-lock.json index 6423c1a84..a2c415835 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,6 @@ "errorhandler": "^1.5.1", "express": "^4.20.0", "form-data": "^4.0.0", - "fs-extra": "^11.2.0", "getport": "^0.1.0", "livereload": "^0.9.3", "lodash.isequal": "^4.5.0", @@ -65,7 +64,6 @@ "@types/cross-spawn": "^6.0.6", "@types/errorhandler": "^1.5.3", "@types/express": "^4.17.21", - "@types/fs-extra": "^11.0.4", "@types/livereload": "^0.9.5", "@types/morgan": "^1.9.9", "@types/multer": "^1.4.12", @@ -2780,16 +2778,6 @@ "@types/send": "*" } }, - "node_modules/@types/fs-extra": { - "version": "11.0.4", - "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-11.0.4.tgz", - "integrity": "sha512-yTbItCNreRooED33qjunPthRcSjERP1r4MqCZc7wv0u2sUkzTFp45tgUfS5+r7FrZPdmCCNflLhVSP/o+SemsQ==", - "dev": true, - "dependencies": { - "@types/jsonfile": "*", - "@types/node": "*" - } - }, "node_modules/@types/http-errors": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.4.tgz", @@ -2801,15 +2789,6 @@ "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz", "integrity": "sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==" }, - "node_modules/@types/jsonfile": { - "version": "6.1.4", - "resolved": "https://registry.npmjs.org/@types/jsonfile/-/jsonfile-6.1.4.tgz", - "integrity": "sha512-D5qGUYwjvnNNextdU59/+fI+spnwtTFmyQP0h+PfIOSkNfpU6AOICUOkm4i0OnSk+NyjdPJrxCDro0sJsWlRpQ==", - "dev": true, - "dependencies": { - "@types/node": "*" - } - }, "node_modules/@types/livereload": { "version": "0.9.5", "resolved": "https://registry.npmjs.org/@types/livereload/-/livereload-0.9.5.tgz", @@ -5195,19 +5174,6 @@ "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==" }, - "node_modules/fs-extra": { - "version": "11.2.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.2.0.tgz", - "integrity": "sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw==", - "dependencies": { - "graceful-fs": "^4.2.0", - "jsonfile": "^6.0.1", - "universalify": "^2.0.0" - }, - "engines": { - "node": ">=14.14" - } - }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", diff --git a/package.json b/package.json index 664c74447..d13639de8 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,6 @@ "@types/cross-spawn": "^6.0.6", "@types/errorhandler": "^1.5.3", "@types/express": "^4.17.21", - "@types/fs-extra": "^11.0.4", "@types/livereload": "^0.9.5", "@types/morgan": "^1.9.9", "@types/multer": "^1.4.12", @@ -87,7 +86,6 @@ "errorhandler": "^1.5.1", "express": "^4.20.0", "form-data": "^4.0.0", - "fs-extra": "^11.2.0", "getport": "^0.1.0", "livereload": "^0.9.3", "lodash.isequal": "^4.5.0", @@ -120,4 +118,4 @@ "universalify": "^2.0.0", "yargs": "^17.7.2" } -} +} \ No newline at end of file diff --git a/src/cli/domain/clean.ts b/src/cli/domain/clean.ts index 47314fdb3..569750877 100644 --- a/src/cli/domain/clean.ts +++ b/src/cli/domain/clean.ts @@ -1,5 +1,6 @@ +import { existsSync } from 'node:fs'; +import fs from 'node:fs/promises'; import path from 'node:path'; -import fs from 'fs-extra'; import makeGetComponentsByDir from './get-components-by-dir'; const getComponentsByDir = makeGetComponentsByDir(); @@ -11,11 +12,11 @@ export async function fetchList(dirPath: string): Promise { const toRemove = list.map((folder) => path.join(folder, 'node_modules')); - return toRemove.filter(fs.existsSync); + return toRemove.filter(existsSync); } export async function remove(list: string[]): Promise { for (const item of list) { - await fs.remove(item); + await fs.rmdir(item, { recursive: true }); } } diff --git a/src/cli/domain/get-components-by-dir.ts b/src/cli/domain/get-components-by-dir.ts index 728ac00be..d787d8bea 100644 --- a/src/cli/domain/get-components-by-dir.ts +++ b/src/cli/domain/get-components-by-dir.ts @@ -1,7 +1,10 @@ +import { readFileSync } from 'node:fs'; +import fs from 'node:fs/promises'; import path from 'node:path'; -import fs from 'fs-extra'; import type { Component } from '../../types'; +const readJsonSync = (path: string) => JSON.parse(readFileSync(path, 'utf8')); + export default function getComponentsByDir() { return async ( componentsDir: string, @@ -13,7 +16,7 @@ export default function getComponentsByDir() { let content: Component; try { - content = fs.readJsonSync(packagePath); + content = readJsonSync(packagePath); } catch (err) { return false; } diff --git a/src/cli/domain/get-mocked-plugins.ts b/src/cli/domain/get-mocked-plugins.ts index 728e2923f..e9d99e207 100644 --- a/src/cli/domain/get-mocked-plugins.ts +++ b/src/cli/domain/get-mocked-plugins.ts @@ -1,11 +1,13 @@ +import { existsSync, readFileSync, realpathSync } from 'node:fs'; import path from 'node:path'; -import fs from 'fs-extra'; import strings from '../../resources/'; import settings from '../../resources/settings'; import type { OcJsonConfig } from '../../types'; import type { Logger } from '../logger'; +const readJsonSync = (path: string) => JSON.parse(readFileSync(path, 'utf8')); + interface MockedPlugin { register: (options: unknown, dependencies: unknown, next: () => void) => void; execute: (...args: unknown[]) => unknown; @@ -97,10 +99,10 @@ const findPath = ( pathToResolve: string, fileName: string ): string | undefined => { - const rootDir = fs.realpathSync('.'); + const rootDir = realpathSync('.'); const fileToResolve = path.join(pathToResolve, fileName); - if (!fs.existsSync(fileToResolve)) { + if (!existsSync(fileToResolve)) { if (pathToResolve === rootDir) { return undefined; } @@ -129,7 +131,7 @@ export default function getMockedPlugins( return plugins; } - const content: OcJsonConfig = fs.readJsonSync(ocJsonPath); + const content: OcJsonConfig = readJsonSync(ocJsonPath); const ocJsonLocation = ocJsonPath.slice(0, -ocJsonFileName.length); if (!content.mocks || !content.mocks.plugins) { diff --git a/src/cli/domain/handle-dependencies/index.ts b/src/cli/domain/handle-dependencies/index.ts index 94adc5e21..86e4b327b 100644 --- a/src/cli/domain/handle-dependencies/index.ts +++ b/src/cli/domain/handle-dependencies/index.ts @@ -1,6 +1,6 @@ +import fs from 'node:fs/promises'; import path from 'node:path'; import coreModules from 'builtin-modules'; -import fs from 'fs-extra'; import strings from '../../../resources'; import type { Component, Template } from '../../../types'; @@ -10,8 +10,10 @@ import ensureCompilerIsDeclaredAsDevDependency from './ensure-compiler-is-declar import getCompiler from './get-compiler'; import installMissingDependencies from './install-missing-dependencies'; +const readJson = (path: string) => fs.readFile(path, 'utf8').then(JSON.parse); + const getComponentPackageJson = (componentPath: string): Promise => - fs.readJson(path.join(componentPath, 'package.json')); + readJson(path.join(componentPath, 'package.json')); const union = (a: ReadonlyArray, b: ReadonlyArray) => [ ...new Set([...a, ...b]) diff --git a/src/cli/domain/init-template/index.ts b/src/cli/domain/init-template/index.ts index 2ec3fac1c..c2f3abdf2 100644 --- a/src/cli/domain/init-template/index.ts +++ b/src/cli/domain/init-template/index.ts @@ -1,5 +1,5 @@ +import fs from 'node:fs/promises'; import path from 'node:path'; -import fs from 'fs-extra'; import * as npm from '../../../utils/npm-utils'; import type { Logger } from '../../logger'; @@ -17,7 +17,7 @@ export default async function initTemplate(options: { const compilerPath = path.join(componentPath, 'node_modules', compiler); const npmOptions = { initPath: componentPath, silent: true }; - await fs.ensureDir(componentPath); + await fs.mkdir(componentPath, { recursive: true }); await npm.init(npmOptions); await installTemplate(options); await scaffold(Object.assign(options, { compilerPath })); diff --git a/src/cli/domain/init-template/scaffold.ts b/src/cli/domain/init-template/scaffold.ts index 10dc396c1..a63bb6f23 100644 --- a/src/cli/domain/init-template/scaffold.ts +++ b/src/cli/domain/init-template/scaffold.ts @@ -1,5 +1,5 @@ +import fs from 'node:fs/promises'; import path from 'node:path'; -import fs from 'fs-extra'; import strings from '../../../resources'; @@ -11,6 +11,10 @@ interface ScaffoldOptions { templateType: string; } +const readJson = (path: string) => fs.readFile(path, 'utf8').then(JSON.parse); +const writeJson = (path: string, data: unknown) => + fs.writeFile(path, JSON.stringify(data, null, 2), 'utf-8'); + export default async function scaffold( options: ScaffoldOptions ): Promise<{ ok: true }> { @@ -19,14 +23,14 @@ export default async function scaffold( const baseComponentPath = path.join(compilerPath, 'scaffold'); const baseComponentFiles = path.join(baseComponentPath, 'src'); - const compilerPackage = await fs.readJson( + const compilerPackage = await readJson( path.join(compilerPath, 'package.json') ); try { - await fs.copy(baseComponentFiles, componentPath); + await fs.cp(baseComponentFiles, componentPath, { recursive: true }); - const componentPackage = await fs.readJson( + const componentPackage = await readJson( path.join(componentPath, 'package.json') ); componentPackage.name = componentName; @@ -34,9 +38,7 @@ export default async function scaffold( componentPackage.scripts.start ??= `oc dev .. --components ${componentName}`; componentPackage.scripts.build ??= 'oc package .'; componentPackage.devDependencies[compiler] = compilerPackage.version; - await fs.writeJson(componentPath + '/package.json', componentPackage, { - spaces: 2 - }); + await writeJson(componentPath + '/package.json', componentPackage); return { ok: true }; } catch (error) { diff --git a/src/cli/domain/local.ts b/src/cli/domain/local.ts index 1f58e98c0..433cf62d6 100644 --- a/src/cli/domain/local.ts +++ b/src/cli/domain/local.ts @@ -1,5 +1,5 @@ +import fs from 'node:fs/promises'; import { promisify } from 'node:util'; -import fs from 'fs-extra'; import targz from 'targz'; import * as validator from '../../registry/domain/validators'; diff --git a/src/cli/domain/mock.ts b/src/cli/domain/mock.ts index 3fecb984d..4d04f3c57 100644 --- a/src/cli/domain/mock.ts +++ b/src/cli/domain/mock.ts @@ -1,17 +1,22 @@ +import { existsSync } from 'node:fs'; +import fs from 'node:fs/promises'; import path from 'node:path'; -import fs from 'fs-extra'; import settings from '../../resources/settings'; +const readJson = (path: string) => fs.readFile(path, 'utf8').then(JSON.parse); +const writeJson = (path: string, data: unknown) => + fs.writeFile(path, JSON.stringify(data, null, 2), 'utf-8'); + export default function mock() { return async (params: { targetType: string; targetValue: string; targetName: string; }): Promise => { - const localConfig = await fs - .readJson(settings.configFile.src) - .catch(() => ({})); + const localConfig = await readJson(settings.configFile.src).catch( + () => ({}) + ); const mockType = params.targetType + 's'; @@ -24,7 +29,7 @@ export default function mock() { } let pluginType = 'static'; - if (fs.existsSync(path.resolve(params.targetValue.toString()))) { + if (existsSync(path.resolve(params.targetValue.toString()))) { pluginType = 'dynamic'; } @@ -35,6 +40,6 @@ export default function mock() { localConfig.mocks[mockType][pluginType][params.targetName] = params.targetValue; - return fs.writeJson(settings.configFile.src, localConfig, { spaces: 2 }); + return writeJson(settings.configFile.src, localConfig); }; } diff --git a/src/cli/domain/package-components.ts b/src/cli/domain/package-components.ts index 1808dde02..a5d73c44a 100644 --- a/src/cli/domain/package-components.ts +++ b/src/cli/domain/package-components.ts @@ -1,11 +1,16 @@ +import { existsSync, readdirSync, statSync, writeFileSync } from 'node:fs'; +import fs from 'node:fs/promises'; import path from 'node:path'; import { promisify } from 'node:util'; -import fs from 'fs-extra'; import * as validator from '../../registry/domain/validators'; import type { Component } from '../../types'; import requireTemplate from './handle-dependencies/require-template'; +const writeJsonSync = (path: string, data: unknown) => + writeFileSync(path, JSON.stringify(data, null, 2), 'utf-8'); +const readJson = (path: string) => fs.readFile(path, 'utf8').then(JSON.parse); + export interface PackageOptions { componentPath: string; minify?: boolean; @@ -19,7 +24,7 @@ interface Sizes { } function checkSizes(folder: string) { - const jsFiles = fs.readdirSync(folder).filter((x) => x.endsWith('.js')); + const jsFiles = readdirSync(folder).filter((x) => x.endsWith('.js')); const sizes: Sizes = { client: 0 @@ -27,9 +32,9 @@ function checkSizes(folder: string) { for (const file of jsFiles) { if (file === 'server.js') { - sizes.server = fs.statSync(path.join(folder, file)).size; + sizes.server = statSync(path.join(folder, file)).size; } else { - sizes.client += fs.statSync(path.join(folder, file)).size; + sizes.client += statSync(path.join(folder, file)).size; } } @@ -42,7 +47,7 @@ function addSizes(folder: string, component: Component, sizes: Sizes) { component.oc.files.dataProvider.size = sizes.server; } - fs.writeJsonSync(path.join(folder, 'package.json'), component, { spaces: 2 }); + writeJsonSync(path.join(folder, 'package.json'), component); } const packageComponents = @@ -56,17 +61,18 @@ const packageComponents = const componentPackagePath = path.join(componentPath, 'package.json'); const ocPackagePath = path.join(__dirname, '../../../package.json'); - if (!fs.existsSync(componentPackagePath)) { + if (!existsSync(componentPackagePath)) { throw new Error('component does not contain package.json'); } - if (!fs.existsSync(ocPackagePath)) { + if (!existsSync(ocPackagePath)) { throw new Error('error resolving oc internal dependencies'); } - await fs.emptyDir(publishPath); + await fs.rm(publishPath, { recursive: true, force: true }); + await fs.mkdir(publishPath); - const componentPackage: Component = await fs.readJson(componentPackagePath); - const ocPackage: Component = await fs.readJson(ocPackagePath); + const componentPackage: Component = await readJson(componentPackagePath); + const ocPackage: Component = await readJson(ocPackagePath); if (!validator.validateComponentName(componentPackage.name)) { throw new Error('name not valid'); diff --git a/src/cli/domain/registry.ts b/src/cli/domain/registry.ts index 9c49c830e..4f784ec43 100644 --- a/src/cli/domain/registry.ts +++ b/src/cli/domain/registry.ts @@ -1,5 +1,6 @@ +import { readFileSync } from 'node:fs'; +import fs from 'node:fs/promises'; import path from 'node:path'; -import fs from 'fs-extra'; import { request } from 'undici'; import * as urlBuilder from '../../registry/domain/url-builder'; @@ -8,9 +9,13 @@ import type { Component } from '../../types'; import put from '../../utils/put'; import * as urlParser from '../domain/url-parser'; +const readJsonSync = (path: string) => JSON.parse(readFileSync(path, 'utf8')); +const readJson = (path: string) => fs.readFile(path, 'utf8').then(JSON.parse); +const writeJson = (path: string, data: unknown) => + fs.writeFile(path, JSON.stringify(data), 'utf-8'); const getOcVersion = (): string => { const ocPackagePath = path.join(__dirname, '../../../package.json'); - const ocInfo = fs.readJsonSync(ocPackagePath); + const ocInfo = readJsonSync(ocPackagePath); return ocInfo.version; }; @@ -41,9 +46,7 @@ export default function registry(opts: RegistryOptions = {}) { if (!apiResponse) throw 'oc registry not available'; if (apiResponse.type !== 'oc-registry') throw 'not a valid oc registry'; - const res = await fs - .readJson(settings.configFile.src) - .catch(() => ({})); + const res = await readJson(settings.configFile.src).catch(() => ({})); if (!res.registries) { res.registries = []; @@ -53,7 +56,7 @@ export default function registry(opts: RegistryOptions = {}) { res.registries.push(registry); } - return fs.writeJson(settings.configFile.src, res); + return writeJson(settings.configFile.src, res); } catch (err) { throw 'oc registry not available'; } @@ -64,7 +67,7 @@ export default function registry(opts: RegistryOptions = {}) { } try { - const res = await fs.readJson(settings.configFile.src); + const res = await readJson(settings.configFile.src); if (!res.registries || res.registries.length === 0) throw 'No oc registries'; @@ -135,12 +138,12 @@ export default function registry(opts: RegistryOptions = {}) { registry += '/'; } - const res: { registries: string[] } = await fs - .readJson(settings.configFile.src) - .catch(() => ({ registries: [] })); + const res: { registries: string[] } = await readJson( + settings.configFile.src + ).catch(() => ({ registries: [] })); res.registries = res.registries.filter((x) => x !== registry); - await fs.writeJson(settings.configFile.src, res); + await writeJson(settings.configFile.src, res); } }; } diff --git a/src/cli/facade/publish.ts b/src/cli/facade/publish.ts index 8a497491c..0e8366875 100644 --- a/src/cli/facade/publish.ts +++ b/src/cli/facade/publish.ts @@ -1,7 +1,8 @@ +import { existsSync } from 'node:fs'; +import fs from 'node:fs/promises'; import path from 'node:path'; import { promisify } from 'node:util'; import colors from 'colors/safe'; -import fs from 'fs-extra'; import readCb from 'read'; import { fromPromise } from 'universalify'; import type { Component } from '../../types'; @@ -13,6 +14,7 @@ import handleDependencies from '../domain/handle-dependencies'; import type { RegistryCli } from '../domain/registry'; const read = promisify(readCb); +const readJson = (path: string) => fs.readFile(path, 'utf8').then(JSON.parse); const publish = ({ logger, @@ -44,7 +46,7 @@ const publish = ({ let errorMessage: string; const readPackageJson = () => - fs.readJson(path.join(packageDir, 'package.json')); + readJson(path.join(packageDir, 'package.json')); const getCredentials = async (): Promise<{ username: string; @@ -188,7 +190,7 @@ const publish = ({ }); await publishToRegistries(registryLocations, component); } else { - if (fs.existsSync(packageDir)) { + if (existsSync(packageDir)) { const component = await readPackageJson().catch((err) => { logger.err(String(err)); return Promise.reject(err); diff --git a/src/components/oc-client/package.json b/src/components/oc-client/package.json index 1e05e99e7..c105618e8 100644 --- a/src/components/oc-client/package.json +++ b/src/components/oc-client/package.json @@ -23,4 +23,4 @@ "devDependencies": { "oc-template-es6-compiler": "^1.0.1" } -} +} \ No newline at end of file diff --git a/src/registry/app-start.ts b/src/registry/app-start.ts index 6dacc94cf..0a8f920da 100644 --- a/src/registry/app-start.ts +++ b/src/registry/app-start.ts @@ -1,10 +1,11 @@ +import { readFileSync } from 'node:fs'; import path from 'node:path'; import colors from 'colors/safe'; -import fs from 'fs-extra'; import type { Config } from '../types'; import type { Repository } from './domain/repository'; -const packageInfo = fs.readJsonSync( +const readJsonSync = (path: string) => JSON.parse(readFileSync(path, 'utf8')); +const packageInfo = readJsonSync( path.join( __dirname, '..', diff --git a/src/registry/domain/get-package-json-from-temp-dir.ts b/src/registry/domain/get-package-json-from-temp-dir.ts index b23750ccb..92db1df24 100644 --- a/src/registry/domain/get-package-json-from-temp-dir.ts +++ b/src/registry/domain/get-package-json-from-temp-dir.ts @@ -1,9 +1,11 @@ +import fs from 'node:fs/promises'; import path from 'node:path'; -import fs from 'fs-extra'; import type { Component } from '../../types'; +const readJson = (path: string) => fs.readFile(path, 'utf8').then(JSON.parse); + export default function getPackageJsonFromTempDir( tempDirPath: string ): Promise { - return fs.readJson(path.join(tempDirPath, 'package.json')); + return readJson(path.join(tempDirPath, 'package.json')); } diff --git a/src/registry/domain/repository.ts b/src/registry/domain/repository.ts index db3705d36..0dec6a998 100644 --- a/src/registry/domain/repository.ts +++ b/src/registry/domain/repository.ts @@ -1,6 +1,7 @@ +import { lstatSync, readFileSync, readdirSync } from 'node:fs'; +import fs from 'node:fs/promises'; import path from 'node:path'; import dotenv from 'dotenv'; -import fs from 'fs-extra'; import getUnixUtcTimestamp from 'oc-get-unix-utc-timestamp'; import type { StorageAdapter } from 'oc-storage-adapters-utils'; @@ -20,7 +21,11 @@ import getPromiseBasedAdapter from './storage-adapter'; import * as validator from './validators'; import * as versionHandler from './version-handler'; -const packageInfo = fs.readJsonSync( +const readJsonSync = (path: string) => JSON.parse(readFileSync(path, 'utf8')); +const readJson = (path: string) => fs.readFile(path, 'utf8').then(JSON.parse); +const writeJson = (path: string, data: unknown) => + fs.writeFile(path, JSON.stringify(data, null, 2), 'utf-8'); +const packageInfo = readJsonSync( path.join(__dirname, '..', '..', '..', 'package.json') ); @@ -46,31 +51,29 @@ export default function repository(conf: Config) { const local = { getCompiledView(componentName: string): string { if (componentName === 'oc-client') { - return fs - .readFileSync( - path.join( - __dirname, - '../../components/oc-client/_package/template.js' - ) + return readFileSync( + path.join( + __dirname, + '../../components/oc-client/_package/template.js', + 'utf-8' ) - .toString(); + ).toString(); } - return fs - .readFileSync( - path.join(conf.path, `${componentName}/_package/template.js`) - ) - .toString(); + return readFileSync( + path.join(conf.path, `${componentName}/_package/template.js`), + 'utf-8' + ).toString(); }, getComponents(): string[] { const validComponents = conf.components || - fs.readdirSync(conf.path).filter((file) => { - const isDir = fs.lstatSync(path.join(conf.path, file)).isDirectory(); + readdirSync(conf.path).filter((file) => { + const isDir = lstatSync(path.join(conf.path, file)).isDirectory(); const isValidComponent = isDir - ? fs - .readdirSync(path.join(conf.path, file)) - .filter((file) => file === '_package').length === 1 + ? readdirSync(path.join(conf.path, file)).filter( + (file) => file === '_package' + ).length === 1 : false; return isValidComponent; @@ -81,9 +84,9 @@ export default function repository(conf: Config) { getComponentVersions(componentName: string): Promise { if (componentName === 'oc-client') { return Promise.all([ - fs - .readJson(path.join(__dirname, '../../../package.json')) - .then((x) => x.version) + readJson(path.join(__dirname, '../../../package.json')).then( + (x) => x.version + ) ]); } @@ -97,9 +100,9 @@ export default function repository(conf: Config) { } return Promise.all([ - fs - .readJson(path.join(conf.path, `${componentName}/package.json`)) - .then((x) => x.version) + readJson(path.join(conf.path, `${componentName}/package.json`)).then( + (x) => x.version + ) ]); }, getDataProvider(componentName: string) { @@ -111,17 +114,17 @@ export default function repository(conf: Config) { : path.join(conf.path, `${componentName}/_package/server.js`); return { - content: fs.readFileSync(filePath).toString(), + content: readFileSync(filePath).toString(), filePath }; }, getEnv(componentName: string): Record { - const pkg: Component = fs.readJsonSync( + const pkg: Component = readJsonSync( path.join(conf.path, `${componentName}/package.json`) ); const filePath = path.join(conf.path, componentName, pkg.oc.files.env!); - return dotenv.parse(fs.readFileSync(filePath).toString()); + return dotenv.parse(readFileSync(filePath).toString()); } }; @@ -180,14 +183,14 @@ export default function repository(conf: Config) { let componentInfo: Component; if (componentName === 'oc-client') { - componentInfo = fs.readJsonSync( + componentInfo = readJsonSync( path.join( __dirname, '../../components/oc-client/_package/package.json' ) ); } else { - componentInfo = fs.readJsonSync( + componentInfo = readJsonSync( path.join(conf.path, `${componentName}/_package/package.json`) ); } @@ -373,7 +376,7 @@ export default function repository(conf: Config) { if (dryRun) return; - await fs.writeJson( + await writeJson( path.join(pkgDetails.outputFolder, 'package.json'), pkgDetails.packageJson ); diff --git a/src/registry/domain/validators/node-version.ts b/src/registry/domain/validators/node-version.ts index 7a5ac3237..d760ed843 100644 --- a/src/registry/domain/validators/node-version.ts +++ b/src/registry/domain/validators/node-version.ts @@ -1,8 +1,9 @@ +import { readFileSync } from 'node:fs'; import path from 'node:path'; -import fs from 'fs-extra'; import semver from 'semver'; -const packageInfo = fs.readJsonSync( +const readJsonSync = (path: string) => JSON.parse(readFileSync(path, 'utf8')); +const packageInfo = readJsonSync( path.join(__dirname, '..', '..', '..', '..', 'package.json') ); diff --git a/src/registry/domain/validators/oc-cli-version.ts b/src/registry/domain/validators/oc-cli-version.ts index 4b660e181..b4620f305 100644 --- a/src/registry/domain/validators/oc-cli-version.ts +++ b/src/registry/domain/validators/oc-cli-version.ts @@ -1,8 +1,10 @@ +import { readFileSync } from 'node:fs'; import path from 'node:path'; -import fs from 'fs-extra'; import semver from 'semver'; -const packageInfo = fs.readJsonSync( +const readJsonSync = (filePath: string) => + JSON.parse(readFileSync(filePath, 'utf8')); +const packageInfo = readJsonSync( path.join(__dirname, '..', '..', '..', '..', 'package.json') ); diff --git a/src/registry/routes/index.ts b/src/registry/routes/index.ts index da7306a0a..8af55446a 100644 --- a/src/registry/routes/index.ts +++ b/src/registry/routes/index.ts @@ -1,6 +1,6 @@ +import { readFileSync } from 'node:fs'; import path from 'node:path'; import async from 'async'; -import fs from 'fs-extra'; import parseAuthor from 'parse-author'; import dateStringified from '../../utils/date-stringify'; @@ -15,7 +15,8 @@ import { fromPromise } from 'universalify'; import type { Author, Component, ParsedComponent } from '../../types'; import type { Repository } from '../domain/repository'; -const packageInfo: PackageJson = fs.readJsonSync( +const readJsonSync = (path: string) => JSON.parse(readFileSync(path, 'utf8')); +const packageInfo: PackageJson = readJsonSync( path.join(__dirname, '..', '..', '..', 'package.json') ); diff --git a/src/registry/routes/static-redirector.ts b/src/registry/routes/static-redirector.ts index 5d8bc28c7..2199c8d79 100644 --- a/src/registry/routes/static-redirector.ts +++ b/src/registry/routes/static-redirector.ts @@ -1,6 +1,6 @@ +import { createReadStream, existsSync, statSync } from 'node:fs'; import path from 'node:path'; import type { Request, Response } from 'express'; -import fs from 'fs-extra'; import * as storageUtils from 'oc-storage-adapters-utils'; import type { Repository } from '../domain/repository'; @@ -76,20 +76,20 @@ export default function staticRedirector(repository: Repository) { req.params[0]; } - if (!fs.existsSync(filePath)) { + if (!existsSync(filePath)) { res.errorDetails = `File ${filePath} not found`; res.status(404).json({ err: res.errorDetails }); return; } - const stats = fs.statSync(filePath); + const stats = statSync(filePath); if (stats.isDirectory()) { res.errorDetails = 'Forbidden: Directory Listing Denied'; res.status(403).json({ err: res.errorDetails }); return; } - const fileStream = fs.createReadStream(filePath); + const fileStream = createReadStream(filePath); const fileInfo = storageUtils.getFileInfo(filePath); if (fileInfo.mimeType) { diff --git a/src/utils/put.ts b/src/utils/put.ts index 4c2e4d11b..adccdda96 100644 --- a/src/utils/put.ts +++ b/src/utils/put.ts @@ -1,6 +1,6 @@ +import { createReadStream } from 'node:fs'; import path from 'node:path'; import FormData from 'form-data'; -import fs from 'fs-extra'; import { request } from 'undici'; async function put( @@ -16,7 +16,7 @@ async function put( for (const file of files) { const fileName = path.basename(file); - form.append(fileName, fs.createReadStream(file)); + form.append(fileName, createReadStream(file)); } const res = await request(urlPath, { diff --git a/tasks/build.js b/tasks/build.js index e03c5f5f5..421c3d258 100644 --- a/tasks/build.js +++ b/tasks/build.js @@ -1,4 +1,4 @@ -const fs = require('fs-extra'); +const { rmSync, mkdirSync, writeFileSync, cpSync } = require('node:fs'); const ocClientBrowser = require('oc-client-browser'); const log = require('./logger'); const path = require('node:path'); @@ -7,10 +7,14 @@ const packageJson = require('../package'); const ocVersion = packageJson.version; const clientComponentDir = '../src/components/oc-client/'; const ocClientPackageInfo = require(`${clientComponentDir}package.json`); +const writeJsonSync = (path, data) => + writeFileSync(path, JSON.stringify(data, null, 2)); log['start']('Building client'); -fs.emptyDirSync(path.join(__dirname, clientComponentDir, 'src')); +const srcPath = path.join(__dirname, clientComponentDir, 'src'); +rmSync(srcPath, { recursive: true, force: true }); +mkdirSync(srcPath); ocClientBrowser.getLibs((err, libs) => { if (err) { @@ -19,17 +23,17 @@ ocClientBrowser.getLibs((err, libs) => { const { dev, prod } = libs; ocClientPackageInfo.version = ocVersion; - fs.writeJsonSync( + writeJsonSync( path.join(__dirname, clientComponentDir, 'package.json'), ocClientPackageInfo, { spaces: 2 } ); - fs.writeFileSync( + writeFileSync( path.join(__dirname, clientComponentDir, 'src/oc-client.min.js'), prod ); - fs.writeFileSync( + writeFileSync( path.join(__dirname, clientComponentDir, 'src/oc-client.js'), dev ); @@ -38,7 +42,7 @@ ocClientBrowser.getLibs((err, libs) => { if (err) { log['error'](err); } - fs.writeFileSync( + writeFileSync( path.join(__dirname, clientComponentDir, 'src/oc-client.min.map'), mapContent ); @@ -54,9 +58,10 @@ ocClientBrowser.getLibs((err, libs) => { local .package(packageOptions) .then(() => { - fs.copySync( + cpSync( path.join(__dirname, clientComponentDir), - path.join(__dirname, clientComponentDir.replace('src', 'dist')) + path.join(__dirname, clientComponentDir.replace('src', 'dist')), + { recursive: true } ); log.complete('Client has been built and packaged'); }) diff --git a/tasks/mochaTest.js b/tasks/mochaTest.js index c172ca7e0..3878e9051 100644 --- a/tasks/mochaTest.js +++ b/tasks/mochaTest.js @@ -1,5 +1,5 @@ const async = require('async'); -const fs = require('fs-extra'); +const { readdirSync } = require('node:fs'); const glob = require('glob'); const log = require('./logger'); const Mocha = require('mocha'); @@ -20,9 +20,9 @@ if (argv.silent) { mocha.reporter('progress'); } -const componentsToPackage = fs - .readdirSync(componentsFixturesPath) - .filter((x) => x !== 'handlebars3-component'); +const componentsToPackage = readdirSync(componentsFixturesPath).filter( + (x) => x !== 'handlebars3-component' +); const packageComponent = (componentName, done) => oc.cli.package( diff --git a/tasks/npm-publish.js b/tasks/npm-publish.js index 2aa168ee6..86ab1a293 100644 --- a/tasks/npm-publish.js +++ b/tasks/npm-publish.js @@ -1,12 +1,14 @@ /* eslint-disable @typescript-eslint/no-var-requires */ -const fs = require('fs-extra'); +const { readFileSync } = require('node:fs'); const log = require('./logger'); const spawn = require('cross-spawn'); -const builtVersion = fs.readJsonSync( +const readJsonSync = (path) => JSON.parse(readFileSync(path, 'utf8')); + +const builtVersion = readJsonSync( './src/components/oc-client/_package/package.json' ).version; -const ocVersion = fs.readJsonSync('./package.json').version; +const ocVersion = readJsonSync('./package.json').version; if (builtVersion !== ocVersion) { log.fatal( diff --git a/tasks/version.js b/tasks/version.js index 37e8daa7d..7039f0d06 100644 --- a/tasks/version.js +++ b/tasks/version.js @@ -1,10 +1,11 @@ -const fs = require('fs-extra'); +const { readFileSync } = require('node:fs'); const log = require('./logger'); const packageJson = require('../package'); const minimist = require('minimist'); const path = require('node:path'); const semver = require('semver'); +const readJsonSync = (path) => JSON.parse(readFileSync(path, 'utf8')); const argv = minimist(process.argv.slice(2), { string: 'type', default: { type: false }, @@ -16,7 +17,7 @@ if ( (argv.type === 'patch' || argv.type === 'minor' || argv.type === 'major') ) { packageJson.version = semver.inc(packageJson.version, argv.type); - fs.writeJsonSync(path.join(__dirname, '../package.json'), packageJson, { + writeJsonSync(path.join(__dirname, '../package.json'), packageJson, { spaces: 2 }); log.complete(`Package version upgraded to: ${packageJson.version}`); diff --git a/test/integration/targz.js b/test/integration/targz.js index c0ac89cdc..7bfdc4d64 100644 --- a/test/integration/targz.js +++ b/test/integration/targz.js @@ -1,5 +1,5 @@ const expect = require('chai').expect; -const fs = require('fs-extra'); +const { existsSync, readFileSync } = require('node:fs'); const path = require('node:path'); const targz = require('targz'); @@ -28,7 +28,7 @@ describe('The targz dependency', () => { }); it('should create the file', () => { - expect(fs.existsSync(file)); + expect(existsSync(file)); }); describe('when decompressing the created file', () => { @@ -53,9 +53,9 @@ describe('The targz dependency', () => { }); it('should contain the files', () => { - expect(fs.existsSync(to)).to.be.true; + expect(existsSync(to)).to.be.true; expect( - fs.readFileSync(path.join(to, 'hello-world/template.html')).toString() + readFileSync(path.join(to, 'hello-world/template.html')).toString() ).to.be.equal('Hello world!'); }); }); diff --git a/test/unit/cli-domain-clean.js b/test/unit/cli-domain-clean.js index ab3ce32e1..d968ac086 100644 --- a/test/unit/cli-domain-clean.js +++ b/test/unit/cli-domain-clean.js @@ -10,9 +10,11 @@ describe('cli : domain : clean', () => { options.getComponentsByDirError ? Promise.reject(options.getComponentsByDirError) : Promise.resolve(['path/to/my-component1', 'path/to/my-component2']), - 'fs-extra': { - existsSync: (dir) => dir.indexOf('my-component1') >= 0, - remove: options.removeMock + 'node:fs': { + existsSync: (dir) => dir.indexOf('my-component1') >= 0 + }, + 'node:fs/promises': { + rmdir: options.rmdirMock }, 'node:path': { join: (...params) => params.join('/') } }); @@ -64,10 +66,10 @@ describe('cli : domain : clean', () => { describe('when removing the folders to clean', () => { describe('happy path', () => { let error; - let removeMock; + let rmdirMock; beforeEach((done) => { - removeMock = sinon.stub().resolves('ok'); - const clean = initialize({ removeMock }); + rmdirMock = sinon.stub().resolves('ok'); + const clean = initialize({ rmdirMock }); clean .remove(['path/to/my-component1/node_modules']) @@ -80,19 +82,19 @@ describe('cli : domain : clean', () => { it('should return no error', () => expect(error).to.be.undefined); it('should remove all the folders', () => { - expect(removeMock.args.length).to.equal(1); - expect(removeMock.args[0][0]).to.eql( + expect(rmdirMock.args.length).to.equal(1); + expect(rmdirMock.args[0][0]).to.eql( 'path/to/my-component1/node_modules' ); }); }); - describe('fs.remove error', () => { + describe('fs.rmdir error', () => { let error; - let removeMock; + let rmdirMock; beforeEach((done) => { - removeMock = sinon.stub().rejects(new Error('nope')); - const clean = initialize({ removeMock }); + rmdirMock = sinon.stub().rejects(new Error('nope')); + const clean = initialize({ rmdirMock }); clean .remove(['path/to/my-component1/node_modules']) @@ -106,8 +108,8 @@ describe('cli : domain : clean', () => { expect(error.toString()).to.be.equal('Error: nope')); it('should try removing all the folders', () => { - expect(removeMock.args.length).to.equal(1); - expect(removeMock.args[0][0]).to.eql( + expect(rmdirMock.args.length).to.equal(1); + expect(rmdirMock.args[0][0]).to.eql( 'path/to/my-component1/node_modules' ); }); diff --git a/test/unit/cli-domain-get-components-by-dir.js b/test/unit/cli-domain-get-components-by-dir.js index 33e0f1a1b..14ef2a0e5 100644 --- a/test/unit/cli-domain-get-components-by-dir.js +++ b/test/unit/cli-domain-get-components-by-dir.js @@ -6,7 +6,7 @@ const sinon = require('sinon'); const initialise = () => { const fsMock = { readdir: sinon.stub(), - readJsonSync: sinon.stub() + readFileSync: sinon.stub() }; const pathMock = { @@ -18,7 +18,8 @@ const initialise = () => { const GetComponentsByDir = injectr( '../../dist/cli/domain/get-components-by-dir.js', { - 'fs-extra': fsMock, + 'node:fs': fsMock, + 'node:fs/promises': fsMock, 'node:path': pathMock }, { __dirname: '' } @@ -49,15 +50,17 @@ describe('cli : domain : get-components-by-dir', () => { 'no-component-but-package-json' ]); - data.fs.readJsonSync.onCall(0).returns({ oc: {} }); - data.fs.readJsonSync + data.fs.readFileSync.onCall(0).returns(JSON.stringify({ oc: {} })); + data.fs.readFileSync .onCall(1) .throws(new Error('ENOENT: no such file or directory')); - data.fs.readJsonSync + data.fs.readFileSync .onCall(2) .throws(new Error('ENOENT: no such file or directory')); - data.fs.readJsonSync.onCall(3).returns({ oc: { packaged: true } }); - data.fs.readJsonSync.onCall(4).returns({}); + data.fs.readFileSync + .onCall(3) + .returns(JSON.stringify({ oc: { packaged: true } })); + data.fs.readFileSync.onCall(4).returns('{}'); executeComponentsListingByDir(data.local) .then((res) => { @@ -88,8 +91,8 @@ describe('cli : domain : get-components-by-dir', () => { .onCall(0) .resolves(['a-broken-component', 'another-component']); - data.fs.readJsonSync.onCall(0).throws(new Error('syntax error: fubar')); - data.fs.readJsonSync.onCall(1).returns({ oc: {} }); + data.fs.readFileSync.onCall(0).throws(new Error('syntax error: fubar')); + data.fs.readFileSync.onCall(1).returns(JSON.stringify({ oc: {} })); executeComponentsListingByDir(data.local) .then((res) => { @@ -120,11 +123,11 @@ describe('cli : domain : get-components-by-dir', () => { .onCall(0) .resolves(['a-broken-component', 'not-a-component-dir', 'file.json']); - data.fs.readJsonSync.onCall(0).throws(new Error('syntax error: fubar')); - data.fs.readJsonSync + data.fs.readFileSync.onCall(0).throws(new Error('syntax error: fubar')); + data.fs.readFileSync .onCall(1) .throws(new Error('ENOENT: no such file or directory')); - data.fs.readJsonSync + data.fs.readFileSync .onCall(2) .throws(new Error('ENOENT: no such file or directory')); @@ -163,11 +166,11 @@ describe('cli : domain : get-components-by-dir', () => { 'package.json' ]); - data.fs.readJsonSync.onCall(0).returns({ oc: {} }); - data.fs.readJsonSync.onCall(1).returns({ oc: {} }); - data.fs.readJsonSync.onCall(2).returns({ oc: {} }); - data.fs.readJsonSync.onCall(3).returns({ oc: {} }); - data.fs.readJsonSync + data.fs.readFileSync.onCall(0).returns(JSON.stringify({ oc: {} })); + data.fs.readFileSync.onCall(1).returns(JSON.stringify({ oc: {} })); + data.fs.readFileSync.onCall(2).returns(JSON.stringify({ oc: {} })); + data.fs.readFileSync.onCall(3).returns(JSON.stringify({ oc: {} })); + data.fs.readFileSync .onCall(4) .throws(new Error('ENOENT: no such file or directory')); diff --git a/test/unit/cli-domain-get-mocked-plugins.js b/test/unit/cli-domain-get-mocked-plugins.js index 4682200b1..339087d87 100644 --- a/test/unit/cli-domain-get-mocked-plugins.js +++ b/test/unit/cli-domain-get-mocked-plugins.js @@ -30,7 +30,8 @@ describe('cli : domain : get-mocked-plugins', () => { args.map((x) => x.replace(/\.\//g, '')).join(''); getMockedPlugins = injectr('../../dist/cli/domain/get-mocked-plugins.js', { - 'fs-extra': fsMock, + 'node:fs': fsMock, + 'node:fs/promises': fsMock, 'node:path': { join: pathJoinStub || fakePathFunc, resolve: fakePathFunc @@ -79,12 +80,12 @@ describe('cli : domain : get-mocked-plugins', () => { } }; - const readMock = sinon.stub().returns(ocJsonComponent); + const readMock = sinon.stub().returns(JSON.stringify(ocJsonComponent)); beforeEach(() => { initialise({ existsSync: sinon.stub().returns(true), - readJsonSync: readMock + readFileSync: readMock }); result = getMockedPlugins(logMock, '/root/components/'); }); @@ -118,8 +119,10 @@ describe('cli : domain : get-mocked-plugins', () => { const readMock = sinon.stub(); const existsMock = sinon.stub(); - readMock.withArgs('/root/components/oc.json').returns(ocJsonComponent); - readMock.withArgs('/root/oc.json').returns(ocJsonRoot); + readMock + .withArgs('/root/components/oc.json') + .returns(JSON.stringify(ocJsonComponent)); + readMock.withArgs('/root/oc.json').returns(JSON.stringify(ocJsonRoot)); existsMock.withArgs('/root/components/oc.json').returns(false); existsMock.withArgs('/root/oc.json').returns(true); @@ -127,7 +130,7 @@ describe('cli : domain : get-mocked-plugins', () => { beforeEach(() => { initialise({ existsSync: existsMock, - readJsonSync: readMock + readFileSync: readMock }); result = getMockedPlugins(logMock, '/root/components/'); }); @@ -161,7 +164,7 @@ describe('cli : domain : get-mocked-plugins', () => { beforeEach(() => { initialise({ existsSync: sinon.stub().returns(true), - readJsonSync: sinon.stub().returns(ocJson) + readFileSync: sinon.stub().returns(JSON.stringify(ocJson)) }); result = getMockedPlugins(logMock, '/root/components/'); }); @@ -187,7 +190,7 @@ describe('cli : domain : get-mocked-plugins', () => { beforeEach(() => { initialise({ existsSync: sinon.stub().returns(true), - readJsonSync: sinon.stub().returns(ocJson) + readFileSync: sinon.stub().returns(JSON.stringify(ocJson)) }); result = getMockedPlugins(logMock, '/root/components/'); }); @@ -218,7 +221,7 @@ describe('cli : domain : get-mocked-plugins', () => { beforeEach(() => { initialise({ existsSync: sinon.stub().returns(true), - readJsonSync: sinon.stub().returns(ocJson) + readFileSync: sinon.stub().returns(JSON.stringify(ocJson)) }); result = getMockedPlugins(logMock, '/root/components/'); }); @@ -250,7 +253,7 @@ describe('cli : domain : get-mocked-plugins', () => { beforeEach(() => { initialise({ existsSync: sinon.stub().returns(true), - readJsonSync: sinon.stub().returns(ocJson) + readFileSync: sinon.stub().returns(JSON.stringify(ocJson)) }); result = getMockedPlugins(logMock, '/root/components/'); }); @@ -283,7 +286,7 @@ describe('cli : domain : get-mocked-plugins', () => { beforeEach(() => { initialise({ existsSync: sinon.stub().returns(true), - readJsonSync: sinon.stub().returns(ocJson) + readFileSync: sinon.stub().returns(JSON.stringify(ocJson)) }); result = getMockedPlugins(logger, '/root/components/'); }); @@ -315,7 +318,7 @@ describe('cli : domain : get-mocked-plugins', () => { beforeEach(() => { initialise({ existsSync: sinon.stub().returns(true), - readJsonSync: sinon.stub().returns(ocJson) + readFileSync: sinon.stub().returns(JSON.stringify(ocJson)) }); result = getMockedPlugins(logger, '/root/components/'); }); diff --git a/test/unit/cli-domain-handle-dependencies.js b/test/unit/cli-domain-handle-dependencies.js index a82795a3e..8dc0a9095 100644 --- a/test/unit/cli-domain-handle-dependencies.js +++ b/test/unit/cli-domain-handle-dependencies.js @@ -48,9 +48,11 @@ describe('cli : domain : handle-dependencies', () => { handleDependencies = injectr( '../../dist/cli/domain/handle-dependencies/index.js', { - 'fs-extra': { - readJson: (path) => - Promise.resolve(components[path.replace('/package.json', '')]) + 'node:fs/promises': { + readFile: (path) => + Promise.resolve( + JSON.stringify(components[path.replace('/package.json', '')]) + ) }, 'node:path': { join: (...args) => args.join('/') }, './ensure-compiler-is-declared-as-devDependency': (options) => { diff --git a/test/unit/cli-domain-init-template.js b/test/unit/cli-domain-init-template.js index 900ec6741..2eab66c33 100644 --- a/test/unit/cli-domain-init-template.js +++ b/test/unit/cli-domain-init-template.js @@ -8,8 +8,8 @@ describe('cli : domain : init-template', () => { const initTemplate = injectr( '../../dist/cli/domain/init-template/index.js', { - 'fs-extra': { - ensureDir: stubs.fsExtraStub + 'node:fs/promises': { + mkdir: stubs.fsStub }, 'node:path': { join: (...args) => args.join('/') }, './install-template': stubs.installTemplateStub, @@ -37,14 +37,14 @@ describe('cli : domain : init-template', () => { }; describe('happy path', () => { - const fsExtraStub = sinon.stub().resolves('ok'); + const fsStub = sinon.stub().resolves('ok'); const npmStub = sinon.stub().resolves('ok'); const installTemplateStub = sinon.stub().resolves('ok'); const scaffoldStub = sinon.stub().resolves('ok'); beforeEach( initialise({ - fsExtraStub, + fsStub, npmStub, installTemplateStub, scaffoldStub @@ -56,7 +56,7 @@ describe('cli : domain : init-template', () => { }); it('should call ensureDir with correct params', () => { - expect(fsExtraStub.args[0][0]).to.equal('path/to/new-component'); + expect(fsStub.args[0][0]).to.equal('path/to/new-component'); }); it('should call npm init with correct parameters', () => { @@ -97,7 +97,7 @@ describe('cli : domain : init-template', () => { describe('when component folder creation fails', () => { beforeEach( initialise({ - fsExtraStub: sinon.stub().rejects(new Error('folder creation error')) + fsStub: sinon.stub().rejects(new Error('folder creation error')) }) ); @@ -109,7 +109,7 @@ describe('cli : domain : init-template', () => { describe('when npm init fails', () => { beforeEach( initialise({ - fsExtraStub: sinon.stub().resolves('ok'), + fsStub: sinon.stub().resolves('ok'), npmStub: sinon.stub().rejects(new Error('npm init failed')) }) ); @@ -122,7 +122,7 @@ describe('cli : domain : init-template', () => { describe('when template compiler installation fails', () => { beforeEach( initialise({ - fsExtraStub: sinon.stub().resolves('ok'), + fsStub: sinon.stub().resolves('ok'), npmStub: sinon.stub().resolves('ok'), installTemplateStub: sinon .stub() @@ -138,7 +138,7 @@ describe('cli : domain : init-template', () => { describe('when template compiler installation fails', () => { beforeEach( initialise({ - fsExtraStub: sinon.stub().resolves('ok'), + fsStub: sinon.stub().resolves('ok'), npmStub: sinon.stub().resolves('ok'), installTemplateStub: sinon.stub().resolves('ok'), scaffoldStub: sinon.stub().rejects(new Error('scaffolding failed')) diff --git a/test/unit/cli-domain-mock.js b/test/unit/cli-domain-mock.js index 90def78aa..2fe098881 100644 --- a/test/unit/cli-domain-mock.js +++ b/test/unit/cli-domain-mock.js @@ -6,8 +6,8 @@ const sinon = require('sinon'); const initialise = () => { const fsMock = { existsSync: sinon.stub(), - readJson: sinon.stub(), - writeJson: sinon.stub().resolves('ok') + readFile: sinon.stub(), + writeFile: sinon.stub().resolves('ok') }; const pathMock = { @@ -19,7 +19,8 @@ const initialise = () => { const Local = injectr( '../../dist/cli/domain/mock.js', { - 'fs-extra': fsMock, + 'node:fs': fsMock, + 'node:fs/promises': fsMock, path: pathMock }, { __dirname: '' } @@ -45,24 +46,30 @@ describe('cli : domain : mock', () => { beforeEach((done) => { data = initialise(); - data.fs.readJson.resolves({ something: 'hello' }); - data.fs.writeJson.resolves('ok'); + data.fs.readFile.resolves(JSON.stringify({ something: 'hello' })); + data.fs.writeFile.resolves('ok'); executeMocking(data.local, 'plugin', 'getValue', 'value', done); }); it('should add mock to oc.json', () => { - expect(data.fs.writeJson.called).to.be.true; - expect(data.fs.writeJson.args[0][1]).to.eql({ - something: 'hello', - mocks: { - plugins: { - static: { - getValue: 'value' + expect(data.fs.writeFile.called).to.be.true; + expect(data.fs.writeFile.args[0][1]).to.eql( + JSON.stringify( + { + something: 'hello', + mocks: { + plugins: { + static: { + getValue: 'value' + } + } } - } - } - }); + }, + null, + 2 + ) + ); }); }); @@ -71,24 +78,30 @@ describe('cli : domain : mock', () => { beforeEach((done) => { data = initialise(); - data.fs.readJson.resolves({ something: 'hello' }); - data.fs.writeJson.resolves('ok'); + data.fs.readFile.resolves(JSON.stringify({ something: 'hello' })); + data.fs.writeFile.resolves('ok'); executeMocking(data.local, 'plugin', 'isTrue', false, done); }); it('should add mock to oc.json', () => { - expect(data.fs.writeJson.called).to.be.true; - expect(data.fs.writeJson.args[0][1]).to.eql({ - something: 'hello', - mocks: { - plugins: { - static: { - isTrue: false + expect(data.fs.writeFile.called).to.be.true; + expect(data.fs.writeFile.args[0][1]).to.eql( + JSON.stringify( + { + something: 'hello', + mocks: { + plugins: { + static: { + isTrue: false + } + } } - } - } - }); + }, + null, + 2 + ) + ); }); }); @@ -97,25 +110,31 @@ describe('cli : domain : mock', () => { beforeEach((done) => { data = initialise(); - data.fs.readJson.resolves({ something: 'hello' }); + data.fs.readFile.resolves(JSON.stringify({ something: 'hello' })); data.fs.existsSync.returns(true); - data.fs.writeJson.resolves('ok'); + data.fs.writeFile.resolves('ok'); executeMocking(data.local, 'plugin', 'getValue', './value.js', done); }); it('should add mock to oc.json', () => { - expect(data.fs.writeJson.called).to.be.true; - expect(data.fs.writeJson.args[0][1]).to.eql({ - something: 'hello', - mocks: { - plugins: { - dynamic: { - getValue: './value.js' + expect(data.fs.writeFile.called).to.be.true; + expect(data.fs.writeFile.args[0][1]).to.eql( + JSON.stringify( + { + something: 'hello', + mocks: { + plugins: { + dynamic: { + getValue: './value.js' + } + } } - } - } - }); + }, + null, + 2 + ) + ); }); }); }); diff --git a/test/unit/cli-domain-package-components.js b/test/unit/cli-domain-package-components.js index c7756fda0..1b2ec5eb7 100644 --- a/test/unit/cli-domain-package-components.js +++ b/test/unit/cli-domain-package-components.js @@ -20,16 +20,17 @@ describe('cli : domain : package-components', () => { const fsMock = { existsSync: sinon.stub(), - emptyDir: sinon.stub().resolves(), - readJson: sinon.stub(), + mkdir: sinon.stub().resolves(), + rm: sinon.stub().resolves(), + readFile: sinon.stub(), readdirSync: sinon.stub().returns(['template.js']), statSync: sinon.stub().returns({ size: 300 }), - writeJsonSync: sinon.stub().returns({}) + writeFileSync: sinon.stub().returns({}) }; fsMock.existsSync.returns(true); - fsMock.readJson.onCall(0).resolves(component); - fsMock.readJson.onCall(1).resolves({ version: '1.2.3' }); + fsMock.readFile.onCall(0).resolves(JSON.stringify(component)); + fsMock.readFile.onCall(1).resolves(JSON.stringify({ version: '1.2.3' })); const pathMock = { join: () => '' @@ -50,7 +51,8 @@ describe('cli : domain : package-components', () => { const PackageComponents = injectr( '../../dist/cli/domain/package-components.js', { - 'fs-extra': fsMock, + 'node:fs': fsMock, + 'node:fs/promises': fsMock, path: pathMock, './handle-dependencies/require-template': requireTemplateMock }, diff --git a/test/unit/cli-domain-registry.js b/test/unit/cli-domain-registry.js index e0cae7b06..01b53fdaa 100644 --- a/test/unit/cli-domain-registry.js +++ b/test/unit/cli-domain-registry.js @@ -4,12 +4,15 @@ const sinon = require('sinon'); const getRegistry = (dependencies, opts) => { dependencies.fs = dependencies.fs || {}; - dependencies.fs.readJsonSync = sinon.stub().returns({ version: '1.2.3' }); + dependencies.fs.readFileSync = sinon + .stub() + .returns(JSON.stringify({ version: '1.2.3' })); const Registry = injectr( '../../dist/cli/domain/registry.js', { undici: dependencies.undici, - 'fs-extra': dependencies.fs, + 'node:fs/promises': dependencies.fs, + 'node:fs': dependencies.fs, '../../utils/put': dependencies.put, '../domain/url-parser': dependencies.urlParser, 'node:path': { @@ -65,11 +68,11 @@ describe('cli : domain : registry', () => { }) }; const fsStub = { - readJson: sinon.stub(), - writeJson: sinon.spy() + readFile: sinon.stub(), + writeFile: sinon.stub().resolves() }; - fsStub.readJson.resolves({}); + fsStub.readFile.resolves('{}'); const registry = getRegistry({ undici: undiciStub, @@ -77,9 +80,11 @@ describe('cli : domain : registry', () => { }); registry.add('http://some-api.com/asd').finally(() => { - expect(fsStub.writeJson.getCall(0).args[1]).to.eql({ - registries: ['http://some-api.com/asd/'] - }); + expect(fsStub.writeFile.getCall(0).args[1]).to.eql( + JSON.stringify({ + registries: ['http://some-api.com/asd/'] + }) + ); done(); }); }); diff --git a/test/unit/cli-facade-publish.js b/test/unit/cli-facade-publish.js index 3904d018b..fe0c2b409 100644 --- a/test/unit/cli-facade-publish.js +++ b/test/unit/cli-facade-publish.js @@ -27,12 +27,13 @@ describe('cli : facade : publish', () => { const fsMock = Object.assign( { existsSync: sinon.stub().returns(true), - readJson: sinon.stub().resolves(mockComponent) + readFile: sinon.stub().resolves(JSON.stringify(mockComponent)) }, fs ); const PublishFacade = injectr('../../dist/cli/facade/publish.js', { - 'fs-extra': fsMock, + 'node:fs': fsMock, + 'node:fs/promises': fsMock, read: readStub }).default; const publishFacade = PublishFacade({ diff --git a/test/unit/registry-app-start.js b/test/unit/registry-app-start.js index c4116a0b7..beb65ffc1 100644 --- a/test/unit/registry-app-start.js +++ b/test/unit/registry-app-start.js @@ -7,7 +7,9 @@ const getAppStart = (mockedRepository, options, callback) => { const appStart = injectr( '../../dist/registry/app-start.js', { - 'fs-extra': { readJsonSync: sinon.stub().returns({ version: '1.2.3' }) } + 'node:fs': { + readFileSync: sinon.stub().returns(JSON.stringify({ version: '1.2.3' })) + } }, { console: { log: sinon.stub() }, diff --git a/test/unit/registry-domain-repository.js b/test/unit/registry-domain-repository.js index 9bc80dfe7..37b1859bf 100644 --- a/test/unit/registry-domain-repository.js +++ b/test/unit/registry-domain-repository.js @@ -1,9 +1,11 @@ const expect = require('chai').expect; -const fs = require('fs-extra'); +const { readFileSync, readdirSync, lstatSync } = require('node:fs'); const injectr = require('injectr'); const path = require('node:path'); const sinon = require('sinon'); +const readJsonSync = (path) => JSON.parse(readFileSync(path, 'utf8')); + describe('registry : domain : repository', () => { let response; const savePromiseResult = (promise, done) => { @@ -30,11 +32,11 @@ describe('registry : domain : repository', () => { }; const fsMock = { - readFileSync: fs.readFileSync, - readdirSync: fs.readdirSync, - lstatSync: fs.lstatSync, - readJsonSync: fs.readJsonSync, - writeJson: () => Promise.resolve() + readFileSync: readFileSync, + readdirSync: readdirSync, + lstatSync: lstatSync, + readJsonSync: readJsonSync, + writeFile: () => Promise.resolve() }; const s3Mock = { @@ -50,7 +52,8 @@ describe('registry : domain : repository', () => { const Repository = injectr( '../../dist/registry/domain/repository.js', { - 'fs-extra': fsMock, + 'node:fs': fsMock, + 'node:fs/promises': fsMock, './components-cache': () => componentsCacheMock, './components-details': () => componentsDetailsMock }, diff --git a/test/unit/registry-domain-validator.js b/test/unit/registry-domain-validator.js index 9d536f541..305c8f74f 100644 --- a/test/unit/registry-domain-validator.js +++ b/test/unit/registry-domain-validator.js @@ -967,8 +967,8 @@ describe('registry : domain : validator', () => { './oc-cli-version': injectr( '../../dist/registry/domain/validators/oc-cli-version.js', { - 'fs-extra': { - readJsonSync: () => ({ version: '0.16.34' }) + 'node:fs': { + readFileSync: () => JSON.stringify({ version: '0.16.34' }) } }, { @@ -1048,8 +1048,9 @@ describe('registry : domain : validator', () => { './node-version': injectr( '../../dist/registry/domain/validators/node-version.js', { - 'fs-extra': { - readJsonSync: () => ({ engines: { node: '>=0.10.35' } }) + 'node:fs': { + readFileSync: () => + JSON.stringify({ engines: { node: '>=0.10.35' } }) } }, {