Skip to content

Commit

Permalink
feat: remove fs-extra as much as possible
Browse files Browse the repository at this point in the history
only generate needs fs-extra copySync now, and it is a dev dep.
  • Loading branch information
meixg committed Mar 29, 2022
1 parent b5c2d38 commit f21c376
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 45 deletions.
24 changes: 13 additions & 11 deletions packages/cli/__tests__/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import inquirer from 'inquirer';
import {mockProcessExit, mockConsoleLog} from 'jest-mock-process';
import {cli} from '../src/generate';
import {getHome} from '../src/util';
import {copySync, mkdirpSync, readJsonSync, removeSync, writeJsonSync} from 'fs-extra';
import {mkdirSync, writeFileSync} from 'fs';
import {copySync} from 'fs-extra';
import {fs} from '@hoth/utils';

/* eslint-disable @typescript-eslint/no-var-requires */

Expand Down Expand Up @@ -53,7 +55,7 @@ describe('hoth cli generate', () => {
await cli([workdir]);

expect(mockLog).toHaveBeenCalledWith('generated package.json');
const pkg = require(`${workdir}/package.json`);
const pkg = await fs.readJson(`${workdir}/package.json`);
expect(pkg.name).toBe('@baidu/myapp-node-ui');
expect(pkg.private).toBe(true);

Expand All @@ -70,7 +72,7 @@ describe('hoth cli generate', () => {
await cli([workdir]);

expect(mockLog).toHaveBeenCalledWith('generated package.json');
const pkg = readJsonSync(`${workdir}/package.json`);
const pkg = await fs.readJson(`${workdir}/package.json`);
expect(pkg.name).toBe('@baidu/myapp-node-ui');
expect(pkg.private).toBe(true);
// expect(pkg.dependencies['@hoth/cli']).toBe(`^${require('../package.json').version}`);
Expand All @@ -89,7 +91,7 @@ describe('hoth cli generate', () => {
});
const mockLog = mockConsoleLog();
await cli(['--app-name=myapp', workdir]);
const pkg = readJsonSync(`${workdir}/package.json`);
const pkg = await fs.readJson(`${workdir}/package.json`);
expect(pkg.name).toBe('@baidu/myapp-node-ui');
mockLog.mockRestore();
});
Expand Down Expand Up @@ -122,23 +124,23 @@ describe('hoth cli generate', () => {
const home = getHome();
const repoDir = join(home, 'repo', 'hoth-template');

removeSync(repoDir);
mkdirpSync(repoDir);
await fs.rm(repoDir, {recursive: true, force: true});
mkdirSync(repoDir, {recursive: true});
copySync(join(__dirname, '..', 'hoth-template'), repoDir);
const jsonfile = join(repoDir, 'templates', 'normal', 'package.json');
const json = readJsonSync(jsonfile);
const json = await fs.readJson(jsonfile);
json.description = 'test';
writeJsonSync(jsonfile, json);
writeFileSync(jsonfile, JSON.stringify(json));


await cli([workdir, '--sub-app', '--repo', 'https://github.com/searchfe/hoth-template']);

const pkg = readJsonSync(`${workdir}/package.json`);
const pkg = await fs.readJson(`${workdir}/package.json`);
expect(pkg.name).toBe('@baidu/myapp-node-ui');
expect(pkg.private).toBe(true);
expect(pkg.description).toBe('test');

removeSync(repoDir);
await fs.rm(repoDir, {recursive: true, force: true});

mockLog.mockRestore();
});
Expand All @@ -154,7 +156,7 @@ describe('hoth cli generate', () => {

await cli([workdir, '--sub-app']);

const pkg = readJsonSync(`${workdir}/package.json`);
const pkg = await fs.readJson(`${workdir}/package.json`);
expect(pkg.name).toBe('@baidu/myapp-node-ui');
expect(pkg.private).toBe(true);
// expect(pkg.devDependencies['@hoth/cli']).toBe(`^${require('../package.json').version}`);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/__tests__/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {mockProcessExit, mockConsoleLog} from 'jest-mock-process';
import fs from 'fs';
import path from 'path';
import os from 'os';
import {removeSync} from 'fs-extra';
import {fs as utilsFs} from '@hoth/utils';

describe('hoth cli util', () => {

Expand Down Expand Up @@ -32,7 +32,7 @@ describe('hoth cli util', () => {
});

it('get home path', async () => {
removeSync(path.join(os.homedir(), '.hoth'));
await utilsFs.rm(path.join(os.homedir(), '.hoth'), {recursive: true, force: true});
expect(fs.existsSync(getHome())).toBe(true);
});
});
6 changes: 4 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"commist": "^1.1.0",
"dotenv": "^10.0.0",
"fastify-warmup": "0.1.1",
"fs-extra": "^10.0.1",
"generify": "^4.2.0",
"help-me": "^2.0.0",
"inquirer": "^8.0.0",
Expand All @@ -50,5 +49,8 @@
"help",
"hoth-template"
],
"gitHead": "0735cada835eabb2ac787ca1c5f2ff4bb8310fe2"
"gitHead": "0735cada835eabb2ac787ca1c5f2ff4bb8310fe2",
"devDependencies": {
"fs-extra": "^10.0.1"
}
}
30 changes: 14 additions & 16 deletions packages/cli/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {exit} from '@hoth/utils';
import {existsSync, mkdirpSync, readdirSync, readJsonSync, writeJsonSync} from 'fs-extra';
import {exit, fs as utilsFs} from '@hoth/utils';
import {existsSync, readdirSync, writeFileSync, mkdirSync} from 'fs';
import {join} from 'path';
import generify from 'generify';
import chalk from 'chalk';
Expand Down Expand Up @@ -97,7 +97,7 @@ export async function cli(args: string[]) {
const subDir = repo.split('/').pop()?.split('.git')[0];
const repoDir = join(getHome(), 'repo');
repoTemplatesDir = join(repoDir, subDir || '', 'templates');
mkdirpSync(repoDir);
mkdirSync(repoDir, {recursive: true});
if (existsSync(repoTemplatesDir)) {
try {
console.log('start to git pull templates:', repo, repoTemplatesDir);
Expand Down Expand Up @@ -179,19 +179,17 @@ export async function cli(args: string[]) {
return;
}

return generate(dir, selectedTempate, data).then(() => {
// 做一个小优化,子app不需要在正式依赖里安装@hoth/cli和fastify
if (opts.subApp) {
const json = readJsonSync(join(dir, 'package.json'));
for (const k of ['@hoth/cli', 'fastify', 'teth-sdk']) {
if (json.dependencies[k]) {
json.devDependencies[k] = json.dependencies[k];
delete json.dependencies[k];
}
await generate(dir, selectedTempate, data);

// 做一个小优化,子app不需要在正式依赖里安装@hoth/cli和fastify
if (opts.subApp) {
const json = await utilsFs.readJson(join(dir, 'package.json'));
for (const k of ['@hoth/cli', 'fastify', 'teth-sdk']) {
if (json.dependencies[k]) {
json.devDependencies[k] = json.dependencies[k];
delete json.dependencies[k];
}
writeJsonSync(join(dir, 'package.json'), json, {
spaces: 4
});
}
});
writeFileSync(join(dir, 'package.json'), JSON.stringify(json, null, 4));
}
}
4 changes: 2 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import updateNotifier from 'update-notifier';

import {cli as startCli} from './start';
import {cli as generate} from './generate';
import {readJsonSync} from 'fs-extra';
import {fs} from '@hoth/utils';

const commander = commist();
const help = helpMe({
Expand All @@ -28,7 +28,7 @@ commander.register('version', function () {
commander.register('start', startCli);
commander.register('generate', generate);

const pkg: Record<string, unknown> = readJsonSync(path.resolve(__dirname, '../package.json'));
const pkg: Record<string, unknown> = fs.readJsonSync(path.resolve(__dirname, '../package.json'));
const notifier = updateNotifier({
pkg,
updateCheckInterval: 1000 * 60 * 60 * 24 * 3,
Expand Down
11 changes: 6 additions & 5 deletions packages/logger/__tests__/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import getLogger from '../src/index';
import format from '../src/format';
import {compile} from '../src/compile';
import {join} from 'path';
import {removeSync, readFileSync, existsSync} from 'fs-extra';
import {readFileSync, existsSync} from 'fs';
import {fs} from '@hoth/utils';
import {advanceTo, clear} from 'jest-date-mock';
import mockConsole from 'jest-mock-console';
import {symbols} from 'pino';
Expand All @@ -13,7 +14,7 @@ describe('@hoth/logger logger', function () {
process.env.HOTH_CLUSTER = 'test';

const rootPath = join(__dirname, 'logs');
removeSync(rootPath);
fs.rmSync(rootPath, {recursive: true, force: true});

advanceTo(new Date(2021, 1, 1, 0, 0, 0));
const restoreConsole = mockConsole();
Expand All @@ -30,12 +31,12 @@ describe('@hoth/logger logger', function () {
]
});

const stream: any = logger[symbols.streamSym as any];
const stream = logger[symbols.streamSym as any];

afterAll(() => {
afterAll(async () => {
clear();
restoreConsole();
removeSync(rootPath);
await fs.rm(rootPath, {recursive: true, force: true});
});

it('file created', function () {
Expand Down
9 changes: 5 additions & 4 deletions packages/logger/__tests__/middleware.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import getLogger, {preHandler} from '../src/index';
import {join} from 'path';
import {removeSync, readFileSync} from 'fs-extra';
import {readFileSync} from 'fs';
import {advanceTo, clear} from 'jest-date-mock';
import mockConsole from 'jest-mock-console';
import fastify from 'fastify';
import {fs} from '@hoth/utils';

describe('@hoth/logger logger', function () {

process.env.HOTH_IDC = 'all';
process.env.HOTH_CLUSTER = 'test';

const rootPath = join(__dirname, 'logs2');
removeSync(rootPath);
fs.rmSync(rootPath, {recursive: true, force: true});

advanceTo(new Date(2021, 1, 1, 0, 0, 0));
const restoreConsole = mockConsole();
Expand Down Expand Up @@ -43,10 +44,10 @@ describe('@hoth/logger logger', function () {
reply.send('ok');
});

afterAll(() => {
afterAll(async () => {
clear();
restoreConsole();
removeSync(rootPath);
await fs.rm(rootPath, {recursive: true, force: true});
});

it('test', async done => {
Expand Down
1 change: 0 additions & 1 deletion packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"dependencies": {
"file-stream-rotator": "^0.5.7",
"flatstr": "^1.0.12",
"fs-extra": "^9.1.0",
"lodash": "^4.17.21",
"pino": "^7.9.2",
"resolve-from": "^5.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs-extra';
import fs from 'fs';
import path from 'path';
import pino from 'pino';
import * as fileStreamRotator from 'file-stream-rotator';
Expand Down Expand Up @@ -51,7 +51,7 @@ export default function createLogger(options: LoggerOptions) {

for (const {name} of apps) {
const logPath = path.join(rootPath, 'log', name);
fs.ensureDirSync(logPath);
fs.mkdirSync(logPath, {recursive: true});
const files = [
`${name}.log.ti`,
`${name}.log`,
Expand Down
40 changes: 40 additions & 0 deletions packages/utils/src/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @file this file is supposed to replace 'fa-extra' for the reason that most
* APIs have already been provided in Node.js v14 or newer. We can remove some
* deps and progressively move to new APIs by using this file.
*/
import fs, {promises as fsPromises} from 'fs';

export async function rm(path: string, options?: Parameters<typeof fsPromises.rm>[1]) {
// @ts-ignore
if (fsPromises.rm) {
return fsPromises.rm(path, options);
}

// below Node.js v14.14
// remove when we stop support Node.js v12
return fsPromises.rmdir(path, {
recursive: options?.recursive
});
}

export function rmSync(path: string, options?: Parameters<typeof fs.rmSync>[1]) {
// @ts-ignore
if (fs.rmSync) {
return fs.rmSync(path, options);
}

return fs.rmdirSync(path, {
recursive: options?.recursive
});
}

export async function readJson(path: string) {
const content = await fsPromises.readFile(path, 'utf-8');
return JSON.parse(content);
}

export function readJsonSync(path: string) {
const content = fs.readFileSync(path, 'utf-8');
return JSON.parse(content);
}
2 changes: 2 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ export async function loadModule(module: string): Promise<any> {
}

export function noop() {} // eslint-disable-line

export * as fs from './fs';

0 comments on commit f21c376

Please sign in to comment.