feat: ensure output directory exists

closes #590
This commit is contained in:
calebboyd
2019-04-04 09:34:12 -05:00
parent 44393a22da
commit af2e381ed4
+16 -11
View File
@@ -1,8 +1,9 @@
import { normalize, relative } from 'path'
import { dirname, normalize, relative } from 'path'
import { createWriteStream, chmodSync, statSync } from 'fs'
import { NexeCompiler } from '../compiler'
import { NexeTarget } from '../target'
import { STDIN_FLAG } from '../util'
import mkdirp = require('mkdirp')
/**
* The "cli" step detects the appropriate input. If no input options are passed,
@@ -16,28 +17,32 @@ import { STDIN_FLAG } from '../util'
*/
export default async function cli(compiler: NexeCompiler, next: () => Promise<void>) {
await next()
const { log } = compiler
const target = compiler.options.targets.shift() as NexeTarget
const deliverable = await compiler.compileAsync(target)
const { log } = compiler,
target = compiler.options.targets.shift() as NexeTarget,
deliverable = await compiler.compileAsync(target),
output = normalize(compiler.output!)
mkdirp.sync(dirname(output))
return new Promise((resolve, reject) => {
const step = log.step('Writing result to file')
deliverable
.pipe(createWriteStream(normalize(compiler.output!)))
.pipe(createWriteStream(output))
.on('error', reject)
.once('close', (e: Error) => {
if (e) {
reject(e)
} else if (compiler.output) {
const output = compiler.output
const mode = statSync(output).mode | 0o111
const output = compiler.output,
mode = statSync(output).mode | 0o111,
inputFileLogOutput = relative(process.cwd(), compiler.options.input),
outputFileLogOutput = relative(process.cwd(), output)
chmodSync(output, mode.toString(8).slice(-3))
const inputFile = relative(process.cwd(), compiler.options.input)
const outputFile = relative(process.cwd(), output)
step.log(
`Entry: '${
compiler.stdinUsed ? (compiler.options.mangle ? STDIN_FLAG : '[none]') : inputFile
}' written to: ${outputFile}`
compiler.stdinUsed ? (compiler.options.mangle ? STDIN_FLAG : '[none]') : inputFileLogOutput
}' written to: ${outputFileLogOutput}`
)
resolve(compiler.quit())
}