UNPKG

14.1 kBJavaScriptView Raw
1/*******************************
2 * Install Task
3 *******************************/
4
5/*
6 Install tasks
7
8 For more notes
9
10 * Runs automatically after npm update (hooks)
11 * (NPM) Install - Will ask for where to put semantic (outside pm folder)
12 * (NPM) Upgrade - Will look for semantic install, copy over files and update if new version
13 * Standard installer runs asking for paths to site files etc
14
15*/
16
17var
18 gulp = require('gulp'),
19
20 // node dependencies
21 console = require('better-console'),
22 extend = require('extend'),
23 fs = require('fs'),
24 mkdirp = require('mkdirp'),
25 path = require('path'),
26
27 // gulp dependencies
28 chmod = require('gulp-chmod'),
29 del = require('del'),
30 jsonEditor = require('gulp-json-editor'),
31 plumber = require('gulp-plumber'),
32 inquirer = require('inquirer'),
33 rename = require('gulp-rename'),
34 replace = require('gulp-replace'),
35 requireDotFile = require('require-dot-file'),
36 wrench = require('wrench-sui'),
37
38 // install config
39 install = require('./config/project/install'),
40
41 // user config
42 config = require('./config/user'),
43
44 // release config (name/title/etc)
45 release = require('./config/project/release'),
46
47 // shorthand
48 questions = install.questions,
49 files = install.files,
50 folders = install.folders,
51 regExp = install.regExp,
52 settings = install.settings,
53 source = install.source
54;
55
56// Export install task
57module.exports = function (callback) {
58
59 var
60 currentConfig = requireDotFile('semantic.json'),
61 manager = install.getPackageManager(),
62 rootQuestions = questions.root,
63 installFolder = false,
64 answers
65 ;
66
67 console.clear();
68
69 /* Test NPM install
70 manager = {
71 name : 'NPM',
72 root : path.normalize(__dirname + '/../')
73 };
74 */
75
76
77 /* Don't do end user config if SUI is a sub-module */
78 if (install.isSubModule()) {
79 console.info('SUI is a sub-module, skipping end-user install');
80 return;
81 }
82
83 /*-----------------
84 Update SUI
85 -----------------*/
86
87// run update scripts if semantic.json exists
88 if (currentConfig && manager.name === 'NPM') {
89
90 var
91 updateFolder = path.join(manager.root, currentConfig.base),
92 updatePaths = {
93 config : path.join(manager.root, files.config),
94 tasks : path.join(updateFolder, folders.tasks),
95 themeImport : path.join(updateFolder, folders.themeImport),
96 definition : path.join(currentConfig.paths.source.definitions),
97 site : path.join(currentConfig.paths.source.site),
98 theme : path.join(currentConfig.paths.source.themes),
99 defaultTheme: path.join(currentConfig.paths.source.themes, folders.defaultTheme)
100 }
101 ;
102
103 // duck-type if there is a project installed
104 if (fs.existsSync(updatePaths.definition)) {
105
106 // perform update if new version
107 if (currentConfig.version !== release.version) {
108 console.log('Updating Semantic UI from ' + currentConfig.version + ' to ' + release.version);
109
110 console.info('Updating ui definitions...');
111 wrench.copyDirSyncRecursive(source.definitions, updatePaths.definition, settings.wrench.overwrite);
112
113 console.info('Updating default theme...');
114 wrench.copyDirSyncRecursive(source.themes, updatePaths.theme, settings.wrench.merge);
115 wrench.copyDirSyncRecursive(source.defaultTheme, updatePaths.defaultTheme, settings.wrench.overwrite);
116
117 console.info('Updating tasks...');
118 wrench.copyDirSyncRecursive(source.tasks, updatePaths.tasks, settings.wrench.overwrite);
119
120 console.info('Updating gulpfile.js');
121 gulp.src(source.userGulpFile)
122 .pipe(plumber())
123 .pipe(gulp.dest(updateFolder))
124 ;
125
126 // copy theme import
127 console.info('Updating theme import file');
128 gulp.src(source.themeImport)
129 .pipe(plumber())
130 .pipe(gulp.dest(updatePaths.themeImport))
131 ;
132
133 console.info('Adding new site theme files...');
134 wrench.copyDirSyncRecursive(source.site, updatePaths.site, settings.wrench.merge);
135
136 console.info('Updating version...');
137
138 // update version number in semantic.json
139 gulp.src(updatePaths.config)
140 .pipe(plumber())
141 .pipe(rename(settings.rename.json)) // preserve file extension
142 .pipe(jsonEditor({
143 version: release.version
144 }))
145 .pipe(gulp.dest(manager.root))
146 ;
147
148 console.info('Update complete! Run "\x1b[92mgulp build\x1b[0m" to rebuild dist/ files.');
149
150 callback();
151 return;
152 } else {
153 console.log('Current version of Semantic UI already installed');
154 callback();
155 return;
156 }
157
158 } else {
159 console.error('Cannot locate files to update at path: ', updatePaths.definition);
160 console.log('Running installer');
161 }
162
163 }
164
165 /*--------------
166 Determine Root
167 ---------------*/
168
169// PM that supports Build Tools (NPM Only Now)
170 if (manager.name === 'NPM') {
171 rootQuestions[0].message = rootQuestions[0].message
172 .replace('{packageMessage}', 'We detected you are using ' + manager.name + ' Nice!')
173 .replace('{root}', manager.root)
174 ;
175 // set default path to detected PM root
176 rootQuestions[0].default = manager.root;
177 rootQuestions[1].default = manager.root;
178
179 // insert PM questions after "Install Type" question
180 Array.prototype.splice.apply(questions.setup, [2, 0].concat(rootQuestions));
181
182 // omit cleanup questions for managed install
183 questions.cleanup = [];
184 }
185
186
187 /*--------------
188 Create SUI
189 ---------------*/
190
191 gulp.task('run setup', function (callback) {
192
193 // If auto-install is switched on, we skip the configuration section and simply reuse the configuration from semantic.json
194 if (install.shouldAutoInstall()) {
195 answers = {
196 overwrite : 'yes',
197 install : 'auto',
198 useRoot : true,
199 semanticRoot: currentConfig.base
200 };
201 callback();
202 } else {
203 return inquirer.prompt(questions.setup)
204 .then((setupAnswers) => {
205 // hoist
206 answers = setupAnswers;
207 });
208 }
209 });
210
211 gulp.task('create install files', function (callback) {
212
213 /*--------------
214 Exit Conditions
215 ---------------*/
216
217 // if config exists and user specifies not to proceed
218 if (answers.overwrite !== undefined && answers.overwrite == 'no') {
219 callback();
220 return;
221 }
222 console.clear();
223 if (install.shouldAutoInstall()) {
224 console.log('Auto-Installing (Without User Interaction)');
225 } else {
226 console.log('Installing');
227 }
228 console.log('------------------------------');
229
230
231 /*--------------
232 Paths
233 ---------------*/
234
235 var
236 installPaths = {
237 config : files.config,
238 configFolder : folders.config,
239 site : answers.site || folders.site,
240 themeConfig : files.themeConfig,
241 themeConfigFolder: folders.themeConfig
242 }
243 ;
244
245 /*--------------
246 NPM Install
247 ---------------*/
248
249 // Check if PM install
250 if (manager && (answers.useRoot || answers.customRoot)) {
251
252 // Set root to custom root path if set
253 if (answers.customRoot) {
254 if (answers.customRoot === '') {
255 console.log('Unable to proceed, invalid project root');
256 callback();
257 return;
258 }
259 manager.root = answers.customRoot;
260 }
261
262 // special install paths only for PM install
263 installPaths = extend(false, {}, installPaths, {
264 definition : folders.definitions,
265 lessImport : folders.lessImport,
266 tasks : folders.tasks,
267 theme : folders.themes,
268 defaultTheme: path.join(folders.themes, folders.defaultTheme),
269 themeImport : folders.themeImport
270 });
271
272 // add project root to semantic root
273 installFolder = path.join(manager.root, answers.semanticRoot);
274
275 // add install folder to all output paths
276 for (var destination in installPaths) {
277 if (installPaths.hasOwnProperty(destination)) {
278 // config goes in project root, rest in install folder
279 installPaths[destination] = (destination == 'config' || destination == 'configFolder')
280 ? path.normalize(path.join(manager.root, installPaths[destination]))
281 : path.normalize(path.join(installFolder, installPaths[destination]))
282 ;
283 }
284 }
285
286 // create project folders
287 try {
288 mkdirp.sync(installFolder);
289 mkdirp.sync(installPaths.definition);
290 mkdirp.sync(installPaths.theme);
291 mkdirp.sync(installPaths.tasks);
292 } catch (error) {
293 console.error('NPM does not have permissions to create folders at your specified path. Adjust your folders permissions and run "npm install" again');
294 }
295
296 console.log('Installing to \x1b[92m' + answers.semanticRoot + '\x1b[0m');
297
298 console.info('Copying UI definitions');
299 wrench.copyDirSyncRecursive(source.definitions, installPaths.definition, settings.wrench.overwrite);
300
301 console.info('Copying UI themes');
302 wrench.copyDirSyncRecursive(source.themes, installPaths.theme, settings.wrench.merge);
303 wrench.copyDirSyncRecursive(source.defaultTheme, installPaths.defaultTheme, settings.wrench.overwrite);
304
305 console.info('Copying gulp tasks');
306 wrench.copyDirSyncRecursive(source.tasks, installPaths.tasks, settings.wrench.overwrite);
307
308 // copy theme import
309 console.info('Adding theme files');
310 gulp.src(source.themeImport)
311 .pipe(plumber())
312 .pipe(gulp.dest(installPaths.themeImport))
313 ;
314 gulp.src(source.lessImport)
315 .pipe(plumber())
316 .pipe(gulp.dest(installPaths.lessImport))
317 ;
318
319 // create gulp file
320 console.info('Creating gulpfile.js');
321 gulp.src(source.userGulpFile)
322 .pipe(plumber())
323 .pipe(gulp.dest(installFolder))
324 ;
325
326 }
327
328
329 /*--------------
330 Site Theme
331 ---------------*/
332
333 // Copy _site templates folder to destination
334 if (fs.existsSync(installPaths.site)) {
335 console.info('Site folder exists, merging files (no overwrite)', installPaths.site);
336 } else {
337 console.info('Creating site theme folder', installPaths.site);
338 }
339 wrench.copyDirSyncRecursive(source.site, installPaths.site, settings.wrench.merge);
340
341 /*--------------
342 Theme Config
343 ---------------*/
344
345 gulp.task('create theme.config', function () {
346 var
347 // determine path to site theme folder from theme config
348 // force CSS path variable to use forward slashes for paths
349 pathToSite = path.relative(path.resolve(installPaths.themeConfigFolder), path.resolve(installPaths.site)).replace(/\\/g, '/'),
350 siteVariable = "@siteFolder : '" + pathToSite + "/';"
351 ;
352
353 // rewrite site variable in theme.less
354 console.info('Adjusting @siteFolder to: ', pathToSite + '/');
355
356 if (fs.existsSync(installPaths.themeConfig)) {
357 console.info('Modifying src/theme.config (LESS config)', installPaths.themeConfig);
358 return gulp.src(installPaths.themeConfig)
359 .pipe(plumber())
360 .pipe(replace(regExp.siteVariable, siteVariable))
361 .pipe(gulp.dest(installPaths.themeConfigFolder))
362 ;
363 } else {
364 console.info('Creating src/theme.config (LESS config)', installPaths.themeConfig);
365 return gulp.src(source.themeConfig)
366 .pipe(plumber())
367 .pipe(rename({extname: ''}))
368 .pipe(replace(regExp.siteVariable, siteVariable))
369 .pipe(gulp.dest(installPaths.themeConfigFolder))
370 ;
371 }
372 });
373
374 /*--------------
375 Semantic.json
376 ---------------*/
377
378 gulp.task('create semantic.json', function () {
379
380 var
381 jsonConfig = install.createJSON(answers)
382 ;
383
384 // adjust variables in theme.less
385 if (fs.existsSync(installPaths.config)) {
386 console.info('Extending config file (semantic.json)', installPaths.config);
387 return gulp.src(installPaths.config)
388 .pipe(plumber())
389 .pipe(rename(settings.rename.json)) // preserve file extension
390 .pipe(jsonEditor(jsonConfig))
391 .pipe(gulp.dest(installPaths.configFolder))
392 ;
393 } else {
394 console.info('Creating config file (semantic.json)', installPaths.config);
395 return gulp.src(source.config)
396 .pipe(plumber())
397 .pipe(rename({extname: ''})) // remove .template from ext
398 .pipe(jsonEditor(jsonConfig, {end_with_newline: true}))
399 .pipe(gulp.dest(installPaths.configFolder))
400 ;
401 }
402
403 });
404
405 gulp.series('create theme.config', 'create semantic.json')(callback);
406 });
407
408 gulp.task('clean up install', function (callback) {
409
410 // Completion Message
411 if (installFolder && !install.shouldAutoInstall()) {
412 console.log('\n Setup Complete! \n Installing Peer Dependencies. \x1b[0;31mPlease refrain from ctrl + c\x1b[0m... \n After completion navigate to \x1b[92m' + answers.semanticRoot + '\x1b[0m and run "\x1b[92mgulp build\x1b[0m" to build');
413 callback();
414 } else {
415 console.log('');
416 console.log('');
417
418 // If auto-install is switched on, we skip the configuration section and simply build the dependencies
419 if (install.shouldAutoInstall()) {
420 gulp.series('build')(callback);
421 } else {
422 // We don't return the inquirer promise on purpose because we handle the callback ourselves
423 inquirer.prompt(questions.cleanup)
424 .then((answers) => {
425 if (answers.cleanup === 'yes') {
426 del(install.setupFiles);
427 }
428 if (answers.build === 'yes') {
429 gulp.series('build')(callback);
430 } else {
431 callback();
432 }
433 });
434 }
435 }
436 });
437
438 gulp.series('run setup', 'create install files', 'clean up install')(callback);
439};