You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

750 lines
19 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. /*******************************
  2. Set-up
  3. *******************************/
  4. var
  5. fs = require('fs'),
  6. path = require('path'),
  7. defaults = require('../defaults'),
  8. release = require('./release'),
  9. requireDotFile = require('require-dot-file')
  10. ;
  11. /*******************************
  12. When to Ask
  13. *******************************/
  14. /* Preconditions for install questions */
  15. var when = {
  16. // path
  17. changeRoot: function(questions) {
  18. return (questions.useRoot !== undefined && questions.useRoot !== true);
  19. },
  20. // permissions
  21. changePermissions: function(questions) {
  22. return (questions.changePermissions && questions.changePermissions === true);
  23. },
  24. // install
  25. hasConfig: function() {
  26. return requireDotFile('semantic.json');
  27. },
  28. allowOverwrite: function(questions) {
  29. return (questions.overwrite === undefined || questions.overwrite == 'yes');
  30. },
  31. notAuto: function(questions) {
  32. return (questions.install !== 'auto' && (questions.overwrite === undefined || questions.overwrite == 'yes'));
  33. },
  34. custom: function(questions) {
  35. return (questions.install === 'custom' && (questions.overwrite === undefined || questions.overwrite == 'yes'));
  36. },
  37. express: function(questions) {
  38. return (questions.install === 'express' && (questions.overwrite === undefined || questions.overwrite == 'yes'));
  39. },
  40. // customize
  41. customize: function(questions) {
  42. return (questions.customize === true);
  43. },
  44. primaryColor: function(questions) {
  45. return (questions.primaryColor);
  46. },
  47. secondaryColor: function(questions) {
  48. return (questions.secondaryColor);
  49. }
  50. };
  51. /*******************************
  52. Response Filters
  53. *******************************/
  54. /* Filters to user input from install questions */
  55. var filter = {
  56. removeTrailingSlash: function(path) {
  57. return path.replace(/(\/$|\\$)+/mg, '');
  58. }
  59. };
  60. /*******************************
  61. Configuration
  62. *******************************/
  63. module.exports = {
  64. // check whether install is setup
  65. isSetup: function() {
  66. return when.hasConfig();
  67. },
  68. // checks if files are in a PM directory
  69. getPackageManager: function(directory) {
  70. var
  71. // returns last matching result (avoid sub-module detection)
  72. walk = function(directory) {
  73. var
  74. pathArray = directory.split(path.sep),
  75. folder = pathArray[pathArray.length - 1],
  76. nextDirectory = path.join(directory, path.sep, '..')
  77. ;
  78. if( folder == 'bower_components') {
  79. return {
  80. name: 'Bower',
  81. root: nextDirectory
  82. };
  83. }
  84. else if(folder == 'node_modules') {
  85. return {
  86. name: 'NPM',
  87. root: nextDirectory
  88. };
  89. }
  90. else if(folder == 'composer') {
  91. return {
  92. name: 'Composer',
  93. root: nextDirectory
  94. };
  95. }
  96. if(path.resolve(directory) == path.resolve(nextDirectory)) {
  97. return false;
  98. }
  99. // recurse downward
  100. return walk(nextDirectory);
  101. }
  102. ;
  103. // start walk from current directory if none specified
  104. directory = directory || (__dirname + path.sep);
  105. return walk(directory);
  106. },
  107. // checks if files is PMed submodule
  108. isSubModule: function(directory) {
  109. var
  110. moduleFolders = 0,
  111. walk = function(directory) {
  112. var
  113. pathArray = directory.split(path.sep),
  114. folder = pathArray[pathArray.length - 2],
  115. nextDirectory = path.join(directory, path.sep, '..')
  116. ;
  117. if( folder == 'bower_components') {
  118. moduleFolders++;
  119. }
  120. else if(folder == 'node_modules') {
  121. moduleFolders++;
  122. }
  123. else if(folder == 'composer') {
  124. moduleFolders++;
  125. }
  126. if(path.resolve(directory) == path.resolve(nextDirectory)) {
  127. return (moduleFolders > 1);
  128. }
  129. // recurse downward
  130. return walk(nextDirectory);
  131. }
  132. ;
  133. // start walk from current directory if none specified
  134. directory = directory || (__dirname + path.sep);
  135. return walk(directory);
  136. },
  137. createJSON: function(answers) {
  138. var
  139. json = {
  140. paths: {
  141. source: {},
  142. output: {}
  143. }
  144. }
  145. ;
  146. // add components
  147. if(answers.components) {
  148. json.components = answers.components;
  149. }
  150. // add rtl choice
  151. if(answers.rtl) {
  152. json.rtl = answers.rtl;
  153. }
  154. // add permissions
  155. if(answers.permission) {
  156. json.permission = answers.permission;
  157. }
  158. // add path to semantic
  159. if(answers.semanticRoot) {
  160. json.base = path.normalize(answers.semanticRoot);
  161. }
  162. // record version number to avoid re-installing on same version
  163. json.version = release.version;
  164. // add dist folder paths
  165. if(answers.dist) {
  166. answers.dist = path.normalize(answers.dist);
  167. json.paths.output = {
  168. packaged : path.normalize(answers.dist + '/'),
  169. uncompressed : path.normalize(answers.dist + '/components/'),
  170. compressed : path.normalize(answers.dist + '/components/'),
  171. themes : path.normalize(answers.dist + '/themes/')
  172. };
  173. }
  174. // add site path
  175. if(answers.site) {
  176. json.paths.source.site = path.normalize(answers.site + '/');
  177. }
  178. if(answers.packaged) {
  179. json.paths.output.packaged = path.normalize(answers.packaged + '/');
  180. }
  181. if(answers.compressed) {
  182. json.paths.output.compressed = path.normalize(answers.compressed + '/');
  183. }
  184. if(answers.uncompressed) {
  185. json.paths.output.uncompressed = path.normalize(answers.uncompressed + '/');
  186. }
  187. return json;
  188. },
  189. // files cleaned up after install
  190. setupFiles: [
  191. './src/theme.config.example',
  192. './semantic.json.example',
  193. './src/_site'
  194. ],
  195. regExp: {
  196. // used to match siteFolder variable in theme.less
  197. siteVariable: /@siteFolder .*\'(.*)/mg
  198. },
  199. // source paths (relative to tasks/install.js )
  200. source: {
  201. config : './semantic.json.example',
  202. definitions : './src/definitions',
  203. gulpFile : './gulpfile.js',
  204. lessImport : './src/semantic.less',
  205. site : './src/_site',
  206. tasks : './tasks',
  207. themeConfig : './src/theme.config.example',
  208. themeImport : './src/theme.less',
  209. themes : './src/themes',
  210. userGulpFile : './tasks/config/npm/gulpfile.js'
  211. },
  212. // expected final filenames
  213. files: {
  214. config : 'semantic.json',
  215. lessImport : 'src/semantic.less',
  216. site : 'src/site',
  217. themeConfig : 'src/theme.config',
  218. themeImport : 'src/theme.less'
  219. },
  220. // folder paths to files relative to root
  221. folders: {
  222. config : './',
  223. definitions : 'src/definitions/',
  224. defaultTheme : 'default/',
  225. lessImport : 'src/',
  226. modules : 'node_modules/',
  227. site : 'src/site/',
  228. tasks : 'tasks/',
  229. themeConfig : 'src/',
  230. themeImport : 'src/',
  231. themes : 'src/themes/'
  232. },
  233. // questions asked during install
  234. questions: {
  235. root: [
  236. {
  237. type : 'list',
  238. name : 'useRoot',
  239. message :
  240. ' \n' +
  241. ' {packageMessage} \n' +
  242. ' \n' +
  243. ' Is this your project folder?\n' +
  244. ' \033[92m{root}\033[0m \n' +
  245. ' \n ' +
  246. '\n',
  247. choices: [
  248. {
  249. name : 'Yes',
  250. value : true
  251. },
  252. {
  253. name : 'No, let me specify',
  254. value : false
  255. }
  256. ]
  257. },
  258. {
  259. type : 'input',
  260. name : 'customRoot',
  261. message : 'Please enter the absolute path to your project root',
  262. default : '/my/project/path',
  263. when : when.changeRoot
  264. },
  265. {
  266. type : 'input',
  267. name : 'semanticRoot',
  268. message : 'Where should we put Semantic UI inside your project?',
  269. default : 'semantic/'
  270. }
  271. ],
  272. setup: [
  273. {
  274. type: 'list',
  275. name: 'overwrite',
  276. message: 'It looks like you have a semantic.json file already.',
  277. when: when.hasConfig,
  278. choices: [
  279. {
  280. name: 'Yes, extend my current settings.',
  281. value: 'yes'
  282. },
  283. {
  284. name: 'Skip install',
  285. value: 'no'
  286. }
  287. ]
  288. },
  289. {
  290. type: 'list',
  291. name: 'install',
  292. message: 'Set-up Semantic UI',
  293. when: when.allowOverwrite,
  294. choices: [
  295. {
  296. name: 'Automatic (Use defaults locations and all components)',
  297. value: 'auto'
  298. },
  299. {
  300. name: 'Express (Set components and output folder)',
  301. value: 'express'
  302. },
  303. {
  304. name: 'Custom (Customize all src/dist values)',
  305. value: 'custom'
  306. }
  307. ]
  308. },
  309. {
  310. type: 'checkbox',
  311. name: 'components',
  312. message: 'What components should we include in the package?',
  313. // duplicated manually from tasks/defaults.js with additional property
  314. choices: [
  315. { name: "reset", checked: true },
  316. { name: "site", checked: true },
  317. { name: "button", checked: true },
  318. { name: "container", checked: true },
  319. { name: "divider", checked: true },
  320. { name: "flag", checked: true },
  321. { name: "header", checked: true },
  322. { name: "icon", checked: true },
  323. { name: "image", checked: true },
  324. { name: "input", checked: true },
  325. { name: "label", checked: true },
  326. { name: "list", checked: true },
  327. { name: "loader", checked: true },
  328. { name: "rail", checked: true },
  329. { name: "reveal", checked: true },
  330. { name: "segment", checked: true },
  331. { name: "step", checked: true },
  332. { name: "breadcrumb", checked: true },
  333. { name: "form", checked: true },
  334. { name: "grid", checked: true },
  335. { name: "menu", checked: true },
  336. { name: "message", checked: true },
  337. { name: "table", checked: true },
  338. { name: "ad", checked: true },
  339. { name: "card", checked: true },
  340. { name: "comment", checked: true },
  341. { name: "feed", checked: true },
  342. { name: "item", checked: true },
  343. { name: "statistic", checked: true },
  344. { name: "accordion", checked: true },
  345. { name: "checkbox", checked: true },
  346. { name: "dimmer", checked: true },
  347. { name: "dropdown", checked: true },
  348. { name: "modal", checked: true },
  349. { name: "nag", checked: true },
  350. { name: "popup", checked: true },
  351. { name: "progress", checked: true },
  352. { name: "rating", checked: true },
  353. { name: "search", checked: true },
  354. { name: "shape", checked: true },
  355. { name: "sidebar", checked: true },
  356. { name: "sticky", checked: true },
  357. { name: "tab", checked: true },
  358. { name: "transition", checked: true },
  359. { name: "video", checked: true },
  360. { name: "api", checked: true },
  361. { name: "form", checked: true },
  362. { name: "state", checked: true },
  363. { name: "visibility", checked: true }
  364. ],
  365. when: when.notAuto
  366. },
  367. {
  368. type: 'list',
  369. name: 'changePermisions',
  370. when: when.notAuto,
  371. message: 'Should we set permissions on outputted files?',
  372. choices: [
  373. {
  374. name: 'No',
  375. value: false
  376. },
  377. {
  378. name: 'Yes',
  379. value: true
  380. },
  381. ]
  382. },
  383. {
  384. type: 'input',
  385. name: 'permission',
  386. message: 'What octal file permission should outputted files receive?',
  387. default: defaults.permission,
  388. when: when.changePermissions
  389. },
  390. {
  391. type: 'list',
  392. name: 'rtl',
  393. message: 'Do you use a RTL (Right-To-Left) language?',
  394. when: when.notAuto,
  395. choices: [
  396. {
  397. name: 'No',
  398. value: false
  399. },
  400. {
  401. name: 'Yes',
  402. value: true
  403. },
  404. ]
  405. },
  406. {
  407. type: 'input',
  408. name: 'dist',
  409. message: 'Where should we output Semantic UI?',
  410. default: defaults.paths.output.packaged,
  411. filter: filter.removeTrailingSlash,
  412. when: when.express
  413. },
  414. {
  415. type: 'input',
  416. name: 'site',
  417. message: 'Where should we put your site folder?',
  418. default: defaults.paths.source.site,
  419. filter: filter.removeTrailingSlash,
  420. when: when.custom
  421. },
  422. {
  423. type: 'input',
  424. name: 'packaged',
  425. message: 'Where should we output a packaged version?',
  426. default: defaults.paths.output.packaged,
  427. filter: filter.removeTrailingSlash,
  428. when: when.custom
  429. },
  430. {
  431. type: 'input',
  432. name: 'compressed',
  433. message: 'Where should we output compressed components?',
  434. default: defaults.paths.output.compressed,
  435. filter: filter.removeTrailingSlash,
  436. when: when.custom
  437. },
  438. {
  439. type: 'input',
  440. name: 'uncompressed',
  441. message: 'Where should we output uncompressed components?',
  442. default: defaults.paths.output.uncompressed,
  443. filter: filter.removeTrailingSlash,
  444. when: when.custom
  445. }
  446. ],
  447. cleanup: [
  448. {
  449. type: 'list',
  450. name: 'cleanup',
  451. message: 'Should we remove set-up files?',
  452. choices: [
  453. {
  454. name: 'Yes (re-install will require redownloading semantic).',
  455. value: 'yes'
  456. },
  457. {
  458. name: 'No Thanks',
  459. value: 'no'
  460. }
  461. ]
  462. },
  463. {
  464. type: 'list',
  465. name: 'build',
  466. message: 'Do you want to build Semantic now?',
  467. choices: [
  468. {
  469. name: 'Yes',
  470. value: 'yes'
  471. },
  472. {
  473. name: 'No',
  474. value: 'no'
  475. }
  476. ]
  477. },
  478. ],
  479. site: [
  480. {
  481. type: 'list',
  482. name: 'customize',
  483. message: 'You have not yet customized your site, can we help you do that?',
  484. choices: [
  485. {
  486. name: 'Yes, ask me a few questions',
  487. value: true
  488. },
  489. {
  490. name: 'No I\'ll do it myself',
  491. value: false
  492. }
  493. ]
  494. },
  495. {
  496. type: 'list',
  497. name: 'headerFont',
  498. message: 'Select your header font',
  499. choices: [
  500. {
  501. name: 'Helvetica Neue, Arial, sans-serif',
  502. value: 'Helvetica Neue, Arial, sans-serif;'
  503. },
  504. {
  505. name: 'Lato (Google Fonts)',
  506. value: 'Lato'
  507. },
  508. {
  509. name: 'Open Sans (Google Fonts)',
  510. value: 'Open Sans'
  511. },
  512. {
  513. name: 'Source Sans Pro (Google Fonts)',
  514. value: 'Source Sans Pro'
  515. },
  516. {
  517. name: 'Droid (Google Fonts)',
  518. value: 'Droid'
  519. },
  520. {
  521. name: 'I\'ll choose on my own',
  522. value: false
  523. }
  524. ],
  525. when: when.customize
  526. },
  527. {
  528. type: 'list',
  529. name: 'pageFont',
  530. message: 'Select your page font',
  531. choices: [
  532. {
  533. name: 'Helvetica Neue, Arial, sans-serif',
  534. value: 'Helvetica Neue, Arial, sans-serif;'
  535. },
  536. {
  537. name: 'Lato (Import from Google Fonts)',
  538. value: 'Lato'
  539. },
  540. {
  541. name: 'Open Sans (Import from Google Fonts)',
  542. value: 'Open Sans'
  543. },
  544. {
  545. name: 'Source Sans Pro (Import from Google Fonts)',
  546. value: 'Source Sans Pro'
  547. },
  548. {
  549. name: 'Droid (Google Fonts)',
  550. value: 'Droid'
  551. },
  552. {
  553. name: 'I\'ll choose on my own',
  554. value: false
  555. }
  556. ],
  557. when: when.customize
  558. },
  559. {
  560. type: 'list',
  561. name: 'fontSize',
  562. message: 'Select your base font size',
  563. default: '14px',
  564. choices: [
  565. {
  566. name: '12px',
  567. },
  568. {
  569. name: '13px',
  570. },
  571. {
  572. name: '14px (Recommended)',
  573. value: '14px'
  574. },
  575. {
  576. name: '15px',
  577. },
  578. {
  579. name: '16px',
  580. },
  581. {
  582. name: 'I\'ll choose on my own',
  583. value: false
  584. }
  585. ],
  586. when: when.customize
  587. },
  588. {
  589. type: 'list',
  590. name: 'primaryColor',
  591. message: 'Select the closest name for your primary brand color',
  592. default: '14px',
  593. choices: [
  594. {
  595. name: 'Blue'
  596. },
  597. {
  598. name: 'Green'
  599. },
  600. {
  601. name: 'Orange'
  602. },
  603. {
  604. name: 'Pink'
  605. },
  606. {
  607. name: 'Purple'
  608. },
  609. {
  610. name: 'Red'
  611. },
  612. {
  613. name: 'Teal'
  614. },
  615. {
  616. name: 'Yellow'
  617. },
  618. {
  619. name: 'Black'
  620. },
  621. {
  622. name: 'I\'ll choose on my own',
  623. value: false
  624. }
  625. ],
  626. when: when.customize
  627. },
  628. {
  629. type: 'input',
  630. name: 'PrimaryHex',
  631. message: 'Enter a hexcode for your primary brand color',
  632. when: when.primaryColor
  633. },
  634. {
  635. type: 'list',
  636. name: 'secondaryColor',
  637. message: 'Select the closest name for your secondary brand color',
  638. default: '14px',
  639. choices: [
  640. {
  641. name: 'Blue'
  642. },
  643. {
  644. name: 'Green'
  645. },
  646. {
  647. name: 'Orange'
  648. },
  649. {
  650. name: 'Pink'
  651. },
  652. {
  653. name: 'Purple'
  654. },
  655. {
  656. name: 'Red'
  657. },
  658. {
  659. name: 'Teal'
  660. },
  661. {
  662. name: 'Yellow'
  663. },
  664. {
  665. name: 'Black'
  666. },
  667. {
  668. name: 'I\'ll choose on my own',
  669. value: false
  670. }
  671. ],
  672. when: when.customize
  673. },
  674. {
  675. type: 'input',
  676. name: 'secondaryHex',
  677. message: 'Enter a hexcode for your secondary brand color',
  678. when: when.secondaryColor
  679. }
  680. ]
  681. },
  682. settings: {
  683. /* Rename Files */
  684. rename: {
  685. json : { extname : '.json' },
  686. },
  687. /* Copy Install Folders */
  688. wrench: {
  689. // overwrite existing files update & install (default theme / definition)
  690. overwrite: {
  691. forceDelete : true,
  692. excludeHiddenUnix : true,
  693. preserveFiles : false
  694. },
  695. // only create files that don't exist (site theme update)
  696. merge: {
  697. forceDelete : false,
  698. excludeHiddenUnix : true,
  699. preserveFiles : true
  700. }
  701. }
  702. }
  703. };