install.js 19.1 KB
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
/*******************************
            Set-up
*******************************/

var
  fs             = require('fs'),
  path           = require('path'),
  defaults       = require('../defaults'),
  release        = require('./release'),

  requireDotFile = require('require-dot-file')
;

/*******************************
          When to Ask
*******************************/

/* Preconditions for install questions */

var when = {

  // path
  changeRoot: function(questions) {
    return (questions.useRoot !== undefined && questions.useRoot !== true);
  },

  // permissions
  changePermissions: function(questions) {
    return (questions.changePermissions && questions.changePermissions === true);
  },

  // install
  hasConfig: function() {
    return requireDotFile('semantic.json', process.cwd());
  },

  allowOverwrite: function(questions) {
    return (questions.overwrite === undefined || questions.overwrite == 'yes');
  },
  notAuto: function(questions) {
    return (questions.install !== 'auto' && (questions.overwrite === undefined || questions.overwrite == 'yes'));
  },
  custom: function(questions) {
    return (questions.install === 'custom' && (questions.overwrite === undefined || questions.overwrite == 'yes'));
  },
  express: function(questions) {
    return (questions.install === 'express' && (questions.overwrite === undefined || questions.overwrite == 'yes'));
  },

  // customize
  customize: function(questions) {
    return (questions.customize === true);
  },
  primaryColor: function(questions) {
    return (questions.primaryColor);
  },
  secondaryColor: function(questions) {
    return (questions.secondaryColor);
  }
};

/*******************************
        Response Filters
*******************************/

/* Filters to user input from install questions */

var filter = {
  removeTrailingSlash: function(path) {
    return path.replace(/(\/$|\\$)+/mg, '');
  }
};

/*******************************
          Configuration
*******************************/

