common/corpus-common-addon/lib/commands/dl-fixtures.js
author Chloe Laisne <chloe.laisne@gmail.com>
Wed, 29 Jun 2016 00:35:03 +0200
changeset 212 f2c6080a73aa
parent 173 cf7b221238fd
child 305 ff6cf3fc5f40
permissions -rw-r--r--
Player backward/forward design logic
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
     1
/*eslint-env node */
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
     2
/*global require:true*/
130
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
'use strict';
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
var Command = require('ember-cli/lib/models/command');
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
var path    = require('path');
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
     7
var Q       = require('q');
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
     8
var request = require('request');
130
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
var _       = require('lodash');
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
var fs      = require('fs');
166
ce580300e4b9 add in fixtures new documents with transcripts
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    11
var chalk   = require('chalk');
130
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
// taken from http://stackoverflow.com/a/17238793
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
// `condition` is a function that returns a boolean
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
// `body` is a function that returns a promise
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
// returns a promise for the completion of the loop
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
function promiseWhile(condition, body) {
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    var done = Q.defer();
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
    21
    function loop(res) {
130
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
        // When the result of calling `condition` is no longer true, we are
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        // done.
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
    24
        if (!condition()) return done.resolve(res);
130
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        // Use `when`, in case `body` does not return a promise.
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
        // When it completes loop again otherwise, if it fails, reject the
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
        // done promise
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        Q.when(body(), loop, done.reject);
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    }
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    // Start running the loop in the next tick so that this function is
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    // completely async. It would be unexpected if `body` was called
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    // synchronously the first time.
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    Q.nextTick(loop);
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    // The promise
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    return done.promise;
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
}
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
module.exports = Command.extend({
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    41
    name: 'dl-fixtures',
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    42
    description: 'Download fixtures',
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    43
    works: 'everywhere',
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    44
    availableOptions: [
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    45
        { name: 'type', type: String, default: 'documents', aliases: ['t'], description: 'type of obejcts to downloads' },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    46
        { name: 'url',  type: String, aliases: ['u'], description: 'Source url' },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    47
        { name: 'dest', type: String, aliases: ['d'], description: 'File destination'  },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    48
        { name: 'page', type: Number, default: 1 , aliases: ['p'], description: 'number of page to download'},
165
3c36874da933 add new documents and transcript for tests
ymh <ymh.work@gmail.com>
parents: 164
diff changeset
    49
        { name: 'format', type: String, default: 'es6' , aliases: ['f'], description: 'Format for module export, es6 or require'},
3c36874da933 add new documents and transcript for tests
ymh <ymh.work@gmail.com>
parents: 164
diff changeset
    50
        { name: 'extra', type: String, default: '', aliases: ['e'], description: 'additionnal data to download (comma separated list of document ids)'}
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    51
    ],
161
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    52
    dl_countmap: function(commandOptions, rootKey) {
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    53
        var done = Q.defer();
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    54
        var that = this;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    55
        request.get({url: commandOptions.url, json: true}, function (err, res, body) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    56
            var objectList = _.reduce(
161
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    57
                _.keys(body[rootKey]),
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    58
                function(res, objKey) {
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    59
                    var objValue = body[rootKey][objKey];
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    60
                    res.push({id: objKey, count: objValue.count, label: objValue.label});
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    61
                    return res;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    62
                },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    63
                []
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    64
            );
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    65
            var prefix = (that.format==='es6')?'export default ':'module.exports = ';
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    66
            fs.writeFile(that.dest, prefix + JSON.stringify(objectList,null,2) + ';', function(err) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    67
                if(err) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    68
                    return done.reject(err);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    69
                }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    70
                done.resolve();
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    71
            });
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    72
        });
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    73
        return done.promise;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    74
    },
