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.

752 lines
19 KiB

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