1 | #!/usr/bin/env node
|
2 |
|
3 | const path = require('path')
|
4 | const { promisify } = require('util')
|
5 | const { exec } = require('child_process')
|
6 | const arg = require('arg')
|
7 | const recursiveCopy = require('recursive-copy')
|
8 | const commandExists = require('command-exists').sync
|
9 | const pkg = require('../package.json')
|
10 |
|
11 | const COMMAND = 'copy-aragon-ui-assets'
|
12 | const VERSION = pkg.version
|
13 |
|
14 | const HELP = `
|
15 | Usage: ${COMMAND} [-hv] [-n dirname] <destination>
|
16 |
|
17 | Options:
|
18 |
|
19 | -h, --help display this help and exit
|
20 | -v, --version display the version number
|
21 | -n, --dirname <dirname> set the directory name (default: aragon-ui)
|
22 |
|
23 | Examples:
|
24 |
|
25 | $ ${COMMAND} .
|
26 | $ ${COMMAND} -n aragon-ui ./public
|
27 | `
|
28 |
|
29 | const argspec = {
|
30 | '--help': Boolean,
|
31 | '--version': Boolean,
|
32 | '--dirname': String,
|
33 | '-h': '--help',
|
34 | '-v': '--version',
|
35 | '-n': '--dirname',
|
36 | }
|
37 |
|
38 | const error = message => console.error(`\n ${COMMAND} error: ${message}`)
|
39 |
|
40 | async function main(argv, argspec) {
|
41 | const args = arg(argspec, { permissive: true })
|
42 |
|
43 | if (args['--help']) return HELP
|
44 | if (args['--version']) return VERSION
|
45 |
|
46 | if (!args._[0]) {
|
47 | throw new Error(`no destination directory provided\n${HELP}`)
|
48 | }
|
49 |
|
50 | const source = path.resolve(__dirname, '..', path.dirname(pkg.main))
|
51 | const destination = path.resolve(
|
52 | process.cwd(),
|
53 | args._[0],
|
54 | args['--dirname'] || 'aragon-ui'
|
55 | )
|
56 |
|
57 | const copy = commandExists('rsync') ? rsyncCopy : nodeCopy
|
58 |
|
59 | return copy(source, destination).then(
|
60 | () =>
|
61 | `\naragonUI assets copied to ${path.relative(
|
62 | process.cwd(),
|
63 | destination
|
64 | )}`
|
65 | )
|
66 | }
|
67 |
|
68 | const rsyncCopy = async (from, to) =>
|
69 | promisify(exec)(
|
70 | `mkdir -p "${to}" && rsync --recursive --update --times "${from}/" "${to}"`
|
71 | )
|
72 |
|
73 | const nodeCopy = async (from, to) =>
|
74 | recursiveCopy(from, to, { overwrite: true })
|
75 |
|
76 | main(process.argv, argspec).then(
|
77 | out => {
|
78 | if (out) console.log(out)
|
79 | process.exit(0)
|
80 | },
|
81 | err => {
|
82 | error(err.message)
|
83 | process.exit(1)
|
84 | }
|
85 | )
|