-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a355584
commit c44c1fd
Showing
17 changed files
with
270 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
# Change versionKind to one of: breaking, feature, fix, internal | ||
changeKind: feature | ||
packages: | ||
- "@chronus/chronus" | ||
--- | ||
|
||
Add `chronus pack` command that will pack all packages that need publishing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import pc from "picocolors"; | ||
import { NodeChronusHost, loadChronusWorkspace } from "../../index.js"; | ||
import { packPackage } from "../../pack/index.js"; | ||
import type { Reporter } from "../../reporters/index.js"; | ||
import { prettyBytes } from "../../utils/misc-utils.js"; | ||
|
||
export interface PackOptions { | ||
readonly reporter: Reporter; | ||
|
||
readonly dir: string; | ||
|
||
/** Directory that should contain the generated `.tgz` */ | ||
readonly packDestination?: string; | ||
} | ||
|
||
export async function pack({ reporter, dir, packDestination }: PackOptions) { | ||
const host = NodeChronusHost; | ||
const workspace = await loadChronusWorkspace(host, dir); | ||
for (const pkg of workspace.packages) { | ||
await reporter.task(`${pc.yellow(pkg.name)} packing`, async (task) => { | ||
const result = await packPackage(workspace, pkg, packDestination); | ||
task.update( | ||
`${pc.yellow(pkg.name)} packed in ${pc.cyan(result.filename)} (${pc.magenta(prettyBytes(result.size))})`, | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { packPackage } from "./pack.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { mkdir } from "fs/promises"; | ||
import { execAsync } from "../utils/exec-async.js"; | ||
import { resolvePath } from "../utils/path-utils.js"; | ||
import type { Package, WorkspaceType } from "../workspace-manager/types.js"; | ||
import type { ChronusWorkspace } from "../workspace/types.js"; | ||
|
||
export interface PackPackageResult { | ||
readonly id: string; | ||
readonly name: string; | ||
readonly version: string; | ||
/** Name of the .tgz file created */ | ||
readonly filename: string; | ||
/** Absolute path of the .tgz file created. */ | ||
readonly path: string; | ||
readonly size: number; | ||
readonly unpackedSize: number; | ||
} | ||
|
||
export async function packPackage( | ||
workspace: ChronusWorkspace, | ||
pkg: Package, | ||
destination?: string, | ||
): Promise<PackPackageResult> { | ||
const pkgDir = resolvePath(workspace.path, pkg.relativePath); | ||
const packDestination = destination ?? pkgDir; | ||
await mkdir(packDestination, { recursive: true }); // Not using the ChronusHost here because it doesn't matter as we need to call npm after. | ||
const command = getPackCommand(workspace.workspace.type, packDestination); | ||
const result = await execAsync(command.command, command.args, { cwd: pkgDir }); | ||
if (result.code !== 0) { | ||
throw new Error(`Failed to pack package ${pkg.name} at ${pkg.relativePath}. Log:\n${result.stdall}`); | ||
} | ||
|
||
const parsedResult = JSON.parse(result.stdout.toString())[0]; | ||
return { | ||
id: parsedResult.id, | ||
name: parsedResult.name, | ||
version: parsedResult.version, | ||
filename: parsedResult.filename, | ||
path: resolvePath(packDestination, parsedResult.filename), | ||
size: parsedResult.size, | ||
unpackedSize: parsedResult.unpackedSize, | ||
}; | ||
} | ||
|
||
function getPackCommand(type: WorkspaceType, destination: string): Command { | ||
switch (type) { | ||
// case "pnpm": | ||
// return getPnpmCommand(destination); | ||
case "npm": | ||
default: | ||
return getNpmCommand(destination); | ||
} | ||
} | ||
|
||
interface Command { | ||
readonly command: string; | ||
readonly args: string[]; | ||
} | ||
|
||
// function getPnpmCommand(destination: string): Command { | ||
// return { command: "pnpm", args: ["pack", "--json", "--pack-destination", destination] }; | ||
// } | ||
function getNpmCommand(destination: string): Command { | ||
return { command: "npm", args: ["pack", "--json", "--pack-destination", destination] }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import pc from "picocolors"; | ||
import { isCI } from "std-env"; | ||
import type { Reporter, Task } from "./types.js"; | ||
|
||
export class BasicReporter implements Reporter { | ||
isTTY = process.stdout?.isTTY && !isCI; | ||
|
||
log(message: string) { | ||
// eslint-disable-next-line no-console | ||
console.log(message); | ||
} | ||
|
||
async task(message: string, action: (task: Task) => Promise<void>) { | ||
let current = message; | ||
const task = { | ||
update: (newMessage: string) => { | ||
current = newMessage; | ||
}, | ||
}; | ||
this.log(`${pc.yellow("-")} ${current}`); | ||
await action(task); | ||
this.log(`${pc.green("✔")} ${current}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import pc from "picocolors"; | ||
import { BasicReporter } from "./basic.js"; | ||
import type { Reporter, Task } from "./types.js"; | ||
import { createSpinner } from "./utils.js"; | ||
|
||
export class DynamicReporter extends BasicReporter implements Reporter { | ||
async task(message: string, action: (task: Task) => Promise<void>) { | ||
if (!this.isTTY) { | ||
return super.task(message, action); | ||
} | ||
|
||
let current = message; | ||
const task = { | ||
update: (newMessage: string) => { | ||
current = newMessage; | ||
}, | ||
}; | ||
|
||
const spinner = createSpinner(); | ||
const interval = setInterval(() => { | ||
this.#printProgress(`\r${pc.yellow(spinner())} ${current}`); | ||
}, 300); | ||
await action(task); | ||
clearInterval(interval); | ||
this.#printProgress(`\r${pc.green("✔")} ${current}\n`); | ||
} | ||
|
||
#printProgress(content: string) { | ||
process.stdout.clearLine(0); | ||
process.stdout.cursorTo(0); | ||
process.stdout.write(content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { BasicReporter } from "./basic.js"; | ||
export { DynamicReporter } from "./dynamic.js"; | ||
export type { Reporter, Task } from "./types.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface Reporter { | ||
readonly log: (message: string) => void; | ||
readonly task: (message: string, action: (task: Task) => Promise<void>) => Promise<void>; | ||
} | ||
|
||
export interface Task { | ||
readonly update: (message: string) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const spinnerFrames = | ||
process.platform === "win32" ? ["-", "\\", "|", "/"] : ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]; | ||
|
||
export function createSpinner() { | ||
let index = 0; | ||
|
||
return () => { | ||
index = ++index % spinnerFrames.length; | ||
return spinnerFrames[index]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,32 @@ | ||
export function isDefined<T>(arg: T | undefined): arg is T { | ||
return arg !== undefined; | ||
} | ||
|
||
const UNITS = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; | ||
|
||
/** | ||
* Format a number of bytes by addding KB, MB, GB, etc. after | ||
* @param bytes Number of bytes to prettify | ||
* @param perecision Number of decimals to keep. @default 2 | ||
*/ | ||
export function prettyBytes(bytes: number, decimals = 2) { | ||
if (!Number.isFinite(bytes)) { | ||
throw new TypeError(`Expected a finite number, got ${typeof bytes}: ${bytes}`); | ||
} | ||
|
||
const neg = bytes < 0; | ||
|
||
if (neg) { | ||
bytes = -bytes; | ||
} | ||
|
||
if (bytes < 1) { | ||
return (neg ? "-" : "") + bytes + " B"; | ||
} | ||
|
||
const exponent = Math.min(Math.floor(Math.log(bytes) / Math.log(1000)), UNITS.length - 1); | ||
const numStr = Number((bytes / Math.pow(1000, exponent)).toFixed(decimals)); | ||
const unit = UNITS[exponent]; | ||
|
||
return (neg ? "-" : "") + numStr + " " + unit; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.