Skip to content

Instantly share code, notes, and snippets.

@tuyen-vuduc
Created August 28, 2023 23:51
Show Gist options
  • Save tuyen-vuduc/05d4fd31f07ff98f3af366cb3a019572 to your computer and use it in GitHub Desktop.
Save tuyen-vuduc/05d4fd31f07ff98f3af366cb3a019572 to your computer and use it in GitHub Desktop.
Convert C# filenamespace to legacy namespace
const fs = require("fs");
const path = require("path");
var folderPath = "./";
var files = fs.readdirSync(folderPath, { recursive: true })
.filter(x => x.endsWith('.cs'))
.map((fileName) => {
return path.join(folderPath, fileName);
})
.map(x => {
let content = fs.readFileSync(x, 'utf-8');
let lines = content.indexOf('\r\n') >= 0
? content.split('\r\n')
: content.split('\n')
return {
filePath: x,
lines: lines
}
})
.filter(x => {
return x.lines.some(line => /^namespace [^;]+;$/.test(line.trim()))
})
.map(x => {
let namespaceLine = x.lines.find(line => /^namespace [^;]+;$/.test(line.trim()));
let indexOfSemicolon = x.lines.indexOf(namespaceLine);
x.lines[indexOfSemicolon] = namespaceLine.replace(';', '');
for(let i=indexOfSemicolon+1; i<x.lines.length; i++) {
if (!x.lines[i] || /^\s+$/.test(x.lines[i])) continue;
x.lines[i] = ' ' + x.lines[i];
}
x.lines.splice(indexOfSemicolon+1, 0, '{')
x.lines.push('}');
return x;
})
.map(x => {
fs.writeFileSync(x.filePath, x.lines.join('\n'));
return x;
});
console.log(files);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment