From 6fcd19ba737f1f5614a56c6925adb882dea43b8d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 5 Jul 2017 13:26:25 +0200 Subject: Move to promises Closes https://github.com/Chocobozzz/PeerTube/issues/74 --- scripts/danger/clean/cleaner.ts | 27 ++++++++++++++------------ scripts/reset-password.ts | 26 +++++++++---------------- scripts/test.sh | 4 ++-- scripts/update-host.ts | 43 ++++++++++++++++++++--------------------- 4 files changed, 47 insertions(+), 53 deletions(-) (limited to 'scripts') diff --git a/scripts/danger/clean/cleaner.ts b/scripts/danger/clean/cleaner.ts index d1dba4650..d80b8d323 100644 --- a/scripts/danger/clean/cleaner.ts +++ b/scripts/danger/clean/cleaner.ts @@ -1,25 +1,28 @@ -import * as eachSeries from 'async/eachSeries' import * as rimraf from 'rimraf' +import * as Promise from 'bluebird' import { CONFIG } from '../../../server/initializers/constants' import { database as db } from '../../../server/initializers/database' -db.init(true, function () { - db.sequelize.drop().asCallback(function (err) { - if (err) throw err - +db.init(true) + .then(() => { + return db.sequelize.drop() + }) + .then(() => { console.info('Tables of %s deleted.', CONFIG.DATABASE.DBNAME) const STORAGE = CONFIG.STORAGE - eachSeries(Object.keys(STORAGE), function (storage, callbackEach) { + Promise.mapSeries(Object.keys(STORAGE), storage => { const storageDir = STORAGE[storage] - rimraf(storageDir, function (err) { - console.info('%s deleted.', storageDir) - return callbackEach(err) + return new Promise((res, rej) => { + rimraf(storageDir, function (err) { + if (err) return rej(err) + + console.info('%s deleted.', storageDir) + return res() + }) }) - }, function () { - process.exit(0) }) + .then(() => process.exit(0)) }) -}) diff --git a/scripts/reset-password.ts b/scripts/reset-password.ts index 50e11c69c..09f27bfa4 100755 --- a/scripts/reset-password.ts +++ b/scripts/reset-password.ts @@ -11,13 +11,11 @@ if (program.user === undefined) { process.exit(-1) } -db.init(true, function () { - db.User.loadByUsername(program.user, function (err, user) { - if (err) { - console.error(err) - return - } - +db.init(true) + .then(() => { + return db.User.loadByUsername(program.user) + }) + .then(user => { if (!user) { console.error('User unknown.') return @@ -40,15 +38,9 @@ db.init(true, function () { rl.on('line', function (password) { user.password = password - user.save().asCallback(function (err) { - if (err) { - console.error(err) - } else { - console.log('User password updated.') - } - - process.exit(0) - }) + user.save() + .then(() => console.log('User password updated.')) + .catch(err => console.error(err)) + .finally(() => process.exit(0)) }) }) -}) diff --git a/scripts/test.sh b/scripts/test.sh index 6e8e38659..6c6312d52 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env sh +#!/bin/bash npm run build:server @@ -6,5 +6,5 @@ cd client || exit -1 npm test || exit -1 cd .. || exit -1 -npm run tslint -- --type-check --project ./tsconfig.json -c ./tslint.json server.ts server/**/*.ts || exit -1 +npm run tslint -- --type-check --project ./tsconfig.json -c ./tslint.json server.ts "server/**/*.ts" || exit -1 mocha --bail server/tests diff --git a/scripts/update-host.ts b/scripts/update-host.ts index 0f6af0942..23e8d5ef3 100755 --- a/scripts/update-host.ts +++ b/scripts/update-host.ts @@ -5,33 +5,32 @@ import { CONFIG, STATIC_PATHS } from '../server/initializers/constants' import { database as db } from '../server/initializers/database' import { hasFriends } from '../server/lib/friends' -db.init(true, function () { - hasFriends(function (err, itHasFriends) { - if (err) throw err - +db.init(true) + .then(() => { + return hasFriends() + }) + .then(itHasFriends => { if (itHasFriends === true) { console.log('Cannot update host because you have friends!') process.exit(-1) } console.log('Updating torrent files.') - db.Video.list(function (err, videos) { - if (err) throw err - - videos.forEach(function (video) { - const torrentName = video.id + '.torrent' - const torrentPath = CONFIG.STORAGE.TORRENTS_DIR + torrentName - const filename = video.id + video.extname - - const parsed = parseTorrent(readFileSync(torrentPath)) - parsed.announce = [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOST + '/tracker/socket' ] - parsed.urlList = [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + filename ] - - const buf = parseTorrent.toTorrentFile(parsed) - writeFileSync(torrentPath, buf) - }) - - process.exit(0) + return db.Video.list() + }) + .then(videos => { + videos.forEach(function (video) { + const torrentName = video.id + '.torrent' + const torrentPath = CONFIG.STORAGE.TORRENTS_DIR + torrentName + const filename = video.id + video.extname + + const parsed = parseTorrent(readFileSync(torrentPath)) + parsed.announce = [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOST + '/tracker/socket' ] + parsed.urlList = [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + filename ] + + const buf = parseTorrent.toTorrentFile(parsed) + writeFileSync(torrentPath, buf) }) + + process.exit(0) }) -}) -- cgit v1.2.3