161
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    75
    dl_discourses: function(commandOptions) {
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    76
        return this.dl_countmap(commandOptions, 'discourses');
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    77
    },
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    78
    dl_themes: function(commandOptions) {
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    79
        return this.dl_countmap(commandOptions, 'themes');
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    80
    },
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
    81
    dl_documents_ids: function(commandOptions, rawArgs, ui) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
    82
164
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
    83
        var nextPageUrl = commandOptions.url;
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    84
        var pageIndex = 1;
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
    85
        var ids = commandOptions.extra?_.map(commandOptions.extra.split(','), function(id) { return { id: id};}):[];
165
3c36874da933 add new documents and transcript for tests
ymh <ymh.work@gmail.com>
parents: 164
diff changeset
    86
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    87
        return promiseWhile(
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    88
            function() { return pageIndex <= commandOptions.page && nextPageUrl; },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    89
            function() {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    90
                var deferred = Q.defer();
166
ce580300e4b9 add in fixtures new documents with transcripts
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    91
                ui.writeLine(chalk.yellow('Download Documents : getting page ' + pageIndex));
ce580300e4b9 add in fixtures new documents with transcripts
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    92
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    93
                request.get({url: nextPageUrl, json: true}, function (err, res, body) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    94
                    if (err) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    95
                        return deferred.reject(err);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    96
                    } else if (res.statusCode !== 200) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    97
                        err = new Error('Unexpected status code: ' + res.statusCode);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    98
                        err.res = res;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    99
                        return deferred.reject(err);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   100
                    }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   101
                    nextPageUrl = body.next_page_url;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   102
                    pageIndex++;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   103
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   104
                    ids = _.reduce(
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   105
                        body.documents,
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   106
                        function(res, doc) {
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   107
                            res.push(doc);
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   108
                            return res;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   109
                        },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   110
                        ids
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   111
                    );
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   112
                    deferred.resolve(ids);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   113
                });
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   114
                return deferred.promise;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   115
            }
164
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   116
        );
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   117
    },
166
ce580300e4b9 add in fixtures new documents with transcripts
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   118
    dl_transcripts: function(commandOptions, rawArgs, ui) {  // eslint-disable-line no-unused-vars
164
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   119
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   120
        return this.dl_documents_ids(commandOptions, rawArgs, ui).then(function(docs) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   121
            return Q.all(_.map(docs, function(doc) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   122
                var id = doc.id;
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   123
                var deferred = Q.defer();
164
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   124
                request.get({url: commandOptions.url + encodeURIComponent(encodeURIComponent(id)) + '/transcript', json: true}, function (err, res, body) {
166
ce580300e4b9 add in fixtures new documents with transcripts
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   125
                    ui.writeLine(chalk.green('Download transcripts : getting transcript ' + id));
164
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   126
                    if (err) {
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   127
                        return deferred.reject(err);
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   128
                    } else if (res.statusCode === 404) {
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   129
                        deferred.resolve({'id': id, 'transcript': undefined});
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   130
                    } else if (res.statusCode !== 200) {
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   131
                        err = new Error('Unexpected status code: ' + res.statusCode);
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   132
                        err.res = res;
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   133
                        return deferred.reject(err);
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   134
                    } else {
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   135
                        deferred.resolve({'id': id, 'transcript': body});
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   136
                    }
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   137
                });
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   138
                return deferred.promise;
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   139
            }));
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   140
        }).then(function(res) {
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   141
            var deferred = Q.defer();
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   142
            var prefix = (this.format==='es6')?'export default ':'module.exports = ';
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   143
            fs.writeFile(this.dest, prefix + JSON.stringify(res,null,2) + ';', function(err) {
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   144
                if(err) {
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   145
                    return deferred.reject(err);
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   146
                }
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   147
                deferred.resolve();
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   148
            });
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   149
            return deferred.promise;
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   150
        }.bind(this));
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   151
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   152
    },
