Skip to content
Open
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
2 changes: 1 addition & 1 deletion ark/attest/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const runThenGetContents = (templatePath: string): string => {
const tempPath = templatePath + ".temp.ts"
copyFileSync(templatePath, tempPath)
try {
shell(`node --import=tsx ${tempPath}`, {
shell("node", ["--import=tsx", tempPath], {
cwd: dirName(),
env: {
ATTEST_failOnMissingSnapshots: "0"
Expand Down
7 changes: 6 additions & 1 deletion ark/attest/cache/snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ const runFormatterIfAvailable = (queuedUpdates: QueuedUpdate[]) => {
const { formatCmd: formatter, shouldFormat } = getConfig()
if (!shouldFormat) return

if (formatter.length === 0)
throw new Error("config formatCmd must be at least length 1")

try {
const updatedPaths = [
...new Set(
Expand All @@ -206,7 +209,9 @@ const runFormatterIfAvailable = (queuedUpdates: QueuedUpdate[]) => {
)
)
]
shell(`${formatter} ${updatedPaths.join(" ")}`)
const command = formatter[0]
const args = formatter.slice(1)
shell(command, [...args, "--", ...updatedPaths])
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The extra -- is to prevent a filename named like --help from being interpreted as a flag. Arguably needs to be applied elsewhere but it gets increasingly difficult elsewhere since shell commands rarely are prepared to actually deal with dashes at the beginning of filenames in arbitrary locations.

} catch {
// If formatter is unavailable or skipped, do nothing.
}
Expand Down
3 changes: 2 additions & 1 deletion ark/attest/cli/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ const generateTraceData = (
): string => {
try {
const output = getShellOutput(
`${baseDiagnosticTscCmd} --project ${tsconfigPath} --generateTrace ${traceDir}`,
baseDiagnosticTscCmd,
["--project", tsconfigPath, "--generateTrace", traceDir],
{ cwd: packageDir }
)
process.stdout.write(output) // Display tsc output directly
Expand Down
4 changes: 2 additions & 2 deletions ark/attest/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type BaseAttestConfig = {
benchErrorOnThresholdExceeded: BenchErrorConfig
filter: string | undefined
testDeclarationAliases: string[]
formatCmd: string
formatCmd: string[]
shouldFormat: boolean
/**
* Provided options will override the following defaults.
Expand Down Expand Up @@ -75,7 +75,7 @@ export const getDefaultAttestConfig = (): BaseAttestConfig => ({
benchErrorOnThresholdExceeded: true,
filter: undefined,
testDeclarationAliases: ["bench", "it", "test"],
formatCmd: `npm exec --no -- prettier --write`,
formatCmd: ["npm", "exec", "--no", "--", "prettier", "--write"],
shouldFormat: true,
typeToStringFormat: {}
})
Expand Down
4 changes: 2 additions & 2 deletions ark/attest/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export const setup = (options?: Partial<AttestConfig>): typeof teardown => {
)
// if we're in our own repo, we need to pnpm to use the root script to execute ts directly
if (fileName().endsWith("ts"))
shell(`pnpm attest precache ${precachePath}`)
shell("pnpm", ["attest", "precache", precachePath])
// otherwise, just use npm to run the CLI command from build output
else shell(`npm exec -c "attest precache ${precachePath}"`)
else shell("npm", ["exec", "-c", "attest", "precache", precachePath])
})
}
return teardown
Expand Down
7 changes: 6 additions & 1 deletion ark/fs/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ export const readPackageJson = (startDir = dirOfCaller()): any =>

export const getSourceControlPaths = (): string[] =>
// include tracked and untracked files as long as they are not ignored
getShellOutput("git ls-files --exclude-standard --cached --others")
getShellOutput("git", [
"ls-files",
"--exclude-standard",
"--cached",
"--others"
])
.split("\n")
.filter(path => existsSync(path) && statSync(path).isFile())

Expand Down
19 changes: 13 additions & 6 deletions ark/fs/shell.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
import { execSync, type ExecSyncOptions } from "node:child_process"
import { spawnSync, type SpawnSyncOptions } from "node:child_process"
import * as process from "node:process"

export type ShellOptions = Omit<ExecSyncOptions, "stdio"> & {
export type ShellOptions = Omit<SpawnSyncOptions, "stdio"> & {
env?: Record<string, string | undefined>
}

/** Run the cmd synchronously. Output goes to terminal. */
export const shell = (
cmd: string,
args: string[],
{ env, ...otherOptions }: ShellOptions = {}
): void => {
execSync(cmd, {
const result = spawnSync(cmd, args, {
env: { ...process.env, ...env },
...otherOptions,
stdio: "inherit"
})
if (result.error) throw result.error
}

/** Run the cmd synchronously, returning output as a string */
export const getShellOutput = (
cmd: string,
args: string[],
{ env, ...otherOptions }: ShellOptions = {}
): string =>
execSync(cmd, {
): string => {
const result = spawnSync(cmd, args, {
env: { ...process.env, ...env },
...otherOptions,
stdio: "pipe"
})!.toString()
})
if (result.error) throw result.error

return result.toString()
}
8 changes: 5 additions & 3 deletions ark/repo/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ const outDir = fromCwd("out")
const packageName = readPackageJson(process.cwd()).name

const buildCurrentProject = () =>
shell(
`node ${fromHere("node_modules", "typescript", "lib", "tsc.js")} --project tsconfig.build.json`
)
shell("node", [
fromHere("node_modules", "typescript", "lib", "tsc.js"),
"--project",
"tsconfig.build.json"
])

try {
rmRf(outDir)
Expand Down
11 changes: 10 additions & 1 deletion ark/repo/dtsGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ export const dtsGen = () => {

console.log(`✍️ Generating DTS bundle for ${pkg.name}...`)

shell("pnpm tsup index.ts --dts-only --dts-resolve --format esm --out-dir .")
shell("pnpm", [
"tsup",
"index.ts",
"--dts-only",
"--dts-resolve",
"--format",
"esm",
"--out-dir",
"."
])

const expectedDtsBundlePath = join(pkg.path, "index.d.ts")

Expand Down
10 changes: 5 additions & 5 deletions ark/repo/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { packages, type ArkPackage } from "./shared.ts"

const tagsToPublish: string[] = []

const existingTags = getShellOutput("git tag").split("\n")
const existingTags = getShellOutput("git", ["tag"]).split("\n")

const publishPackage = (pkg: ArkPackage, alias?: string) => {
const tagName = `${alias ?? pkg.name}@${pkg.version}`

if (!existingTags.includes(tagName)) {
if (alias) rewritePackageJsonName(pkg.packageJsonPath, alias)

shell(`git tag ${tagName}`)
shell("git", ["tag", tagName])
tagsToPublish.push(tagName)
shell("pnpm publish --no-git-checks", { cwd: pkg.path })
shell("pnpm", ["publish", "--no-git-checks"], { cwd: pkg.path })

if (alias) rewritePackageJsonName(pkg.packageJsonPath, pkg.name)
}
Expand All @@ -34,7 +34,7 @@ for (const pkg of packages) {
}
}

shell("git push --tags")
shell("git", ["push", "--tags"])

for (const tagName of tagsToPublish)
shell(`gh release create ${tagName} --latest`)
shell("gh", ["release", "create", tagName, "--latest"])
11 changes: 6 additions & 5 deletions ark/repo/testPackage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { fromHere, shell } from "@ark/fs"

shell(
`pnpm mocha --config ${fromHere("mocha.package.jsonc")} ${process.argv
.slice(2)
.join(" ")}`
)
shell("pnpm", [
"mocha",
"--config",
fromHere("mocha.package.jsonc"),
process.argv.slice(2).join(" ")
])
Loading