Skip to content

Commit

Permalink
refactor: remove cd command
Browse files Browse the repository at this point in the history
  • Loading branch information
Zamiell committed Nov 2, 2023
1 parent 46bfae8 commit 3a45eae
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 50 deletions.
25 changes: 10 additions & 15 deletions packages/docs/scripts/deploy.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
$s,
$op,
appendFile,
cd,
cp,
dirName,
echo,
Expand Down Expand Up @@ -50,8 +49,7 @@ rm(DOCS_REPO);
cp(BUILD_DIRECTORY_PATH, DOCS_REPO);
mv(DOCS_REPO_GIT_BACKUP, DOCS_REPO_GIT);

cd(DOCS_REPO);
if (isGitRepositoryClean()) {
if (isGitRepositoryClean(DOCS_REPO)) {
echo("There are no documentation website changes to deploy.");
exit();
}
Expand All @@ -60,32 +58,29 @@ if (isGitRepositoryClean()) {
// that another commit has been pushed in the meantime, in which case we should do nothing and wait
// for the CI on that commit to finish.)
// https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
cd(REPO_ROOT);
if (!isGitRepositoryLatestCommit()) {
if (!isGitRepositoryLatestCommit(REPO_ROOT)) {
echo(
"A more recent commit was found in the repository; skipping website deployment.",
);
exit();
}

echo(`Deploying changes to the documentation website: ${DOCS_REPO_NAME}`);
cd(DOCS_REPO);
$s`git config --global user.email "github-actions@users.noreply.github.com"`;
$s`git config --global user.name "github-actions"`;
const $$ = $op({ cwd: DOCS_REPO });
$$.sync`git config --global user.email "github-actions@users.noreply.github.com"`;
$$.sync`git config --global user.name "github-actions"`;
// We overwrite the previous commit instead of adding a new one in order to keep the size of the
// repository as small as possible. This speeds up deployment because with thousands of commits, it
// takes a very long time to clone.
$s`git add --all`;
$s`git commit --message deploy --amend`;
$s`git push --force-with-lease`;

cd(REPO_ROOT);
$$.sync`git add --all`;
$$.sync`git commit --message deploy --amend`;
$$.sync`git push --force-with-lease`;

// Wait for the website to be be live (which usually takes around 5 minutes).
const shortCommitSHA1 = commitSHA1.slice(0, 7);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
while (true) {
if (!isGitRepositoryLatestCommit()) {
if (!isGitRepositoryLatestCommit(REPO_ROOT)) {
echo(
"A more recent commit was found in the repository; skipping website scraping.",
);
Expand Down
4 changes: 1 addition & 3 deletions packages/docs/scripts/fixIsaacScriptCommon.mts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ custom_edit_url: null

import { globSync } from "glob";
import {
cd,
deleteFileOrDirectory,
dirName,
echo,
Expand Down Expand Up @@ -431,8 +430,7 @@ function isValidDuplicate(fileName: string, directoryName: string) {

/** Because we manually moved files around, internal links generated by TypeDoc will break. */
function fixLinks() {
cd(PACKAGE_DOCS_DIR);
const markdownFilePaths = globSync("**/*.md");
const markdownFilePaths = globSync("**/*.md", { cwd: PACKAGE_DOCS_DIR });

for (const filePath of markdownFilePaths) {
const fileContents = readFile(filePath);
Expand Down
6 changes: 3 additions & 3 deletions packages/isaacscript-cli/src/commands/publish/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import {
isGitRepositoryClean,
isLoggedInToNPM,
} from "isaacscript-common-node";
import { PROJECT_NAME } from "../../constants.js";
import { CWD, PROJECT_NAME } from "../../constants.js";
import { execPowershell } from "../../exec.js";

export function validate(
typeScript: boolean,
setVersion: string | undefined,
verbose: boolean,
): void {
if (!isGitRepository()) {
if (!isGitRepository(CWD)) {
fatalError(
"Failed to publish since the current working directory is not inside of a git repository.",
);
}

if (!isGitRepositoryClean()) {
if (!isGitRepositoryClean(CWD)) {
fatalError(
"Failed to publish since the Git repository was dirty. Before publishing, you must push any current changes to git. (Version commits should not contain any code changes.)",
);
Expand Down
25 changes: 16 additions & 9 deletions packages/isaacscript-common-node/src/functions/git.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { $o, $s } from "./execa.js";
import { $op } from "./execa.js";

export function isGitRepository(): boolean {
const returnBase = $s`git rev-parse --is-inside-work-tree`;
export function isGitRepository(gitRepositoryDirectoryPath: string): boolean {
const $$ = $op({ cwd: gitRepositoryDirectoryPath });
const returnBase = $$.sync`git rev-parse --is-inside-work-tree`;
return returnBase.exitCode === 0;
}

export function isGitRepositoryClean(): boolean {
const gitStatus = $o`git status --porcelain`;
export function isGitRepositoryClean(
gitRepositoryDirectoryPath: string,
): boolean {
const $$ = $op({ cwd: gitRepositoryDirectoryPath });
const gitStatus = $$.sync`git status --porcelain`.stdout;
return gitStatus === "";
}

export function isGitRepositoryLatestCommit(): boolean {
$s`git fetch`;
export function isGitRepositoryLatestCommit(
gitRepositoryDirectoryPath: string,
): boolean {
const $$ = $op({ cwd: gitRepositoryDirectoryPath });
$$.sync`git fetch`;

const output1 = $o`git rev-parse HEAD`;
const output2 = $o`git rev-parse @{u}`;
const output1 = $$.sync`git rev-parse HEAD`.stdout;
const output2 = $$.sync`git rev-parse @{u}`.stdout;

return output1 === output2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function script(
const tsConfigJSONPath = path.join(packageRoot, "tsconfig.json");
const outDir = getTSConfigJSONOutDir(tsConfigJSONPath);

cd(packageRoot);
process.chdir(packageRoot);

const startTime = Date.now();
const data = { packageRoot, outDir };
Expand All @@ -107,11 +107,6 @@ export async function script(
}
}

/** An alias for "process.chdir". */
export function cd(directoryPath: string): void {
process.chdir(directoryPath);
}

/**
* An alias for "console.log".
*
Expand Down
2 changes: 1 addition & 1 deletion packages/isaacscript-tsconfig/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "isaacscript-tsconfig",
"version": "4.1.1",
"version": "5.0.0",
"description": "A sharable TypeScript config for TypeScript and IsaacScript projects.",
"keywords": [
"isaacscript",
Expand Down
6 changes: 2 additions & 4 deletions scripts/packageJSONLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { globSync } from "glob";
import {
PACKAGE_JSON,
cd,
dirName,
echo,
exit,
Expand All @@ -25,15 +24,14 @@ main();
function main() {
echo('Checking "package.json" files...');

cd(REPO_ROOT);

if (!packageJSONLint(REPO_ROOT_PACKAGE_JSON_PATH, undefined)) {
exit(1);
}
const rootDeps = getDeps(REPO_ROOT_PACKAGE_JSON_PATH);

const packageJSONPaths = globSync(`./packages/**/${PACKAGE_JSON}`, {
ignore: ["node_modules/**", "dist/**"],
ignore: ["**/dist/**", "node_modules/**"],
cwd: REPO_ROOT,
});

checkRootDepsUpToDate(rootDeps, packageJSONPaths);
Expand Down
19 changes: 10 additions & 9 deletions scripts/publish.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import chalk from "chalk";
import {
$,
$o,
$op,
$s,
cd,
PACKAGE_JSON,
echo,
exit,
fatalError,
Expand Down Expand Up @@ -72,7 +72,7 @@ await script(async ({ packageRoot }) => {
exit(1);
}

cd(packageDir);
const $$ = $op({ cwd: packageDir });

// Before bumping the version, check to see if this package compiles and lints and tests (so that
// we can avoid unnecessary version bumps).
Expand All @@ -83,7 +83,7 @@ await script(async ({ packageRoot }) => {
for (const scriptName of ["build", "lint", "test"]) {
const scriptCommand = scripts[scriptName];
if (typeof scriptCommand === "string") {
promises.push($`npm run ${scriptName}`);
promises.push($$`npm run ${scriptName}`);
}
}

Expand All @@ -100,13 +100,14 @@ await script(async ({ packageRoot }) => {
* Thus, we manually revert to doing a commit ourselves.
*/
if (versionBump === VersionBump.dev) {
$s`npm version prerelease --preid=dev --commit-hooks=false`;
$$.sync`npm version prerelease --preid=dev --commit-hooks=false`;
} else {
$s`npm version ${versionBump} --commit-hooks=false`;
$$.sync`npm version ${versionBump} --commit-hooks=false`;
}

// Manually make a Git commit. (See above comment.)
$s`git add "${packageDir}/package.json"`;
const packageJSONPath = path.join(packageDir, PACKAGE_JSON);
$s`git add ${packageJSONPath}`;
const newVersion = getPackageJSONVersion(packageDir);
const tag = `${packageName}-${newVersion}`;
const commitMessage = `chore(release): ${tag}`;
Expand All @@ -118,12 +119,12 @@ await script(async ({ packageRoot }) => {
const npmTag = versionBump === VersionBump.dev ? "next" : "latest";
// The "--access=public" flag is only technically needed for the first publish, but it is saved
// here for posterity.
$s`npm publish --access=public --tag=${npmTag}`; // --otp=${otpCode}
$$.sync`npm publish --access=public --tag=${npmTag}`;

// Finally, check for dependency updates to ensure that we keep the monorepo up to date.
await updateIsaacScriptMonorepo();

if (!isGitRepositoryClean()) {
if (!isGitRepositoryClean(packageRoot)) {
const gitCommitMessage = "chore: updating dependencies";
$s`git add --all`;
$s`git commit --message ${gitCommitMessage}`;
Expand Down

0 comments on commit 3a45eae

Please sign in to comment.