Skip to content

Commit

Permalink
chore: Completed all refactoring of @farmfe/cli (#1678)
Browse files Browse the repository at this point in the history
* chore: Completed all refactoring of @farmfe/cli

* chore: Completed all refactoring of @farmfe/cli
  • Loading branch information
ErKeLost authored Jul 29, 2024
1 parent aa66390 commit 7dc3164
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 130 deletions.
2 changes: 1 addition & 1 deletion examples/refactor-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "react-template",
"version": "1.0.0",
"scripts": {
"dev": "farm start",
"dev": "farm",
"start": "farm start",
"build": "farm build",
"preview": "farm preview",
Expand Down
14 changes: 10 additions & 4 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@
pnpm add @farmfe/cli -D
```

start your farm project:
start your farm project in development mode:

```sh
farm start
farm dev
```

build:
build your farm project in production mode:

```sh
farm build
```

preview:
preview your farm project in production mode:

```sh
farm preview
```

clean your farm persistent cache:

```sh
farm clean
```
16 changes: 4 additions & 12 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,20 @@
},
"files": [
"dist",
"bin",
"templates"
"bin"
],
"scripts": {
"start": "rimraf dist && tsc -b -w",
"build": "tsc -b",
"type-check": "tsc --noEmit",
"prepublishOnly": "npm run build"
},
"engines": {
"node": ">= 16"
},
"dependencies": {
"cac": "^6.7.14",
"cross-spawn": "^7.0.3",
"inquirer": "9.2.12",
"walkdir": "^0.4.1"
"cac": "^6.7.14"
},
"devDependencies": {
"@farmfe/cli": "workspace:*",
"@farmfe/core": "workspace:*",
"@types/cross-spawn": "^6.0.2",
"@types/inquirer": "^9.0.3"
"peerDependencies": {
"@farmfe/core": "workspace:*"
}
}
23 changes: 7 additions & 16 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { VERSION as CORE_VERSION } from '@farmfe/core';
import { cac } from 'cac';

import {
VERSION,
handleAsyncOperationErrors,
resolveCliConfig,
resolveCommandOptions,
resolveCore,
version
resolveCore
} from './utils.js';

import type { UserConfig } from '@farmfe/core';
Expand Down Expand Up @@ -120,7 +120,7 @@ cli
.action(async (root: string, options: CliBuildOptions & GlobalCliOptions) => {
const defaultOptions = {
root,
configFile: options.configFile,
configFile: options.config,
mode: options.mode,
watch: options.watch,
compilation: {
Expand Down Expand Up @@ -158,7 +158,7 @@ cli
.action(async (root: string, options: CliBuildOptions & GlobalCliOptions) => {
const defaultOptions = {
root,
configFile: options.configFile,
configFile: options.config,
mode: options.mode,
compilation: {
watch: options.watch,
Expand Down Expand Up @@ -202,7 +202,7 @@ cli
host: options.host,
open: options.open
},
configPath: options.configPath,
configFile: options.config,
port: options.port,
compilation: {
output: {
Expand Down Expand Up @@ -236,17 +236,8 @@ cli
);
});

// Listening for unknown command
cli.on('command:*', async () => {
const { Logger } = await import('@farmfe/core');
const logger = new Logger();
logger.error(
`Unknown command place Run "farm --help" to see available commands`
);
});

cli.help();

cli.version(version);
cli.version(`@farmfe/cli ${VERSION} @farmfe/core ${CORE_VERSION}`);

cli.parse();
13 changes: 6 additions & 7 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ export interface GlobalCliOptions {
'--'?: string[];
c?: boolean | string;
config?: string;
configPath?: string;
base?: string;
m?: string;
mode?: 'development' | 'production' | string;
l?: boolean;
lazy?: boolean;
port?: number;
clearScreen?: boolean;
}

Expand All @@ -19,7 +15,6 @@ export interface CleanOptions {
export interface CliServerOptions {
port?: number;
open?: boolean;
https?: boolean;
hmr?: boolean;
cors?: boolean;
strictPort?: boolean;
Expand All @@ -32,11 +27,12 @@ export interface CliBuildOptions {
input?: string;
w?: boolean;
watch?: boolean;
l?: boolean;
lazy?: boolean;
sourcemap?: boolean;
minify?: boolean;
treeShaking?: boolean;
format?: 'cjs' | 'esm';
configFile?: string | undefined;
target?:
| 'browser'
| 'node'
Expand All @@ -46,7 +42,10 @@ export interface CliBuildOptions {
| 'browser-legacy'
| 'browser-es2015'
| 'browser-es2017'
| 'browser-esnext';
| 'browser-esnext'
| 'library'
| 'library-browser'
| 'library-node';
}

export interface CliPreviewOptions {
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export function cleanOptions(
* @returns resolve command options
*/
export function resolveCommandOptions(
options: GlobalCliOptions
): GlobalCliOptions {
options: GlobalCliOptions & CliServerOptions
): GlobalCliOptions & CliServerOptions {
const resolveOptions = { ...options };
filterDuplicateOptions(resolveOptions);
return cleanOptions(resolveOptions);
Expand Down Expand Up @@ -143,6 +143,8 @@ export function resolveCliConfig(
};
}

export const { version } = JSON.parse(
const { version } = JSON.parse(
readFileSync(new URL('../package.json', import.meta.url)).toString()
);

export const VERSION = version;
8 changes: 8 additions & 0 deletions packages/core/src/config/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { readFileSync } from 'node:fs';

export const DEFAULT_CONFIG_NAMES = [
'farm.config.ts',
'farm.config.js',
Expand All @@ -15,3 +17,9 @@ export const CUSTOM_KEYS = {
};

export const FARM_RUST_PLUGIN_FUNCTION_ENTRY = 'func.js';

const { version } = JSON.parse(
readFileSync(new URL('../../package.json', import.meta.url)).toString()
);

export const VERSION = version;
1 change: 1 addition & 0 deletions packages/core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import type {
} from './types.js';

export * from './types.js';
export * from './constants.js';

export function defineFarmConfig(config: UserConfig): UserConfig;
export function defineFarmConfig(
Expand Down
Loading

0 comments on commit 7dc3164

Please sign in to comment.