module.exports = {

  // check whether install is setup
  isSetup: function() {
    return when.hasConfig();
  },

  // detect whether there is a semantic.json configuration and that the auto-install option is set to true
  shouldAutoInstall: function() {
    var
      config = when.hasConfig()
    ;
    return config['autoInstall'];
  },

  // checks if files are in a PM directory
  getPackageManager: function(directory) {
    var
      // returns last matching result (avoid sub-module detection)
      walk = function(directory) {
        var
          pathArray     = directory.split(path.sep),
          folder        = pathArray[pathArray.length - 1],
          nextDirectory = path.join(directory, path.sep, '..')
        ;
        if( folder == 'bower_components') {
          return {
            name: 'Bower',
            root: nextDirectory
          };
        }
        else if(folder == 'node_modules') {
         return {
            name: 'NPM',
            root: nextDirectory
          };
        }
        else if(folder == 'composer') {
         return {
            name: 'Composer',
            root: nextDirectory
          };
        }
        if(path.resolve(directory) == path.resolve(nextDirectory)) {
          return false;
        }
        // recurse downward
        return walk(nextDirectory);
      }
    ;
    // start walk from current directory if none specified
    directory = directory || (__dirname + path.sep);
    return walk(directory);
  },

  // checks if files is PMed submodule
  isSubModule: function(directory) {
    var
      moduleFolders = 0,
      walk = function(directory) {
        var
          pathArray     = directory.split(path.sep),
          folder        = pathArray[pathArray.length - 2],
          nextDirectory = path.join(directory, path.sep, '..')
        ;
        if( folder == 'bower_components') {
          moduleFolders++;
        }
        else if(folder == 'node_modules') {
          moduleFolders++;
        }
        else if(folder == 'composer') {
          moduleFolders++;
        }
        if(path.resolve(directory) == path.resolve(nextDirectory)) {
          return (moduleFolders > 1);
        }
        // recurse downward
        return walk(nextDirectory);
      }
    ;
    // start walk from current directory if none specified
    directory = directory || (__dirname + path.sep);
    return walk(directory);
  },


  createJSON: function(answers) {
    var
      json = {
        paths: {
          source: {},
          output: {}
        }
      }
    ;

    // add components
    if(answers.components) {
      json.components = answers.components;
    }

    // add rtl choice
    if(answers.rtl) {
      json.rtl = answers.rtl;
    }

    // add permissions
    if(answers.permission) {
      json.permission = answers.permission;
    }

    // add path to semantic
    if(answers.semanticRoot) {
      json.base = path.normalize(answers.semanticRoot);
    }

    // record version number to avoid re-installing on same version
    json.version = release.version;

    // add dist folder paths
    if(answers.dist) {
      answers.dist = path.normalize(answers.dist);

      json.paths.output = {
        packaged     : path.normalize(answers.dist + '/'),
        uncompressed : path.normalize(answers.dist + '/components/'),
        compressed   : path.normalize(answers.dist + '/components/'),
        themes       : path.normalize(answers.dist + '/themes/')
      };
    }

    // add site path
    if(answers.site) {
      json.paths.source.site = path.normalize(answers.site + '/');
    }
    if(answers.packaged) {
      json.paths.output.packaged = path.normalize(answers.packaged + '/');
    }
    if(answers.compressed) {
      json.paths.output.compressed = path.normalize(answers.compressed + '/');
    }
    if(answers.uncompressed) {
      json.paths.output.uncompressed = path.normalize(answers.uncompressed + '/');
    }
    return json;
  },

  // files cleaned up after install
  setupFiles: [
    './src/theme.config.example',
    './semantic.json.example',
    './src/_site'
  ],

  regExp: {
    // used to match siteFolder variable in theme.less
    siteVariable: /@siteFolder .*\'(.*)/mg
  },

  // source paths (when installing)
  source: {
    config       : './semantic.json.example',
    definitions  : './src/definitions',
    gulpFile     : './gulpfile.js',
    lessImport   : './src/semantic.less',
    site         : './src/_site',
    tasks        : './tasks',
    themeConfig  : './src/theme.config.example',
    themeImport  : './src/theme.less',
    themes       : './src/themes',
    defaultTheme : './src/themes/default',
    userGulpFile : './tasks/config/npm/gulpfile.js'
  },

  // expected final filenames
  files: {
    config      : 'semantic.json',
    lessImport  : 'src/semantic.less',
    site        : 'src/site',
    themeConfig : 'src/theme.config',
    themeImport : 'src/theme.less'
  },

  // folder paths to files relative to root
  folders: {
    config       : './',
    definitions  : 'src/definitions/',
    lessImport   : 'src/',
    modules      : 'node_modules/',
    site         : 'src/site/',
    tasks        : 'tasks/',
    themeConfig  : 'src/',
    themeImport  : 'src/',
    themes       : 'src/themes/',

    defaultTheme : 'default/' // only path that is relative to another directory and not root
  },

  // questions asked during install
  questions: {

    root: [
      {
        type    : 'list',
        name    : 'useRoot',
        message :
          '{packageMessage} Is this your project folder? {root}',
        choices: [
          {
            name  : 'Yes',
            value : true
          },
          {
            name  : 'No, let me specify',
            value : false
          }
        ]
      },
      {
        type    : 'input',
        name    : 'customRoot',
        message : 'Please enter the absolute path to your project root',
        default : '/my/project/path',
        when    : when.changeRoot
      },
      {
        type    : 'input',
        name    : 'semanticRoot',
        message : 'Where should we put Semantic UI inside your project?',
        default : 'semantic/'
      }
    ],

    setup: [
      {
        type: 'list',
        name: 'overwrite',
        message: 'It looks like you have a semantic.json file already.',
        when: when.hasConfig,
        choices: [
          {
            name: 'Yes, extend my current settings.',
            value: 'yes'
          },
          {
            name: 'Skip install',
            value: 'no'
          }
        ]
      },
      {
        type: 'list',
        name: 'install',
        message: 'Set-up Semantic UI',
        when: when.allowOverwrite,
        choices: [
          {
            name: 'Automatic (Use default locations and all components)',
            value: 'auto'
          },
          {
            name: 'Express (Set components and output folder)',
            value: 'express'
          },
          {
            name: 'Custom (Customize all src/dist values)',
            value: 'custom'
          }
        ]
      },
      {
        type: 'checkbox',
        name: 'components',
        message: 'What components should we include in the package?',

        // duplicated manually from tasks/defaults.js with additional property
        choices: [
          { name: "reset", checked: true },
          { name: "site", checked: true },
          { name: "button", checked: true },
          { name: "container", checked: true },
          { name: "divider", checked: true },
          { name: "flag", checked: true },
          { name: "header", checked: true },
          { name: "icon", checked: true },
          { name: "image", checked: true },
          { name: "input", checked: true },
          { name: "label", checked: true },
          { name: "list", checked: true },
          { name: "loader", checked: true },
          { name: "rail", checked: true },
          { name: "reveal", checked: true },
          { name: "segment", checked: true },
          { name: "step", checked: true },
          { name: "breadcrumb", checked: true },
          { name: "form", checked: true },
          { name: "grid", checked: true },
          { name: "menu", checked: true },
          { name: "message", checked: true },
          { name: "table", checked: true },
          { name: "ad", checked: true },
          { name: "card", checked: true },
          { name: "comment", checked: true },
          { name: "feed", checked: true },
          { name: "item", checked: true },
          { name: "statistic", checked: true },
          { name: "accordion", checked: true },
          { name: "checkbox", checked: true },
          { name: "dimmer", checked: true },
          { name: "dropdown", checked: true },
          { name: "embed", checked: true },
          { name: "modal", checked: true },
          { name: "nag", checked: true },
          { name: "popup", checked: true },
          { name: "progress", checked: true },
          { name: "rating", checked: true },
          { name: "search", checked: true },
          { name: "shape", checked: true },
          { name: "sidebar", checked: true },
          { name: "sticky", checked: true },
          { name: "tab", checked: true },
          { name: "transition", checked: true },
          { name: "api", checked: true },
          { name: "form", checked: true },
          { name: "state", checked: true },
          { name: "visibility", checked: true }
        ],
        when: when.notAuto
      },
      {
        type: 'list',
        name: 'changePermissions',
        when: when.notAuto,
        message: 'Should we set permissions on outputted files?',
        choices: [
          {
            name: 'No',
            value: false
          },
          {
            name: 'Yes',
            value: true
          }
        ]
      },
      {
        type: 'input',
        name: 'permission',
        message: 'What octal file permission should outputted files receive?',
        default: defaults.permission,
        when: when.changePermissions
      },
      {
        type: 'list',
        name: 'rtl',
        message: 'Do you use a RTL (Right-To-Left) language?',
        when: when.notAuto,
        choices: [
          {
            name: 'No',
            value: false
          },
          {
            name: 'Yes',
            value: true
          },
          {
            name: 'Build Both',
            value: 'both'
          }
        ]
      },
      {
        type: 'input',
        name: 'dist',
        message: 'Where should we output Semantic UI?',
        default: defaults.paths.output.packaged,
        filter: filter.removeTrailingSlash,
        when: when.express
      },
      {
        type: 'input',
        name: 'site',
        message: 'Where should we put your site folder?',
        default: defaults.paths.source.site,
        filter: filter.removeTrailingSlash,
        when: when.custom
      },
      {
        type: 'input',
        name: 'packaged',
        message: 'Where should we output a packaged version?',
        default: defaults.paths.output.packaged,
        filter: filter.removeTrailingSlash,
        when: when.custom
      },
      {
        type: 'input',
        name: 'compressed',
        message: 'Where should we output compressed components?',
        default: defaults.paths.output.compressed,
        filter: filter.removeTrailingSlash,
        when: when.custom
      },
      {
        type: 'input',
        name: 'uncompressed',
        message: 'Where should we output uncompressed components?',
        default: defaults.paths.output.uncompressed,
        filter: filter.removeTrailingSlash,
        when: when.custom
      }
    ],


    cleanup: [
      {
        type: 'list',
        name: 'cleanup',
        message: 'Should we remove set-up files?',
        choices: [
          {
            name: 'Yes (re-install will require redownloading semantic).',
            value: 'yes'
          },
          {
            name: 'No Thanks',
            value: 'no'
          }
        ]
      },
      {
        type: 'list',
        name: 'build',
        message: 'Do you want to build Semantic now?',
        choices: [
          {
            name: 'Yes',
            value: 'yes'
          },
          {
            name: 'No',
            value: 'no'
          }
        ]
      },
    ],
    site: [
      {
        type: 'list',
        name: 'customize',
        message: 'You have not yet customized your site, can we help you do that?',
        choices: [
          {
            name: 'Yes, ask me a few questions',
            value: true
          },
          {
            name: 'No I\'ll do it myself',
            value: false
          }
        ]
      },
      {
        type: 'list',
        name: 'headerFont',
        message: 'Select your header font',
        choices: [
          {
            name: 'Helvetica Neue, Arial, sans-serif',
            value: 'Helvetica Neue, Arial, sans-serif;'
          },
          {
            name: 'Lato (Google Fonts)',
            value: 'Lato'
          },
          {
            name: 'Open Sans (Google Fonts)',
            value: 'Open Sans'
          },
          {
            name: 'Source Sans Pro (Google Fonts)',
            value: 'Source Sans Pro'
          },
          {
            name: 'Droid (Google Fonts)',
            value: 'Droid'
          },
          {
            name: 'I\'ll choose on my own',
            value: false
          }
        ],
        when: when.customize
      },
      {
        type: 'list',
        name: 'pageFont',
        message: 'Select your page font',
        choices: [
          {
            name: 'Helvetica Neue, Arial, sans-serif',
            value: 'Helvetica Neue, Arial, sans-serif;'
          },
          {
            name: 'Lato (Import from Google Fonts)',
            value: 'Lato'
          },
          {
            name: 'Open Sans (Import from Google Fonts)',
            value: 'Open Sans'
          },
          {
            name: 'Source Sans Pro (Import from Google Fonts)',
            value: 'Source Sans Pro'
          },
          {
            name: 'Droid (Google Fonts)',
            value: 'Droid'
          },
          {
            name: 'I\'ll choose on my own',
            value: false
          }
        ],
        when: when.customize
      },
      {
        type: 'list',
        name: 'fontSize',
        message: 'Select your base font size',
        default: '14px',
        choices: [
          {
            name: '12px',
          },
          {
            name: '13px',
          },
          {
            name: '14px (Recommended)',
            value: '14px'
          },
          {
            name: '15px',
          },
          {
            name: '16px',
          },
          {
            name: 'I\'ll choose on my own',
            value: false
          }
        ],
        when: when.customize
      },
      {
        type: 'list',
        name: 'primaryColor',
        message: 'Select the closest name for your primary brand color',
        default: '14px',
        choices: [
          {
            name: 'Blue'
          },
          {
            name: 'Green'
          },
          {
            name: 'Orange'
          },
          {
            name: 'Pink'
          },
          {
            name: 'Purple'
          },
          {
            name: 'Red'
          },
          {
            name: 'Teal'
          },
          {
            name: 'Yellow'
          },
          {
            name: 'Black'
          },
          {
            name: 'I\'ll choose on my own',
            value: false
          }
        ],
        when: when.customize
      },
      {
        type: 'input',
        name: 'PrimaryHex',
        message: 'Enter a hexcode for your primary brand color',
        when: when.primaryColor
      },
      {
        type: 'list',
        name: 'secondaryColor',
        message: 'Select the closest name for your secondary brand color',
        default: '14px',
        choices: [
          {
            name: 'Blue'
          },
          {
            name: 'Green'
          },
          {
            name: 'Orange'
          },
          {
            name: 'Pink'
          },
          {
            name: 'Purple'
          },
          {
            name: 'Red'
          },
          {
            name: 'Teal'
          },
          {
            name: 'Yellow'
          },
          {
            name: 'Black'
          },
          {
            name: 'I\'ll choose on my own',
            value: false
          }
        ],
        when: when.customize
      },
      {
        type: 'input',
        name: 'secondaryHex',
        message: 'Enter a hexcode for your secondary brand color',
        when: when.secondaryColor
      }
    ]

  },

  settings: {

    /* Rename Files */
    rename: {
      json : { extname : '.json' }
    },

    /* Copy Install Folders */
    wrench: {

      // overwrite existing files update & install (default theme / definition)
      overwrite: {
        forceDelete       : true,
        excludeHiddenUnix : true,
        preserveFiles     : false
      },

      // only create files that don't exist (site theme update)
      merge: {
        forceDelete       : false,
        excludeHiddenUnix : true,
        preserveFiles     : true
      }

    }
  }
};