add getBasePath

This commit is contained in:
时允 2017-06-16 14:38:35 +08:00
parent d33e71fcd3
commit e30cbe7079
2 changed files with 37 additions and 4 deletions

View File

@ -38,6 +38,34 @@ if (!argv._.length) {
throw new Error('must specify a file');
}
function getBasePath(files) {
let i = 0;
if (files.length <= 1) return '';
while (i < files[0].length) {
let char = '';
const equal = files.every((file, index) => {
if (index === 0) {
char = file[i]
return true;
} else if (file[i] === char) {
return true;
}
return false;
});
if (!equal) {
break;
}
i++;
}
return files[0].slice(0, i);
}
let basePath = null;
if (argv._.length > 1) {
basePath = getBasePath(argv._);
}
argv._.forEach((p) => {
let out;
if (argv.out) {
@ -47,6 +75,10 @@ argv._.forEach((p) => {
}
glob(p, (err, files) => {
if (err) throw err;
if (!basePath) {
basePath = getBasePath(files);
}
console.log(basePath)
files.forEach(file => {
const files = thriftTs.default({
filename: file,
@ -56,9 +88,10 @@ argv._.forEach((p) => {
spaceAsTab: argv.spaceAsTab,
int64AsString: argv.int64AsString,
});
files.forEach(file => {
console.log(path.join(out, file.filename))
fs.writeFileSync(path.join(out, file.filename), file.content);
files.forEach(newFile => {
const outfile = path.join(out, path.relative(basePath, path.dirname(file)), newFile.filename);
console.log(outfile);
fs.writeFileSync(outfile, newFile.content);
})
});
});

View File

@ -1,6 +1,6 @@
{
"name": "thrift-ts",
"version": "1.0.5",
"version": "1.0.6",
"description": "",
"main": "src/index.js",
"scripts": {