Add a command-line script to create example repos (#155)

Summary:
We’ll use this to create the repositories on disk and then push them to
GitHub.

Test Plan:
Generate both kinds of repository, and check out the SHAs:
```shell
$ yarn backend
$ node bin/createExampleRepo.js /tmp/repo
$ node bin/createExampleRepo.js --submodule /tmp/repo-submodule
$ node bin/createExampleRepo.js --no-submodule /tmp/repo-no-submodule
$ # (first and third lines do the same thing)
$ git -C /tmp/repo rev-parse HEAD
677b340674bde17fdaac3b5f5eef929139ef2a52
$ git -C /tmp/repo-submodule rev-parse HEAD
29ef158bc982733e2ba429fcf73e2f7562244188
$ git -C /tmp/repo-no-submodule rev-parse HEAD
677b340674bde17fdaac3b5f5eef929139ef2a52
```
Then, note that these SHAs are expected per the snapshot file in
`exampleRepo.test.js.snap`.

wchargin-branch: create-example-repo-command
This commit is contained in:
William Chargin 2018-04-26 19:53:46 -07:00 committed by GitHub
parent ef7610a440
commit d6e9b0a72b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 0 deletions

View File

@ -62,5 +62,6 @@ module.exports = {
fetchAndPrintGithubRepo: resolveApp(
"src/plugins/github/bin/fetchAndPrintGithubRepo.js"
),
createExampleRepo: resolveApp("src/plugins/git/bin/createExampleRepo.js"),
},
};

View File

@ -0,0 +1,74 @@
// @flow
import fs from "fs";
import {
createExampleRepo,
createExampleSubmoduleRepo,
} from "../demoData/exampleRepo";
function parseArgs() {
const argv = process.argv.slice(2);
const fail = () => {
const invocation = process.argv.slice(0, 2).join(" ");
throw new Error(`Usage: ${invocation} [--[no-]submodule] TARGET_DIRECTORY`);
};
let submodule: boolean = false;
let target: ?string = null;
for (const arg of argv) {
if (arg === "--submodule") {
submodule = true;
} else if (arg === "--no-submodule") {
submodule = false;
} else {
if (target == null) {
target = arg;
} else {
fail();
}
}
}
if (target == null) {
fail();
throw new Error("Unreachable"); // for Flow
}
return {
submodule,
target: (target: string),
};
}
function ensureEmptyDirectoryOrNonexistent(target: string) {
const files = (() => {
try {
return fs.readdirSync(target);
} catch (e) {
if (e.code === "ENOTDIR") {
throw new Error("Target exists, but is not a directory.");
} else if (e.code === "ENOENT") {
// No problem. We'll create it.
return [];
} else {
throw e;
}
}
})();
if (files.length > 0) {
throw new Error("Target directory exists, but is nonempty.");
}
}
function main() {
const args = parseArgs();
ensureEmptyDirectoryOrNonexistent(args.target);
if (args.submodule) {
createExampleSubmoduleRepo(args.target);
} else {
createExampleRepo(args.target);
}
}
main();