Skip to content

Instantly share code, notes, and snippets.

@zfwf
Created July 15, 2021 03:46
Show Gist options
  • Save zfwf/5900b581f05a206409c827b32a3d3961 to your computer and use it in GitHub Desktop.
Save zfwf/5900b581f05a206409c827b32a3d3961 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const dir = './src';
const rootDir = 'web';
const targetPath = 'src/shared';
const replaceDir = path.join('@jarden-digital/megablue-apps-shared');
function walk(dir, callback) {
fs.readdir(dir, function (err, files) {
if (err) throw err;
files.forEach(function (file) {
const filepath = path.join(dir, file);
fs.stat(filepath, function (err, stats) {
if (stats.isDirectory()) {
walk(filepath, callback);
} else if (stats.isFile()) {
callback(filepath, stats);
}
});
});
});
}
const callback = (filepath, stats) => {
const content = fs.readFileSync(filepath, 'utf8');
if (content.indexOf(replaceDir) != -1) {
const rawRelativePath = path.relative(
path.join(rootDir, filepath),
path.join(rootDir, targetPath)
);
const relativePath = rawRelativePath.slice(3);
const content = fs.readFileSync(filepath, 'utf8');
const updated = content.replace(
/@jarden-digital\/megablue-apps-shared/g,
relativePath
);
/* console.log('filepath', filepath); */
/* console.log('relativepath', relativePath, rawRelativePath); */
/* console.log('updated', updated.slice(0, 500)); */
fs.writeFileSync(filepath, updated);
}
};
walk(dir, callback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment