|
#!/usr/bin/env node |
|
// this is to use when you duplicate a module. |
|
// For example, lets say you have all the crud basic feature for "user" |
|
// and you would like to seed the feature "product" with it. |
|
// You'll have to copy "user" into "product", search and replace every instance |
|
// of user and replace them into "product"... |
|
// and "User" into "Product"... |
|
// and "USER" into "PRODUCT"... |
|
// and rename all the files.. include XXX_USER.foo into XXX_PRODUCT.foo |
|
// |
|
// this script takes care of that in just 3 lines: |
|
// |
|
// cp -r user product |
|
// cd product |
|
// fromModule user product |
|
|
|
const fs = require("fs"); |
|
|
|
const source = process.argv[2]; |
|
const destination = process.argv[3]; |
|
|
|
if (!source) { |
|
console.error("ERROR: source is missing"); |
|
usage(); |
|
process.exit(1); |
|
} |
|
|
|
if (!destination) { |
|
console.error("ERROR: destination is missing"); |
|
usage(); |
|
process.exit(1); |
|
} |
|
|
|
function usage() { |
|
console.log( |
|
"\nMake sure fromModule file is in a folder in your path and type:" |
|
); |
|
console.log("fromModule <source_folder> <destination_folder>"); |
|
} |
|
|
|
function capitalize(value) { |
|
return value.charAt(0).toUpperCase() + value.slice(1); |
|
} |
|
|
|
function rename(folder, path) { |
|
const newPath = path |
|
.replace(source.toLowerCase(), destination.toLowerCase()) // xxx_old.yyy => xxx_new.yyy |
|
.replace(capitalize(source), capitalize(destination)) // xxx_Old.yyy => xxx_New.yyy |
|
.replace(source.toUpperCase(), destination.toUpperCase()); // xxx_OLD.yyy => xxx_NEW.yyy |
|
|
|
if (`${folder}${path}` !== `${folder}${newPath}`) { |
|
fs.renameSync(`${folder}${path}`, `${folder}${newPath}`); |
|
console.log(`Renamed "${folder}${path}" to "${folder}${newPath}"`); |
|
} |
|
|
|
return newPath; |
|
} |
|
|
|
function interpolate(path) { |
|
const file = fs.readFileSync(path); |
|
|
|
const result = file |
|
.toString() |
|
.replace(new RegExp(source.toLowerCase(), "g"), destination.toLowerCase()) |
|
.replace(new RegExp(capitalize(source), "g"), capitalize(destination)) |
|
.replace(new RegExp(source.toUpperCase(), "g"), destination.toUpperCase()); |
|
|
|
console.log(`Interpolated ${path}"`); |
|
|
|
fs.writeFileSync(path, result); |
|
} |
|
|
|
function explore(folder) { |
|
const files = fs.readdirSync(folder); |
|
for (const file of files) { |
|
const stat = fs.statSync(`${folder}${file}`); |
|
if (stat.isDirectory()) { |
|
explore(`${folder}${file}/`); |
|
rename(folder, file); |
|
} else { |
|
interpolate(`${folder}${file}`); |
|
rename(folder, file); |
|
} |
|
} |
|
} |
|
|
|
explore("./"); |