-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
WIP #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { flattenObject } from "flatten-anything"; | ||
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); | ||
}; | ||
} |
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, | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the main logic regarding |
||
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. | ||
|
@@ -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 }); | ||
} | ||
|
||
|
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, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: {}, | ||
}; | ||
|
@@ -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)) { | ||
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure all files are backed up first. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. must also include |
||
]); | ||
|
||
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, | ||
|
@@ -94,8 +107,8 @@ export async function runScenario( | |
} | ||
|
||
function applyScenarioPackageJson( | ||
packageJson: object, | ||
packageJsonScenario: object, | ||
packageJson: PackageJson, | ||
packageJsonScenario: PackageJson, | ||
) { | ||
const packageJsonScenarioFlat = flattenObject(packageJsonScenario); | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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:
Or something similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very good idea 👍