Add prettier run on generated CHANGELOG.json and fix scripts

This commit is contained in:
Fabio Berger
2018-04-02 15:49:48 +09:00
parent cd23f220a0
commit 2a6a71ea6c
4 changed files with 21 additions and 13 deletions

View File

@@ -19,7 +19,7 @@
"lerna:publish": "run-s lerna:install lerna:rebuild script:publish", "lerna:publish": "run-s lerna:install lerna:rebuild script:publish",
"lerna:publish:dry": "run-s lerna:install lerna:rebuild script:publish:dry", "lerna:publish:dry": "run-s lerna:install lerna:rebuild script:publish:dry",
"script:publish": "node ./packages/monorepo-scripts/lib/publish.js", "script:publish": "node ./packages/monorepo-scripts/lib/publish.js",
"script:publish:dry": "IS_DRY_RUN=false yarn script:publish" "script:publish:dry": "IS_DRY_RUN=true yarn script:publish"
}, },
"config": { "config": {
"mnemonic": "concert load couple harbor equip island argue ramp clarify fence smart topic" "mnemonic": "concert load couple harbor equip island argue ramp clarify fence smart topic"

View File

@@ -9,10 +9,10 @@
"lint": "tslint --project . 'src/**/*.ts'", "lint": "tslint --project . 'src/**/*.ts'",
"clean": "shx rm -rf lib", "clean": "shx rm -rf lib",
"build": "tsc", "build": "tsc",
"publish": "run-s build script:publish", "test:publish": "run-s build script:publish",
"convert_changelogs": "run-s build script:convert_changelogs", "convert_changelogs": "run-s build script:convert_changelogs",
"script:deps_versions": "node ./lib/deps_versions.js", "script:deps_versions": "node ./lib/deps_versions.js",
"script:publish": "node ./lib/publish.js", "script:publish": "IS_DRY_RUN=true node ./lib/publish.js",
"script:convert_changelogs": "node ./lib/convert_changelogs.js" "script:convert_changelogs": "node ./lib/convert_changelogs.js"
}, },
"repository": { "repository": {

View File

@@ -10,6 +10,7 @@ import lernaGetPackages = require('lerna-get-packages');
import * as _ from 'lodash'; import * as _ from 'lodash';
import * as moment from 'moment'; import * as moment from 'moment';
import * as path from 'path'; import * as path from 'path';
import { exec as execAsync } from 'promisify-child-process';
import { constants } from './constants'; import { constants } from './constants';
import { Changelog, Changes, UpdatedPackage } from './types'; import { Changelog, Changes, UpdatedPackage } from './types';
@@ -20,7 +21,7 @@ const HEADER_PRAGMA = '##';
(async () => { (async () => {
const allLernaPackages = lernaGetPackages(constants.monorepoRootPath); const allLernaPackages = lernaGetPackages(constants.monorepoRootPath);
const publicLernaPackages = _.filter(allLernaPackages, pkg => !pkg.package.private); const publicLernaPackages = _.filter(allLernaPackages, pkg => !pkg.package.private);
_.each(publicLernaPackages, lernaPackage => { for (const lernaPackage of publicLernaPackages) {
const changelogMdIfExists = getChangelogMdIfExists(lernaPackage.package.name, lernaPackage.location); const changelogMdIfExists = getChangelogMdIfExists(lernaPackage.package.name, lernaPackage.location);
if (_.isUndefined(changelogMdIfExists)) { if (_.isUndefined(changelogMdIfExists)) {
throw new Error(`${lernaPackage.package.name} should have CHANGELOG.md b/c it's public. Add one.`); throw new Error(`${lernaPackage.package.name} should have CHANGELOG.md b/c it's public. Add one.`);
@@ -76,9 +77,13 @@ const HEADER_PRAGMA = '##';
changelog.changes.push(changes); changelog.changes.push(changes);
} }
} }
const changelogJson = JSON.stringify(changelogs, null, '\t'); const changelogJson = JSON.stringify(changelogs);
fs.writeFileSync(`${lernaPackage.location}/CHANGELOG.json`, changelogJson); const changelogJsonPath = `${lernaPackage.location}/CHANGELOG.json`;
fs.writeFileSync(changelogJsonPath, changelogJson);
await execAsync(`prettier --write ${changelogJsonPath} --config .prettierrc`, {
cwd: constants.monorepoRootPath,
}); });
}
})().catch(err => { })().catch(err => {
utils.log(err.stdout); utils.log(err.stdout);
process.exit(1); process.exit(1);

View File

@@ -13,7 +13,7 @@ import { constants } from './constants';
import { Changelog, Changes, SemVerIndex, UpdatedPackage } from './types'; import { Changelog, Changes, SemVerIndex, UpdatedPackage } from './types';
import { utils } from './utils'; import { utils } from './utils';
const IS_DRY_RUN = process.env.IS_DRY_RUN === 'false'; const IS_DRY_RUN = process.env.IS_DRY_RUN === 'true';
const TODAYS_TIMESTAMP = moment().unix(); const TODAYS_TIMESTAMP = moment().unix();
const LERNA_EXECUTABLE = './node_modules/lerna/bin/lerna.js'; const LERNA_EXECUTABLE = './node_modules/lerna/bin/lerna.js';
const semverNameToIndex: { [semver: string]: number } = { const semverNameToIndex: { [semver: string]: number } = {
@@ -30,11 +30,11 @@ const semverNameToIndex: { [semver: string]: number } = {
const updatedPublicLernaPackages = _.filter(allLernaPackages, pkg => { const updatedPublicLernaPackages = _.filter(allLernaPackages, pkg => {
return _.includes(updatedPackageNames, pkg.package.name); return _.includes(updatedPackageNames, pkg.package.name);
}); });
const relevantPackageNames = _.map(updatedPublicLernaPackages, pkg => pkg.package.name); const updatedPublicLernaPackageNames = _.map(updatedPublicLernaPackages, pkg => pkg.package.name);
utils.log(`Will update CHANGELOGs and publish: \n${relevantPackageNames.join('\n')}\n`); utils.log(`Will update CHANGELOGs and publish: \n${updatedPublicLernaPackageNames.join('\n')}\n`);
const packageToVersionChange: { [name: string]: string } = {}; const packageToVersionChange: { [name: string]: string } = {};
_.each(updatedPublicLernaPackages, lernaPackage => { for (const lernaPackage of updatedPublicLernaPackages) {
const packageName = lernaPackage.package.name; const packageName = lernaPackage.package.name;
const changelogJSONPath = path.join(lernaPackage.location, 'CHANGELOG.json'); const changelogJSONPath = path.join(lernaPackage.location, 'CHANGELOG.json');
const changelogJSON = getChangelogJSONOrCreateIfMissing(lernaPackage.package.name, changelogJSONPath); const changelogJSON = getChangelogJSONOrCreateIfMissing(lernaPackage.package.name, changelogJSONPath);
@@ -77,14 +77,17 @@ const semverNameToIndex: { [semver: string]: number } = {
} }
// Save updated CHANGELOG.json // Save updated CHANGELOG.json
fs.writeFileSync(changelogJSONPath, JSON.stringify(changelogs, null, '\t')); fs.writeFileSync(changelogJSONPath, JSON.stringify(changelogs));
await execAsync(`prettier --write ${changelogJSONPath} --config .prettierrc`, {
cwd: constants.monorepoRootPath,
});
utils.log(`${packageName}: Updated CHANGELOG.json`); utils.log(`${packageName}: Updated CHANGELOG.json`);
// Generate updated CHANGELOG.md // Generate updated CHANGELOG.md
const changelogMd = generateChangelogMd(changelogs); const changelogMd = generateChangelogMd(changelogs);
const changelogMdPath = path.join(lernaPackage.location, 'CHANGELOG.md'); const changelogMdPath = path.join(lernaPackage.location, 'CHANGELOG.md');
fs.writeFileSync(changelogMdPath, changelogMd); fs.writeFileSync(changelogMdPath, changelogMd);
utils.log(`${packageName}: Updated CHANGELOG.md`); utils.log(`${packageName}: Updated CHANGELOG.md`);
}); }
if (!IS_DRY_RUN) { if (!IS_DRY_RUN) {
await execAsync(`git add . --all`, { cwd: constants.monorepoRootPath }); await execAsync(`git add . --all`, { cwd: constants.monorepoRootPath });