diff options
author | Fabio Berger <me@fabioberger.com> | 2018-07-19 23:48:06 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-07-19 23:48:06 +0800 |
commit | d8898cf9a30cc349868afcf2b78e6369e57aa726 (patch) | |
tree | f96d1aa76c7e5aa9e3311d5cdbd0d31c8ec8d7fb /packages/monorepo-scripts/src/utils | |
parent | 1aaf633df883f62fad890b2d87a2dc89067821c5 (diff) | |
parent | 3de88d5345c7a4549bc69e2ca28f0601f5d42189 (diff) | |
download | dexon-sol-tools-d8898cf9a30cc349868afcf2b78e6369e57aa726.tar dexon-sol-tools-d8898cf9a30cc349868afcf2b78e6369e57aa726.tar.gz dexon-sol-tools-d8898cf9a30cc349868afcf2b78e6369e57aa726.tar.bz2 dexon-sol-tools-d8898cf9a30cc349868afcf2b78e6369e57aa726.tar.lz dexon-sol-tools-d8898cf9a30cc349868afcf2b78e6369e57aa726.tar.xz dexon-sol-tools-d8898cf9a30cc349868afcf2b78e6369e57aa726.tar.zst dexon-sol-tools-d8898cf9a30cc349868afcf2b78e6369e57aa726.zip |
merge v2-prototype
Diffstat (limited to 'packages/monorepo-scripts/src/utils')
-rw-r--r-- | packages/monorepo-scripts/src/utils/changelog_utils.ts | 8 | ||||
-rw-r--r-- | packages/monorepo-scripts/src/utils/utils.ts | 52 |
2 files changed, 46 insertions, 14 deletions
diff --git a/packages/monorepo-scripts/src/utils/changelog_utils.ts b/packages/monorepo-scripts/src/utils/changelog_utils.ts index dbafb6f16..4781b3b7d 100644 --- a/packages/monorepo-scripts/src/utils/changelog_utils.ts +++ b/packages/monorepo-scripts/src/utils/changelog_utils.ts @@ -10,7 +10,7 @@ import { Change, Changelog, VersionChangelog } from '../types'; const CHANGELOG_MD_HEADER = ` <!-- -This file is auto-generated using the monorepo-scripts package. Don't edit directly. +changelogUtils.file is auto-generated using the monorepo-scripts package. Don't edit directly. Edit the package's CHANGELOG.json file only. --> @@ -74,7 +74,7 @@ export const changelogUtils = { }, getChangelogOrCreateIfMissing(packageName: string, packageLocation: string): Changelog { const changelogJSONPath = path.join(packageLocation, 'CHANGELOG.json'); - let changelogJsonIfExists = this.getChangelogJSONIfExists(changelogJSONPath); + let changelogJsonIfExists = changelogUtils.getChangelogJSONIfExists(changelogJSONPath); if (_.isUndefined(changelogJsonIfExists)) { // If none exists, create new, empty one. changelogJsonIfExists = '[]'; @@ -91,12 +91,12 @@ export const changelogUtils = { async writeChangelogJsonFileAsync(packageLocation: string, changelog: Changelog): Promise<void> { const changelogJSONPath = path.join(packageLocation, 'CHANGELOG.json'); fs.writeFileSync(changelogJSONPath, JSON.stringify(changelog, null, '\t')); - await this.prettifyAsync(changelogJSONPath, constants.monorepoRootPath); + await changelogUtils.prettifyAsync(changelogJSONPath, constants.monorepoRootPath); }, async writeChangelogMdFileAsync(packageLocation: string, changelogMdString: string): Promise<void> { const changelogMarkdownPath = path.join(packageLocation, 'CHANGELOG.md'); fs.writeFileSync(changelogMarkdownPath, changelogMdString); - await this.prettifyAsync(changelogMarkdownPath, constants.monorepoRootPath); + await changelogUtils.prettifyAsync(changelogMarkdownPath, constants.monorepoRootPath); }, async prettifyAsync(filePath: string, cwd: string): Promise<void> { await execAsync(`prettier --write ${filePath} --config .prettierrc`, { diff --git a/packages/monorepo-scripts/src/utils/utils.ts b/packages/monorepo-scripts/src/utils/utils.ts index f819ab6f1..8ba59c81d 100644 --- a/packages/monorepo-scripts/src/utils/utils.ts +++ b/packages/monorepo-scripts/src/utils/utils.ts @@ -1,10 +1,10 @@ -import lernaGetPackages = require('lerna-get-packages'); +import * as fs from 'fs'; import * as _ from 'lodash'; import { exec as execAsync } from 'promisify-child-process'; import semver = require('semver'); import { constants } from '../constants'; -import { GitTagsByPackageName, UpdatedPackage } from '../types'; +import { GitTagsByPackageName, Package, UpdatedPackage } from '../types'; import { changelogUtils } from './changelog_utils'; @@ -12,15 +12,47 @@ export const utils = { log(...args: any[]): void { console.log(...args); // tslint:disable-line:no-console }, - async getUpdatedLernaPackagesAsync(shouldIncludePrivate: boolean): Promise<LernaPackage[]> { - const updatedPublicPackages = await this.getLernaUpdatedPackagesAsync(shouldIncludePrivate); + getPackages(rootDir: string): Package[] { + const rootPackageJsonString = fs.readFileSync(`${rootDir}/package.json`, 'utf8'); + const rootPackageJson = JSON.parse(rootPackageJsonString); + if (_.isUndefined(rootPackageJson.workspaces)) { + throw new Error(`Did not find 'workspaces' key in root package.json`); + } + const packages = []; + for (const workspace of rootPackageJson.workspaces) { + // HACK: Remove allowed wildcards from workspace entries. + // This might be entirely comprehensive. + const workspacePath = workspace.replace('*', '').replace('**/*', ''); + const subpackageNames = fs.readdirSync(`${rootDir}/${workspacePath}`); + for (const subpackageName of subpackageNames) { + if (_.startsWith(subpackageName, '.')) { + continue; + } + const pathToPackageJson = `${rootDir}/${workspacePath}${subpackageName}`; + try { + const packageJsonString = fs.readFileSync(`${pathToPackageJson}/package.json`, 'utf8'); + const packageJson = JSON.parse(packageJsonString); + const pkg = { + location: pathToPackageJson, + packageJson, + }; + packages.push(pkg); + } catch (err) { + utils.log(`Couldn't find a 'package.json' for ${subpackageName}. Skipping.`); + } + } + } + return packages; + }, + async getUpdatedPackagesAsync(shouldIncludePrivate: boolean): Promise<Package[]> { + const updatedPublicPackages = await utils.getLernaUpdatedPackagesAsync(shouldIncludePrivate); const updatedPackageNames = _.map(updatedPublicPackages, pkg => pkg.name); - const allLernaPackages = lernaGetPackages(constants.monorepoRootPath); - const updatedPublicLernaPackages = _.filter(allLernaPackages, pkg => { - return _.includes(updatedPackageNames, pkg.package.name); + const allPackages = utils.getPackages(constants.monorepoRootPath); + const updatedPackages = _.filter(allPackages, pkg => { + return _.includes(updatedPackageNames, pkg.packageJson.name); }); - return updatedPublicLernaPackages; + return updatedPackages; }, async getLernaUpdatedPackagesAsync(shouldIncludePrivate: boolean): Promise<UpdatedPackage[]> { const result = await execAsync(`${constants.lernaExecutable} updated --json`, { @@ -110,7 +142,7 @@ export const utils = { } catch (err) { throw new Error(`Failed to delete local git tag. Got err: ${err}`); } - this.log(`Removed local tag: ${tagName}`); + utils.log(`Removed local tag: ${tagName}`); }, async removeRemoteTagAsync(tagName: string): Promise<void> { try { @@ -120,6 +152,6 @@ export const utils = { } catch (err) { throw new Error(`Failed to delete remote git tag. Got err: ${err}`); } - this.log(`Removed remote tag: ${tagName}`); + utils.log(`Removed remote tag: ${tagName}`); }, }; |