Skip to content

Commit

Permalink
feat: update cli --help, output prefix trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Apr 19, 2019
1 parent 024fa28 commit e8e97d8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $ npm i -g ts2php
$ ts2php ./a.ts # 编译输出到 stdout
```

使用配置并输出到文件
使用[配置][options]并输出到文件

```bash
$ cat config.js
Expand Down Expand Up @@ -487,3 +487,5 @@ $ddd = array( "a" => strlen($str), "b" => strlen($str) + 1 );
## Thanks to

Based on [Typescript](https://github.com/Microsoft/TypeScript) compiler

[options]: https://max-team.github.io/ts2php/interfaces/ts2phpoptions.html
43 changes: 28 additions & 15 deletions src/bin/ts2php.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
#!/usr/bin/env node

import { compile } from '../index'
import { join, resolve, dirname } from 'path'
import { normalize, join, resolve, dirname } from 'path'
import { readdirSync, lstatSync, ensureDirSync, writeFileSync } from 'fs-extra'
import yargs = require('yargs')

yargs.usage('ts2php [options] <files ...>')
.describe('Transpile TypeScript files to PHP, see: https://github.com/max-team/ts2php')
.example('$0 -c config.js src/index.ts', 'Transpile using a config file')
.alias('d', 'show-diagnostics').describe('d', 'Show diagnostics').boolean('d')
.alias('H', 'emit-header').describe('H', 'Emit header').boolean('H')
.alias('c', 'config').describe('c', 'Specify a config file').string('c')
.alias('c', 'config').describe('c', 'Specify a config file, see: https://max-team.github.io/ts2php/interfaces/ts2phpoptions.html').string('c')
.alias('o', 'out').describe('o', 'Output directory, defaults to stdout').string('o')
.demandCommand(1, 'Invalid options, specify a file')
.help('h').alias('h', 'help')
.argv;

const argv = yargs.argv

const options = argv.config
? require(resolve(argv.config))
: {
showDiagnostics: !!argv.showDiagnostics,
emitHeader: !!argv.emitHeader,
};
let options = {};
if (argv.config) {
const configFile = resolve(argv.config)
console.error('[options]', configFile)
options = require(configFile)
}

console.error('using options:', options)
const stripPrefix = argv._.length === 1 && lstatSync(argv._[0]).isDirectory()
argv._.forEach(compilePath)

function compileFile(filepath) {
console.error(`transpiling ${filepath}...`)
console.error(`[compile] ${filepath}...`)

const { errors, phpCode } = compile(filepath, options);
if (errors.length) throw new Error('error:' + JSON.stringify(errors))
if (errors.length) {
errors.forEach(({file, messageText, start}) => {
const text = typeof messageText === 'string' ? messageText : messageText.messageText
console.error(`[error] ${text} in ${file.fileName} at ${start}`)
})
process.exit(1);
}
if (argv.out) {
const outpath = join(argv.out, filepath).replace(/\.ts$/, '.php')
const outpath = outPath(filepath)
ensureDirSync(dirname(outpath))
writeFileSync(outpath, phpCode, 'utf8')
console.error(`${phpCode.length} chars written to ${outpath}`)
console.error(`[written] ${phpCode.length} chars to ${outpath}`)
} else {
console.log(phpCode)
}
Expand All @@ -50,3 +54,12 @@ function compilePath(path) {
readdirSync(path).forEach(file => compilePath(join(path, file)))
}
}

function outPath(filepath: string) {
if (stripPrefix) {
filepath = normalize(filepath)
const prefix = normalize(argv._[0])
filepath = filepath.replace(prefix, '')
}
return join(argv.out, filepath).replace(/\.ts$/, '.php')
}

0 comments on commit e8e97d8

Please sign in to comment.