Skip to content

Commit

Permalink
Force version bump in prerelease (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
ifiokjr committed Oct 10, 2020
1 parent 049134a commit de6a430
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 55 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -49,13 +49,13 @@ jobs:

- name: check versions are valid
if: github.event.pull_request_target
run: pnpm ci:version:check
run: pnpm version:check

- name: create versions
uses: changesets/action@master
if: github.ref == 'refs/heads/next'
with:
version: pnpm ci:version
version: pnpm version
commit: 'Update versions with changeset'
title: 'Update versions with changeset'
env:
Expand Down
18 changes: 10 additions & 8 deletions package.json
Expand Up @@ -19,13 +19,6 @@
"checks:enable": "cpy support/.config.sample.json ./ --rename=\".config.json\"",
"checks:fix": "run-s -c fix typecheck test",
"checks:release": "run-s checks build test:e2e",
"ci:version": "run-s ci:version:changeset ci:version:date ci:version:repo fix:prettier ci:version:install",
"ci:version:changeset": "changeset version",
"ci:version:check": "run-s ci:version:pr ci:version:changeset ci:version:repo",
"ci:version:date": "node support/scripts/changelog-dates.js",
"ci:version:install": "unset CI && pnpm install --frozen-lockfile=false",
"ci:version:pr": "node support/scripts/enable-pr-changeset.js",
"ci:version:repo": "unset CI && run-s fix:repo update:workspace",
"clean": "pnpm if-clean git clean -- -fdx --exclude=.config.json --exclude=node_modules --exclude=**/node_modules",
"clean:all": "git clean -fdX --exclude='.config.json'",
"clean:modules": "git clean -fdX packages support",
Expand Down Expand Up @@ -80,7 +73,16 @@
"ts": "ts-node -P support/tsconfig.base.json -O '{\"module\":\"commonjs\"}' --transpile-only",
"typecheck": "tsc -b ./tsconfig.references.json",
"update:deps": "pnpm update --latest --recursive -i",
"update:workspace": "pnpm up -r --workspace \"@remirror/*\""
"update:workspace": "pnpm up -r --workspace \"@remirror/*\"",
"version": "run-s version:changeset version:date version:repo fix:prettier version:install",
"preversion:changeset": "node support/scripts/changeset-forced-update.js",
"version:changeset": "changeset version",
"postversion:changeset": "node support/scripts/changeset-forced-update.js --clean",
"version:check": "run-s version:pr version:changeset version:repo",
"version:date": "node support/scripts/changelog-dates.js",
"version:install": "unset CI && pnpm install --frozen-lockfile=false",
"version:pr": "node support/scripts/enable-pr-changeset.js",
"version:repo": "unset CI && run-s fix:repo update:workspace"
},
"browserslist": [
"since 2017"
Expand Down
68 changes: 68 additions & 0 deletions support/scripts/changeset-forced-update.js
@@ -0,0 +1,68 @@
const { promises: fs } = require('fs');
const { getAllDependencies, baseDir, readChangesetState, formatFiles, rm } = require('./helpers');
const writeJson = require('write-json-file');

const [, , ...args] = process.argv;
const clean = args.includes('--clean');

const FORCED_FILE_NAME = 'pre-forced-update';
const FORCED_FILE_PATH = baseDir('.changeset', `${FORCED_FILE_NAME}.md`);
const PRE_FILE_PATH = baseDir('.changeset', 'pre.json');

/** @typedef {import('@manypkg/get-packages').Package['packageJson']} PackageJSON */

/**
* @param {PackageJSON[]} packages
* @returns {Promise<void>}
*/
async function createForcedUpdateFile(packages) {
const frontMatter = packages.map((pkg) => `'${pkg.name}': patch`);
const fileContents = `---
${frontMatter.join('\n')}
---
Forced update in pre-release mode.`;

await fs.writeFile(FORCED_FILE_PATH, fileContents);
}

async function run() {
const { changesets, preState } = await readChangesetState();

if (!preState) {
return;
}

if (clean) {
await rm(FORCED_FILE_PATH);
await writeJson(PRE_FILE_PATH, {
...preState,
changesets: preState.changesets.filter((name) => name !== FORCED_FILE_NAME),
});

await formatFiles(PRE_FILE_PATH);
return;
}

/** @type {Set<string>} */
const includedNames = new Set();

for (const changeset of changesets) {
changeset.releases.forEach((release) => {
includedNames.add(release.name);
});
}

const packages = (await getAllDependencies()).filter(
(pkg) => !pkg.private && !includedNames.has(pkg.name),
);

if (packages.length === 0) {
return;
}

await createForcedUpdateFile(packages);
await formatFiles(FORCED_FILE_PATH);
}

run();
34 changes: 33 additions & 1 deletion support/scripts/helpers/index.js
@@ -1,7 +1,10 @@
const { resolve, join, relative } = require('path');
const { readPreState } = require('@changesets/pre');
const readChangesets = require('@changesets/read').default;
const { getPackages } = require('@manypkg/get-packages');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const rm = require('util').promisify(require('rimraf'));

const separator = '__';

Expand All @@ -23,7 +26,7 @@ const baseDir = (...paths) => resolve(__dirname, '../../..', join(...paths));
const getRelativePathFromJson = (json) => relative(baseDir(), json.location);

const formatFiles = async (path = '', silent = false) => {
const { stderr, stdout } = await exec(`prettier ${path} --write`);
const { stderr, stdout } = await exec(`prettier --loglevel warn ${path} --write`);

if (silent) {
return;
Expand Down Expand Up @@ -104,6 +107,33 @@ const getTypedPackagesWithPath = async (relative = false) => {
return typedPackages;
};

/**
* @typedef { Object } ChangesetState
* @property { import("@changesets/types").PreState | undefined } preState
* @property { import("@changesets/types").NewChangeset[] } changesets
*/

/**
* @param { string } [cwd]
* @returns { Promise<ChangesetState> }
*/
async function readChangesetState(cwd = process.cwd()) {
const preState = await readPreState(cwd);
const isInPreMode = preState !== undefined && preState.mode === 'pre';

let changesets = await readChangesets(cwd);

if (isInPreMode) {
const changesetsToFilter = new Set(preState.changesets);
changesets = changesets.filter((x) => !changesetsToFilter.has(x.id));
}

return {
preState: isInPreMode ? preState : undefined,
changesets,
};
}

module.exports = {
getAllDependencies,
getTypedPackagesWithPath,
Expand All @@ -113,4 +143,6 @@ module.exports = {
unmangleScopedPackage,
mangleScopedPackageName,
exec,
rm,
readChangesetState,
};
5 changes: 2 additions & 3 deletions support/scripts/linaria.js
Expand Up @@ -13,14 +13,13 @@ const transform = require('linaria/lib/transform').default;
const path = require('path');
const groupBy = require('lodash.groupby');
const chalk = require('chalk');
const { baseDir } = require('./helpers');
const { baseDir, rm } = require('./helpers');
const prettier = require('prettier');
const postcssNested = require('postcss-nested');
const postcssImport = require('postcss-import');
const autoprefixer = require('autoprefixer');
const postcss = require('postcss');
const { camelCase, pascalCase } = require('case-anything');
const rimraf = require('util').promisify(require('rimraf'));
const cpy = require('cpy');

/**
Expand Down Expand Up @@ -276,7 +275,7 @@ async function removeGeneratedFiles() {
].join(' ');

// Delete it all 🤭
await rimraf(files);
await rm(files);
}

/**
Expand Down
10 changes: 0 additions & 10 deletions support/scripts/patch-changesets.js

This file was deleted.

33 changes: 2 additions & 31 deletions support/scripts/run-if-publishable.js
@@ -1,37 +1,8 @@
const { readPreState } = require('@changesets/pre');
const readChangesets = require('@changesets/read').default;
const { execSync } = require('child_process');
const { readChangesetState, exec } = require('./helpers');

const [, , ...args] = process.argv;
const command = args.join(' ');

/**
* @typedef { Object } ChangesetState
* @property { import("@changesets/types").PreState | undefined } preState
* @property { import("@changesets/types").NewChangeset[] } changesets
*/

/**
* @param { string } [cwd]
* @returns { Promise<ChangesetState> }
*/
async function readChangesetState(cwd = process.cwd()) {
const preState = await readPreState(cwd);
const isInPreMode = preState !== undefined && preState.mode === 'pre';

let changesets = await readChangesets(cwd);

if (isInPreMode) {
const changesetsToFilter = new Set(preState.changesets);
changesets = changesets.filter((x) => !changesetsToFilter.has(x.id));
}

return {
preState: isInPreMode ? preState : undefined,
changesets,
};
}

async function run() {
const { changesets, preState } = await readChangesetState();
const shouldSkipCommand = changesets.length > 0;
Expand All @@ -51,7 +22,7 @@ async function run() {
return;
}

execSync(publishCommand, { stdio: 'inherit' });
await exec(publishCommand, { stdio: 'inherit' });
}

run();

1 comment on commit de6a430

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://remirror.io as production
🚀 Deployed on https://5f818d62d1e6204ee2c0c2f2--remirror.netlify.app

Please sign in to comment.