Skip to content

Commit

Permalink
Introduces --registry-log-level and fixes a session bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ManasJayanth committed May 29, 2024
1 parent 039fae7 commit a586bde
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 25 deletions.
30 changes: 24 additions & 6 deletions src/bin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ program.version(version);

program
.option("-C, --cwd [cwd]", "Set current working directory")
.option(
"-r, --registry-log-level [level]",
"To configure log level from test verdaccio server. Ref: https://verdaccio.org/docs/logger/#configuration",
)
.option(
"-i, --prefix-path [path]",
"Path that esy can use for cache area as it runs the tests",
Expand All @@ -36,10 +40,15 @@ program
cwd = process.cwd(),
storagePath,
prefixPath,
registryLogLevel,
} = program.opts();
await defaultCommand(pack, cwd, storagePath, prefixPath).catch(
globalErrorHandler,
);
await defaultCommand(
pack,
cwd,
storagePath,
prefixPath,
registryLogLevel,
).catch(globalErrorHandler);
});

program
Expand All @@ -65,6 +74,10 @@ program

program
.command("shell")
.option(
"-r, --registry-log-level",
"Flag to turn on logs from test verdaccio server",
)
.option("-C, --cwd [cwd]", "Set current working directory")
.description(
"Given an esy.json manifest, and an test folder containing test project using the package, it drops you into a shell to debug the package build",
Expand All @@ -87,10 +100,15 @@ program
cwd = process.cwd(),
storagePath,
prefixPath,
registryLogLevel,
} = program.opts();
await shellCommand(pack, cwd, storagePath, prefixPath).catch(
globalErrorHandler,
);
await shellCommand(
pack,
cwd,
storagePath,
prefixPath,
registryLogLevel,
).catch(globalErrorHandler);
});

program.parse(process.argv);
52 changes: 39 additions & 13 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as cp from "child_process";
import Debug from "debug";
import * as fs from "fs";
import * as crypto from "crypto";
import * as Npm from "./npm-session";
Expand All @@ -15,7 +14,7 @@ import { pkg } from "./package";
import * as Defaults from "./defaults";
import { esy, esyi, withPrefixPath, setupTemporaryEsyPrefix } from "./esy";

const debug = Debug("bale:lib:info");
const debug = require("debug")("bale:lib:info");
export let localNpmRc = `${
process.platform === "win32" ? process.env.USERPROFILE : process.env.HOME
}/.npmrc`;
Expand All @@ -24,11 +23,15 @@ export * from "./npm-session";
export * from "./package";
export * from "./fetch";

