diff --git a/package.json b/package.json index 26999f9..16dd955 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/packages/monorepo/semantic-release.js b/packages/monorepo/semantic-release.js index e5b769c..364e816 100644 --- a/packages/monorepo/semantic-release.js +++ b/packages/monorepo/semantic-release.js @@ -1,5 +1,7 @@ const {MonoRepo} = require("./src"); - +/** + * @type {MonoRepo} + */ let monoRepo; module.exports = { diff --git a/packages/monorepo/src/MonoRepo.js b/packages/monorepo/src/MonoRepo.js index ca66f54..6eaefe4 100644 --- a/packages/monorepo/src/MonoRepo.js +++ b/packages/monorepo/src/MonoRepo.js @@ -295,6 +295,7 @@ export class MonoRepo { const newCtx = this.fork(options); switch (type) { + case "workspaces": case "workspace": return this.buildWorkspace(); case "packages": @@ -350,6 +351,7 @@ export class MonoRepo { } publish(type, options = {}) { + console.log(type, options); switch (type) { case "packages": return this.publishPackages(options); @@ -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 = {}) { diff --git a/packages/monorepo/src/commands/publish/PublishCmd.js b/packages/monorepo/src/commands/publish/PublishCmd.js index 1175c2a..f656bde 100644 --- a/packages/monorepo/src/commands/publish/PublishCmd.js +++ b/packages/monorepo/src/commands/publish/PublishCmd.js @@ -6,6 +6,10 @@ export class PublishCmd { }; } + /** + * + * @param context {MonoRepo} + */ getTasks(context) { return [ { @@ -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", diff --git a/packages/monorepo/src/utils/packages/publishPackages.js b/packages/monorepo/src/utils/packages/publishPackages.js index e0b4a07..4486e5b 100644 --- a/packages/monorepo/src/utils/packages/publishPackages.js +++ b/packages/monorepo/src/utils/packages/publishPackages.js @@ -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"; @@ -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} */ 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 = []; @@ -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) {