166
ce580300e4b9 add in fixtures new documents with transcripts
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   153
    dl_documents: function(commandOptions, rawArgs, ui) { // eslint-disable-line no-unused-vars
164
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   154
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   155
        var destFiles = {
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   156
            docs: this.dest
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   157
        };
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   158
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   159
        return this.dl_documents_ids(commandOptions, rawArgs, ui).then(function(docs) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   160
            return Q.all(_.map(docs, function(doc) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   161
                var deferred = Q.defer();
164
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   162
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   163
                if(Object.keys(doc).length === 1) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   164
                    var id = doc.id;
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   165
                    request.get(
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   166
                        {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   167
                            url: commandOptions.url + encodeURIComponent(encodeURIComponent(id)),
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   168
                            qs: {short: true},
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   169
                            json: true
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   170
                        },
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   171
                        function (err, res, body) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   172
                            ui.writeLine(chalk.green('Download documents : completing doc ' + id));
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   173
                            if (err) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   174
                                return deferred.reject(err);
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   175
                            } else if (res.statusCode !== 200) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   176
                                err = new Error('Unexpected status code: ' + res.statusCode);
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   177
                                err.res = res;
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   178
                                return deferred.reject(err);
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   179
                            }
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   180
                            deferred.resolve(body.document);
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   181
                        }
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   182
                    );
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   183
                }
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   184
                else {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   185
                    deferred.resolve(doc);
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   186
                }
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   187
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   188
                return deferred.promise;
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   189
            }));
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   190
        }).then(function(docs) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   191
            var docsdeferred = Q.defer();
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   192
            Q.all(_.map(docs, function(docu) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   193
                var id = docu.id;
164
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   194
                var deferred = Q.defer();
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   195
                request.get({url: commandOptions.url + encodeURIComponent(encodeURIComponent(id)), json: true}, function (err, res, body) {
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   196
                    ui.writeLine(chalk.green('Download documents : getting doc ' + id));
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   197
                    if (err) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   198
                        return deferred.reject(err);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   199
                    } else if (res.statusCode !== 200) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   200
                        err = new Error('Unexpected status code: ' + res.statusCode);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   201
                        err.res = res;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   202
                        return deferred.reject(err);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   203
                    }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   204
                    deferred.resolve(body.document);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   205
                });
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   206
                return deferred.promise;
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   207
            })).then(
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   208
                function(downloadedDocs) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   209
                    docsdeferred.resolve({list: docs, docs: downloadedDocs});
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   210
                },
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   211
                function(err) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   212
                    return docsdeferred.reject(err);
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   213
                }
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   214
            );
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   215
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   216
            return docsdeferred.promise;
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   217
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   218
        }).then(function(resList) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   219
            var format = this.format;
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   220
            return Q.all(_.map(resList, function(res, key) {
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   221
                var deferred = Q.defer();
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   222
                if(key in destFiles) {
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   223
                    ui.writeLine(chalk.green('Writing ' + key + ' in file ' + destFiles[key]));
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   224
                    var prefix = (format==='es6')?'export default ':'module.exports = ';
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   225
                    fs.writeFile(destFiles[key], prefix + JSON.stringify(res,null,2) + ';', function(err) {
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   226
                        if(err) {
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   227
                            return deferred.reject(err);
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   228
                        }
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   229
                        deferred.resolve();
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   230
                    });
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   231
                } else {
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   232
                    deferred.resolve();
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   233
                }
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
   234
                return deferred.promise;
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   235
            }));
159
dd4ee1d4122c upgrade ember
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   236
        }.bind(this));
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   237
    },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   238
    run: function(commandOptions, rawArgs) { // eslint-disable-line no-unused-vars
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   239
        var type = commandOptions.type || 'documents';
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   240
        this.dest = commandOptions.dest || '.' + path.sep + type + '.js';
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   241
        this.format = commandOptions.format || 'es6';
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   242
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   243
166
ce580300e4b9 add in fixtures new documents with transcripts
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   244
        return this['dl_' + type](commandOptions, rawArgs, this.ui);
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   245
    }
130
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
});