Skip to content

Commit

Permalink
chore: code style
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Nov 25, 2024
1 parent 9812357 commit 3767d84
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import chokidar from 'chokidar';
import path from 'pathe';
import { BundlerType, createBundler } from './bundler/bundler';
import * as logger from './fishkit/logger';
import { PluginHookType } from './plugin/plugin_manager';
import { sync } from './sync/sync';
import { type Context, Mode } from './types';
import { type Context } from './types';

export async function build({
context,
Expand All @@ -28,7 +29,7 @@ export async function build({
ignoreInitial: true,
})
.on('all', async (event, path) => {
console.log(`File ${path} has been ${event}`);
logger.info(`File ${path} has been ${event}`);
await runSync();
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import yargsParser from 'yargs-parser';
import { loadConfig } from './config/config';
import { FRAMEWORK_NAME, MIN_NODE_VERSION } from './constants';
import { debug, error, info, warn } from './fishkit/logger';
import * as logger from './fishkit/logger';
import { checkVersion, setNoDeprecation, setNodeTitle } from './fishkit/node';
import { PluginManager } from './plugin/plugin_manager';
import { type Context, Mode } from './types';
Expand Down Expand Up @@ -73,6 +74,6 @@ checkVersion(MIN_NODE_VERSION);
setNodeTitle(FRAMEWORK_NAME);

run(process.cwd()).catch((err) => {
console.error(err.message);
logger.error(err.message);
process.exit(1);
});
3 changes: 2 additions & 1 deletion src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { updateConfig } from 'c12/update';
import path from 'pathe';
import pc from 'picocolors';
import { CONFIG_FILE } from '../constants';
import * as logger from '../fishkit/logger';
import type { Context } from '../types';
import type { Config } from './types';
import { ConfigSchema } from './types';
Expand Down Expand Up @@ -48,7 +49,7 @@ export function watchConfig(opts: ConfigOpts) {
onUpdate({ oldConfig, newConfig, getDiff }) {
const result = ConfigSchema.safeParse(newConfig);
if (!result.success) {
console.error(`Invalid configuration: ${result.error.message}`);
logger.error(`Invalid configuration: ${result.error.message}`);
return;
}
console.log('onUpdate', oldConfig, result.data, getDiff());
Expand Down
21 changes: 11 additions & 10 deletions src/fishkit/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import https from 'https';
import path from 'pathe';
import pc from 'picocolors';
import spdy from 'spdy';
import * as logger from './logger';

export interface HttpsServerOptions {
key?: string;
Expand Down Expand Up @@ -37,22 +38,22 @@ export async function resolveHttpsConfig(httpsConfig: HttpsServerOptions) {
// use mkcert -help instead of mkcert --version for checking if mkcert is installed, cause mkcert --version does not exists in Linux
await execa('mkcert', ['-help']);
} catch (e) {
console.error('[HTTPS] The mkcert has not been installed.');
console.info('[HTTPS] Please follow the guide to install manually.');
logger.error('[HTTPS] The mkcert has not been installed.');
logger.info('[HTTPS] Please follow the guide to install manually.');
switch (process.platform) {
case 'darwin':
console.log(pc.green('$ brew install mkcert'));
console.log(pc.gray('# If you use firefox, please install nss.'));
console.log(pc.green('$ brew install nss'));
console.log(pc.green('$ mkcert -install'));
logger.info(pc.green('$ brew install mkcert'));
logger.info(pc.gray('# If you use firefox, please install nss.'));
logger.info(pc.green('$ brew install nss'));
logger.info(pc.green('$ mkcert -install'));
break;
case 'win32':
console.log(
logger.info(
pc.green('Checkout https://github.com/FiloSottile/mkcert#windows'),
);
break;
case 'linux':
console.log(
logger.info(
pc.green('Checkout https://github.com/FiloSottile/mkcert#linux'),
);
break;
Expand All @@ -74,7 +75,7 @@ export async function resolveHttpsConfig(httpsConfig: HttpsServerOptions) {
!fs.existsSync(json) ||
!hasHostsChanged(json, hosts!)
) {
console.log('[HTTPS] Generating cert and key files...');
logger.info('[HTTPS] Generating cert and key files...');
await execa('mkcert', ['-cert-file', cert, '-key-file', key, ...hosts!]);
fs.writeFileSync(json, JSON.stringify({ hosts }), 'utf-8');
}
Expand Down Expand Up @@ -105,7 +106,7 @@ export async function createHttpsServer(
app: RequestListener,
httpsConfig: HttpsServerOptions,
) {
console.log('[HTTPS] Starting service in https mode...');
logger.info('[HTTPS] Starting service in https mode...');
const { key, cert } = await resolveHttpsConfig(httpsConfig);
const createServer = (
httpsConfig.http2 === false ? https.createServer : spdy.createServer
Expand Down
11 changes: 6 additions & 5 deletions src/fishkit/node.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import * as logger from './logger';

export function setNoDeprecation() {
// Use magic to suppress node deprecation warnings
// See: https://github.com/nodejs/node/blob/6311de332223e855e7f1ce03b7c920f51f308e95/lib/internal/process/warning.js#L95
// @ts-ignore
process.noDeprecation = '1';
}

export function checkVersion(minVersion: number, message?: string) {
export function checkVersion(minVersion: number) {
const ver = parseInt(process.version.slice(1));
const isOdd = ver % 2 === 1;
if (isOdd) {
console.error(
logger.error(
`Odd node version is not supported, your node version is ${ver}.`,
);
process.exit(1);
}
if (ver < minVersion) {
console.error(
message ||
`Your node version ${ver} is not supported, please upgrade to ${minVersion} or above.`,
logger.error(
`Your node version ${ver} is not supported, please upgrade to ${minVersion} or above.`,
);
process.exit(1);
}
Expand Down
3 changes: 2 additions & 1 deletion src/fishkit/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import http from 'http';
import type { Config } from '../config/types';
import { DEFAULT_PORT } from '../constants';
import { createHttpsServer } from './https';
import * as logger from './logger';

export interface ServerOpts {
devServer: Config['devServer'];
Expand Down Expand Up @@ -80,7 +81,7 @@ export async function createServer(opts: ServerOpts): Promise<{

server.listen(_port, () => {
const protocol = https ? 'https:' : 'http:';
console.log(`Server is running on ${protocol}//${host}:${_port}`);
logger.info(`Server is running on ${protocol}//${host}:${_port}`);
});

return { server, app, port: _port, ip, host };
Expand Down
3 changes: 3 additions & 0 deletions src/funplugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Fun Plugins

Notice: funplugins will be extracted to separate packages in the future.
14 changes: 14 additions & 0 deletions src/funplugins/mock/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Plugin } from '../../plugin/types';

interface MockOptions {
paths: string[];
}

export function mock(opts: MockOptions): Plugin {
return {
name: 'mock',
configureServer(server) {
// server.middlewares.use(mockMiddleware(opts.paths));
},
};
}
5 changes: 3 additions & 2 deletions src/generate/generate_page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs-extra';
import path from 'pathe';
// @ts-ignore
import randomColor from 'random-color';
import * as logger from '../fishkit/logger';
import type { Context } from '../types';

export async function generatePage({ context }: { context: Context }) {
Expand Down Expand Up @@ -45,6 +46,6 @@ function ${componentName}() {
await fs.writeFile(pagePath, pageContent);
await fs.writeFile(styleModulePath, styleContent);

console.log(`Generated page at: ${pagePath}`);
console.log(`Generated styles at: ${styleModulePath}`);
logger.info(`Generated page at: ${pagePath}`);
logger.info(`Generated styles at: ${styleModulePath}`);
}
3 changes: 2 additions & 1 deletion src/sync/sync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import * as logger from '../fishkit/logger';
import type { Context } from '../types';
import { writeClientEntry } from './write_client_entry';
import { writeGlobalStyle } from './write_global_style';
Expand Down Expand Up @@ -36,5 +37,5 @@ export async function sync(opts: SyncOptions) {
tailwindcssPath,
});

console.log('Synced');
logger.info('Synced');
}

0 comments on commit 3767d84

Please sign in to comment.