forked from domwashburn/postcss-parent-selector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart
executable file
·157 lines (138 loc) · 4.55 KB
/
start
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
#!/usr/bin/env node
var exec = require('child_process').exec;
var path = require('path');
var fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf8');
var questionCallback = null;
var lastQuestion = 0;
process.stdin.on('data', function (text) {
questionCallback(text.trim());
});
var questions = {
AUTHOR_NAME: 'Your name: ',
AUTHOR_EMAIL: 'Your email: ',
GITHUB_NAME: 'GitHub username: ',
PLUGIN_NAME: 'Plugin name: postcss-',
PLUGIN_DESC: '\nFinish sentence with plugin description:\nPostCSS plugin ',
KEYWORDS: '\nFinish plugin keywords:\npostcss, css, postcss-plugin, '
};
var autodetects = {
AUTHOR_NAME: 'git config user.name',
AUTHOR_EMAIL: 'git config user.email'
};
var answers = { };
function copyFileSync(source, target) {
var targetFile = target;
if ( fs.existsSync(target) ) {
if ( fs.lstatSync(target).isDirectory() ) {
targetFile = path.join( target, path.basename(source) );
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
function copyFolderSync(source, target) {
var files = [];
if ( !fs.existsSync(target) ) fs.mkdirSync(target);
if ( fs.lstatSync(source).isDirectory() ) {
files = fs.readdirSync(source);
files.forEach(function (file) {
var targetFile = path.join(target, file);
var curSource = path.join(source, file);
if ( fs.lstatSync(curSource).isDirectory() ) {
copyFolderSync(curSource, targetFile);
} else {
copyFileSync(curSource, target);
}
});
}
}
function fillFiles(dir) {
fs.readdirSync(dir).forEach(function (file) {
if ( file === '.git' ) return;
var filepath = path.join(dir, file);
if ( fs.lstatSync(filepath).isDirectory() ) {
fillFiles(filepath);
} else {
var content = fs.readFileSync(filepath).toString();
for ( var code in answers ) {
while ( content.indexOf(code) !== -1 ) {
content = content.replace(code, answers[code]);
}
}
fs.writeFileSync(filepath, content);
}
});
}
function cleanRepo(dir, callback) {
var readme = path.join(dir, 'README.md');
fs.unlinkSync(readme);
fs.renameSync(readme + '.example', readme);
fs.unlinkSync(path.join(dir, 'start'));
exec('cd "' + dir + '" && rm -Rf .git/', callback);
}
function initProject(dir, callback) {
process.stdout.write('\nInstalling npm packages...');
exec('cd "' + dir + '" && git init && npm install', callback);
}
function autodetect(code, callback) {
var command = autodetects[code];
if ( !command ) return callback();
try {
exec(command, function (_, stdout) {
var value = stdout.trim();
if ( value ) {
process.stdout.write(questions[code] + value + '\n');
answers[code] = value;
lastQuestion += 1;
nextQuestion();
} else {
callback();
}
});
} catch (error) {
callback();
}
}
function nextQuestion() {
var code = Object.keys(questions)[lastQuestion];
if ( code ) {
autodetect(code, function () {
process.stdout.write(questions[code]);
questionCallback = function (result) {
if ( result === '' ) {
nextQuestion();
} else {
answers[code] = result;
lastQuestion += 1;
nextQuestion();
}
};
});
} else {
answers.PLUGIN_NAME = 'postcss-' + answers.PLUGIN_NAME;
answers.PLUGIN_TITLE = answers.PLUGIN_NAME
.replace(/-\w/g, function (str) {
return ' ' + str[1].toUpperCase();
})
.replace(/^postcss/, 'PostCSS');
answers.KEYWORDS = ',\n ' + answers.KEYWORDS
.split(',')
.map(function (i) {
return '"' + i.trim() + '"';
})
.join(',\n ');
var dir = path.join(__dirname, '..', answers.PLUGIN_NAME);
copyFolderSync(__dirname, dir);
fillFiles(dir);
cleanRepo(dir, function () {
initProject(dir, function () {
process.stdout.write(
'\nDone. Remove this dir and continue work in ' +
'../' + answers.PLUGIN_NAME + '/\n');
process.exit(0);
});
});
}
}
nextQuestion();