8000 feat: display warning if dirty git workspace by manuel3108 · Pull Request #373 · svelte-add/svelte-add · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

feat: display warning if dirty git workspace #373

Merged
merged 2 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-pens-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@svelte-add/core": minor
---

feat: display warning if dirty git workspace
3 changes: 2 additions & 1 deletion packages/core/adder/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ async function executePlan<Args extends OptionDefinition>(
adderDetails = adderDetails.filter((x) => userSelectedAdders.includes(x.config.metadata.id));

// preconditions
if (!executionPlan.commonCliOptions.skipPreconditions) await validatePreconditions(adderDetails, isTesting);
if (!executionPlan.commonCliOptions.skipPreconditions)
await validatePreconditions(adderDetails, executingAdder.name, executionPlan.workingDirectory, isTesting);

// ask the user questions about unselected options
await requestMissingOptionsFromUser(adderDetails, executionPlan);
Expand Down
61 changes: 56 additions & 5 deletions packages/core/adder/preconditions.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,71 @@
import { config } from "process";
import { booleanPrompt, endPrompts, messagePrompt } from "../utils/prompts.js";
import { AdderDetails } from "./execute.js";
import { OptionDefinition } from "./options.js";
import pc from "picocolors";
import { Precondition } from "./config.js";
import { executeCli } from "../utils/common.js";

function getGlobalPreconditions(
executingCli: string,
workingDirectory: string,
): { name: string; preconditions: Precondition[] | undefined } {
return {
name: executingCli,
preconditions: [
{
name: "clean working directory",
run: async () => {
let outputText = "";

try {
// If a user has pending git changes the output of the following command will list
// all files that have been added/modified/deleted and thus the output will not be empty.
// In case the output of the command below is an empty text, we can safely assume
// there are no pending changes. If the below command is run outside of a git repository,
// git will exit with a failing exit code, which will trigger the catch statement.
// also see https://remarkablemark.org/blog/2017/10/12/check-git-dirty/#git-status
await executeCli("git", ["status", "--short"], workingDirectory, {
onData: (data, program, resolve) => {
outputText += data;
},
});

if (outputText) {
return { success: false, message: "Found modified files" };
}

return { success: true, message: undefined };
} catch (error) {
return { success: false, message: "Not a git repository" };
}
},
},
],
};
}

export async function validatePreconditions<Args extends OptionDefinition>(
adderDetails: AdderDetails<Args>[],
executingCliName: string,
workingDirectory: string,
isTesting: boolean,
) {
const multipleAdders = adderDetails.length > 1;
let allPreconditionsPassed = true;
const preconditionLog: string[] = [];
for (const { config, checks } of adderDetails) {
if (!checks.preconditions) continue;

for (const precondition of checks.preconditions) {
const adderPreconditions = adderDetails.map(({ config, checks }) => {
return {
name: config.metadata.name,
preconditions: checks.preconditions,
};
});
const combinedPreconditions = [getGlobalPreconditions(executingCliName, workingDirectory), ...adderPreconditions];

for (const { name, preconditions } of combinedPreconditions) {
if (!preconditions) continue;

for (const precondition of preconditions) {
let message;
let preconditionPassed;
try {
Expand All @@ -33,7 +84,7 @@ export async function validatePreconditions<Args extends OptionDefinition>(
}

if (multipleAdders) {
message = `${config.metadata.name}: ${message}`;
message = `${name}: ${message}`;
}

message = preconditionPassed ? pc.green(message) : pc.yellow(message);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function executeCli(
commandArgs: string[],
cwd: string,
options?: {
onData?: (data: string, program: ChildProcess, resolve: (value: any) => any) => void;
onData?: (data: string, program: ChildProcess, resolve: (value?: any) => any) => void;
stdio?: "pipe" | "inherit";
env?: Record<string, string>;
},
Expand Down
0