1 | import { promises as fs } from 'fs'
|
2 | import chalk from 'chalk'
|
3 | import cleanup from './clean.js'
|
4 | import minimist from 'minimist'
|
5 | import path from 'path'
|
6 | import pkg from './package.json'
|
7 | import updateNotifier from 'update-notifier'
|
8 | import watch from './watch.js'
|
9 |
|
10 | let wait = time => new Promise(resolve => setTimeout(resolve, time))
|
11 |
|
12 | ;(async function() {
|
13 | let {
|
14 | _,
|
15 | as,
|
16 | clean,
|
17 | help,
|
18 | tools,
|
19 | watch: shouldWatch,
|
20 | verbose,
|
21 | version,
|
22 | } = minimist(process.argv.slice(2), {
|
23 | alias: {
|
24 | help: 'h',
|
25 | },
|
26 | booleans: ['clean', 'help', 'tools', 'watch', 'version'],
|
27 | default: {
|
28 | as: 'react-dom',
|
29 | clean: false,
|
30 | tools: true,
|
31 | verbose: true,
|
32 | version: false,
|
33 | watch: false,
|
34 | },
|
35 | })
|
36 |
|
37 | if (!shouldWatch && tools) {
|
38 | tools = false
|
39 | }
|
40 |
|
41 | if (help) {
|
42 | console.log(`
|
43 | views-morph [directory]
|
44 | --as target platform
|
45 | react-dom (default)
|
46 | react-native
|
47 | react-pdf
|
48 | --clean clean the autogenerated .view.js files
|
49 | --tools use with Views Tools, defauls to true when
|
50 | --watch is enabled, otherwise defaults to false
|
51 | --verbose defaults to true
|
52 | --version print the version
|
53 | --watch watch a directory and produce .view.js files
|
54 | `)
|
55 |
|
56 | process.exit()
|
57 | }
|
58 |
|
59 | if (version) {
|
60 | console.log(`v${pkg.version}`)
|
61 | process.exit()
|
62 | }
|
63 |
|
64 | let input = Array.isArray(_) && _[0]
|
65 |
|
66 | if (!input || !(await fs.stat(input)).isDirectory()) {
|
67 | console.error(
|
68 | `You need to specify an input directory to watch. ${input} is a file.`
|
69 | )
|
70 | process.exit()
|
71 | }
|
72 |
|
73 | if (!path.isAbsolute(input)) {
|
74 | input = path.normalize(path.join(process.cwd(), input))
|
75 | }
|
76 |
|
77 | try {
|
78 | if ((await fs.stat(path.join(input, 'src'))).isDirectory()) {
|
79 | input = path.join(input, 'src')
|
80 | }
|
81 | } catch (error) {}
|
82 |
|
83 | if (clean) {
|
84 | console.log(`Cleaning up ${input}...`)
|
85 | await cleanup(input, verbose)
|
86 | process.exit()
|
87 | }
|
88 |
|
89 | updateNotifier({ pkg }).notify()
|
90 |
|
91 | if (verbose) {
|
92 | console.log(chalk.underline(`Views Tools morpher v${pkg.version}`))
|
93 |
|
94 | console.log(
|
95 | `\nWill morph files at "${chalk.green(input)}" as "${chalk.green(as)}" ${
|
96 | tools ? 'with Views Tools' : 'without Views Tools'
|
97 | }`
|
98 | )
|
99 |
|
100 | if (shouldWatch && !tools) {
|
101 | console.log(
|
102 | chalk.bgRed(' ')
|
103 | )
|
104 | console.log()
|
105 | console.log(`🚨 You're missing out!!!`)
|
106 | console.log(
|
107 | chalk.bold(
|
108 | '🚀 Views Tools can help you find product market\n fit before you run out of money.'
|
109 | )
|
110 | )
|
111 | console.log()
|
112 | console.log(
|
113 | '✨ Find out how 👉',
|
114 | chalk.bold(chalk.green('https://views.tools'))
|
115 | )
|
116 | console.log()
|
117 | console.log(
|
118 | chalk.bgRed(' ')
|
119 | )
|
120 | await wait(15000)
|
121 | }
|
122 |
|
123 | console.log(chalk.yellow('A'), '= Added')
|
124 | console.log(chalk.green('M'), `= Morphed`)
|
125 | console.log(chalk.blue('X'), `= Deleted`)
|
126 | console.log('\nPress', chalk.blue('ctrl+c'), 'to stop at any time.\n')
|
127 | }
|
128 |
|
129 | watch({
|
130 | as,
|
131 | once: !shouldWatch,
|
132 | src: input,
|
133 | tools,
|
134 | verbose,
|
135 | })
|
136 | })()
|