-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathgenerate.js
More file actions
68 lines (55 loc) · 2.22 KB
/
Copy pathgenerate.js
File metadata and controls
68 lines (55 loc) · 2.22 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
var fs = require('fs');
var path = require('path');
module.exports = function (grunt) {
grunt.registerTask('generator', function () {
var config = grunt.config('generator.settings');
var mixinsDir = path.resolve(__dirname, '..', 'mixins');
// Check whether mixins folder exists
if (!fs.existsSync(mixinsDir)) {
grunt.fail.fatal('Mixins folder does not exist. Try to install the whole workflow again,\nor send issue on github.');
}
// Check whether concrete mixin folder exists
if (fs.existsSync(path.resolve(mixinsDir, config.mixin_css_name))) {
grunt.fail.fatal(config.mixin_css_name + ' folder already exists. Choose another name.');
}
// Create folder
var folder = fs.mkdirSync(path.resolve(mixinsDir, config.mixin_css_name));
grunt.log.ok('Folder created – DONE');
// Create files inside folder
var srcDir = path.resolve(__dirname, 'src');
// Check whether mixin template exists
if (fs.existsSync(path.resolve(srcDir, 'mixin-template.js'))) {
grunt.fail.fatal('Mixin template does not exist. Try to install the whole workflow again,\nor send issue on github.');
}
function fileCreator(mixin_name, data, exported, done) {
done = done || function(){};
if (mixin_name) {
var mixinTemplatePath = path.resolve(srcDir, mixin_name + '.js');
var mixinFile = fs.readFileSync(mixinTemplatePath, 'utf8');
mixinFile = mixinFile.replace(/{{\s*(\w+\s*\|?\s*\w+)\s*}}/g, function (match, variable) {
return (!(config[variable] instanceof Array) && (config[variable])) || (JSON.stringify(data[variable]).replace(/"/g, '\''));
});
}
fs.writeFileSync(path.resolve(mixinsDir, exported.path, exported.file), mixinFile || '##');
done();
}
fileCreator('mixin-template', config, {
path: config.mixin_css_name,
file: config.mixin_css_name + '.js'
}, function () {
grunt.log.ok('Mixin file created – DONE');
});
fileCreator('test-template', config, {
path: config.mixin_css_name,
file: 'test.js'
}, function () {
grunt.log.ok('Test file created – DONE');
});
fileCreator('documentation-template', config, {
path: config.mixin_css_name,
file: config.mixin_css_name + '.md'
}, function () {
grunt.log.ok('Document file created – DONE');
});
});
};