Skip to content

Commit

Permalink
fix(apps/cli): resolve issues with publish command, init command, and…
Browse files Browse the repository at this point in the history
… boilerplate files (#1418)
  • Loading branch information
amirabbas-gh authored Dec 19, 2024
1 parent 89f16e2 commit cc6f1bb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 291 deletions.
27 changes: 0 additions & 27 deletions apps/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ import {
allEnginesSchema,
backtickify,
doubleQuotify,
execPromise,
getCodemodProjectFiles,
isJavaScriptName,
isTypeScriptProjectFiles,
parseCodemodConfig,
} from "@codemod-com/utilities";
import { safeParse } from "valibot";
import { getCurrentUserData } from "#auth-utils.js";
import { oraCheckmark } from "#utils/constants.js";
import { detectCodemodEngine } from "#utils/detectCodemodEngine.js";
import { isFile } from "#utils/general.js";

Expand Down Expand Up @@ -201,30 +198,6 @@ export const handleInitCliCommand = async (options: {
chalk.cyan("Codemod package created at", `${chalk.bold(codemodBaseDir)}.`),
);

// Install packages
if (isTypeScriptProjectFiles(files)) {
const installSpinner = printer.withLoaderMessage(
chalk.cyan("Installing npm dependencies..."),
);

await execPromise("pnpm i", { cwd: codemodBaseDir }).catch(() =>
execPromise("npm i", { cwd: codemodBaseDir }).catch((err: Error) => {
installSpinner.fail();
printer.printConsoleMessage(
"error",
`Failed to install npm dependencies:\n${err.message}.`,
);
}),
);

if (installSpinner.isSpinning) {
installSpinner.stopAndPersist({
symbol: oraCheckmark,
text: chalk.green("Dependencies installed."),
});
}
}

const howToRunText = chalk(
`Run ${chalk.bold(doubleQuotify(`codemod --source ${codemodBaseDir}`))}`,
"to run the codemod on current working directory",
Expand Down
9 changes: 9 additions & 0 deletions apps/cli/src/commands/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ export const handlePublishCliCommand = async (options: {
source,
esm,
codemodRc.engine,
false,
).catch(() => null);

if (builtExecutable === null) {
Expand All @@ -310,6 +311,14 @@ export const handlePublishCliCommand = async (options: {
);
}

const cdmdDistPath = join(source, "cdmd_dist");
await fs.promises.mkdir(cdmdDistPath, { recursive: true });
await fs.promises.writeFile(
join(cdmdDistPath, "index.cjs"),
builtExecutable,
"utf8",
);

codemodFileBuffers.push({
name: DEFAULT_BUILD_PATH,
data: Buffer.from(builtExecutable),
Expand Down
21 changes: 12 additions & 9 deletions packages/runner/src/source-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,20 @@ export const getCodemodExecutable = async (
source: string,
esm?: boolean,
engine?: string,
shouldCheckForExisting = true,
) => {
const existing = await glob(BUILT_SOURCE_GLOB, {
cwd: source,
absolute: true,
});

if (existing.length > 0) {
// biome-ignore lint: it exists
return await readFile(existing[0]!, { encoding: "utf8" }).catch(() => {
throw new Error(`Could not read ${existing[0]}`);
if (shouldCheckForExisting) {
const existing = await glob(BUILT_SOURCE_GLOB, {
cwd: source,
absolute: true,
});

if (existing.length > 0) {
// biome-ignore lint: it exists
return await readFile(existing[0]!, { encoding: "utf8" }).catch(() => {
throw new Error(`Could not read ${existing[0]}`);
});
}
}

const { path: entryPoint } = await getEntryPath({
Expand Down
Loading

0 comments on commit cc6f1bb

Please sign in to comment.