Skip to content

Commit

Permalink
fix(cli): support for Windows [publish] (#1237)
Browse files Browse the repository at this point in the history
  • Loading branch information
r4zendev authored Aug 15, 2024
1 parent e96a4d4 commit ceea74c
Showing 1 changed file with 66 additions and 51 deletions.
117 changes: 66 additions & 51 deletions apps/cli/src/commands/publish.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spawn } from "node:child_process";
import * as fs from "node:fs";
import { join } from "node:path";
import { join, sep } from "node:path";
import { glob } from "glob";
import inquirer from "inquirer";
import * as semver from "semver";
Expand Down Expand Up @@ -150,58 +150,67 @@ export const handlePublishCliCommand = async (options: {
});
}

if (!codemodRc.meta?.git) {
const { gitUrl } = await inquirer.prompt<{
gitUrl: string;
}>({
type: "input",
name: "gitUrl",
suffix: " (leave empty if none)",
message:
"Enter the URL of the git repository where this codemod is located.",
validate: (input) => {
const stringParsingResult = v.safeParse(v.string(), input);
if (stringParsingResult.success === false) {
return stringParsingResult.issues[0].message;
}

const stringInput = stringParsingResult.output;
if (stringInput.length === 0) {
return true;
}

const urlParsingResult = v.safeParse(
v.pipe(v.string(), v.url()),
stringInput,
);
if (urlParsingResult.success === false) {
return urlParsingResult.issues[0].message;
}

return true;
},
});
let gitUrl = codemodRc.meta?.git ?? null;
if (gitUrl === null) {
const repoGitUrl = await execPromise("git config --get remote.origin.url", {
cwd: source,
}).catch(() => null);

if (gitUrl) {
try {
await execPromise("git init", { cwd: source });
if (repoGitUrl !== null) {
const url = repoGitUrl.stdout.trim();

await execPromise(`git remote add origin ${gitUrl}`, {
cwd: source,
});
gitUrl = url.startsWith("git@")
? `https://${url.slice(4).replace(":", "/")}`
: url;
} else {
const { gitUrl: userAnsweredGitUrl } = await inquirer.prompt<{
gitUrl: string;
}>({
type: "input",
name: "gitUrl",
suffix: " (leave empty if none)",
message:
"Enter the URL of the git repository where this codemod is located.",
validate: (input) => {
const stringParsingResult = v.safeParse(v.string(), input);
if (stringParsingResult.success === false) {
return stringParsingResult.issues[0].message;
}

codemodRc.meta = { tags: [], ...codemodRc.meta, git: gitUrl };
const stringInput = stringParsingResult.output;
if (stringInput.length === 0) {
return true;
}

await updateCodemodRC(codemodRc);
} catch (err) {
printer.printConsoleMessage(
"error",
`Failed to initialize a git package with provided repository link:\n${
(err as Error).message
}. Setting it to null...`,
);
const urlParsingResult = v.safeParse(
v.pipe(v.string(), v.url()),
stringInput,
);
if (urlParsingResult.success === false) {
return urlParsingResult.issues[0].message;
}

return true;
},
});

if (userAnsweredGitUrl?.length > 0) {
gitUrl = userAnsweredGitUrl;

try {
await execPromise("git init", { cwd: source });
await execPromise(`git remote add origin ${userAnsweredGitUrl}`, {
cwd: source,
});
} catch (err) {}
}
}

// If it was null but we changed it, we need to update the RC file
if (gitUrl !== null) {
codemodRc.meta = { tags: [], ...codemodRc.meta, git: gitUrl };
await updateCodemodRC(codemodRc);
}
}

if (!codemodRc.meta?.tags || codemodRc.meta.tags.length === 0) {
Expand Down Expand Up @@ -238,10 +247,16 @@ export const handlePublishCliCommand = async (options: {
});

const codemodFileBuffers = await Promise.all(
codemodFilePaths.map(async (path) => ({
name: path.replace(new RegExp(`.*${source}/`), ""),
data: await fs.promises.readFile(path),
})),
codemodFilePaths.map(async (path) => {
const searchTerm = `${source}${sep}`;

return {
name: path
.slice(path.indexOf(searchTerm) + searchTerm.length)
.replace(/\\/g, "/"),
data: await fs.promises.readFile(path),
};
}),
);

if (codemodRc.engine !== "recipe") {
Expand Down

0 comments on commit ceea74c

Please sign in to comment.