Skip to content

Commit

Permalink
fix: NPM build and release
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed May 19, 2021
1 parent ba22702 commit 0a0cdd7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"test:lint:fix": "eslint '{packages,test}/**/*.js' --fix",
"prettier": "prettier '{packages,test}/**/*.js' --write",
"build": "node packages/monorepo/bin/monorepo-build.js --verbose",
"publish": "node packages/monorepo/bin/monorepo-publish.js --dry-run",
"publish": "node packages/monorepo/bin/monorepo-publish.js --dry-run packages",
"release": "semantic-release",
"release:dryRun": "semantic-release --dry-run",
"prepare": "husky install"
Expand Down
4 changes: 3 additions & 1 deletion packages/monorepo/semantic-release.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const {MonoRepo} = require("./src");

/**
* @type {MonoRepo}
*/
let monoRepo;

module.exports = {
Expand Down
9 changes: 3 additions & 6 deletions packages/monorepo/src/MonoRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export class MonoRepo {
const newCtx = this.fork(options);

switch (type) {
case "workspaces":
case "workspace":
return this.buildWorkspace();
case "packages":
Expand Down Expand Up @@ -350,6 +351,7 @@ export class MonoRepo {
}

publish(type, options = {}) {
console.log(type, options);
switch (type) {
case "packages":
return this.publishPackages(options);
Expand All @@ -372,12 +374,7 @@ export class MonoRepo {
return;
}

return publishPackages(
this.fork({
...options,
rootDir: join(this.rootDir, this.outputDir)
})
);
return publishPackages(this.fork(options));
}

async publishGhPages(options = {}) {
Expand Down
8 changes: 6 additions & 2 deletions packages/monorepo/src/commands/publish/PublishCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export class PublishCmd {
};
}

/**
*
* @param context {MonoRepo}
*/
getTasks(context) {
return [
{
Expand All @@ -22,13 +26,13 @@ export class PublishCmd {
{
title: `Publish packages on NPM ${context.dryRun ? "(DryRun)" : ""}`,
enabled: () => ["packages"].includes(context.type),
task: () => context.publish(context.type, context)
task: () => context.publish(context.type)
},
{
title: "Publish on DockerHub",
enabled: () => ["docker"].includes(context.type),
skip: () => context.dryRun,
task: () => context.publish(context.type, context)
task: () => context.publish(context.type)
},
{
title: "Publish on Heroku",
Expand Down
52 changes: 29 additions & 23 deletions packages/monorepo/src/utils/packages/publishPackages.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from "chalk";
import fs from "fs-extra";
import get from "lodash/get";
import {dirname, join} from "path";
import {basename, dirname, join} from "path";
import {npm} from "../cli";
import {findPackages} from "./findPackages";

Expand Down Expand Up @@ -29,15 +29,32 @@ function writeNpmrc(path, registries, scope) {
return npmrc;
}

async function publishPackage(pkg, {url, cwd}, context) {
const {npmAccess, dryRun, registry} = context;
const npmrc = writeNpmrc(cwd, [url], pkg.name.split("/")[0]);

if (dryRun) {
npm.pack().sync({
cwd,
env: {
NPM_TOKEN: "test"
}
});
} else {
await npm.publish("--userconfig", npmrc, "--access", npmAccess, "--registry", registry).cwd(cwd);
}
}

/**
*
* @param context {MonoRepo}
* @returns {Promise<void>}
*/
export async function publishPackages(context) {
const {logger, npmAccess, dryRun, registry, registries} = context;
const {logger, registry, registries} = context;

const packages = await findPackages(context);
const distDir = join(context.rootDir, context.outputDir);

const urls = [...new Set(registries.concat(registry).filter(Boolean))];
const errors = [];
Expand All @@ -46,28 +63,17 @@ export async function publishPackages(context) {
.map(async ({path, pkg}) => {
logger.info("Publish package", chalk.cyan(pkg.name));

const registries = get(pkg, "monorepo", urls);

try {
const cwd = dirname(path);

if (dryRun) {
npm.pack().sync({
cwd,
env: {
NPM_TOKEN: "test"
}
});
} else {
for (const url of registries) {
const npmrc = writeNpmrc(cwd, [url], pkg.name.split("/")[0]);
try {
logger.info("Publish package", chalk.cyan(pkg.name), "on", url);
await npm.publish("--userconfig", npmrc, "--access", npmAccess, "--registry", registry).cwd(cwd);
} catch (er) {
errors.push({pkg, error: er, registry});
logger.error(chalk.red(er.message), chalk.red(er.stack));
}
const cwd = join(distDir, basename(dirname(path)));
const registries = get(pkg, "monorepo", urls);

for (const url of registries) {
try {
logger.info("Publish package", chalk.cyan(pkg.name), "on", url);
await publishPackage(pkg, {cwd, url}, context);
} catch (er) {
errors.push({pkg, error: er, registry});
logger.error(chalk.red(er.message), chalk.red(er.stack));
}
}
} catch (er) {
Expand Down

0 comments on commit 0a0cdd7

Please sign in to comment.