forked from Josh-Miller/stylize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·165 lines (140 loc) · 3.96 KB
/
cli.js
File metadata and controls
executable file
·165 lines (140 loc) · 3.96 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
'use strict';
var program = require('commander'),
_ = require('lodash'),
fs = require('fs-extra'),
chokidar = require('chokidar'),
childProcess = require('child_process'),
stylizeRegression = require('stylize-regression'),
chalk = require('chalk'),
path = require('path'),
stylize = require('./index');
// CLI
var cliCompile = require('./lib/compile'),
cliExport = require('./lib/export'),
cliInit = require('./lib/init');
var log = console.log.bind(console);
var projectPath = process.cwd();
// Watch patterns
var watch = function(params) {
log(chalk.green('Watching'));
var watchDir = params.watchDir;
var cmdPath = process.cwd();
var watcher = chokidar.watch(cmdPath + '/src', {
usePolling: true,
persistent: true,
ignored: /[\/\\]\./,
});
var compileParams = {
projectPath: cmdPath
};
if (params.output) {
compileParams.output = params.output;
}
cliCompile.run(compileParams, function() {
log(chalk.green('Fin'));
});
watcher.on('change', function(path, stats) {
var pathArr = path.split('/');
var fileArr = pathArr[pathArr.length - 1].split('.');
var fileSuffix = fileArr[fileArr.length - 1];
if (fileSuffix === 'yml') {
console.log(chalk.cyan('Updated', fileArr));
cliCompile.run(compileParams, function() {
log(chalk.green('Fin'));
});
} else {
var singlePatternParams = {
file: path
};
if (params.output) {
singlePatternParams.output = params.output;
}
cliCompile.singlePattern(singlePatternParams, function(fileName) {
log(chalk.cyan('Updated', fileName));
});
}
});
watcher.on('add', function(path, stats) {
var singlePatternParams = {
file: path
};
if (params.output) {
singlePatternParams.output = params.output;
}
cliCompile.singlePattern(singlePatternParams, function(fileName) {
log(chalk.cyan('Added', fileName));
});
});
};
program
.version('0.0.1');
program
.command('init [env]')
.description('Builds a new instance of Stylize')
.action(function() {
log(chalk.green('Initializing new Stylize project...'));
cliInit.run();
});
program
.command('compile [env]')
.alias('c')
.description('Compile patterns')
.option('-w, --watch', 'Watch and compile patterns on change')
.option('-p, --path [value]', 'Set the path to your Stylize project root')
.option('-o, --output [value]', 'Set an output path')
.action(function(env, options) {
var mode = options.watch || false;
if (mode) {
var watchDir = {};
var params = {
watchDir: watchDir,
}
if (options.output) {
params.output = options.output;
}
watch(params);
} else {
if (options.path) {
projectPath = path.join(process.cwd(), options.path);
}
var params = {
projectPath: projectPath,
}
if (options.output) {
params.output = options.output;
}
cliCompile.run(params, function() {
log(chalk.green('Fin'));
});
}
});
program
.command('regression [env]')
.alias('r')
.description('Run regression test')
.option('-p, --path [value]', 'Set the path to your Stylize project root')
.action(function(env, options) {
if (options.path) {
projectPath = path.join(process.cwd(), options.path);
}
stylizeRegression.get(projectPath, function(patterns) {
stylizeRegression.takeScreenshot(patterns);
});
});
program
.command('export [env]')
.alias('e')
.description('Export patterns')
.option('-o, --output [value]', 'Set an output path')
.option('-p, --path [value]', 'Set the path to your Stylize project root')
.action(function(env, options) {
var output = options.output || false;
if (options.path) {
projectPath = path.join(process.cwd(), options.path);
}
cliExport.run(projectPath, function() {
log(chalk.green('Fin'));
});
});
program.parse(process.argv);