3.7.2 • Published 28 days ago

@syyfe/syy-mp-service v3.7.2

Weekly downloads
-
License
ISC
Repository
-
Last release
28 days ago

商业云小程序脚手架

安装

npm i @syyfe/syy-mp-service -D

IOptions 接口

./src/config/appConfig 实现了该接口并同时也实现 IAppConfig

syy-mp-cli 也同样实现该接口,命令调用传递相关的参数

export interface IOptions {
    /** 当前执行路径,默认当前bash 路径 */
    rootPath: string;

    /** 是否开启缓存 默认 true*/
    cache: boolean;

    /** 当前执行用户 */
    mode: string;

    /** 是否上传代码 默认 false*/
    upload: boolean;

    /** 上传代码备注说明 */
    desc: string;
}

AppConfig 系统配置属性

export interface IAppConfig {
    /** 源码所在目录 */
    srcDir: string;

    /** 编译后输出目录 */
    outputDir: string;

    /** tsconfig.json 配置 */
    tsConfig: string;
}

UserConfig 用户配置

export interface IUserConfig {
    appConfig: IAppConfig;

    /**
     * 当前用户配置对象 syy.config.ts
     * @type {*}
     * @memberof UserConfig
     */
    context: any;

    /** 上传图片OSS */
    isUseOSS?: boolean;

    /** SyyImage 图片服务对象,用来开启代理和上传OSS使用 */
    imageOperator: any;

    /** 项目根目录下的package.json */
    srcPackageJsonPath?: string;

    /** 输出目录下的package.json */
    packageJsonPath?: string;

    /** 项目根目录下的project.config.json */
    srcProjectConfigJson?: string;

    /** 输出目录下的project.config.json */
    projectConfigJson?: string;

    /**
     * 加载用户配置 (根目录下的 syy.config.js 文件),在 env 环境变量加载后执行
     * @param rootPath
     */
    initUserOptions(rootPath: string): any;
    /**
     * 初始化图片服务
     */
    initImageService(): Promise<void>;

    /**
     * OSS 图片重写方法
     * @param url 当前文件url
     */
    ossRewriter(url: string): string;
}

插件相关配置

export interface IPlugin {
    /** 插件名称 */
    name: string;
    /**
     * 插件实例方法
     * @param {Service} ctx 上下文 当前service 对象
     * @memberof IPlugin
     */
    apply(ctx: Service): void;
}

插件需添加 name属性 和 实现 apply 方法 , 方法里可以实现相应的插件初始、逻辑等操作

ctx.on 钩子

  • beforeCompile: 插件初始化之后,编译前
  • done: 编译完成后

插件 Demo

import { Service, IPlugin } from "@syyfe/syy-mp-service";
import { Log } from "@syyfe/syy-utils-node";
import { name } from "../package.json";
const fsx = require("fs-extra");
const writeJsonFile = require("write-json-file");

export default class SyyMpPluginLive implements IPlugin {
    name: string = name;
    options: any;

    constructor(options?: any) {
        this.options = options;
    }

    getArgs(): boolean {
        const args = require("minimist")(process.argv.slice(2), {
            boolean: ["live"]
        });
        return args["live"];
    }

    apply(ctx: Service): void {
        const isLive = this.getArgs();
        if (isLive) {
            ctx.on("done", async () => {
                const appJsonPath = ctx.appConfig.outputDir + "/app.json";
                const app = fsx.readJSONSync(appJsonPath);

                Log.info(`当前正在构建 "直播版" 小程序`);
                app.plugins = {
                    "live-player-plugin": {
                        version: "1.3.0",
                        provider: "wx2b03c6e691cd7370"
                    }
                };
                writeJsonFile.sync(appJsonPath, app, { detectIndent: true });
            });
        }
    }
}