Skip to content

Commit

Permalink
Bugfixes with globbing and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxattax97 committed Mar 13, 2019
1 parent d29195b commit 033adc7
Showing 1 changed file with 70 additions and 50 deletions.
120 changes: 70 additions & 50 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,30 @@ async function format(str, tmpDir) {

const clangConversionResult = await convertIncludesToClang(str);

const virtualFile = {
path: path.join(tmpDir.path, 'input.scad'),
};
await fs.writeFile(virtualFile.path, clangConversionResult.str);
try {
const { path: tmpFilePath, cleanup: cleanupTmpFile } = await tmp.file({ dir: tmpDir.path, postfix: '.scad' });

let result = await getClangFormattedString(virtualFile);
const virtualFile = {
path: tmpFilePath,
};
await fs.writeFile(virtualFile.path, clangConversionResult.str);

result = await convertIncludesToScad(result, clangConversionResult.backup);
let result = await getClangFormattedString(virtualFile);

try {
await fs.remove(virtualFile.path);
result = await convertIncludesToScad(result, clangConversionResult.backup);

try {
await fs.remove(virtualFile.path);
} catch (err) {
console.error('Failed to remove temporary input file');
}

cleanupTmpFile();

return result;
} catch (err) {
console.error('Failed to remove temporary input file');
console.error(err);
}

return result;
}

async function feed(input, output, tmpDir) {
Expand All @@ -139,20 +147,25 @@ async function feed(input, output, tmpDir) {

str = str.toString();

const result = await format(str, tmpDir);

if (result) {
if (output) {
await fs.writeFile(output, result);
} else if (input && argv.isCLI) {
// Write it back to the source location.
await fs.writeFile(input, result);
} else if (argv.isCLI) {
process.stdout.write(result);
try {
const result = await format(str, tmpDir);

if (result) {
if (output) {
await fs.writeFile(output, result);
} else if (input && argv.isCLI) {
// Write it back to the source location.
await fs.writeFile(input, result);
} else if (argv.isCLI) {
process.stdout.write(result);
}
return result;
}
return result;

throw new Error('Failed to format content string');
} catch (err) {
console.error(err);
}
throw new Error('Failed to format content string');
}

async function main(input, output) {
Expand All @@ -165,7 +178,14 @@ async function main(input, output) {
}

try {
argv.input = await globby(argv.input);
argv.input = await globby(argv.input, {
deep: true,
// expandDirectories: {
// files: ['*.scad'],
// extensions: ['scad'],
// },
gitignore: true,
});
} catch (err) {
console.error(err, argv.input);
}
Expand All @@ -182,43 +202,43 @@ async function main(input, output) {
const tmpDir = await tmp.dir({ unsafeCleanup: true });

try {
await fs.copy('.clang-format', path.join(tmpDir.path, '.clang-format'));
} catch (err) {
console.error('Failed to copy clang format config file to temporary directory');
}

if (argv.input) {
await Promise.all(argv.input.map(async (file) => {
await fs.copy(path.join(__dirname, '.clang-format'), path.join(tmpDir.path, '.clang-format'));

if (argv.input) {
await Promise.all(argv.input.map(async (file) => {
try {
const result = await feed(file, argv.output, tmpDir);
resultList.push({ source: file, formatted: result });
} catch (err) {
console.error(err);
}
}));
} else {
// Use stdin.
try {
const result = await feed(file, argv.output, tmpDir);
resultList.push({ source: file, formatted: result });
const result = await feed(null, argv.output, tmpDir);
resultList.push({ source: 'stdin', formatted: result });
} catch (err) {
console.error(err);
}
}));
} else {
// Use stdin.
}

try {
const result = await feed(null, argv.output, tmpDir);
resultList.push({ source: 'stdin', formatted: result });
await fs.remove(path.join(tmpDir.path, '.clang-format'));
} catch (err) {
console.error(err);
console.error('Failed to remove temporary clang format config file');
}
}

try {
await fs.remove(path.join(tmpDir.path, '.clang-format'));
} catch (err) {
console.error('Failed to remove temporary clang format config file');
}
try {
tmpDir.cleanup();
} catch (err) {
console.error('Failed to cleanup temporary directory');
}

try {
tmpDir.cleanup();
return resultList;
} catch (err) {
console.error('Failed to cleanup temporary directory');
console.error(err);
}

return resultList;
} catch (err) {
console.error(err);
}
Expand Down

0 comments on commit 033adc7

Please sign in to comment.