Skip to content

Commit f1e5e8e

Browse files
committed
bundle pass update, exit codes
1 parent 675b4a1 commit f1e5e8e

File tree

8 files changed

+109
-45
lines changed

8 files changed

+109
-45
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-bundle",
3-
"version": "0.8.8",
3+
"version": "0.8.9",
44
"description": "bundle modular typescript projects for the browser.",
55
"main": "./index.js",
66
"scripts": {},

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ typescript-bundle does however mandate the following settings be set. So it is r
101101
]
102102
}
103103
```
104-
note: the ```--moduleResolution``` and ```--outDir``` must be left unset.
104+
note: the ```--outDir``` must be left unset.
105105

106106
note: the ```files``` should contain only one file path.
107107

src/bundle.ts

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { Options } from "./options"
3030
import { touch } from "./touch"
3131
import { watch } from "./watch"
3232
import { compile } from "./compile"
33+
import { delay } from "./delay"
3334
import { shim } from "./shim"
3435
import { help, version, info, errors, done } from "./help"
3536

@@ -43,42 +44,69 @@ import * as path from "path"
4344
* @returns {Promise<any>}
4445
*/
4546
export const bundle = async (options: Options, log: Function) => {
46-
// run validation, help and version checks.
47-
if(options.errors.length > 0) return log(errors(options))
48-
if(options.properties.help) return log(help())
49-
if(options.properties.version) return log(await version())
50-
51-
// display compiler settings.
52-
log(info(options))
53-
54-
// if we are loading from a project, then we need to resolve
55-
// the output file path from the view of the config.
56-
const outputFile = (options.properties.project === undefined)
57-
? path.resolve(process.cwd(), options.properties.outputFile)
58-
: path.resolve(
59-
path.dirname(
60-
path.resolve(process.cwd(), options.properties.project
61-
) ), options.properties.outputFile)
62-
63-
64-
// ensure the output file exists.
47+
// ----------------------------------------------------------
48+
//
49+
// run validation, help and version checks.
50+
//
51+
// ----------------------------------------------------------
52+
if(options.errors.length > 0) { return log(errors(options)) }
53+
if(options.properties.help) { return log(help()) }
54+
if(options.properties.version) { return log(await version()) }
55+
56+
// ----------------------------------------------------------
57+
//
58+
// display compiler settings.
59+
//
60+
// ----------------------------------------------------------
61+
log(info(options))
62+
63+
// ----------------------------------------------------------
64+
//
65+
// if we are loading from a project, then we need to resolve
66+
// the output file path from the view of the config.
67+
//
68+
// ----------------------------------------------------------
69+
const outputFile = (options.properties.project === undefined)
70+
? path.resolve(process.cwd(), options.properties.outputFile)
71+
: path.resolve(
72+
path.dirname(
73+
path.resolve(process.cwd(), options.properties.project
74+
) ), options.properties.outputFile);
75+
76+
// ----------------------------------------------------------
77+
//
78+
// watch
79+
//
80+
// ----------------------------------------------------------
81+
if (options.properties.watch === true) {
82+
83+
// ensure the output file exists..
6584
await touch(outputFile)
85+
86+
// start watcher process..
87+
watch(outputFile, () => shim(outputFile, options.properties.exportAs, options.properties.importAs))
88+
89+
// compile..
90+
compile (options.command, log)
91+
}
92+
93+
// ----------------------------------------------------------
94+
//
95+
// standard
96+
//
97+
// ----------------------------------------------------------
98+
else {
99+
100+
// compile
101+
await compile (options.command, log)
102+
103+
// wait a bit..
104+
await delay (100)
66105

67-
// watch output file for changes. (compile on save support)
68-
const watcher = watch(outputFile, () => {
69-
shim(outputFile, options.properties.exportAs, options.properties.importAs)
70-
})
106+
// shim
107+
await shim (outputFile, options.properties.exportAs, options.properties.importAs)
71108

72-
// run compilation.
73-
const start = new Date()
74-
try {
75-
await compile (options.command, log)
76-
await shim (outputFile, options.properties.exportAs, options.properties.importAs)
77-
watcher.close ()
78-
log(done())
79-
} catch (e) {
80-
shim(outputFile, options.properties.exportAs, options.properties.importAs)
81-
watcher.close()
82-
log(done())
83-
}
109+
// complete
110+
log(done())
111+
}
84112
}

src/compile.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
THE SOFTWARE.
2626
2727
---------------------------------------------------------------------------*/
28-
import {shell} from "./shell"
28+
import { shell } from "./shell"
2929

3030
/**
3131
* invokes the typescript compiler with the given command.
3232
* @param {string} command the typescript shell command.
3333
* @param {Function} log the logging function.
34-
* @returns {Promise<any>}
34+
* @returns {Promise<void>}
3535
*/
36-
export const compile = async (command: string, log: Function = function() {}) : Promise<any> => {
36+
export const compile = async (command: string, log: Function = function() {}) : Promise<void> => {
3737
const exitcode = await shell(command, log)
38-
if(exitcode !== 0) throw Error("unexpected exit code")
38+
if(exitcode !== 0) {
39+
process.exit(exitcode)
40+
}
3941
}

src/delay.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*--------------------------------------------------------------------------
2+
3+
typescript-bundle - bundle modular typescript projects for the browser
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2016-2017 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
---------------------------------------------------------------------------*/
28+
29+
/**
30+
* preforms a simple delay with the given milliseconds.
31+
* @param {number} ms the number of milliseconds to delay.
32+
* @returns {Promise<void>}
33+
*/
34+
export const delay = (ms: number) => new Promise<void>(resolve => setTimeout(() => resolve(), ms))

src/help.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ THE SOFTWARE.
2626
2727
---------------------------------------------------------------------------*/
2828

29-
import {Options} from "./options"
30-
import {shell} from "./shell"
29+
import { Options } from "./options"
30+
import { shell } from "./shell"
3131

3232
const VERSION = "[[[VERSION_STRING]]]"
3333

src/shell.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ THE SOFTWARE.
2626
2727
---------------------------------------------------------------------------*/
2828

29-
import {spawn} from "child_process"
29+
import { spawn } from "child_process"
3030

3131
/**
3232
* executes a shell command.

src/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ THE SOFTWARE.
2626
2727
---------------------------------------------------------------------------*/
2828

29-
import * as fs from "fs"
29+
import * as fs from "fs"
3030

3131
/** simple event debouncer */
3232
class Debounce {

0 commit comments

Comments
 (0)