early/ts/index.ts

56 lines
1.7 KiB
TypeScript
Raw Normal View History

2016-09-15 12:45:58 +00:00
import 'typings-global'
import path = require('path')
import chalk = require('chalk')
import q = require('q')
import childProcess = require('child_process')
let earlyChild
2016-06-10 03:18:03 +00:00
2016-09-15 12:45:58 +00:00
let doAnimation: boolean = true
let doText: boolean = false
let moduleName: string = 'undefined module name'
let startTime
2016-08-20 05:03:49 +00:00
2016-09-15 12:45:58 +00:00
if (process.argv.indexOf('-v') !== -1 || process.env.CI) {
doAnimation = false
2016-08-20 05:51:04 +00:00
}
2016-09-15 12:45:58 +00:00
if (process.argv.indexOf('-v') === -1 && process.env.CI) {
doText = true
2016-06-10 03:18:03 +00:00
}
2016-05-21 18:33:13 +00:00
// exports
2016-09-15 12:45:58 +00:00
export let start = function (moduleNameArg: string = '', loaderLengthArg: string = '10') {
2016-08-20 05:29:27 +00:00
2016-09-15 12:45:58 +00:00
startTime = Date.now()
2016-08-20 05:03:49 +00:00
2016-09-15 12:45:58 +00:00
moduleName = moduleNameArg
2016-08-20 05:03:49 +00:00
if (doAnimation) {
2016-09-15 12:45:58 +00:00
earlyChild = childProcess.fork(path.join(__dirname, 'early.child.js'), [], {
2016-06-10 03:18:03 +00:00
env: {
2016-08-20 05:03:49 +00:00
moduleNameArg: moduleNameArg,
loaderLengthArg: loaderLengthArg,
2016-06-10 03:18:03 +00:00
CI: process.env.CI
}
2016-09-15 12:45:58 +00:00
})
2016-06-10 03:36:06 +00:00
} else if (doText) {
2016-09-15 12:45:58 +00:00
console.log(`**** starting ${chalk.green(moduleNameArg)} ****`)
2016-06-10 03:18:03 +00:00
}
2016-09-15 12:45:58 +00:00
}
2016-05-20 17:06:25 +00:00
export let stop = (): q.Promise<number> => {
let done = q.defer<number>()
2016-09-15 12:45:58 +00:00
let endTime = Date.now()
let earlyExecutionTime: number = (endTime - startTime)
let earlyExecutionTimeString: string = (earlyExecutionTime / 1000).toString()
2016-08-20 05:03:49 +00:00
if (doAnimation) {
2016-09-15 12:45:58 +00:00
earlyChild.kill('SIGINT')
earlyChild.on('close', function () {
console.log(`loaded ${chalk.green(moduleName)} in ${earlyExecutionTimeString} seconds!`)
done.resolve(earlyExecutionTime)
2016-06-11 21:47:52 +00:00
})
2016-06-11 21:53:48 +00:00
} else {
console.log(`... finished loading ${moduleName} in ${earlyExecutionTimeString}`)
done.resolve(earlyExecutionTime)
2016-06-10 03:18:03 +00:00
}
2016-09-15 12:45:58 +00:00
return done.promise
}