README
@flatjs/cli
The cli tools for the @flatjs libraries
The usages
$ npm i -g @flatjs/cli
The configurations
- flatjs.cli.js
const flatjsConfig = {
flatjsCli: {
commandConfigs: {
publishCmd: {
ftp: {
host: '10.100.200.200',
},
qiniu: {},
myService(type) {
console.log(type);
},
},
},
},
};
Features
Provides common command-line tools
flat dts
flat info
flat pack
flat publish
flat update
flat config
flat new
flat serve
flat mock
flat build
Allow us extends builtin commands
- Create an new npm module named as
@flatjs/cli-plugin-*
(offical plugin) Orflatjs-cli-plugin-*
- Write plugin code
// Create your command handler
// `handler.ts`
import { defaultFlatjsConfig, FlatjsPartialConfig } from '@flatjs/cli';
import {
AbstractCommand,
BaseArguments,
loadFlatjsConfig,
Package,
TypeAny,
YargsArguments,
} from '@flatjs/common';
export interface UploadCommandArgs extends BaseArguments {
/**
* The api base of galaxy platform
* @default ``
*/
apiBase: string;
}
export class UploadCommand extends AbstractCommand<UploadCommandArgs> {
private fileFromCwd: string;
private apiBase: string;
private flatjsConfig: FlatjsPartialConfig & {
pkgJson: Partial<Package>;
};
constructor(args: YargsArguments<UploadCommandArgs>) {
super(args, 'upload');
this.flatjsConfig = this.merge(
defaultFlatjsConfig,
loadFlatjsConfig<'flatjsCli', FlatjsPartialConfig>(process.cwd(), 'flatjsCli', 'flatjs.cli.js'),
);
const { apiBase } = this.flatjsConfig.flatjsCli.pluginConfigs?.uploadCmd || {};
this.apiBase = apiBase || this.args.apiBase;
this.fileFromCwd = process.cwd();
this.logger.verbose(this.flatjsConfig, this.logContext);
}
async runHandle(): Promise<TypeAny> {
// ...
// It's your plugin logics.
}
}
// Create command builder
// `builder.ts`
import { createCmdBuilder } from '@flatjs/common';
import { UploadCommand, UploadCommandArgs } from './handler';
export const uploadCmd = createCmdBuilder<UploadCommandArgs>(
{
command: 'upload',
describe: 'upload package to remote galaxy platform',
builder(args) {
return args.example(`$0 upload`, 'upload package to remote galaxy platform').option('apiBase', {
type: 'string',
default: '',
describe: 'The api base of galaxy platform',
});
},
},
UploadCommand,
);
// update your flatjs.cli.js
// @ts-check
/** @type import('@flatjs/cli').FlatjsPartialConfig */
const flatjsConfig = {
flatjsCli: {
pluginConfigs: {
// `upload`Cmd --- upload must be same as `cmd` name
uploadCmd: {
apiBase: 'https://your.domain.com/api',
},
},
},
};
module.exports = flatjsConfig;
- export plugin as
cliPluginCmd
import { uploadCmd } from './upload';
/**
* Always need to exports plugin named as `cliPluginCmd`
*/
export const cliPluginCmd = uploadCmd;