UNPKG

2.75 kBJavaScriptView Raw
1#!/usr/bin/env node
2/**
3 * list project rollup entries {file,script} based on ./rollup.json
4 * @module @ctx-core/rollup/rollup-cmd
5 * @example
6 * #!/bin/sh
7 * rollup-cmd.js -t http
8 * # http build file list
9 * rollup-cmd.js -t browser
10 * # browser build file list
11 */
12const fs = require('fs')
13console.info(_rollup__cmd())
14module.exports = _rollup__cmd
15function _rollup__cmd() {
16 const minimist = require('minimist')
17 const argv = minimist(process.argv.slice(2), {
18 '--': true,
19 alias: { c: 'config', h: 'help', t: 'target', w: 'watch' }
20 })
21 const { help } = argv
22 if (help) return help__msg()
23 const suffix = (argv['--'] || []).join(' ')
24 const config_file =
25 argv.config
26 || process.env.ROLLUP_JSON
27 || './rollup.json'
28 const {
29 target = 'browser',
30 watch
31 } = argv
32 const json__config = fs.readFileSync(config_file, 'utf8')
33 const config = JSON.parse(json__config)
34 const a1__cmd__target__config = config[target] || []
35 const { length } = a1__cmd__target__config
36 const code =
37 watch
38 ? _code__watch()
39 : _code__cmds()
40 return code
41 function _code__cmds() {
42 const cmds = []
43 for (let i = 0; i < length; i++) {
44 const cmd__target = a1__cmd__target__config[i]
45 let cmd = ''
46 if (/^\$/.test(cmd__target)) {
47 cmd += cmd__target.replace(/^\$/, '')
48 } else {
49 cmd += `rollup -c '${cmd__target}'`
50 }
51 if (suffix) {
52 cmd += (' ' + suffix)
53 }
54 cmds.push(cmd)
55 }
56 return cmds.join('\n')
57 }
58 function _code__watch() {
59 const a1__cmd__windows = []
60 const a1__cmd__send_keys = []
61 for (let i = 0; i < length; i++) {
62 const cmd__target = a1__cmd__target__config[i]
63 let cmd = ''
64 if (/^\$/.test(cmd__target)) {
65 cmd += cmd__target.replace(/^\$/, '')
66 } else {
67 cmd += `rollup -c '${cmd__target}'`
68 }
69 if (watch) {
70 cmd += ' --watch'
71 }
72 if (suffix) {
73 cmd += ` ${suffix}`
74 }
75 if (i) {
76 a1__cmd__windows.push(`tmux split-window`)
77 }
78 const cmds__tmux =
79 ['[ -f ~/.bashrc ] && . ~/.bashrc || [ -f ~/.bash_profile ] && . ~/.bash_profile',
80 'direnv reload',
81 cmd]
82 for (let j = 0; j < cmds__tmux.length; j++) {
83 const cmd__tmux = cmds__tmux[j]
84 a1__cmd__send_keys.push(
85 `tmux send-keys -t ${target}:window.${i} "${cmd__tmux}" C-m`)
86 }
87 }
88 const code__watch = [
89 `tmux new-session -s ${target} -n window -y 1000 -d`,
90 ...a1__cmd__windows,
91 'tmux select-layout even-vertical',
92 ...a1__cmd__send_keys,
93 `tmux attach -t ${target}`
94 ].join('\n')
95 return code__watch
96 }
97}
98function help__msg() {
99 return `
100Usage: rollup-cmd.js [-c <config-file>] [-t <target>]
101
102Options:
103
104-c, --config Use config file (defaults to './rollup.json')
105-t, --target Use build target defined in config file (defaults to 'browser')
106-h, --help This help message
107 `.trim()
108}