UNPKG

6.05 kBJavaScriptView Raw
1#!/usr/bin/env node
2///@ts-check
3"use strict";
4const colors = require("ansi-colors");
5var config = require("../src/load-config");
6var tasks = require("../src/task");
7
8function cb(str) { return colors.cyanBright.bold(str) }
9function cw(str) { return colors.magentaBright.bold(str) }
10function cd(str) { return colors.cyan.italic(str) }
11function od(str) { return colors.italic(str) }
12function odc(str) { return colors.reset.gray(str) }
13function ed(str) { return colors.gray(str) }
14
15var argv = require("yargs")
16 .scriptName("mp")
17 .usage(colors.whiteBright.bold.inverse("\nWechat MiniProgram build tools <微信小程序编译打包工具>\n"))
18 .usage(`${colors.italic("Short Usage")} <短用法>: ${colors.bold.yellowBright("$0")} ${colors.cyanBright("[command...]")} ${colors.italic("[--option]")}`)
19 .usage(`${colors.italic("Full Name")} <完整名称>: ${colors.bold.yellowBright("miniprogram-build")} ${colors.cyanBright("[命令...]")} ${colors.italic("[--选项]")}`)
20 .example(colors.green.italic("$0 js js-watch"), ed("编译并监测生成js文件"))
21 .example(colors.green.italic("$0 --config=mpconfig.json"), ed("指定配置文件"))
22 .example(colors.green.italic("$0 --release --var.APP_ID=1234"), ed("优化编译,并替换变量{{APP_ID}}"))
23 // configuration
24 .pkgConf("mpconfig")
25 .config(config.default())
26 .config("config", od(`JSONC config file ${odc("<配置置文件,命令参数优先级高于配置>")}`), config.load)
27 .alias("c", "config")
28 .help("help", od(`show help ${odc("<显示帮助信息>")}`))
29 .alias("h", "help")
30 .describe("version", od(`show version number ${odc("<查看本版号>")}`))
31 .epilog(colors.italic(colors.gray("2018 - " + new Date().getFullYear()) + " " + colors.cyan.dim("@ NewFuture")))
32 .option("release", {
33 describe: od(`production mode ${odc("<发布模式会优化压缩>")}`),
34 default: false,
35 boolean: true,
36 })
37 .option("src", {
38 describe: od(`source folder ${odc("<源文件目录>")}`),
39 default: "src",
40 type: "string",
41 })
42 .option("dist", {
43 describe: od(`output folder ${odc("<编译输出目录>")}`),
44 default: "dist",
45 type: "string",
46 })
47 .option("exclude", {
48 describe: od(`ignored files ${odc("<编译忽略文件(夹)>")}`),
49 example: "types/**/*",
50 array: true,
51 string: true,
52 })
53 .option("tsconfig", {
54 describe: od(`typescript config file ${odc("<TS配置,未设置会自动查找tsconfig.json>")}`),
55 // default: '',
56 // type:'string',
57 })
58 .option("copy", {
59 describe: od(`files to copy ${odc("<复制的文件>")}`),
60 })
61 .option("var", {
62 describe: od(`KEY value pair to replace in js/json ${odc("<替换JS和JSON中的变量>")}`),
63 // type: 'object',
64 })
65 .option("assets", {
66 describe: od(`assets folder under src/ for compling style, wont put to dist ${odc("<样式所需资源文件;会监测文件修改,但不会编译或复制到输出目录>")}`),
67 default: "assets",
68 type: "string",
69 })
70 // .showHelpOnFail(true,'--help for available options')
71 .command(["*", "dev"], cd(`build and watch ${odc("<构建和检测文件修改>")}`))
72 .command(cb("build"), cd(`clean and compile ${odc("<清理和编译所有文件>")}`))
73 .command(cw("watch"), cd(`watch file changes ${odc("<监测文件变化>")}`))
74 .command(cb("clean"), cd(`remove all files in dist ${odc("<清理dist>")}`))
75 .command(cb("compile"), cd(`compile all source files to dist ${odc("<编译所有源文件>")}`))
76 .command(cb("js"), cd(`compile ts/js files to \`.js\` ${odc("<编译生成js>")}`))
77 .command(cb("typescript"), cd(`compile ts files to \`.js\` ${odc("<编译ts>")}`))
78 .command(cb("javascript"), cd(`compile all js as JavaScript ${odc("<编译js>")}`))
79 .command(cb("wxs"), cd(`compile wxs/wxts to \`wxs\` ${odc("<编译生成wxs>")}`))
80 .command(cb("wxts"), cd(`compile WeiXinTypeScript to \`wxs\` ${odc("<编译wxts>")}`))
81 .command(cb("wxjs"), cd(`compile wxs as JavaScript ${odc("<编译wxs>")}`))
82 .command(cb("wxss"), cd(`compile scss/sass/css/wxss to \`.wxss\` ${odc("<编译生成wxss>")}`))
83 .command(cb("wxml"), cd(`compile html/wxml files to \`.wxml\` ${odc("<编译生成wxml>")}`))
84 .command(cb("json"), cd(`compile all json/jsonc files to json ${odc("<编译生成json>")}`))
85 .command(cb("image"), cd(`compresse all images in source to dist ${odc("<压缩所有图片>")}`))
86 .command(cb("copy"), cd(`copy all files match \`copy\` ${odc("<复制需要复制的文件>")}`))
87 .command(cb("npm"), cd(`build npm dependencies to dist ${odc("<编译npm依赖>")}`))
88 .command(cw("js-watch"), cd(`watch changes of ts/js files ${odc("<监测ts/js改动>")}`))
89 .command("typescript-watch", false) //"watch changes of ts files ${odc("<监测ts改动>"
90 .command("javascript-watch", false) // "watch changes of js files ${odc("<监测js改动>"
91 .command(cw("wxs-watch"), cd(`watch changes of .wxs/.wxts ${odc("<监测.wxs/.wxts>")}`))
92 .command("wxts-watch", false) //"watch changes of .wxts files ${odc("<监测wxts改动>"
93 .command("wxjs-watch", false) //"watch changes of .wxs files ${odc("<监测wxs改动>"
94 .command(cw("wxss-watch"), cd(`watch changes of scss/sass/css/wxss ${odc("<实时生成wxss>")}`))
95 .command(cw("wxml-watch"), cd(`watch changes of html/wxml files ${odc("<实时生成wxml>")}`))
96 .command(cw("json-watch"), cd(`watch changes of all json/jsonc files ${odc("<实时生成json>")}`))
97 .command(cw("image-watch"), cd(`watch changes of all images in source ${odc("<实时压缩图片>")}`))
98 .command(cw("copy-watch"), cd(`watch changes of \`copy\` files ${odc("<实时复制更新文件>")}`))
99 .command(cw("npm-watch"), cd(`watch changes of npm dependencies ${odc("<实时更新npm依赖>")}`))
100 .strict().argv;
101
102Object.assign(tasks.$config, argv);
103tasks.$execute(argv._.length === 0 ? ["dev"] : argv._);