Skip to content

Commit

Permalink
feat: create release script and refactor build
Browse files Browse the repository at this point in the history
  • Loading branch information
taixw2 committed Apr 27, 2020
1 parent ee64d60 commit 2488c34
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 51 deletions.
71 changes: 20 additions & 51 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,15 @@ const gzip = require('gzip-size');
const babelCore = require('@babel/core');
const react = require('react');
const reactDom = require('react-dom');

const packages = [
{
entry: '@dxjs/core',
global: 'Dx',
externals: [
{ entry: 'react', global: 'React' },
{ entry: 'react-dom', global: 'ReactDom' },
{ entry: 'redux', global: 'redux' },
{ entry: 'react-redux', global: 'reactRedux' },
{ entry: '@dxjs/common', global: 'DxCommon' },
{ entry: 'reflect-metadata', global: '' },
],
},
{
entry: '@dxjs/common',
global: 'DxCommon',
externals: [
{ entry: 'react', global: 'React' },
{ entry: 'react-dom', global: 'ReactDom' },
{ entry: 'react-redux', global: 'ReactRedux' },
{ entry: 'reflect-metadata', global: '' },
],
},
];

const UMD = 'UMD';
const UMD_DEV = 'UMD_DEV';
const CJS = 'CJS';
const CJS_DEV = 'CJS_DEV';

const bundleTypes = [UMD, UMD_DEV, CJS, CJS_DEV];
const bundles = require('./bundles');

function getPackgeFileName(packageName, bundleType) {
switch (bundleType) {
case CJS:
case UMD:
case bundles.CJS:
case bundles.UMD:
return `${packageName}.production.min.js`;
case UMD_DEV:
case CJS_DEV:
case bundles.bundles.UMD_DEV:
case bundles.bundles.CJS_DEV:
return `${packageName}.development.js`;
default:
//
Expand All @@ -63,11 +32,11 @@ function getPackgeFileName(packageName, bundleType) {

function getFormat(bundleType) {
switch (bundleType) {
case UMD_DEV:
case UMD:
case bundles.bundles.UMD_DEV:
case bundles.UMD:
return 'umd';
case CJS:
case CJS_DEV:
case bundles.CJS:
case bundles.bundles.CJS_DEV:
return 'cjs';
default:
//
Expand All @@ -82,11 +51,11 @@ function getOutoutPath(packageName, bundleType, filename) {

function getNodeEnv(bundleType) {
switch (bundleType) {
case CJS:
case UMD:
case bundles.CJS:
case bundles.UMD:
return 'production';
case UMD_DEV:
case CJS_DEV:
case bundles.bundles.UMD_DEV:
case bundles.bundles.CJS_DEV:
return 'development';
default:
//
Expand Down Expand Up @@ -129,7 +98,7 @@ async function createBundle(package, bundleType) {
commonjs({
namedExports: {
react: Object.keys(react),
'react-dom': Object.keys(reactDom)
'react-dom': Object.keys(reactDom),
},
}),
babel({
Expand Down Expand Up @@ -193,12 +162,12 @@ function copyResource() {
async function build() {
await rmfr('build');

for (let index = 0; index < packages.length; index++) {
const package = packages[index];
await createBundle(package, UMD);
await createBundle(package, UMD_DEV);
await createBundle(package, CJS);
await createBundle(package, CJS_DEV);
for (let index = 0; index < bundles.packages.length; index++) {
const package = bundles.packages[index];
await createBundle(package, bundles.UMD);
await createBundle(package, bundles.bundles.UMD_DEV);
await createBundle(package, bundles.CJS);
await createBundle(package, bundles.bundles.CJS_DEV);
}
// 遍历所有的包
await copyResource();
Expand Down
35 changes: 35 additions & 0 deletions scripts/bundles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
exports.packages = [
{
entry: '@dxjs/core',
global: 'Dx',
externals: [
{ entry: 'react', global: 'React' },
{ entry: 'react-dom', global: 'ReactDom' },
{ entry: 'redux', global: 'redux' },
{ entry: 'react-redux', global: 'reactRedux' },
{ entry: '@dxjs/common', global: 'DxCommon' },
{ entry: 'reflect-metadata', global: '' },
],
},
{
entry: '@dxjs/common',
global: 'DxCommon',
externals: [
{ entry: 'react', global: 'React' },
{ entry: 'react-dom', global: 'ReactDom' },
{ entry: 'react-redux', global: 'ReactRedux' },
{ entry: 'reflect-metadata', global: '' },
],
},
];

const UMD = 'UMD';
const UMD_DEV = 'UMD_DEV';
const CJS = 'CJS';
const CJS_DEV = 'CJS_DEV';

exports.bundleTypes = [UMD, UMD_DEV, CJS, CJS_DEV];
exports.UMD = UMD;
exports.UMD_DEV = UMD_DEV;
exports.CJS = CJS;
exports.CJS_DEV = CJS_DEV;
59 changes: 59 additions & 0 deletions scripts/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* 当前脚本会在 pre-push 的时候执行,
* 执行前判断 commit log 是否 release(release_type): xxx 的格式
* 如果是 commit type 是 release 则修改 package.json 并且打个 tag
*/

const fs = require('fs');
const path = require('path');
const semver = require('semver');
const { sync } = require('conventional-commits-parser');
const defaultChangelogOpts = require('conventional-changelog-angular');
const cp = require('child_process');

const CWD = process.cwd();

function formatCommitMessage() {
const commitLog = cp
.execSync('git log -n 1 --pretty=format:"%B"')
.toString('utf8')
.trim();

return sync(commitLog, defaultChangelogOpts);
}

function updateVersion(pkgPath) {
if (!fs.existsSync(pkgPath)) return;
const commit = formatCommitMessage();
if (commit.type.toLowerCase() !== 'release') return;

const packageJSON = JSON.parse(fs.readFileSync(pkgPath).toString('utf8'));
const currentVersion = packageJSON.version;
const nextVersion = semver.inc(currentVersion, commit.scope || 'patch');
fs.writeFileSync(pkgPath, JSON.stringify({ ...packageJSON, version: nextVersion }, null, 2));

return nextVersion;
}

function updatePackageVersion() {
fs.readdirSync('packages').forEach(name => {
const dirname = path.join(CWD, 'packages', name);
if (!fs.statSync(dirname).isDirectory) return;
updateVersion(path.join(dirname, 'package.json'));
});
}

function updateGlobalVersion() {
const version = updateVersion(path.join(CWD, 'package.json'));

cp.execSync('git tag v' + version);
}

function run() {
updateGlobalVersion();
updatePackageVersion();

cp.execSync('git add package.json && git add */**/package.json && git commit --amend --no-edit');
}

run();

0 comments on commit 2488c34

Please sign in to comment.