Created
July 4, 2019 14:48
-
-
Save ThisIsMissEm/1b2f4ae2c78f89dfdf4c118533470db2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs").promises; | |
const path = require("path"); | |
const execa = require("execa"); | |
const assert = require("assert"); | |
const gitUrlParse = require("git-url-parse"); | |
const fse = require("fs-extra"); | |
async function findRepos(workingDirectory) { | |
const nodes = await fs.readdir(workingDirectory, { withFileTypes: true }); | |
const directories = nodes.filter(node => node.isDirectory()); | |
return Promise.all( | |
directories.map(async function findGitRemotes({ name: directory }) { | |
const repoPath = path.join(workingDirectory, directory); | |
const repoGitPath = path.join(repoPath, ".git"); | |
const { stdout } = await execa("git", [ | |
`--git-dir=${repoGitPath}`, | |
"remote", | |
"get-url", | |
"origin" | |
]); | |
return { | |
name: directory, | |
path: repoPath, | |
remotes: { origin: gitUrlParse(stdout) } | |
}; | |
}) | |
); | |
} | |
// new path = repos. | |
async function copyRepos(workingDirectory, repos) { | |
const noOwners = repos.filter(repo => !repo.remotes.origin.owner); | |
assert( | |
noOwners.length == 0, | |
"Repositories found with no owners: ${noOwners}" | |
); | |
const owners = Object.keys( | |
repos.reduce( | |
(owners, repo) => ( | |
(owners[repo.remotes.origin.owner.toLowerCase()] = 1), owners | |
), | |
{} | |
) | |
); | |
await Promise.all( | |
owners.map(owner => { | |
const ownerPath = path.join(workingDirectory, owner); | |
console.log(`Creating directory for owner: ${ownerPath}`); | |
return fse.ensureDir(ownerPath); | |
}) | |
); | |
console.log("Moves:"); | |
return Promise.all( | |
repos.map(repo => { | |
const repoName = repo.name.toLowerCase(); | |
const newRepoPath = path.join( | |
workingDirectory, | |
repo.remotes.origin.owner.toLowerCase(), | |
repo.remotes.origin.resource.toLowerCase() === "gist.github.com" | |
? `${repoName}-gist` | |
: repoName | |
); | |
console.log(repo.path, "=>", newRepoPath); | |
return fse.copy(repo.path, newRepoPath, { | |
preserveTimestamps: true, | |
filter: (src, dest) => { | |
const keep = path.basename(src) !== ".virtualenv"; | |
return keep; | |
} | |
}); | |
}) | |
); | |
} | |
async function start(inputPath) { | |
const workingDirectory = path.resolve(process.cwd(), inputPath); | |
const outputDirectory = path.resolve( | |
workingDirectory, | |
`../restructure-${path.basename(workingDirectory)}` | |
); | |
console.log(outputDirectory); | |
const repos = await findRepos(workingDirectory); | |
await copyRepos(outputDirectory, repos); | |
console.log("Done"); | |
} | |
start(process.argv[2]).then( | |
console.log.bind(console), | |
console.error.bind(console) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "restructure-repos", | |
"version": "1.0.0", | |
"main": "index.js", | |
"author": "", | |
"license": "MIT", | |
"dependencies": { | |
"execa": "^2.0.2", | |
"fs-extra": "^8.1.0", | |
"git-url-parse": "^11.1.2", | |
"mkdirp": "^0.5.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks