# all files / src/models/ blueprint.js

50% Statements 76/152
48.15% Branches 26/54
69.05% Functions 29/42
38.39% Lines 43/112
1 statement, 16 functions, 4 branches Ignored

    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
    2961×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×

    1×
    2×
    2×
    2×

    1×

    3×
    3×
    3×

    3×

    2×

    2×

    2×
    1×

    1×

    2×

    3×

    2×
    2×

    2×
    2×

    2×
    2×

    2×
    1×

    1×
    1×

    1×
    1×

    1×
    1×
    1×
    1×

    1×

     import path from 'path';
    import _ from 'lodash';
    import walkSync from 'walk-sync';
    import fs from 'fs';

    import { fileExists } from '../util/fs';
    import mixin from '../util/mixin';
    import { normalizeCasing } from '../util/text-helper';
    import FileInfo from './file-info';
    import config from '../config';

    const { basePath } = config;

    function generateLookupPaths(lookupPaths) {
      lookupPaths = lookupPaths || [];
      lookupPaths = lookupPaths.concat(Blueprint.defaultLookupPaths());
      return _.uniq(lookupPaths);
    }

    function dir(fullPath) {
      if (fileExists(fullPath)) {
        return fs.readdirSync(fullPath).map(function(fileName) {
          return path.join(fullPath, fileName);
        });
      } else {
        return [];
      }
    }

    export default class Blueprint {
      constructor(blueprintPath) {
        this.path = blueprintPath;
        this.name = path.basename(blueprintPath);
      }

      // HOOK: that can be overridden.  Defaults to look in <blueprint-name>/files.
      filesPath() {
        return path.join(this.path, 'files');
      }

      files() {
        Iif (this._files) { return this._files; }

        let filesPath = this.filesPath();

        if (fileExists(filesPath)) {
          this._files = walkSync(filesPath);
        } else {
          this._files = [];
        }
        return this._files;
      }

      static defaultLookupPaths() {
        return [
          path.resolve(path.join(basePath, 'blueprints')),
          path.resolve(__dirname, '..', '..', 'blueprints')
        ];
      }
      // find blueprint given a path or return error
      // look inside current project first and then redux-cli defaults
      static lookup(name, options = {}) {
        const lookupPaths = generateLookupPaths(options.paths);

        let lookupPath;
        let blueprintPath;

        for (let i = 0; (lookupPath = lookupPaths[i]); i++) {
          blueprintPath = path.resolve(lookupPath, name);

          if (fileExists(blueprintPath)) {
            return Blueprint.load(blueprintPath);
          }
        }

        Eif (!options.ignoreMissing) {
          throw new Error('Unknown blueprint: ' + name);
        }
      }

      // load in the blueprint that was found, extend this class to load it
      static load(blueprintPath) {
        let Constructor;
        const constructorPath = path.resolve(blueprintPath, 'index.js');

        Eif (fs.lstatSync(blueprintPath).isDirectory()) {
          Eif (fileExists(constructorPath)) {
            const blueprintModule = require(constructorPath);
            Constructor = mixin(Blueprint, blueprintModule);

            return new Constructor(blueprintPath);
          }
        }
      }

      static list(options = {}) {
        return generateLookupPaths(options.paths).map(lookupPath => {
          const blueprintFiles = dir(lookupPath);
          const packagePath = path.join(lookupPath, '../package.json');
          let source;

          if (fileExists(packagePath)) {
            source = require(packagePath).name;
          } else {
            source = path.basename(path.join(lookupPath, '..'));
          }

          const blueprints = blueprintFiles.map(filePath => {
            const blueprint = this.load(filePath);

            if (blueprint) {
              let description;
              const name = blueprint.name;

              if (blueprint.description) {
                description = blueprint.description();
              } else {
                description = 'N/A';
              }

              return {
                name,
                description
              };
            }
          });

          return {
            source,
            blueprints: _.compact(blueprints)
          };
        });
      }

      _fileMapTokens(options) {
        const standardTokens = {
          __name__: (options) => {
            const name = options.entity.name;
            const casing = options.settings.getSetting('fileCasing') || 'default';
            return normalizeCasing(name, casing);
          },
          __path__: (options) => {
            return options.originalBlueprintName;
          },
          __root__: (options) => {
            return options.settings.getSetting('sourceBase');
          },
          __test__: (options) => {
            return options.settings.getSetting('testBase');
          }
        };

        // HOOK: calling into the blueprints fileMapTokens() hook, passing along
        // an options hash coming from _generateFileMapVariables()
        const customTokens = this.fileMapTokens(options) || {};
        return Object.assign({}, standardTokens, customTokens);
      }

      generateFileMap(fileMapOptions) {
        const tokens = this._fileMapTokens(fileMapOptions);
        const fileMapValues = _.values(tokens);
        const tokenValues = fileMapValues.map(token => token(fileMapOptions));
        const tokenKeys = _.keys(tokens);
        return _.zipObject(tokenKeys, tokenValues);
      }

      // Set up options to be passed to fileMapTokens that get generated.
      _generateFileMapVariables(entityName, locals, options) {
        const originalBlueprintName = options.originalBlueprintName || this.name;
        const { settings, entity } = options;

        return {
          originalBlueprintName,
          settings,
          entity,
          locals
        };
      }

      // Given a file and a fileMap from locals, convert path names
      // to be correct string
      mapFile(file, locals) {
        let pattern, i;
        const fileMap = locals.fileMap || { __name__: locals.camelEntityName };
        for (i in fileMap) {
          pattern = new RegExp(i, 'g');
          file = file.replace(pattern, fileMap[i]);
        }
        return file;
      }

      _locals(options) {
        const entityName = options.entity && options.entity.name;
        const customLocals = this.locals(options);
        const fileMapVariables = this._generateFileMapVariables(entityName, customLocals, options);
        const fileMap = this.generateFileMap(fileMapVariables);

        const standardLocals = {
          pascalEntityName: normalizeCasing(entityName, 'pascal'),
          camelEntityName:  normalizeCasing(entityName, 'camel'),
          snakeEntityName: normalizeCasing(entityName, 'snake'),
          dashesEntityName: normalizeCasing(entityName, 'dashes'),
          fileMap
        };

        return Object.assign({}, standardLocals, customLocals);
      }

      _process(options, beforeHook, process, afterHook) {
        const locals = this._locals(options);
        return Promise.resolve()
          .then(beforeHook.bind(this, options, locals))
          .then(process.bind(this, locals))
          .then(afterHook.bind(this, options));
      }

      processFiles(locals) {
        const files = this.files();
        const fileInfos = files.map(file => this.buildFileInfo(locals, file));
        this.ui.writeDebug(`built file infos: ${fileInfos.length}`);
        const filesToWrite = fileInfos.filter(info => info.isFile());
        this.ui.writeDebug(`files to write: ${filesToWrite.length}`);
        filesToWrite.map(file => file.writeFile());
      }

      buildFileInfo(locals, file) {
        const mappedPath = this.mapFile(file, locals);
        this.ui.writeDebug(`mapped path: ${mappedPath}`);

        return new FileInfo({
          ui: this.ui,
          templateVariables: locals,
          originalPath: this.srcPath(file),
          mappedPath: this.destPath(mappedPath),
          outputPath: this.destPath(file)
        });
      }

      // where the files will be written to
      destPath(mappedPath) {
        return path.resolve(basePath, mappedPath);
      }

      // location of the string templates
      srcPath(file) {
        return path.resolve(this.filesPath(), file);
      }

      /*
       * install options:

        const blueprintOptions = {
          originalBlueprintName: name,
          ui: this.ui,
          settings: this.settings,
          entity: {
            name: cliArgs.entity.name,
            options cliArgs.entity.options
          }
        };
      */
      install(options) {
        const ui = this.ui = options.ui;

        ui.writeInfo('installing blueprint...');
        return this._process(
          options,
          this.beforeInstall,
          this.processFiles,
          this.afterInstall
        ).then(() => {
          ui.writeInfo('finished installing blueprint.');
        });
      }

      // uninstall() {
      // }

      // HOOKS:
      locals() {}
      fileMapTokens() {}
      beforeInstall() {}
      afterInstall() {}

      // TODO: add uninstall hooks once support for uninstall exists
      // beforeUninstall() {}
      // afterUninstall() {}

      // HOOK: for normalizing entity name that gets passed in as an arg
      // via the CLI
      // normalizeEntityName(options) {
        // return normalizeEntityName(name);
      // }
    }