gitorial-cli 0.1.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,47 +2,96 @@
2
2
 
3
3
  The gitorial-cli is a CLI tool for helping manage and work with a Git repo following the [Gitorial format](https://github.com/gitorial-sdk).
4
4
 
5
+ ## Install
6
+
7
+ Install this CLI via NPM:
8
+
9
+ ```sh
10
+ npm install -g gitorial-cli
11
+ ```
12
+
5
13
  ## Commands
6
14
 
7
- The following commands are available in this CLI:
15
+ The CLI exposes various commands for managing a Gitorial.
16
+
17
+ ```sh
18
+ Commands:
19
+ unpack [options] Unpack a Gitorial into another branch.
20
+ repack [options] Create a repacked Gitorial from an unpacked Gitorial. Must repack into a new branch.
21
+ mdbook [options] Scaffold the contents of a Gitorial in a new branch in the mdBook source format. You need to initialize an mdBook yourself
22
+ ```
8
23
 
9
24
  ### unpack
10
25
 
11
26
  ```sh
12
- yarn start unpack <gitorialPath> <gitorialBranch> <unpackedBranch>
27
+ Usage: index unpack [options]
28
+
29
+ Unpack a Gitorial into another branch.
30
+
31
+ Options:
32
+ -p, --path <path> The local path for the git repo containing the Gitorial.
33
+ -i, --inputBranch <inputBranch> The branch in the repo with the Gitorial.
34
+ -o, --outputBranch <outputBranch> The branch where you want to unpack the Gitorial.
35
+ -s, --subFolder <subFolder> The subfolder (relative to the <path>) where you want the unpacked Gitorial to be placed.
36
+ -h, --help display help for command
13
37
  ```
14
38
 
15
- The `unpack` command will take a branch in the Gitorial format, and unpack the steps of the gitorial into numbered folders in a separate branch.
39
+ Example: Convert an Gitorial repository from branch `gitorial` into branch `master` as an unpacked set of numbered steps in a folder named `steps`.
16
40
 
17
- `unpackedBranch` can be an existing branch, and a new commit will be added on top of the existing history.
41
+ ```sh
42
+ gitorial-cli unpack -p /path/to/project -i gitorial -o master -s steps
43
+ ```
18
44
 
19
- A `gitorial_metadata.json` file will be created in each folder, allowing you to update the `commitMessage` for that step when the Gitorial is repacked.
45
+ Output:
46
+
47
+ ```sh
48
+ # git branch: master
49
+ steps/
50
+ ├─ 0/
51
+ ├─ 1/
52
+ ├─ 2/
53
+ ├─ ...
54
+ ```
20
55
 
21
56
  ### repack
22
57
 
23
58
  ```sh
24
- yarn start repack <gitorialPath> <unpackedBranch> <repackBranch>
59
+ Usage: index repack [options]
60
+
61
+ Create a repacked Gitorial from an unpacked Gitorial. Must repack into a new branch.
62
+
63
+ Options:
64
+ -p, --path <path> The local path for the git repo containing the Gitorial.
65
+ -i, --inputBranch <inputBranch> The branch in the repo with the unpacked Gitorial.
66
+ -o, --outputBranch <outputBranch> The branch where you want to repack the Gitorial. Branch must not exist.
67
+ -s, --subFolder <subFolder> The subfolder (relative to the <path>) where you can find the unpacked Gitorial
68
+ --force Force the repack, even if it would replace an existing branch. WARNING: this can delete the branch history!
69
+ -h, --help display help for command
70
+ ```
71
+
72
+ Example: Convert an "unpacked" Gitorial on branch `master` in folder `steps` to a branch `gitorial`
73
+
74
+ ```sh
75
+ gitorial-cli repack -p /path/to/project -i master -s steps -o gitorial
25
76
  ```
26
77
 
27
- The `repack` command will take a branch which is in the `unpacked` format (numbered folders with the content of each step), and create a fresh git commit history with those steps as commits in the Gitorial.
78
+ ### mdBook
28
79
 
29
- `repackBranch` cannot be an existing branch name, as it risks deleting your Git history by mistake.
80
+ ```sh
81
+ Usage: index mdbook [options]
30
82
 
31
- The `gitorial_metadata.json` file will not be included in your repacked Gitorial.
83
+ Scaffold the contents of a Gitorial in a new branch in the mdBook source format. You need to initialize an mdBook yourself
32
84
 
33
- ## Workflow
85
+ Options:
86
+ -p, --path <path> The local path for the git repo containing the Gitorial.
87
+ -i, --inputBranch <inputBranch> The branch in the repo with the Gitorial.
88
+ -o, --outputBranch <outputBranch> The branch where you want your mdBook to live
89
+ -s, --subFolder <subFolder> The subfolder (relative to the <path>) where you want the mdBook source material to be placed. (default: "src")
90
+ -h, --help display help for command
91
+ ```
34
92
 
35
- The workflow for managing a gitorial is still in development, but assumes the following:
93
+ Example: Convert a Gitorial at branch `gitorial` to an [mdBook](https://rust-lang.github.io/mdBook/) rendered at branch `mdbook`.
36
94
 
37
- - You have a Git repo with a branch in the Gitorial format.
38
- - You can `unpack` that branch into a branch named `unpacked`.
39
- - You can keep this branch around forever, and always unpack into it. It can act as a history of changes to your Gitorial, since your raw Gitorial always resets its history.
40
- - You can then make changes to files in the `unpacked` branch. For example:
41
- - Creating new step folders.
42
- - Editing README or other documentation.
43
- - Editing code.
44
- - Although it is suggested to make code changes on the Gitorial branch, allowing you to use Git merge to ensure all changes are propagated through the tutorial.
45
- - You can commit changes or merge pull requests into the `unpacked` branch like normal.
46
- - When you are happy with the `unpacked` branch, you can `repack` it into a new branch `repacked`.
47
- - You can audit your changes in Git history (making sure step by step changes have a clean diff).
48
- - Finally, you can use `git reset repacked && git push --force` on your Gitorial branch to update your Gitorial.
95
+ ```sh
96
+ gitorial-cli mdbook -p /path/to/project -i gitorial -o mdbook
97
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gitorial-cli",
3
- "version": "0.1.0",
3
+ "version": "1.0.1",
4
4
  "description": "CLI Tools for Creating and Managing a Gitorial",
5
5
  "main": "src/index.js",
6
6
  "bin": "src/index.js",
@@ -20,5 +20,8 @@
20
20
  "dependencies": {
21
21
  "commander": "^12.0.0",
22
22
  "simple-git": "^3.24.0"
23
- }
23
+ },
24
+ "files": [
25
+ "src/*"
26
+ ]
24
27
  }
package/src/index.js CHANGED
@@ -27,8 +27,9 @@ program
27
27
  .requiredOption('-i, --inputBranch <inputBranch>', 'The branch in the repo with the unpacked Gitorial.')
28
28
  .requiredOption('-o, --outputBranch <outputBranch>', 'The branch where you want to repack the Gitorial. Branch must not exist.')
29
29
  .option('-s, --subFolder <subFolder>', 'The subfolder (relative to the <path>) where you can find the unpacked Gitorial')
30
- .action(({ path, inputBranch, outputBranch, subFolder }) => {
31
- require('./repack')(path, inputBranch, outputBranch, subFolder);
30
+ .option('--force', 'Force the repack, even if it would replace an existing branch. WARNING: this can delete the branch history!')
31
+ .action(({ path, inputBranch, outputBranch, subFolder, force }) => {
32
+ require('./repack')(path, inputBranch, outputBranch, subFolder, force);
32
33
  });
33
34
 
34
35
  // Command to scaffold an mdBook source from a Gitorial.
package/src/mdbook.js CHANGED
@@ -75,12 +75,15 @@ async function processGitorial(sourceDir, mdbookDir) {
75
75
  const commitHash = log.hash;
76
76
  const commitMessage = log.message;
77
77
 
78
+ // These are the possible gitorial commit types
78
79
  const isReadme = commitMessage.toLowerCase().startsWith("readme: ");
79
80
  const isTemplate = commitMessage.toLowerCase().startsWith("template: ");
80
81
  const isSolution = commitMessage.toLowerCase().startsWith("solution: ");
81
82
  const isSection = commitMessage.toLowerCase().startsWith("section: ");
82
83
  const isAction = commitMessage.toLowerCase().startsWith("action: ");
84
+ const isStartingTemplate = commitMessage.toLowerCase().startsWith("starting-template");
83
85
 
86
+ // A step may not increment with a new commit, for example a `template` and `solution` happen in one step.
84
87
  let stepFolder = path.join(mdbookDir, stepCounter.toString());
85
88
  if (!fs.existsSync(stepFolder)) {
86
89
  fs.mkdirSync(stepFolder);
@@ -93,6 +96,11 @@ async function processGitorial(sourceDir, mdbookDir) {
93
96
  // Default assumption is output is not a template or solution
94
97
  let outputFolder = sourceFolder;
95
98
 
99
+ // We skip the starting template commit since it is only used for starting the project.
100
+ if (isStartingTemplate) {
101
+ continue;
102
+ }
103
+
96
104
  if (isTemplate) {
97
105
  // Check there isn't a template already in queue
98
106
  if (templateFound) {
@@ -207,7 +215,7 @@ async function processGitorial(sourceDir, mdbookDir) {
207
215
  name: getStepName(templateFolder),
208
216
  is_section: false,
209
217
  });
210
- } else {
218
+ } else if (isAction) {
211
219
  markdownContent = sourceMarkdown;
212
220
  let sourceFileText = generateFileMarkdown("source", sourceFiles);
213
221
  markdownContent = markdownContent.replace(
@@ -225,6 +233,8 @@ async function processGitorial(sourceDir, mdbookDir) {
225
233
  name: getStepName(sourceFolder),
226
234
  is_section: false,
227
235
  });
236
+ } else {
237
+ console.error(`Unknown Gitorial Commit Type: ${commitMessage}`)
228
238
  }
229
239
  // Create a Markdown file in the commit folder
230
240
  const markdownFilePath = path.join(stepFolder, "README.md");
@@ -255,6 +265,11 @@ function generateFileMarkdown(type, files) {
255
265
  continue;
256
266
  }
257
267
 
268
+ // Skip all hidden folders, like `.gitorial`.
269
+ if (file.file.startsWith(".")) {
270
+ continue;
271
+ }
272
+
258
273
  let filepath = `./${type}/${file.file}`;
259
274
  let filename = path.parse(filepath).base;
260
275
 
package/src/repack.js CHANGED
@@ -5,10 +5,28 @@ const path = require('path');
5
5
  const { GITORIAL_METADATA } = require('./constants');
6
6
  const { copyAllContentsAndReplace } = require('./utils')
7
7
 
8
- async function repack(repoPath, inputBranch, outputBranch, subFolder) {
8
+ async function repack(repoPath, inputBranch, outputBranch, subFolder, force) {
9
9
  try {
10
10
  const git = simpleGit(repoPath);
11
- await git.raw(['switch', '--orphan', outputBranch]);
11
+
12
+ if (force) {
13
+ let saveBranch = `${outputBranch}-__gitorial-old`;
14
+ // Delete the existing `__gitorial-old` branch if it exists
15
+ try {
16
+ await git.raw(['branch', '-D', saveBranch]);
17
+ } catch (error) {
18
+ // Ignore the error if the branch does not exist
19
+ }
20
+ // Move the output branch to the save branch
21
+ try {
22
+ await git.branch(['-m', outputBranch, saveBranch]);
23
+ } catch (error) {
24
+ // Ignore the error if the branch does not exist
25
+ }
26
+ await git.raw(['switch', '--orphan', outputBranch]);
27
+ } else {
28
+ await git.raw(['switch', '--orphan', outputBranch]);
29
+ }
12
30
 
13
31
  const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gitorial-repack-'));
14
32
  await git.clone(repoPath, tempDir, ['--branch', inputBranch]);
@@ -44,8 +62,13 @@ async function repack(repoPath, inputBranch, outputBranch, subFolder) {
44
62
 
45
63
  // Create commit with commit message
46
64
  await git.commit(commitMessage);
47
-
48
65
  console.log(`Commit created for step ${step} with message: ${commitMessage}`);
66
+
67
+ // We want to tag the `starting-template` so it can be easily referenced.
68
+ if (commitMessage == 'starting-template') {
69
+ await git.raw(['tag', 'starting-template', '--force']);
70
+ console.log(`Added ${commitMessage} tag.`);
71
+ }
49
72
  }
50
73
 
51
74
  // Clean up temp folder