Skip to content

Commit

Permalink
refactor(cli): divide all commands
Browse files Browse the repository at this point in the history
- Remove experimental warning prevention
- Split all commands into separate files
  • Loading branch information
fu050409 committed Jul 5, 2024
1 parent f4c8498 commit fe162a3
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 273 deletions.
73 changes: 73 additions & 0 deletions packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { defineCommand } from 'citty';
import { getOptionFromBuildOption } from '../config.js';
import {
FarmCLIBuildOptions,
FarmCLICommonOptions,
NormalizedFarmCLIBuildOptions
} from '../types.js';
import {
handleAsyncOperationErrors,
resolveCliConfig,
resolveCore
} from '../utils.js';

export default defineCommand({
meta: {
name: 'build',
description: 'compile the project in production mode'
},
args: {
root: {
type: 'positional',
description: 'root path',
required: false,
valueHint: 'path'
},
outDir: {
type: 'string',
alias: 'o',
description: 'output directory'
},
input: {
type: 'string',
alias: 'i',
description: 'input file path'
},
watch: { type: 'boolean', alias: 'w', description: 'watch file change' },
target: {
type: 'string',
description: 'transpile targetEnv node, browser'
},
format: {
type: 'string',
description: 'transpile format esm, commonjs'
},
sourcemap: {
type: 'boolean',
description: 'output source maps for build'
},
treeShaking: {
type: 'boolean',
description: 'Eliminate useless code without side effects'
},
minify: {
type: 'boolean',
description: 'code compression at build time'
}
},
async run({ args }: { args: FarmCLICommonOptions & FarmCLIBuildOptions }) {
const { root, configPath } = resolveCliConfig(
args.root,
args.config ?? args.c
);

const defaultOptions = {
root,
configPath,
...getOptionFromBuildOption(args as NormalizedFarmCLIBuildOptions)
};

const { build } = await resolveCore();
handleAsyncOperationErrors(build(defaultOptions), 'error during build');
}
});
37 changes: 37 additions & 0 deletions packages/cli/src/commands/clean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { defineCommand } from 'citty';
import { FarmCLICommonOptions, ICleanOptions } from '../types.js';
import { resolveCliConfig, resolveCore } from '../utils.js';

export default defineCommand({
meta: {
name: 'clean',
description: 'Clean up the cache built incrementally'
},
args: {
root: {
type: 'positional',
description: 'root path',
required: false,
valueHint: 'path'
},
recursive: {
type: 'boolean',
alias: 'r',
description:
'Recursively search for node_modules directories and clean them'
}
},
async run({ args }: { args: FarmCLICommonOptions & ICleanOptions }) {
const { root } = resolveCliConfig(args.root, args.config);

const { clean } = await resolveCore();
try {
await clean(root, args.recursive);
} catch (e) {
const { Logger } = await import('@farmfe/core');
const logger = new Logger();
logger.error(`Failed to clean cache: \n ${e.stack}`);
process.exit(1);
}
}
});
55 changes: 55 additions & 0 deletions packages/cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { defineCommand } from 'citty';
import {
FarmCLICommonOptions,
FarmCLIServerOptions,
GlobalFarmCLIOptions
} from '../types.js';
import {
handleAsyncOperationErrors,
resolveCliConfig,
resolveCommandOptions,
resolveCore
} from '../utils.js';

export default defineCommand({
meta: {
name: 'dev',
description:
'Compile the project in dev mode and serve it with farm dev server'
},
args: {
root: {
type: 'positional',
description: 'root path',
required: false,
valueHint: 'path'
},
lazy: { type: 'boolean', alias: 'l', description: 'lazyCompilation' },
host: { type: 'string', description: 'specify host' },
port: { type: 'string', description: 'specify port' },
open: { type: 'boolean', description: 'open browser on server start' },
hmr: { type: 'boolean', description: 'enable hot module replacement' },
cors: { type: 'boolean', description: 'enable cors' },
strictPort: {
type: 'boolean',
description: 'specified port is already in use, exit with error'
}
},
async run({ args }: { args: FarmCLICommonOptions & FarmCLIServerOptions }) {
const { root, configPath } = resolveCliConfig(args.root, args.config);

const resolvedOptions = resolveCommandOptions(args as GlobalFarmCLIOptions);
const defaultOptions = {
root,
compilation: {
lazyCompilation: args.lazy
},
server: resolvedOptions,
clearScreen: args.clearScreen,
configPath,
mode: args.mode
};
const { start } = await resolveCore();
handleAsyncOperationErrors(start(defaultOptions), 'Failed to start server');
}
});
50 changes: 50 additions & 0 deletions packages/cli/src/commands/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { defineCommand } from 'citty';
import {
FarmCLICommonOptions,
FarmCLIPreviewOptions,
GlobalFarmCLIOptions
} from '../types.js';
import {
handleAsyncOperationErrors,
resolveCliConfig,
resolveCommandOptions,
resolveCore
} from '../utils.js';

export default defineCommand({
meta: {
name: 'preview',
description: 'compile the project in watch mode'
},
args: {
root: {
type: 'positional',
description: 'root path',
required: false,
valueHint: 'path'
},
port: { type: 'string', description: 'specify port' },
open: {
type: 'boolean',
description: 'open browser on server preview start'
}
},
async run({ args }: { args: FarmCLICommonOptions & FarmCLIPreviewOptions }) {
const { root, configPath } = resolveCliConfig(args.root, args.config);

const resolvedOptions = resolveCommandOptions(args as GlobalFarmCLIOptions);
const defaultOptions = {
root,
mode: args.mode,
server: resolvedOptions,
configPath,
port: resolvedOptions.port
};

const { preview } = await resolveCore();
handleAsyncOperationErrors(
preview(defaultOptions),
'Failed to start preview server'
);
}
});
75 changes: 75 additions & 0 deletions packages/cli/src/commands/watch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { defineCommand } from 'citty';
import { getOptionFromBuildOption } from '../config.js';
import {
FarmCLIBuildOptions,
GlobalFarmCLIOptions,
NormalizedFarmCLIBuildOptions
} from '../types.js';
import {
handleAsyncOperationErrors,
resolveCliConfig,
resolveCore
} from '../utils.js';

export default defineCommand({
meta: {
name: 'watch',
description: 'watch file change'
},
args: {
root: {
type: 'positional',
description: 'root path',
required: false,
valueHint: 'path'
},
outDir: {
type: 'string',
alias: 'o',
description: 'output directory'
},
input: {
type: 'string',
alias: 'i',
description: 'input file path'
},
target: {
type: 'string',
description: 'transpile targetEnv node, browser'
},
format: {
type: 'string',
description: 'transpile format esm, commonjs'
},
sourcemap: {
type: 'boolean',
description: 'output source maps for build'
},
treeShaking: {
type: 'boolean',
description: 'Eliminate useless code without side effects'
},
minify: {
type: 'boolean',
description: 'code compression at build time'
}
},
async run({ args }: { args: FarmCLIBuildOptions & GlobalFarmCLIOptions }) {
const { root, configPath } = resolveCliConfig(
args.root,
args.config ?? args.c
);

const defaultOptions = {
root,
configPath,
...getOptionFromBuildOption(args as NormalizedFarmCLIBuildOptions)
};

const { watch } = await resolveCore();
handleAsyncOperationErrors(
watch(defaultOptions),
'error during watch project'
);
}
});
Loading

0 comments on commit fe162a3

Please sign in to comment.