Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
feat: support using .js file instead of version
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Nov 20, 2019
1 parent 6935f6d commit 7ac6ce4
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 145 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ module.exports = {
return '\\' + name + '\\';
}
};
```

## use .js file directly

While you working on ts2php, you may want to specify a file to use.

```sh
ts2php-check 0.12.12 /path/to/ts2php/dist/index.js
```
6 changes: 5 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ gulp.task('default', () =>
gulp.src('src/**/*.ts')
.pipe(babel())
.pipe(gulp.dest('dist'))
);
);

gulp.task('watch', () =>
gulp.watch('src/**/*.ts', gulp.series('default'))
)
165 changes: 30 additions & 135 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "dist/index.js",
"scripts": {
"build": "gulp",
"dev": "gulp watch",
"semantic-release": "semantic-release"
},
"repository": {
Expand Down Expand Up @@ -33,11 +34,13 @@
},
"dependencies": {
"@babel/preset-typescript": "^7.7.2",
"@types/semver": "^6.2.0",
"chalk": "^3.0.0",
"commander": "^4.0.1",
"git-diff": "^2.0.6",
"glob": "^7.1.6",
"progress": "^2.0.3",
"semver": "^6.3.0",
"ts2php~v0.16.1": "npm:ts2php@^0.16.1",
"typescript": "~3.4.5"
}
Expand Down
29 changes: 20 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as ts2php from 'ts2php~v0.16.1';
import gitDiff from "git-diff";
import glob from "glob";
import ProgressBar from 'progress';
import semver from 'semver';

const info = chalk.bold.green;
const error = chalk.bold.red;
Expand Down Expand Up @@ -47,17 +48,27 @@ async function compileByVersion(filePath: string, version: string, options?: ts2
}

async function getTs2phpByVersion(version: string) {
const moduleName = makeModuleName(version);
try {
require.resolve(moduleName);
if (semver.valid(version)) {
const moduleName = makeModuleName(version);
try {
require.resolve(moduleName);
}
catch(e) {
console.log(info(`Install ts2php version ${version}...`));
await run('npm', ['i', `${moduleName}@npm:ts2php@${version}`, '--registry=https://registry.npm.taobao.org'], {cwd: __dirname});
console.log(info(`Ts2php version ${version} installed.`))
}

return require(moduleName) as typeof ts2php;
}
catch(e) {
console.log(info(`Install ts2php version ${version}...`));
await run('npm', ['i', `${moduleName}@npm:ts2php@${version}`, '--registry=https://registry.npm.taobao.org'], {cwd: __dirname});
console.log(info(`Ts2php version ${version} installed.`))
else {
try {
return require(version);
}
catch (e) {
throw new Error(`invalid version: ${version}`);
}
}

return require(moduleName) as typeof ts2php;
}

function makeModuleName(version: string) {
Expand Down

0 comments on commit 7ac6ce4

Please sign in to comment.