export async function createSession(registryUrl: string): Promise<string> {
export async function createSession(
server: NpmServer.$Server,
): Promise<string> {
let token: string;
let testUsername = "foo-" + crypto.randomBytes(4).toString("hex"); // TODO centralise this
let testEmail = "[email protected]"; // TODO centralise this
let testPassword = "bar"; // TODO centralise this
let { addr, port } = server;
const registryUrl = NpmServer.getUrl(server);
try {
debug("Attempting npm login");
token = await Npm.login(testUsername, testPassword, registryUrl);
Expand All @@ -43,7 +46,7 @@ export async function createSession(registryUrl: string): Promise<string> {
);
}
// Writing to .npmrc by hand. See docs/notes.org to see why
fs.appendFileSync(localNpmRc, `//${registryUrl}/:_authToken="${token}"\n`);
fs.appendFileSync(localNpmRc, `//${addr}:${port}/:_authToken="${token}"\n`);
return token;
}

Expand Down Expand Up @@ -97,37 +100,52 @@ async function fetchAndPkg(pack: string, cwd: path) {
return `${cwd}/package.tar.gz`;
}

async function setupLocalVerdaccio(storagePath: path, manifest: any) {
async function setupLocalVerdaccio(
storagePath: path,
manifest: any,
registryLogLevel: string,
) {
Log.info("Setting up local verdaccio server");
return NpmServer.setup(storagePath, manifest);
return NpmServer.setup(storagePath, manifest, registryLogLevel);
}

async function publishToLocalVerdaccio(registryUrl: url, tarballPath: path) {
async function publishToLocalVerdaccio(server: any, tarballPath: path) {
Log.info("Publishing to verdaccio server");
await createSession(registryUrl);
const registryUrl = NpmServer.getUrl(server);
await createSession(server);
Log.process("verdaccio", await NpmClient.publish(registryUrl, tarballPath));
}

async function getLocalVerdaccioWithPackage(
pack: string, // TODO make it optional
cwd: path,
storagePath: path,
registryLogLevel: string,
): Promise<NpmServer.$Server> {
const manifest = require(Path.join(cwd, "esy.json"));
const tarballPath = await fetchAndPkg(pack, cwd);
const server = await setupLocalVerdaccio(storagePath, manifest);
const registryUrl = NpmServer.getUrl(server);
await publishToLocalVerdaccio(registryUrl, tarballPath);
const server = await setupLocalVerdaccio(
storagePath,
manifest,
registryLogLevel,
);
await publishToLocalVerdaccio(server, tarballPath);
return server;
}

async function withPackagePublishedToLocalTestEnv(
pack: string, // TODO make it optional
cwd: path,
storagePath: path,
registryLogLevel: string,
f: (server: NpmServer.$Server) => Promise<void>,
): Promise<void> {
const server = await getLocalVerdaccioWithPackage(pack, cwd, storagePath);
const server = await getLocalVerdaccioWithPackage(
pack,
cwd,
storagePath,
registryLogLevel,
);
await f(server);
cleanup(server);
}
Expand All @@ -137,6 +155,7 @@ export async function defaultCommand(
cwd: path,
storagePath: path = Defaults.storagePath,
userSpecifiedPrefixPath: path = null,
registryLogLevel: string,
) {
let returnStatus: number;
let server: any;
Expand All @@ -154,6 +173,7 @@ export async function defaultCommand(
pack,
cwd,
storagePath,
registryLogLevel,
async (server: NpmServer.$Server) => {
const registryUrl = NpmServer.getUrl(server);
await runE2E(
Expand Down Expand Up @@ -207,6 +227,7 @@ export async function shellCommand(
cwd: path,
storagePath: path = Defaults.storagePath,
userSpecifiedPrefixPath: path = null,
registryLogLevel: string,
) {
let returnStatus: number;
let server: any;
Expand All @@ -215,7 +236,12 @@ export async function shellCommand(
const packageRecipeTestsPath = Path.join(cwd, "esy-test");
if (fse.existsSync(packageRecipeTestsPath)) {
// TODO see note in defaultCommand
const server = await getLocalVerdaccioWithPackage(pack, cwd, storagePath);
const server = await getLocalVerdaccioWithPackage(
pack,
cwd,
storagePath,
registryLogLevel,
);
const registryUrl = NpmServer.getUrl(server);
await e2eShell(
packageRecipeTestsPath,
Expand Down
17 changes: 11 additions & 6 deletions src/lib/npm-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { runServer } from "verdaccio";
import { REGISTRY_PORT } from "../config";
import Debug from "debug";
import fse from "fs-extra";
import { delay } from "./utils";

export let REGISTRY_ADDR = "localhost";
const debug = Debug("bale:npm-server:info");
Expand All @@ -16,15 +17,18 @@ async function init(
storagePath: path,
addr: string,
port: number,
registryLogLevel: string,
): Promise<$Server> {
debug("Storage Path", storagePath);
debug("Log Level", registryLogLevel);
let config = {
storage: storagePath,
self_path: configPath,
listen: `${addr}:${port}`,
auth: {
htpasswd: { file: Path.join(__dirname, "htpasswd") },
},
logs: { type: "stdout", format: "json", level: "http" },
logs: { type: "stdout", level: registryLogLevel || "error" },
uplinks: {
npmjs: {
url: "https://registry.npmjs.org/",
Expand Down Expand Up @@ -63,10 +67,6 @@ export function getUrl({ port, addr }) {
return `http://${registryHost}`;
}

function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export function start(args: any): Promise<void> {
let { port, addr, server } = args;
return new Promise((resolve, reject) => {
Expand All @@ -82,7 +82,11 @@ export function stop(args) {
server.close();
}

export async function setup(storagePath: path, manifest: any) {
export async function setup(
storagePath: path,
manifest: any,
registryLogLevel: string,
) {
debug("Clearing storage path meant for verdaccio", storagePath);
rimraf.sync(storagePath);
fse.mkdirp(storagePath);
Expand All @@ -93,6 +97,7 @@ export async function setup(storagePath: path, manifest: any) {
storagePath,
REGISTRY_ADDR,
REGISTRY_PORT,
registryLogLevel,
);
debug("Setting up verdaccio user session");
await start(server);
Expand Down
4 changes: 4 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,7 @@ export async function download(urlStrWithChecksum: $path, pkgPath: $path) {

return downloadedPath;
}

export function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

0 comments on commit a586bde

Please sign in to comment.