Initial commit.
This commit is contained in:
36
tasks/build.ts
Normal file
36
tasks/build.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import fs from 'fs'
|
||||
import { run } from './process'
|
||||
|
||||
function readOutDirNameFromConfig (): string | null {
|
||||
const outDirRegExp = new RegExp(/["|']outDir["|']:\s?["|'](.*)["|']/)
|
||||
const configData = fs.readFileSync('./tsconfig.json', 'utf8')
|
||||
const matchResult = configData.match(outDirRegExp)
|
||||
|
||||
if (matchResult == null) {
|
||||
return null
|
||||
} else {
|
||||
return matchResult[1]
|
||||
}
|
||||
}
|
||||
|
||||
export async function build (mode: 'production' | 'development'): Promise<string> {
|
||||
const distPath = readOutDirNameFromConfig()
|
||||
if (distPath == null) {
|
||||
throw new Error('Option "compilerOptions.outDir" must be specified in tsconfig.json.')
|
||||
}
|
||||
|
||||
let buildCommand = 'tsc'
|
||||
if (mode === 'development') {
|
||||
buildCommand += ' --project ./tsconfig-dev.json'
|
||||
}
|
||||
|
||||
console.info('Build mode: ' + mode + '.')
|
||||
|
||||
if (fs.existsSync(distPath)) {
|
||||
console.info('Removing previous build...')
|
||||
fs.rmdirSync(distPath, { recursive: true })
|
||||
}
|
||||
|
||||
console.info('Running command: ' + buildCommand)
|
||||
return await run(buildCommand)
|
||||
}
|
||||
53
tasks/process.ts
Normal file
53
tasks/process.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { exec } from 'child_process'
|
||||
|
||||
export function finishWithFail (error: Error): void {
|
||||
console.error(error)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
export function finishWithSuccess (message?: string): void {
|
||||
if (message != null) {
|
||||
console.info(message)
|
||||
}
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
export async function run (command: string): Promise<string> {
|
||||
return await new Promise(function (resolve, reject) {
|
||||
const output: string[] = []
|
||||
|
||||
const child = exec(command, {
|
||||
cwd: process.cwd(),
|
||||
env: process.env
|
||||
})
|
||||
|
||||
if (child.stdout != null) {
|
||||
child.stdout.on('data', function (data) {
|
||||
console.log(data)
|
||||
output.push(data.toString())
|
||||
})
|
||||
}
|
||||
|
||||
if (child.stderr != null) {
|
||||
child.stderr.on('data', function (data) {
|
||||
console.log(data)
|
||||
output.push(data.toString())
|
||||
})
|
||||
}
|
||||
|
||||
child.on('exit', function (code) {
|
||||
const outputString = output.join('\n')
|
||||
|
||||
if (code === 0) {
|
||||
resolve(outputString)
|
||||
} else {
|
||||
const errorMessage = {
|
||||
code: code,
|
||||
message: outputString
|
||||
}
|
||||
reject(new Error(JSON.stringify(errorMessage)))
|
||||
}
|
||||
})
|
||||
child.on('error', reject)
|
||||
})
|
||||
}
|
||||
13
tasks/run-build.ts
Normal file
13
tasks/run-build.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { build } from './build'
|
||||
import { finishWithFail, finishWithSuccess } from './process'
|
||||
|
||||
try {
|
||||
const args = process.argv.slice(2)
|
||||
const mode = args.includes('-d') ? 'development' : 'production'
|
||||
|
||||
build(mode)
|
||||
.then(finishWithSuccess.bind(null, 'Successfully built.'))
|
||||
.catch(finishWithFail)
|
||||
} catch (error) {
|
||||
finishWithFail(error)
|
||||
}
|
||||
Reference in New Issue
Block a user