-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.js
executable file
·98 lines (83 loc) · 3.02 KB
/
init.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
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
#!/usr/bin/env node
var fs = require('fs');
var rm = require('rimraf');
var glob = require('multi-glob').glob;
var program = require('commander');
var inquirer = require('inquirer');
var package = require('./package.json');
var bower = require('./bower.json');
program
.version(package.version)
.option('-n, --name', 'project name')
.parse(process.argv);
function replaceAll(find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
}
// Initialisation prompt questions
var questions = [{
name: 'name',
message: 'Plugin name (lowercase):',
validate: function(input) {
return input !== '' ? true : 'You must enter a valid name.';
}
}, {
name: 'version',
message: 'Project version:',
default: '0.0.0',
validate: function(input) {
return input !== '' ? true : 'You must enter a valid version.';
}
}, {
name: 'description',
message: 'Plugin description:'
}, {
name: 'keywords',
message: 'Plugin keywords:'
}, {
name: 'repository',
message: 'Plugin repository:'
}];
// Prompt user for project informations
inquirer.prompt(questions, function(answers) {
// Update package.json values
package.name = 'vtt-jquery-' + answers.name;
package.version = answers.version;
package.description = answers.description;
package.keywords = answers.keywords;
package.repository = answers.repository;
// Update bower.json values
bower.name = 'vtt-jquery-' + answers.name;
bower.version = answers.version;
bower.description = answers.description;
bower.keywords = answers.keywords;
bower.main = 'dist/jquery.' + answers.name + '.js';
bower.homepage = answers.repository;
// Update package.json
fs.writeFile('./package.json', JSON.stringify(package, null, 2), function(error) {
if (error) return console.log(error);
});
// Update bower.json
fs.writeFile('./bower.json', JSON.stringify(bower, null, 2), function(error) {
if (error) return console.log(error);
});
// Rename plugin main file
fs.renameSync('./src/jquery.plugin.js', './src/jquery.' + answers.name + '.js');
// Save new project values
glob(['./src/jquery.' + answers.name + '.js', './demo/index.html', './README.md'], function(error, files) {
if (error) return console.log(error);
for (var i = 0, t = files.length; i < t; i++) {
(function(i) {
var file = files[i];
// Replace module name inside template file
fs.readFile(file, 'utf8', function(error, data) {
if (error) return console.log(error);
data = replaceAll('MODULENAME_LOWERCASE', answers.name, data);
data = replaceAll('MODULENAME_UPPERCASE', answers.name.charAt(0).toUpperCase() + answers.name.slice(1), data);
fs.writeFile(file, data, 'utf8', function(error) {
if (error) return console.log(error);
});
});
})(i);
}
});
});