Skip to content

Instantly share code, notes, and snippets.

@typehorror
Last active July 9, 2020 18:19
Show Gist options
  • Save typehorror/83e3740eb20be24bb95f8b179d3a4503 to your computer and use it in GitHub Desktop.
Save typehorror/83e3740eb20be24bb95f8b179d3a4503 to your computer and use it in GitHub Desktop.
Create a feature folder from an existing feature folder

This script 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 file:

  • replace "user" by "product"
  • and "User" by "Product"
  • and "USER" by "PRODUCT"
  • and rename files XXX_USER.py to XXX_PRODUCT.py
  • and UserXXX.js to ProductXXX.js
  • and createUser.tsx to createProduct.tsx
  • and rename the folder userLibs to productLibs etc...

Install

You'll need to have Nodejs installed. Copy this script in a folder in your path, make it executable and you should be good to go.

curl https://gist.githubusercontent.com/debrice/83e3740eb20be24bb95f8b179d3a4503/raw -o /usr/local/bin/fromModule
chmod +x /usr/local/bin/fromModule

Usage

this script takes care of that in just 3 lines:

cp -r user product
cd product
fromModule user product
#!/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("./");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment