Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active December 5, 2023 10:40
Show Gist options
  • Save WebReflection/a737e00584373a38de1afcc32dd5e616 to your computer and use it in GitHub Desktop.
Save WebReflection/a737e00584373a38de1afcc32dd5e616 to your computer and use it in GitHub Desktop.
Personal coding setup

This gist contains few scripts I use daily to help myself work with git for both personal projects and work.

GNOME

My GNOME tips are here https://gist.github.com/WebReflection/7538865e739c0c2eeb542dfac235e84c

./find

This utility finds outer dependent modules. I've used it on the top most folder with all cloned repo.

#!/usr/bin/env node

// ./find module-name
// simple utility to find folders depending on module-name
// by Andrea Giammarchi - @webreflection

let [_, find, search] = process.argv;
const fs = require('fs');
const path = require('path');

const cwd = process.cwd();
const dir = path.dirname(path.resolve(find));

if (cwd !== dir) {
  if (!search) {
    search = path.basename(cwd);
  }
  process.chdir(dir);
}

if (!search)
  console.log(' ./find module-name');
else
  fs.readdir('.', (err, dirs) => {
    console.log('');
    const lower = search.toLowerCase();
    const promises = [];
    const noPackage = [];
    dirs.forEach(dir => {
      promises.push(new Promise($ => {
        fs.stat(dir, (err, stats) => {
          if (!err &&  stats.isDirectory()) {
            try {
              const package = require(`./${dir}/package.json`);
              const deps = [];
              if (
                search in (package.dependencies || {}) ||
                lower in (package.dependencies || {})
              )
                deps.push('dependency');
              if (
                search in (package.devDependencies || {}) ||
                lower in (package.devDependencies || {})
              )
                deps.push('devDependency');
              if (deps.length)
                console.info(` \x1B[1m${dir}\x1B[0m as ${deps.join(', ')}`);
            } catch(o_O) {
              noPackage.push(dir);
            }
          }
          $();
        });
      }));
    });
    Promise.all(promises).then(() => {
      console.log('');
      console.info(` \x1B[2mSearched for \x1B[0m${search}\x1B[2m in \x1B[0m${dir}`);
      if (noPackage.length) {
        console.warn(` \x1B[2mNo package.json in ${noPackage.join(', ')}\x1B[0m`);
      }
      console.log('');
    });
  });

./update

This utility updates --prod, --dev, --opt or, by default, --all package.json dependencies. I've used it on the top most folder with all cloned repo.

#!/usr/bin/env node

// ./update        # updates all dependencies
// ./update --all  # default, updates all dependencies
// ./update --prod # update only dependencies
// ./update --dev  # update only devDependencies
// ./update --opt  # update only optionalDependencies
// simple utility to update dependencies
// by Andrea Giammarchi - @webreflection

const path = require('path');
const {spawnSync} = require('child_process');

const bold = what => `\x1B[1m${what}\x1B[0m`;

const dep = process.argv.some(arg => /^--(?:dep|prod)$/.test(arg));
const dev = process.argv.some(arg => /^--dev$/.test(arg));
const opt = process.argv.some(arg => /^--opt(?:ional)?$/.test(arg));
const all = process.argv.some(arg => arg === '--all') || !(dev || dep || opt);

const cwd = process.cwd();
const json = path.join(cwd, 'package.json');
const package = require(json);

const result = {};

if (all || dep)
  update(package, 'dependencies', '--save-prod');

if (all || dev)
  update(package, 'devDependencies', '--save-dev');

if (all || opt)
  update(package, 'optionalDependencies', '--save-optional');

Object.keys(result).forEach(group => {
  console.log('');
  console.log(group);
  if (result[group].length)
    result[group].forEach(info => console.log(info));
  else
    console.log(' no updates needed');
});
console.log('');

function update(old, dep, how) {
  const updates = old[dep];
  if (updates) {
    const group = bold(dep.replace(/([a-z])([A-Z])/g, '$1 $2').toUpperCase());
    result[group] = [];
    const modules = Object.keys(updates)
                      .filter(name => /^\^/.test(updates[name]))
                      .map(name => `${name}@latest`);
    if (modules.length) {
      const args = ['i', how].concat(modules);
      const {status, stderr} = spawnSync('npm', args, {
        cwd,
        stdio: ['inherit', 'inherit', 'inherit']
      });
      if (status) {
        console.error(`${bold('Error')}: ${stderr}`);
        process.exit(status);
      }
      delete require.cache[json];
      const package = require(json)[dep];
      Object.keys(package).forEach(name => {
        const curr = bold(package[name]);
        const prev = bold(updates[name]);
        if (curr !== prev)
          result[group].push(` updated ${bold(name)} from ${prev} to ${curr}`);
      });
    }
  }
}

.gitconfig

This config uses either ~/.gitconfig-personal or ~/.gitconfig-work accordingly with the folder I am working with.

There are also a couple of handy aliases, sich as git pf, to amend and push force (instead of squashing) changes to the same branch, or git publish to do the usual git push && git push --tags && npm publish dance at once.

[push]
	default = simple

[includeIf "gitdir:~/git/"]
        path = ~/.gitconfig-personal

[includeIf "gitdir:~/gitlab/"]
        path = ~/.gitconfig-work

[alias]
        cb = !sh -c \"git branch | grep \\* | cut -d ' ' -f2\"
        delete = !sh -c \"git branch -d $1 && git push origin --delete $1\"
        me = !sh -c \"ssh -T [email protected]\"
        pf = !sh -c \"git commit --amend --no-edit && git push --force origin $(git branch | grep \\* | cut -d ' ' -f2)\"
        pr = !sh -c \"git pull --rebase origin $(git branch | grep \\* | cut -d ' ' -f2)\"
        pur = !sh -c \"git fetch upstream && git pull --rebase upstream $(git branch | grep \\* | cut -d ' ' -f2)\"
        publish = !sh -c \"git push && git push --tags && npm publish\"

You could also just add aliases via:

curl -s https://archibold.io/install/git-alias >> ~/.gitconfig

.gitconfig-personal

[user]
	name = Andrea Giammarchi
	email = [email protected]

[core]
	sshCommand = "ssh -i ~/.ssh/id_rsa"

.gitconfig-work

[user]
	name = Andrea Giammarchi
	email = [email protected]

[core]
	sshCommand = "ssh -i ~/.ssh/id_rsa_company"

aliases

json-date

alias json-date='date "+%FT%H:%M:%S.%NZ" | sed -e "s/\.\([0-9][0-9][0-9]\)[0-9]*Z$/.\1Z/"'

./asdualmodule

This utility converts old repositories into proper NodeJS ocmpatible dual-module. The structure might be the classic on used in my repo, with both esm/index.js and cjs/index.js avaiable.

#!/usr/bin/env bash

repo="$1"

if [ "$repo" = "" ]; then
  echo ''
  echo "$(tput bold)usage$(tput sgr0)"
  echo ''
  echo './asdualmodule [email protected]:YourName/your-project.git'
  echo ''
  echo 'it will erase the folder and clone from scratch,'
  echo 'so please watch out unsafed changes'
  echo ''
  exit 0
fi

folder="$(basename $repo | sed 's/.git$//')"

rm -rf $folder

git clone "$repo"

cd "$folder"

if [ -f cjs/index.js ] && [ -f esm/index.js ]; then
  node -e '
    const package = require("./package.json");
    package.main = "./cjs/index.js";
    package.module = "./esm/index.js";
    package.type = "module";
    package.exports = {
      "import": "./esm/index.js",
      "default": "./cjs/index.js"
    };
    if (package.devDependencies.istanbul) {
      package.devDependencies.nyc = "latest";
      package.scripts.test = "nyc node test/index.js"
      delete package.devDependencies.istanbul;
      if (package.scripts.coveralls)
        package.scripts.coveralls = "nyc report --reporter=text-lcov | coveralls";
    }
    require("fs").writeFileSync("./package.json", JSON.stringify(package, null, "  "));
  '
  echo '{"type":"commonjs"}' > cjs/package.json
  echo '{"type":"commonjs"}' > test/package.json
  if [ ! -d ~/git/umap ]; then
    cd ~/git
    git clone [email protected]:WebReflection/umap.git
    cd -
  fi
  rm -rf coveralls
  cp ~/git/umap/.gitignore ./
  cp ~/git/umap/.npmignore ./
  ~/git/update
  npm run build && echo "" && echo "$(tput bold)$folder$(tput sgr0) ready to be published as dual module" && echo ""
  read -n1 -r -p "would you like to publish this module? [y/N]" publish
  if [ "$publish" = "y" ]; then
    git add . && git commit -m "$folder is now a proper dualmodule + dependencies updates" && npm version minor && npm publish
  fi
else
  echo "$folder not suitable for dual module"
  exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment