1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict'
|
10 |
|
11 | process.on('unhandledRejection', (err) => {
|
12 | throw err
|
13 | })
|
14 |
|
15 | const {existsSync} = require('fs')
|
16 | const {join} = require('path')
|
17 | const yParser = require('yargs-parser')
|
18 | const chalk = require('chalk')
|
19 | const signale = require('signale')
|
20 | const updater = require('update-notifier')
|
21 | const spawn = require('cross-spawn')
|
22 | const pkg = require('../package.json')
|
23 | const start = require('../scripts/start')
|
24 | const build = require('../scripts/build')
|
25 | const publish = require('../scripts/publish')
|
26 |
|
27 | const args = yParser(process.argv.slice(2))
|
28 | if (args.v || args.version) {
|
29 | console.log(pkg.version)
|
30 | if (existsSync(join(__dirname, '../.local'))) {
|
31 | console.log(chalk.cyan('@local'))
|
32 | }
|
33 | process.exit(0)
|
34 | }
|
35 |
|
36 | updater({pkg}).notify({defer: true})
|
37 |
|
38 | async function run() {
|
39 | const cwd = process.cwd()
|
40 | const lernaPath = join(__dirname, '../node_modules/.bin/lerna')
|
41 |
|
42 | switch (args._[0]) {
|
43 | case 'dev':
|
44 | start({
|
45 | cwd,
|
46 | watch: true,
|
47 | })
|
48 | break
|
49 | case 'build':
|
50 | build({
|
51 | cwd,
|
52 | watch: false,
|
53 | })
|
54 | break
|
55 | case 'publish':
|
56 | publish({
|
57 | cwd,
|
58 | }).catch((err) => {
|
59 | signale.error(err)
|
60 | process.exit(1)
|
61 | })
|
62 | break
|
63 | case 'lerna':
|
64 | spawn.sync(lernaPath, process.argv.slice(3), {
|
65 | stdio: 'inherit',
|
66 | cwd,
|
67 | })
|
68 |
|
69 | break
|
70 | case 'h':
|
71 | case undefined:
|
72 | printHelp()
|
73 | break
|
74 | default:
|
75 | console.error(chalk.red(`Unsupported command ${args._[0]}`))
|
76 | process.exit(1)
|
77 | }
|
78 | }
|
79 |
|
80 | function printHelp() {
|
81 | console.log(`
|
82 | Usage: hz-lib <command> [options]
|
83 |
|
84 | Commands:
|
85 | ${chalk.green('dev')} dev library
|
86 | ${chalk.green('build')} build library
|
87 | ${chalk.green('publish')} publish library
|
88 | ${chalk.green('lerna command')} leran command
|
89 | `)
|
90 | }
|
91 |
|
92 | run().catch((err) => {
|
93 | signale.error(err)
|
94 | process.exit(1)
|
95 | })
|