UNPKG

1.98 kBPlain TextView Raw
1#!/usr/bin/env node
2
3const path = require('path')
4const { promisify } = require('util')
5const { exec } = require('child_process')
6const arg = require('arg')
7const recursiveCopy = require('recursive-copy')
8const commandExists = require('command-exists').sync
9const pkg = require('../package.json')
10
11const COMMAND = 'copy-aragon-ui-assets'
12const VERSION = pkg.version
13
14const 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
29const argspec = {
30 '--help': Boolean,
31 '--version': Boolean,
32 '--dirname': String,
33 '-h': '--help',
34 '-v': '--version',
35 '-n': '--dirname',
36}
37
38const error = message => console.error(`\n ${COMMAND} error: ${message}`)
39
40async 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
68const rsyncCopy = async (from, to) =>
69 promisify(exec)(
70 `mkdir -p "${to}" && rsync --recursive --update --times "${from}/" "${to}"`
71 )
72
73const nodeCopy = async (from, to) =>
74 recursiveCopy(from, to, { overwrite: true })
75
76main(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)