From 261a96f289f118369084ff2ba8cbb870dee95339 Mon Sep 17 00:00:00 2001 From: Yury Orlov Date: Fri, 7 Jul 2017 15:57:25 +0300 Subject: [PATCH] chore: publishing scripts (#188) --- package.json | 12 ++- scripts/gh-pages-files/.gitattributes | 13 ++++ scripts/gh-pages-files/shippable.yml | 3 + scripts/publish-npm.js | 108 ++++++++++++++++++++++++++ scripts/publish-site.js | 44 +++++++++++ 5 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 scripts/gh-pages-files/.gitattributes create mode 100644 scripts/gh-pages-files/shippable.yml create mode 100644 scripts/publish-npm.js create mode 100644 scripts/publish-site.js diff --git a/package.json b/package.json index 03f784b7be..d56ea99fdf 100644 --- a/package.json +++ b/package.json @@ -11,13 +11,17 @@ }, "license": "SEE LICENSE IN README.md", "devDependencies": { - "concurrently": "^3.4.0", + "conventional-changelog-cli": "^1.3.1", + "fs-extra": "^3.0.1", + "gh-pages": "^1.0.0", "gulp": "^3.9.1", "gulp-clean": "^0.3.2", "gulp-intercept": "^0.1.0", "gulp-rename": "^1.2.2", + "inquirer": "^3.1.1", "lerna": "2.0.0-rc.5", - "run-sequence": "^1.2.2" + "run-sequence": "^1.2.2", + "semver": "^5.3.0" }, "scripts": { "postinstall": "lerna bootstrap --concurrency=1", @@ -28,6 +32,8 @@ "watch": "lerna run --parallel build:watch", "lint": "lerna run lint", "lint:ci": "lerna run lint:ci", - "test": "lerna run test" + "test": "lerna run test", + "publish:npm": "node ./scripts/publish-npm.js", + "publish:site": "node ./scripts/publish-site.js" } } diff --git a/scripts/gh-pages-files/.gitattributes b/scripts/gh-pages-files/.gitattributes new file mode 100644 index 0000000000..f905ad26ed --- /dev/null +++ b/scripts/gh-pages-files/.gitattributes @@ -0,0 +1,13 @@ +* text eol=lf + +# images +*.gif binary +*.jpg binary +*.png binary +*.svg binary +*.ico binary + +# fonts +*.eot binary +*.ttf binary +*.woff binary diff --git a/scripts/gh-pages-files/shippable.yml b/scripts/gh-pages-files/shippable.yml new file mode 100644 index 0000000000..3ec8ec6656 --- /dev/null +++ b/scripts/gh-pages-files/shippable.yml @@ -0,0 +1,3 @@ +branches: + except: + - gh-pages diff --git a/scripts/publish-npm.js b/scripts/publish-npm.js new file mode 100644 index 0000000000..3cb0da3dba --- /dev/null +++ b/scripts/publish-npm.js @@ -0,0 +1,108 @@ +const { join } = require('path'); +const { readFileSync, writeFileSync } = require('fs'); +const { execSync } = require('child_process'); +const { prompt } = require('inquirer'); +const { valid, lt, inc, prerelease } = require('semver'); + +console.log('===================='); +console.log(`| Publishing npm packages`); +console.log('===================='); + +const currentVersion = require('../lerna.json').version; +const suggestedVersion = inc(currentVersion, (prerelease(currentVersion) !== null ? 'prerelease' : 'patch')); + +prompt({ + name: 'version', + message: `Enter new version [current: ${currentVersion}]:`, + default: suggestedVersion, + validate: (input) => { + if(valid(input) === null) + return 'Version is invalid'; + if(lt(input, currentVersion)) + return 'New version should be greate than current'; + + return true; + } +}) +.then(({ version }) => { + console.log(); + + console.log('Fetching the latest changes from upstream...'); + execSync('git checkout master'); + execSync('git fetch upstream master'); + execSync('git merge --ff-only'); + + console.log('Cleaning previous build result...'); + const rmScript = "require('fs-extra').removeSync(require('path').join(process.cwd(), 'dist'))"; + execSync(`./node_modules/.bin/lerna exec --loglevel silent -- node -e "\\"${rmScript}\\""`); + + console.log('Installing dependencies...'); + execSync('npm install'); + + console.log('Building...'); + execSync('npm run build'); + + console.log('Genereting CHANGELOG.md...'); + const changelogFile = join(process.cwd(), 'CHANGELOG.md'); + execSync('./node_modules/.bin/conventional-changelog -p angular -i CHANGELOG.md -s'); + writeFileSync( + changelogFile, + String(readFileSync(changelogFile)) + .replace('name=""', `name="${version}"`) + .replace('[](', `[${version}](`) + .replace('...v)', `...v${version})`), + ); + + prompt({ + message: 'Ready to publish. Please check build result and CHANGELOG.md. Is it ok?', + name: 'publishNpm', + type: 'confirm', + default: false, + }) + .then(({ publishNpm }) => { + if (!publishNpm) { + console.log('Npm publishing is aborted...'); + return; + } + + console.log('Login into npm...'); + try { execSync('npm logout', { stdio: 'ignore' }); } catch (e) {} + execSync('npm login', { stdio: 'inherit' }); + + console.log('Publishing npm...'); + execSync(`./node_modules/.bin/lerna publish --exact --force-publish * --repo-version ${version} --yes --skip-git`); + + console.log('Logout from npm...'); + execSync('npm logout', { stdio: 'ignore' }); + + console.log('Preparing pull request...'); + const commitMessage = `chore: publish ${version}`; + const branchName = `v${version.replace(/\./g, '-')}`; + execSync(`git checkout -b "${branchName}"`); + execSync('git add .'); + execSync(`git commit -m "${commitMessage}"`); + execSync(`git push origin ${branchName}`); + execSync(`git checkout master`); + + console.log('--------------------'); + console.log(`You have to pull request changes from branch ${branchName}!`); + console.log(`Don\'t forget to create a release on GitHub!`); + console.log('--------------------'); + + prompt({ + message: 'It is a good point to publish site. Proceed?', + name: 'publishSite', + type: 'confirm', + default: false, + }) + .then(({ publishSite }) => { + if (!publishSite) { + console.log('Site publishing is aborted...'); + return; + } + + execSync(`npm run publish:site`); + }); + }); +}); + diff --git a/scripts/publish-site.js b/scripts/publish-site.js new file mode 100644 index 0000000000..afcfe309c3 --- /dev/null +++ b/scripts/publish-site.js @@ -0,0 +1,44 @@ +const { join } = require('path'); +const { execSync } = require('child_process'); +const { publish } = require('gh-pages'); +const { copySync, removeSync } = require('fs-extra'); + + +const SITE_DIRECTORY = join(process.cwd(), 'site'); +const GENERATED_SITE_DIRECTORY = join(process.cwd(), 'site/_site'); +const REPO = 'devexpress/devextreme-reactive'; +const BRANCH = 'gh-pages'; +const COMMIT_MESSAGE = 'chore: update site'; + + +console.log('===================='); +console.log(`| Publishing site to ${REPO}@${BRANCH}`); +console.log('| Assume that repo is clean and up to date') +console.log('===================='); +console.log(); + +console.log('Building site content...'); +execSync('npm run build:site'); + +console.log('Cleaning generated site...'); +removeSync(GENERATED_SITE_DIRECTORY); + +console.log('Generating site...'); +execSync(`bundle exec jekyll build --source ${SITE_DIRECTORY} --destination ${GENERATED_SITE_DIRECTORY}`, { cwd: SITE_DIRECTORY }); + +console.log('Coping github staff...'); +copySync(join(__dirname, 'gh-pages-files'), GENERATED_SITE_DIRECTORY); + +console.log('Publishing...'); +publish(GENERATED_SITE_DIRECTORY, { + branch: BRANCH, + repo: `https://github.com/${REPO}.git`, + message: COMMIT_MESSAGE, + src: ['**/*', '.gitattributes'], +}, (err) => { + if (err) { + console.log(String(err)); + return; + } + console.log(`Published!`); +});