common/corpus-common-addon/lib/commands/dl-fixtures.js
author ymh <ymh.work@gmail.com>
Tue, 11 Oct 2016 02:49:59 +0200
changeset 326 226d5b17a119
parent 305 ff6cf3fc5f40
child 454 710a2ae08a74
permissions -rw-r--r--
- First implementation of filter for languages. - Language is now an array in the document - various corrections linked to the above change - Simplify the IndexDocumet loop
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
     1
'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
     2
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
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
     4
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
     5
var Q       = require('q');
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
     6
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
     7
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
     8
var fs      = require('fs');
166
ce580300e4b9 add in fixtures new documents with transcripts
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
     9
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
    10
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
    11
// 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
    12
// `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
    13
// `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
    14
// 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
    15
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
    16
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
    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
    18
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
    19
    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
    20
        // 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
    21
        // 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
    22
        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
    23
        // 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
    24
        // 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
    25
        // 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
    26
        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
    27
    }
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
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
    // 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
    30
    // 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
    31
    // 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
    32
    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
    33
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
    // 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
    35
    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
    36
}
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
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
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
    39
    name: 'dl-fixtures',
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    40
    description: 'Download fixtures',
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    41
    works: 'everywhere',
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    42
    availableOptions: [
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    43
        { 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
    44
        { 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
    45
        { 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
    46
        { 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
    47
        { 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
    48
        { 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
    49
    ],
161
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    50
    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
    51
        var done = Q.defer();
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    52
        var that = this;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    53
        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
    54
            var objectList = _.reduce(
161
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    55
                _.keys(body[rootKey]),
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    56
                function(res, objKey) {
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    57
                    var objValue = body[rootKey][objKey];
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    58
                    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
    59
                    return res;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    60
                },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    61
                []
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
            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
    64
            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
    65
                if(err) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    66
                    return done.reject(err);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    67
                }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    68
                done.resolve();
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
        });
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    71
        return done.promise;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    72
    },
161
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    73
    dl_discourses: function(commandOptions) {
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    74
        return this.dl_countmap(commandOptions, 'discourses');
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    75
    },
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    76
    dl_themes: function(commandOptions) {
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    77
        return this.dl_countmap(commandOptions, 'themes');
5f011170de74 correct download of discourses fxtures
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    78
    },
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
    79
    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
    80
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 305
diff changeset
    81
        var nextPageUrl = commandOptions.url+"?sort=_graph";
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    82
        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
    83
        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
    84
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    85
        return promiseWhile(
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    86
            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
    87
            function() {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    88
                var deferred = Q.defer();
166
ce580300e4b9 add in fixtures new documents with transcripts
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    89
                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
    90
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    91
                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
    92
                    if (err) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    93
                        return deferred.reject(err);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    94
                    } 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
    95
                        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
    96
                        err.res = res;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    97
                        return deferred.reject(err);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    98
                    }
326
226d5b17a119 - First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents: 305
diff changeset
    99
                    nextPageUrl = body.meta.next_page_url;
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   100
                    pageIndex++;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   101
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   102
                    ids = _.reduce(
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   103
                        body.documents,
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   104
                        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
   105
                            res.push(doc);
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   106
                            return res;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   107
                        },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   108
                        ids
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
                    deferred.resolve(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
                return deferred.promise;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   113
            }
164
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   114
        );
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   115
    },
166
ce580300e4b9 add in fixtures new documents with transcripts
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   116
    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
   117
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
   118
        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
   119
            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
   120
                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
   121
                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
   122
                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
   123
                    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
   124
                    if (err) {
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   125
                        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
   126
                    } 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
   127
                        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
   128
                    } 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
   129
                        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
   130
                        err.res = res;
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   131
                        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
   132
                    } else {
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   133
                        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
   134
                    }
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   135
                });
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   136
                return deferred.promise;
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
        }).then(function(res) {
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   139
            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
   140
            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
   141
            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
   142
                if(err) {
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   143
                    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
   144
                }
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   145
                deferred.resolve();
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
            return deferred.promise;
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   148
        }.bind(this));
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   149
5f1e1cc17e8a add mirage fixtures and api path for testing in app client
ymh <ymh.work@gmail.com>
parents: 161
diff changeset
   150
    },
166
ce580300e4b9 add in fixtures new documents with transcripts
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   151
    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
   152
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
   153
        var destFiles = {
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   154
            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
   155
        };
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
   156
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
        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
   158
            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
   159
                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
   160
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
   161
                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
   162
                    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
   163
                    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
   164
                        {
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
                            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
   166
                            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
   167
                            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
   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
   169
                        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
   170
                            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
   171
                            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
   172
                                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
   173
                            } 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
   174
                                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
   175
                                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
   176
                                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
   177
                            }
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
                            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
   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
                    );
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
                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
   183
                    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
   184
                }
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
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
                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
   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
        }).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
   189
            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
   190
            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
   191
                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
   192
                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
   193
                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
   194
                    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
   195
                    if (err) {
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   196
                        return deferred.reject(err);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   197
                    } 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
   198
                        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
   199
                        err.res = res;
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   200
                        return deferred.reject(err);
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   201
                    }
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   202
                    deferred.resolve(body.document);
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
                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
   205
            })).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
   206
                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
   207
                    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
   208
                },
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
                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
   210
                    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
   211
                }
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
            );
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
            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
   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
        }).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
   217
            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
   218
            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
   219
                var deferred = Q.defer();
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   220
                if(key in destFiles) {
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   221
                    ui.writeLine(chalk.green('Writing ' + key + ' in file ' + destFiles[key]));
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   222
                    var prefix = (format==='es6')?'export default ':'module.exports = ';
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   223
                    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
   224
                        if(err) {
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   225
                            return deferred.reject(err);
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   226
                        }
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   227
                        deferred.resolve();
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
                } 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
   230
                    deferred.resolve();
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   231
                }
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
                return deferred.promise;
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   233
            }));
159
dd4ee1d4122c upgrade ember
ymh <ymh.work@gmail.com>
parents: 158
diff changeset
   234
        }.bind(this));
158
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   235
    },
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   236
    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
   237
        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
   238
        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
   239
        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
   240
366509ae2f37 Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   241
166
ce580300e4b9 add in fixtures new documents with transcripts
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   242
        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
   243
    }
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
   244
});