-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ember-learn/refactor-reuse
split out functions that can be reused in multiple commands
- Loading branch information
Showing
6 changed files
with
116 additions
and
104 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pnpm-lock.yaml | ||
pnpm-lock.yaml | ||
node_modules | ||
CHANGELOG.md |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { execaCommand } from 'execa'; | ||
|
||
/** | ||
* | ||
* @param {string} command | ||
* @param {boolean} dryRun | ||
*/ | ||
export function dryExeca(command, dryRun = true) { | ||
if (dryRun) { | ||
console.log(`🌵 Dry run: '${command}'`); | ||
} else { | ||
console.log(`🤖 Running command '${command}'`); | ||
return execaCommand(command, { | ||
preferLocal: true, | ||
stdout: 'inherit', | ||
stdin: 'inherit', | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { writeFile } from 'node:fs/promises'; | ||
/** | ||
* | ||
* @param {string} file | ||
* @param {string} contents | ||
* @param {boolean} dryRun | ||
*/ | ||
export default function dryWrite(file, contents, dryRun = true) { | ||
if (dryRun) { | ||
console.log(`🌵 Dry run: Updating the contents of '${file}' to be the following: | ||
${contents}`); | ||
} else { | ||
return writeFile(file, contents, 'utf-8'); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { execa } from 'execa'; | ||
|
||
import { fatalError, automated } from './log.js'; | ||
import { dryExeca } from './dry-execa.js'; | ||
|
||
export default async function ensureRepo(repo, branch, dryRun) { | ||
let stdout; | ||
|
||
try { | ||
let result = await execa`git remote get-url origin`; | ||
stdout = result.stdout; | ||
} catch (err) { | ||
fatalError( | ||
`Error checking current remote: [${err.message}]. Make sure you are in the cloned folder for ${repo}`, | ||
); | ||
} | ||
|
||
if (repo !== stdout) { | ||
fatalError( | ||
`It does not look like you are in the repo ${repo}. You can verify that you are by running 'git remote get-url origin'`, | ||
); | ||
} | ||
|
||
let { stdout: cleanDir } = await execa`git status --porcelain`; | ||
|
||
if (cleanDir.length) { | ||
fatalError(`Make sure you are in a clean working directory. You can verify this by making sure 'git status --porcelain' returns nothign. | ||
Current response: | ||
${cleanDir}`); | ||
} | ||
|
||
let { stdout: currentBranch } = await execa`git rev-parse --abbrev-ref HEAD`; | ||
if (currentBranch !== branch) { | ||
fatalError( | ||
`Make sure you are on the '${branch}' branch. You are currently on '${currentBranch}'`, | ||
); | ||
} | ||
|
||
automated('Pulling latest changes from origin'); | ||
await dryExeca('git pull', dryRun); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as readline from 'node:readline/promises'; | ||
import { stdin as input, stdout as output } from 'node:process'; | ||
|
||
/** | ||
* | ||
* @param {string} description | ||
*/ | ||
export function automated(description) { | ||
console.log(`🤖 ${description}`); | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} error | ||
*/ | ||
export function fatalError(error) { | ||
console.error(error); | ||
process.exit(1); | ||
} | ||
|
||
export async function manual(description) { | ||
const rl = readline.createInterface({ input, output }); | ||
await rl.question(`🧑💻 ${description} | ||
Press enter to continue...`); | ||
rl.close(); | ||
} |