UNPKG

6.3 kBJavaScriptView Raw
1#!/usr/bin/env node
2///@ts-check
3"use strict";
4const colors = require("ansi-colors");
5const config = require("../src/config");
6
7function cb(str) { return colors.cyanBright.bold(str) }
8function cw(str) { return colors.magentaBright.bold(str) }
9function cd(str) { return colors.cyan.italic(str) }
10function od(str) { return colors.italic(str) }
11function odc(str) { return colors.reset.gray(str) }
12function ed(str) { return colors.gray(str) }
13
14var argv = require("yargs")
15 .scriptName("mp")
16 .usage(colors.whiteBright.bold.inverse("\nWechat MiniProgram build tools <微信小程序编译打包工具>\n"))
17 .usage(`${colors.italic("Short Usage")} <短用法>: ${colors.bold.yellowBright("$0")} ${colors.cyanBright("[command...]")} ${colors.italic("[--option]")}`)
18 .usage(`${colors.italic("Full Name")} <完整名称>: ${colors.bold.yellowBright("miniprogram-build")} ${colors.cyanBright("[命令...]")} ${colors.italic("[--选项]")}`)
19 .example(colors.green.italic("$0 js js-watch"), ed("编译并监测生成js文件"))
20 .example(colors.green.italic("$0 --config=mpconfig.json"), ed("指定配置文件"))
21 .example(colors.green.italic("$0 --release --var.APP_ID=1234"), ed("优化编译,并替换变量{{APP_ID}}"))
22 // configuration
23 .pkgConf("mpconfig")
24 .config(config.auto())
25 .config("config", od(`JSONC config file ${odc("<配置置文件,命令参数优先级高于配置>")}`), config.load)
26 .alias("c", "config")
27 .help("help", od(`show help ${odc("<显示帮助信息>")}`))
28 .alias("h", "help")
29 .describe("version", od(`show version number ${odc("<查看本版号>")}`))
30 .epilog(colors.italic(colors.gray("2018 - " + new Date().getFullYear()) + " " + colors.cyan.dim("@ NewFuture")))
31 .option("release", {
32 describe: od(`production mode ${odc("<发布模式会优化压缩>")}`),
33 default: false,
34 boolean: true,
35 })
36 .option("src", {
37 describe: od(`source folder ${odc("<源文件目录>")}`),
38 default: "src",
39 type: "string",
40 })
41 .option("dist", {
42 describe: od(`output folder ${odc("<编译输出目录>")}`),
43 default: "dist",
44 type: "string",
45 })
46 .option("exclude", {
47 describe: od(`ignored files ${odc("<编译忽略文件(夹)>")}`),
48 example: "types/**/*",
49 array: true,
50 string: true,
51 })
52 .option("tsconfig", {
53 describe: od(`typescript config file ${odc("<TS配置,未设置会自动查找tsconfig.json>")}`),
54 // default: '',
55 // type:'string',
56 })
57 .option("copy", {
58 describe: od(`files to copy ${odc("<复制的文件>")}`),
59 })
60 .option("var", {
61 describe: od(`KEY value pair to replace in js/json ${odc("<替换JS和JSON中的变量>")}`),
62 // type: 'object',
63 })
64 .option("assets", {
65 describe: od(`assets folder under src/ for compling style, wont put to dist ${odc("<样式所需资源文件;会监测文件修改,但不会编译或复制到输出目录>")}`),
66 default: "assets",
67 type: "string",
68 })
69 // .showHelpOnFail(true,'--help for available options')
70 .command(["*", "dev"], cd(`build and watch ${odc("<构建和检测文件修改>")}`))
71 .command("init", cd(`create config file ${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(config.default, argv, { $0: undefined, _: undefined });
103
104// console.log(argv['config'])
105if (argv._.length === 1 && argv._[0] === "init") {
106 config.save(config.default);
107} else {
108 const tasks = require("../src/task")
109 tasks.$execute(argv._.length === 0 ? ["dev"] : argv._);
110}
111