@lerna/publish
Version:
Publish packages in the current project
206 lines (205 loc) • 7.27 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var import_fs_extra = __toESM(require("fs-extra"));
var import_libnpmpublish = require("libnpmpublish");
var import_otplease = require("./otplease");
var import_run_lifecycle = require("./run-lifecycle");
var import_path = __toESM(require("path"));
var import_package = require("./package");
var import_npm_publish = require("./npm-publish");
jest.mock("./run-lifecycle");
jest.mock("./otplease");
jest.mock("read-package-json");
jest.mock("libnpmpublish");
jest.mock("fs-extra");
const readJSON = require("read-package-json");
const fs = jest.mocked(import_fs_extra.default);
const publish = jest.mocked(import_libnpmpublish.publish);
const runLifecycle = jest.mocked(import_run_lifecycle.runLifecycle);
const otplease = jest.mocked(import_otplease.otplease);
describe("npm-publish", () => {
const mockTarData = Buffer.from("MOCK");
const mockManifest = { _normalized: true };
fs.readFile.mockName("fs.readFile").mockResolvedValue(mockTarData);
publish.mockName("libnpmpublish").mockResolvedValue();
readJSON.mockName("read-package-json").mockImplementation((file, cb) => cb(null, mockManifest));
runLifecycle.mockName("./run-lifecycle").mockResolvedValue();
otplease.mockName("./otplease").mockImplementation((cb, opts) => Promise.resolve(cb(opts)));
const tarFilePath = "/tmp/test-1.10.100.tgz";
const rootPath = import_path.default.normalize("/test");
const pkg = new import_package.Package(
{ name: "@scope/test", version: "1.10.100" },
import_path.default.join(rootPath, "npmPublish/test"),
rootPath
);
it("calls external libraries with correct arguments", async () => {
const opts = { tag: "published-tag" };
await (0, import_npm_publish.npmPublish)(pkg, tarFilePath, opts);
expect(fs.readFile).toHaveBeenCalledWith(tarFilePath);
expect(readJSON).toHaveBeenCalledWith(pkg.manifestLocation, expect.any(Function));
expect(publish).toHaveBeenCalledWith(
mockManifest,
mockTarData,
expect.objectContaining({
defaultTag: "published-tag",
projectScope: "@scope"
})
);
});
it("defaults opts.tag to 'latest'", async () => {
await (0, import_npm_publish.npmPublish)(pkg, tarFilePath);
expect(publish).toHaveBeenCalledWith(
mockManifest,
mockTarData,
expect.objectContaining({
defaultTag: "latest"
})
);
});
it("overrides pkg.publishConfig.tag when opts.tag is explicitly configured", async () => {
readJSON.mockImplementationOnce(
(file, cb) => cb(null, {
publishConfig: {
tag: "beta"
}
})
);
const opts = { tag: "temp-tag" };
await (0, import_npm_publish.npmPublish)(pkg, tarFilePath, opts);
expect(publish).toHaveBeenCalledWith(
expect.objectContaining({
publishConfig: {
tag: "temp-tag"
}
}),
mockTarData,
expect.objectContaining({
defaultTag: "temp-tag"
})
);
});
it("respects pkg.publishConfig.tag when opts.defaultTag matches default", async () => {
readJSON.mockImplementationOnce(
(file, cb) => cb(null, {
publishConfig: {
tag: "beta"
}
})
);
await (0, import_npm_publish.npmPublish)(pkg, tarFilePath);
expect(publish).toHaveBeenCalledWith(
expect.objectContaining({
publishConfig: {
tag: "beta"
}
}),
mockTarData,
expect.objectContaining({
defaultTag: "beta"
})
);
});
it("uses pkg.contents manifest when pkg.publishConfig.directory is defined", async () => {
const fancyPkg = new import_package.Package(
{
name: "fancy",
version: "1.10.100",
publishConfig: {
directory: "dist"
}
},
import_path.default.join(rootPath, "npmPublish/fancy"),
rootPath
);
readJSON.mockImplementationOnce(
(file, cb) => cb(null, {
name: "fancy-fancy",
version: "1.10.100"
})
);
await (0, import_npm_publish.npmPublish)(fancyPkg, tarFilePath);
expect(readJSON).toHaveBeenCalledWith(
import_path.default.join(fancyPkg.location, "dist/package.json"),
expect.any(Function)
);
expect(publish).toHaveBeenCalledWith(
expect.objectContaining({
name: "fancy-fancy"
}),
mockTarData,
expect.objectContaining({
defaultTag: "latest"
})
);
});
it("merges pkg.publishConfig.registry into options", async () => {
readJSON.mockImplementationOnce(
(file, cb) => cb(null, {
publishConfig: {
registry: "http://pkg-registry.com"
}
})
);
const opts = { registry: "https://global-registry.com" };
await (0, import_npm_publish.npmPublish)(pkg, tarFilePath, opts);
expect(publish).toHaveBeenCalledWith(
expect.objectContaining({
publishConfig: {
registry: "http://pkg-registry.com"
}
}),
mockTarData,
expect.objectContaining({
registry: "http://pkg-registry.com"
})
);
});
it("respects opts.dryRun", async () => {
const opts = { dryRun: true };
await (0, import_npm_publish.npmPublish)(pkg, tarFilePath, opts);
expect(publish).not.toHaveBeenCalled();
expect(runLifecycle).toHaveBeenCalledTimes(2);
});
it.each([["true"], [true], ["false"], [false]])(
"aliases strict-ssl to strictSSL",
async (strictSSLValue) => {
const opts = { "strict-ssl": strictSSLValue };
await (0, import_npm_publish.npmPublish)(pkg, tarFilePath, opts);
expect(publish).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
expect.objectContaining({
strictSSL: strictSSLValue
})
);
}
);
it("calls publish lifecycles", async () => {
const options = expect.objectContaining({
projectScope: "@scope"
});
await (0, import_npm_publish.npmPublish)(pkg, tarFilePath);
expect(runLifecycle).toHaveBeenCalledWith(pkg, "publish", options);
expect(runLifecycle).toHaveBeenLastCalledWith(pkg, "postpublish", options);
});
});