Skip to content

Commit

Permalink
Merge pull request #130 from zowe-actions/reg-config
Browse files Browse the repository at this point in the history
Allow for `NPM_CONFIG_` properties
  • Loading branch information
zFernand0 committed Feb 6, 2024
2 parents 3ee426d + 284ffca commit 54a84ab
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 29 deletions.
24 changes: 16 additions & 8 deletions dist/npm.js

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

25 changes: 20 additions & 5 deletions dist/run-script.js

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

11 changes: 8 additions & 3 deletions packages/npm/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import * as utils from "./utils";
export default async function (context: IContext, config: IPluginConfig, inDir?: string): Promise<void> {
const cwd = inDir || process.cwd();
const packageJson = JSON.parse(fs.readFileSync(path.join(cwd, "package.json"), "utf-8"));
const npmRegistry: string = packageJson.publishConfig?.registry || DEFAULT_NPM_REGISTRY;

if (config.pruneShrinkwrap) {
if (packageJson.scripts.preshrinkwrap != null) {
Expand All @@ -33,7 +34,7 @@ export default async function (context: IContext, config: IPluginConfig, inDir?:
}

if (config.tarballDir != null) {
const tgzFile = await utils.npmPack(inDir);
const tgzFile = await utils.npmPack(packageJson.name, npmRegistry, inDir);
fs.mkdirSync(config.tarballDir, { recursive: true });
fs.renameSync(path.join(cwd, tgzFile), path.resolve(context.rootDir, config.tarballDir, tgzFile));
}
Expand All @@ -50,13 +51,17 @@ export default async function (context: IContext, config: IPluginConfig, inDir?:
}

try {
const npmRegistry: string = packageJson.publishConfig?.registry || DEFAULT_NPM_REGISTRY;
const packageTag = context.branch.channel as string;

// Publish package
const publishedVersions = await utils.npmView(packageJson.name, npmRegistry, "versions");
if (!publishedVersions?.includes(packageJson.version)) {
await utils.npmPublish(context, packageTag, npmRegistry, inDir);
await utils.npmPublish(context, {
tag: packageTag,
pkgSpec: packageJson.name,
registry: npmRegistry,
inDir
});

context.releasedPackages.npm = [
...(context.releasedPackages.npm || []),
Expand Down
19 changes: 14 additions & 5 deletions packages/npm/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,26 @@ export async function npmInstall(pkgSpec: string, registry: string, inDir?: stri
await exec.exec("npm", ["install", pkgSpec, `--${registryPrefix}registry=${registry}`], { cwd: inDir });
}

export async function npmPack(inDir?: string): Promise<string> {
const cmdOutput = await exec.getExecOutput("npm", ["pack"], { cwd: inDir });
export async function npmPack(pkgSpec: string, registry: string, inDir?: string): Promise<string> {
const registryPrefix = pkgSpec.startsWith("@") ? `${pkgSpec.split("/")[0]}:` : "";
const cmdArgs = ["pack", `--${registryPrefix}registry=${registry}`];
const cmdOutput = await exec.getExecOutput("npm", cmdArgs, { cwd: inDir });
return cmdOutput.stdout.trim().split(/\s+/).pop() as string;
}

export async function npmPublish(context: IContext, tag: string, registry: string, inDir?: string): Promise<void> {
const cmdArgs = ["publish", "--tag", tag, "--registry", registry];
export interface INpmPublishOptions {
tag: string;
pkgSpec: string;
registry: string;
inDir?: string;
}
export async function npmPublish(context: IContext, options: INpmPublishOptions): Promise<void> {
const registryPrefix = options.pkgSpec.startsWith("@") ? `${options.pkgSpec.split("/")[0]}:` : "";
const cmdArgs = ["publish", "--tag", options.tag, `--${registryPrefix}registry=${options.registry}`];
if (context.dryRun) {
cmdArgs.push("--dry-run");
}
await exec.exec("npm", cmdArgs, { cwd: inDir });
await exec.exec("npm", cmdArgs, { cwd: options.inDir });
}

export async function npmVersion(newVersion: string, inDir?: string): Promise<void> {
Expand Down
35 changes: 27 additions & 8 deletions packages/run-script/scripts/npmUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const updateDetails: string[] = [];
let resolutions: Record<string, string> = {};

interface IProtectedBranchWithDeps extends IProtectedBranch {
dependencies: string[] | Record<string, string>;
devDependencies: string[] | Record<string, string>;
dependencies: string[] | Record<string, string | string[]>;
devDependencies: string[] | Record<string, string | string[]>;
}

function getDependencies(context: IContext, branch: IProtectedBranchWithDeps, dev: boolean) {
Expand All @@ -44,23 +44,42 @@ function getDependencies(context: IContext, branch: IProtectedBranchWithDeps, de
return dependencyMap;
}

async function updateDependency(context: IContext, pkgName: string, pkgTag: string, dev: boolean): Promise<void> {
context.logger.debug(`Updating ${dev ? "devD" : "d"}ependency for: ${pkgName}@${pkgTag}`);
const cmdOutput = (await exec.getExecOutput("npm", ["list", pkgName, "--json", "--depth", "0"])).stdout;
async function updateDependency(
context: IContext,
pkgName: string,
pkgTag: string | string[],
dev: boolean
): Promise<void> {
let tempPkgTag = "";
let moreArgs: string[] = [];
const env: { [key: string]: string } = { ...process.env as any } // ?
if (!Array.isArray(pkgTag)) {
tempPkgTag = pkgTag;
} else {
tempPkgTag = pkgTag.shift() ?? "";
moreArgs = pkgTag;
for (const reg of moreArgs) {
const propKey = "NPM_CONFIG_" + reg.split("=")[0].toUpperCase();
env[propKey] = reg.split("=")[1];
}
}

context.logger.debug(`Updating ${dev ? "devD" : "d"}ependency for: ${pkgName}@${tempPkgTag}`);
const cmdOutput = (await exec.getExecOutput("npm", ["list", pkgName, "--json", "--depth", "0"], { env })).stdout;
const currentVersion = JSON.parse(cmdOutput).dependencies[pkgName].version;

if (resolutions[pkgName] == null) {
context.logger.debug(`Gathering version information for: ${pkgName}@${pkgTag}`);
context.logger.debug(`Gathering version information for: ${pkgName}@${tempPkgTag}`);
resolutions[pkgName] = (await exec.getExecOutput("npm",
["view", `${pkgName}@${pkgTag}`, "version"])).stdout.trim();
["view", `${pkgName}@${tempPkgTag}`, "version"], { env })).stdout.trim();
}
const latestVersion = resolutions[pkgName];

if (currentVersion !== latestVersion) {
const npmArgs = dev ? ["--save-dev"] : ["--save-prod", "--save-exact"];
const newUpdate = `${pkgName}: ${currentVersion} -> ${latestVersion}`;
context.logger.debug(`Updating ${newUpdate}`);
await exec.exec("npm", ["install", `${pkgName}@${latestVersion}`, ...npmArgs]);
await exec.exec("npm", ["install", `${pkgName}@${latestVersion}`, ...npmArgs], { env });
updateDetails.push(newUpdate);
}
}
Expand Down

0 comments on commit 54a84ab

Please sign in to comment.