Skip to content

Commit

Permalink
chore: add script to update package.json versions (#2574)
Browse files Browse the repository at this point in the history
* chore: add script to update package.json versions
closes #2563

* chore: add script to update schematic lib versions
closes #2562

* chore: add previous major version to dropdown
Related to #2557
  • Loading branch information
timdeschryver authored Jun 19, 2020
1 parent 1ae3332 commit 256978b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
101 changes: 101 additions & 0 deletions build/update-version-numbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { readFileSync, writeFileSync } from 'fs';
import { EOL } from 'os';
import * as readline from 'readline';
import * as glob from 'glob';
import { createBuilder } from './util';
import { packages } from './config';

// get the version from the command
// e.g. ts-node ./build/update-version-numbers.ts 10.0.0
const [newVersion] = process.argv.slice(2);

if (newVersion) {
updateVersions(newVersion);
} else {
// if no version is provided, ask for it
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

rl.question(`What's the new version? `, (version) => {
rl.close();
updateVersions(version);
});
}

function updateVersions(version: string) {
const publishNext = createBuilder([
['Update package.json', createPackageJsonBuilder(version)],
['Update ng-add schematic', createUpdateAddSchematicBuilder(version)],
['Update docs version picker', createArchivePreviousDocsBuilder(version)],
]);

publishNext({
scope: '@ngrx',
packages,
}).catch((err) => {
console.error(err);
process.exit(1);
});
}

function createPackageJsonBuilder(version: string) {
return async () => {
glob
.sync('**/package.json', { ignore: '**/node_modules/**' })
.map((file) => {
const content = readFileSync(file, 'utf-8');
const pkg = JSON.parse(content);
if (pkg?.version && pkg?.name?.startsWith('@ngrx')) {
pkg.version = version;
writeAsJson(file, pkg);
}
});
};
}

function createUpdateAddSchematicBuilder(version: string) {
return async () => {
glob
.sync('**/libs-version.ts', { ignore: '**/node_modules/**' })
.map((file) => {
writeFileSync(
file,
`export const platformVersion = '^${version}';${EOL}`
);
});
};
}

function createArchivePreviousDocsBuilder(version: string) {
return async () => {
// only deprecate previous version on MAJOR releases
if (!version.endsWith('.0.0')) {
return;
}
const path = './projects/ngrx.io/content/navigation.json';

const [major] = version.split('.');
const prevousVersion = Number(major) - 1;
const content = readFileSync(path, 'utf-8');
const navigation = JSON.parse(content);
navigation['docVersions'] = [
{
title: `v${prevousVersion}`,
url: `https://v${prevousVersion}.ngrx.io`,
},
...navigation['docVersions'],
];
writeAsJson(path, navigation);

console.log(
'\r\n⚠ Previous version added to website doc versions but site needs to be archived manually.'
);
};
}

function writeAsJson(path: string, json: object) {
const content = JSON.stringify(json, null, 2);
writeFileSync(path, `${content}${EOL}`);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"format:check": "nx format:check",
"update": "ng update @nrwl/workspace",
"update:check": "ng update",
"update:versions": "ts-node ./build/update-version-numbers.ts",
"lint": "nx workspace-lint && ng lint",
"dep-graph": "nx dep-graph",
"workspace-schematic": "nx workspace-schematic",
Expand Down

0 comments on commit 256978b

Please sign in to comment.