Skip to content

WIP #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ export async function readConfig(cwd: string, _configPath?: string) {
);

configFactory = _configFactory;
} catch {
} catch (cause) {
throw new PrettyError(
`Could not find or import config file \`${configPath}\`.`,
{ cause },
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ember-try.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function isEmberTryScenario(scenario: any) {
/**
* Transform an `ember-try` scenario to a `try-or-die` scenario.
*/
export function transformEmberTryScenario(scenario: any): Scenario {
export function transformEmberTryScenario(scenario: any): Partial<Scenario> {
return {
env: scenario.env,
name: scenario.name,
Expand Down
22 changes: 22 additions & 0 deletions src/file-handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { flattenObject } from "flatten-anything";
Copy link
Owner Author

@bertdeblock bertdeblock Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An operation that allows you to modify the contents of a JSON file more easily would also be useful I think:

files: {
  'package.json': fileOperations.modifyJson((pkg) => {
    delete pkg.foo.bar;

    return pkg;
  }),
},

Or something similar.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very good idea 👍

import set from "lodash.set";
import { FileHandler } from "./types.js";

export function mergeJson(object: any): FileHandler {
return (content) => {
const target = JSON.parse(content);
const objectFlat = flattenObject(object);

Object.keys(objectFlat).forEach((key) => {
set(target, key, objectFlat[key]);
});

return JSON.stringify(target);
};
}

export function writeJson(object: any): FileHandler {
return () => {
return JSON.stringify(object);
};
}
29 changes: 28 additions & 1 deletion src/files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createHash } from "node:crypto";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import {
copy,
Expand All @@ -8,6 +9,32 @@ import {
writeJson,
} from "fs-extra/esm";
import tempDir from "temp-dir";
import { PackageJson } from "type-fest";
import { Scenario } from "./types.js";

/**
* Apply a scenario's files.
*/
export function applyScenarioFiles(
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main logic regarding files.

cwd: string,
files: Scenario["files"],
): Promise<void[]> {
return Promise.all(
Object.keys(files).map(async (file) => {
const path = join(cwd, file);
const newContent = files[file];

if (typeof newContent === "string") {
await writeFile(path, newContent);
} else if (typeof newContent === "function") {
await writeFile(
path,
await newContent(await readFile(path, { encoding: "utf-8" })),
);
}
}),
);
}

/**
* Back up multiple files.
Expand Down Expand Up @@ -46,7 +73,7 @@ export function readPackageFile(cwd: string) {
/**
* Write a `package.json` file.
*/
export function writePackageFile(cwd: string, packageJson: object) {
export function writePackageFile(cwd: string, packageJson: PackageJson) {
return writeJson(join(cwd, "package.json"), packageJson, { spaces: 2 });
}

Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { mergeJson, writeJson } from "./file-handlers.js";

export const fileHandlers = {
mergeJson,
writeJson,
};
31 changes: 22 additions & 9 deletions src/run-scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ import { execa } from "execa";
import { flattenObject } from "flatten-anything";
import set from "lodash.set";
import { stderr, stdout } from "node:process";
import { PackageJson } from "type-fest";
import { debugFactory } from "./debug-factory.js";
import { isEmberTryScenario, transformEmberTryScenario } from "./ember-try.js";
import { applyScenarioEnv } from "./env.js";
import { PrettyError } from "./errors.js";
import { backUpFiles, readPackageFile, writePackageFile } from "./files.js";
import {
applyScenarioFiles,
backUpFiles,
readPackageFile,
writePackageFile,
} from "./files.js";
import { PACKAGE_MANAGER_LOCKFILE } from "./package-managers.js";
import { type Command, type Config, type Scenario } from "./types.js";
import { printBanner, printInfo, printSuccess } from "./ui.js";

const DEFAULT_SCENARIO = {
const DEFAULT_SCENARIO: Scenario = {
env: {},
files: {},
name: "default",
packageJson: {},
};
Expand All @@ -28,7 +35,7 @@ export async function runScenario(
config: Config,
{ restore = true } = {},
) {
const scenario = { ...DEFAULT_SCENARIO };
const scenario: Scenario = { ...DEFAULT_SCENARIO };
const scenarioStartTime = Date.now();

if (isEmberTryScenario(providedScenario)) {
Expand All @@ -43,18 +50,24 @@ export async function runScenario(
printBanner(`START ${scenario.name}`);

const lockfile = PACKAGE_MANAGER_LOCKFILE[config.packageManager];
const packageJson = await readPackageFile(cwd);
// const packageJson = await readPackageFile(cwd);

const restoreProcessEnv = applyScenarioEnv(scenario.env);
const restoreFiles = await backUpFiles(cwd, ["package.json", lockfile]);
const restoreFiles = await backUpFiles(cwd, [
// "package.json",
lockfile,
...Object.keys(scenario.files),
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure all files are backed up first.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must also include package.json when config.packageJson is present

]);

const restoreState = async () => {
restoreProcessEnv();
await restoreFiles();
};

applyScenarioPackageJson(packageJson, scenario.packageJson);
// applyScenarioPackageJson(packageJson, scenario.packageJson);

await writePackageFile(cwd, packageJson);
await applyScenarioFiles(cwd, scenario.files);
// await writePackageFile(cwd, packageJson);
await runCommand({
command: [
config.packageManager,
Expand Down Expand Up @@ -94,8 +107,8 @@ export async function runScenario(
}

function applyScenarioPackageJson(
packageJson: object,
packageJsonScenario: object,
packageJson: PackageJson,
packageJsonScenario: PackageJson,
) {
const packageJsonScenarioFlat = flattenObject(packageJsonScenario);

Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ export interface PrintOptions {
prefix?: string;
}

export type FileHandler = (content: string) => string | Promise<string>;

export interface Scenario {
env: Record<string, any>;
files: { [filePath: string]: string | FileHandler };
name: string;
npm?: Record<string, any>;
packageJson: PackageJson;
Expand Down
120 changes: 69 additions & 51 deletions test-app/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading