UNPKG

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