-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathindex.js
48 lines (40 loc) · 1.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const concise = require('../src/index')
const chokidar = require('chokidar')
const chalk = require('chalk')
const command = {
name: process.argv[2],
flags: process.argv[3],
input: process.argv[process.argv.length - 2],
output: process.argv[process.argv.length - 1]
}
const build = (input, output) => {
concise.process(fs.readFileSync(input, 'utf8'), { from: input }).then(result => {
// Create all the parent directories if required
fs.mkdirSync(path.dirname(output), { recursive: true })
// Write the CSS
fs.writeFile(output, result.css, err => {
if (err) throw err
console.log(chalk.bold.green('File written: ') + output)
console.log(chalk.bold.green('From: ') + input)
})
})
}
const watch = path => {
console.log(chalk.bold.blue('\nCurrently watching for changes in: ') + chalk.gray(path))
chokidar.watch(path, { ignored: /\.css$/, ignoreInitial: true }).on('all', (eventType, filename) => {
console.log(chalk.bold.blue(`${eventType.charAt(0).toUpperCase() + eventType.slice(1)} in: `) + filename)
build(command.input, command.output)
})
}
switch (command.name) {
case 'compile':
build(command.input, command.output)
if (command.flags === '-w') watch(path.dirname(command.input))
break
default:
console.log('Unknown command')
break
}