Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move from fs-extra to node:fs #1378

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -120,4 +118,4 @@
"universalify": "^2.0.0",
"yargs": "^17.7.2"
}
}
}
7 changes: 4 additions & 3 deletions src/cli/domain/clean.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -11,11 +12,11 @@ export async function fetchList(dirPath: string): Promise<string[]> {

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<void> {
for (const item of list) {
await fs.remove(item);
await fs.rmdir(item, { recursive: true });
}
}
7 changes: 5 additions & 2 deletions src/cli/domain/get-components-by-dir.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,7 +16,7 @@ export default function getComponentsByDir() {
let content: Component;

try {
content = fs.readJsonSync(packagePath);
content = readJsonSync(packagePath);
} catch (err) {
return false;
}
Expand Down
10 changes: 6 additions & 4 deletions src/cli/domain/get-mocked-plugins.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions src/cli/domain/handle-dependencies/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<Component> =>
fs.readJson(path.join(componentPath, 'package.json'));
readJson(path.join(componentPath, 'package.json'));

const union = (a: ReadonlyArray<string>, b: ReadonlyArray<string>) => [
...new Set([...a, ...b])
Expand Down
4 changes: 2 additions & 2 deletions src/cli/domain/init-template/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 }));
Expand Down
16 changes: 9 additions & 7 deletions src/cli/domain/init-template/scaffold.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import fs from 'fs-extra';

import strings from '../../../resources';

Expand All @@ -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 }> {
Expand All @@ -19,24 +23,22 @@ 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;
componentPackage.scripts ??= {};
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/domain/local.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
17 changes: 11 additions & 6 deletions src/cli/domain/mock.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
const localConfig = await fs
.readJson(settings.configFile.src)
.catch(() => ({}));
const localConfig = await readJson(settings.configFile.src).catch(
() => ({})
);

const mockType = params.targetType + 's';

Expand All @@ -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';
}

Expand All @@ -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);
};
}
26 changes: 16 additions & 10 deletions src/cli/domain/package-components.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,17 +24,17 @@ 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
};

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;
}
}

Expand All @@ -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 =
Expand All @@ -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');
Expand Down
Loading
Loading