UNPKG

7.24 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 c(str) { return colors.greenBright.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.inverse.bold("\n> Wechat 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", od(`JSONC config file ${odc("<配置置文件,命令参数优先级高于配置>")}`), config.load)
26 .config(config.auto())
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.red.bold("♥ ")
32 + colors.italic(colors.gray("2018 - " + new Date().getFullYear()) + colors.cyan.dim(" @NewFuture "))
33 + colors.gray(" → https://github.com/NewFuture/miniprogram-build")
34 + colors.red.bold(" ♥")
35 )
36 .option("production", {
37 describe: od(`production mode ${odc("<发布模式会优化压缩>")}`),
38 default: process.env.NODE_ENV === "production" || process.env.NODE_ENV === "prod",
39 boolean: true,
40 })
41 .alias("production", "release")
42 .option("src", {
43 describe: od(`source folder ${odc("<源文件目录>")}`),
44 default: "src",
45 type: "string",
46 })
47 .option("dist", {
48 describe: od(`output folder ${odc("<编译输出目录>")}`),
49 default: "dist",
50 type: "string",
51 })
52 .option("exclude", {
53 describe: od(`ignored files ${odc("<编译忽略文件(夹)>")}`),
54 example: "types/**/*",
55 array: true,
56 string: true,
57 })
58 .option("tsconfig", {
59 describe: od(`typescript config file ${odc("<TS配置,未设置会自动查找tsconfig.json>")}`),
60 // default: '',
61 // type:'string',
62 })
63 .option("copy", {
64 describe: od(`files to copy ${odc("<复制的文件>")}`),
65 })
66 .option("var", {
67 describe: od(`KEY value pair to replace in js/json ${odc("<替换JS和JSON中的变量>")}`),
68 // type: 'object',
69 })
70 .option("assets", {
71 describe: od(`assets folder under src/ for compling style, wont put to dist ${odc("<样式所需资源文件;会监测文件修改,但不会编译或复制到输出目录>")}`),
72 default: "assets",
73 type: "string",
74 })
75 // .showHelpOnFail(true,'--help for available options')
76 .command(["*", "dev"], cd(`build and watch ${odc("<构建和检测文件修改>")}`))
77 .command("init", cd(`create config file ${odc("<创建配置文件>")}`))
78 .command(cb("build"), cd(`clean and compile ${odc("<清理和编译所有文件>")}`))
79 .command(cw("watch"), cd(`watch file changes ${odc("<监测文件变化>")}`))
80
81 .command(cb("clean"), cd(`remove all files in dist ${odc("<清理dist>")}`))
82 .command(cb("compile"), cd(`compile all source files to dist ${odc("<编译所有源文件>")}`))
83 .command(cb("js"), cd(`compile ts/js files to \`.js\` ${odc("<编译生成js>")}`))
84 .command(cb("typescript"), cd(`compile ts files to \`.js\` ${odc("<编译ts>")}`))
85 .command(cb("javascript"), cd(`compile all js as JavaScript ${odc("<编译js>")}`))
86 .command(cb("wxs"), cd(`compile wxs/wxts to \`wxs\` ${odc("<编译生成wxs>")}`))
87 .command(cb("wxts"), cd(`compile WeiXinTypeScript to \`wxs\` ${odc("<编译wxts>")}`))
88 .command(cb("wxjs"), cd(`compile wxs as JavaScript ${odc("<编译wxs>")}`))
89 .command(cb("wxss"), cd(`compile scss/sass/css/wxss to \`.wxss\` ${odc("<编译生成wxss>")}`))
90 .command(cb("wxml"), cd(`compile html/wxml files to \`.wxml\` ${odc("<编译生成wxml>")}`))
91 .command(cb("json"), cd(`compile all json/jsonc files to json ${odc("<编译生成json>")}`))
92 .command(cb("image"), cd(`compresse all images in source to dist ${odc("<压缩所有图片>")}`))
93 .command(cb("copy"), cd(`copy all files match \`copy\` ${odc("<复制需要复制的文件>")}`))
94 .command(cb("npm"), cd(`build npm dependencies to dist ${odc("<编译npm依赖>")}`))
95
96 .command(c("upload"), cd(`upload dist project ${odc("<上传小程序>")}`))
97 .command(c("open"), cd(`open dist in wechat devtool ${odc("<开发工具中打开项目>")}`))
98 .command(c("close"), cd(`close dist in Wechat devtool ${odc("<开发工具中关闭项目>")}`))
99 .command(c("quit"), cd(`quit Wechat devtool ${odc("<退出微信开发工具>")}`))
100 .command(c("autopreview"), cd(`auto-preview project ${odc("<当前项目发布自动预览>")}`))
101 .command("auto-preview", false)
102 .command("try-open", false)
103 .command("try-quit", false)
104
105 .command(cw("js-watch"), cd(`watch changes of ts/js files ${odc("<监测ts/js改动>")}`))
106 .command("typescript-watch", false) //"watch changes of ts files ${odc("<监测ts改动>"
107 .command("javascript-watch", false) // "watch changes of js files ${odc("<监测js改动>"
108 .command(cw("wxs-watch"), cd(`watch changes of .wxs/.wxts ${odc("<监测.wxs/.wxts>")}`))
109 .command("wxts-watch", false) //"watch changes of .wxts files ${odc("<监测wxts改动>"
110 .command("wxjs-watch", false) //"watch changes of .wxs files ${odc("<监测wxs改动>"
111 .command(cw("wxss-watch"), cd(`watch changes of scss/sass/css/wxss ${odc("<实时生成wxss>")}`))
112 .command(cw("wxml-watch"), cd(`watch changes of html/wxml files ${odc("<实时生成wxml>")}`))
113 .command(cw("json-watch"), cd(`watch changes of all json/jsonc files ${odc("<实时生成json>")}`))
114 .command(cw("image-watch"), cd(`watch changes of all images in source ${odc("<实时压缩图片>")}`))
115 .command(cw("copy-watch"), cd(`watch changes of \`copy\` files ${odc("<实时复制更新文件>")}`))
116 .command(cw("npm-watch"), cd(`watch changes of npm dependencies ${odc("<实时更新npm依赖>")}`))
117 .strict().argv;
118
119Object.assign(config.default, argv, { $0: undefined, _: undefined });
120
121if (argv._.length === 1 && argv._[0] === "init") {
122 config.save(config.default);
123} else {
124 if (config.default.production) {
125 process.env.NODE_ENV = "production"
126 }
127 const tasks = require("../src/task")
128 tasks.$execute(argv._.length === 0 ? ["dev"] : argv._);
129}
130