Skip to content

Commit

Permalink
Initial scripts to deal with problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Stone committed Jan 24, 2017
1 parent c96972c commit a804bcb
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 39 deletions.
24 changes: 12 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ language: node_js
node_js:
- '4'
- '6'
matrix:
include:
- env: SYSTEST=embedded
- env: SYSTEST=web
- env: SYSTEST=hlf SYSTEST_HLF=hlf
sudo: required
services:
- docker
- env: SYSTEST=hlf SYSTEST_HLF=ibm
sudo: required
services:
- docker
# matrix:
# include:
# - env: SYSTEST=embedded
# - env: SYSTEST=web
# - env: SYSTEST=hlf SYSTEST_HLF=hlf
# sudo: required
# services:
# - docker
# - env: SYSTEST=hlf SYSTEST_HLF=ibm
# sudo: required
# services:
# - docker
dist: trusty
before_install: |
set -ev
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
"main": "index.js",
"private": true,
"scripts": {
"postinstall": "npm run bootstrap",
"postinstall": "npm run pkgcheck && npm run bootstrap",
"bootstrap": "lerna bootstrap",
"test": "lerna run test",
"script": "./scripts/script.sh",
"deploy": "./scripts/deploy.sh",
"scanlicenses": "./scripts/scan-all-licenses.sh"
"scanlicenses": "./scripts/scan-all-licenses.sh",
"pkgcheck": "node ./scripts/pkgcheck.js",
"pkgstamp": "node ./scripts/pkgstamp.js",
"pkgbump": "node ./scripts/pkgbump.js && node ./scripts/pkgcheck.js --fix"
},
"repository": {
"type": "git",
Expand Down
33 changes: 25 additions & 8 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,31 @@ if [[ "${TRAVIS_REPO_SLUG}" != Blockchain-WW-Labs* ]]; then
exit 0
fi

# Determine the repository wide version.
export VERSION=$(node -e "console.log(require('$DIR/package.json').version)")

# If this is not for a tagged (release) build, set the prerelease version.
# Push the code to npm.
if [ -z "${TRAVIS_TAG}" ]; then
export TIMESTAMP=$(date +%Y%m%d%H%M%S)
export VERSION="${VERSION}-${TIMESTAMP}"
lerna publish --skip-git --npm-tag unstable --force-publish --yes --repo-version "${VERSION}" 2>&1 | tee

# Set the prerelease version.
npm run pkgstamp

# Publish with unstable tag. These are development builds.
echo "Pushing with tag unstable"
lerna exec --ignore '@ibm/concerto-systests' -- npm publish --scope=@ibm --tag=unstable 2>&1 | tee

else
lerna publish --skip-git --force-publish --yes --repo-version "${VERSION}" 2>&1 | tee

# Publish with latest tag (default). These are release builds.
echo "Pushing with tag latest"
lerna exec --ignore '@ibm/concerto-systests' -- npm publish --scope=@ibm 2>&1 | tee

# Bump the version number.
npm run pkgbump

# Push the changes back into GitHub.
git config user.name "Travis CI"
git config user.email "[email protected]"
git checkout develop
git add .
git commit -m "Automated version bump"
git push

fi
25 changes: 8 additions & 17 deletions scripts/timestamp.js → scripts/pkgbump.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@
'use strict';

const fs = require('fs');
const moment = require('moment');
const path = require('path');
const semver = require('semver');

if (process.argv.length !== 3) {
console.error('Usage: timestamp.js <package.json>');
process.exit(1);
}

let fileName = process.argv[2];
let fileContents = fs.readFileSync(fileName, 'utf8');
let file = JSON.parse(fileContents);

let timestamp = moment().format('YYYYMMDDHHmmss');

file.version = file.version.replace(/-.*/, '');
file.version += '-' + timestamp;

fileContents = JSON.stringify(file, null, 2);
fs.writeFileSync(fileName, fileContents, 'utf8');
const lernaDirectory = path.resolve('.');
const lernaConfigFile = path.resolve(lernaDirectory, 'lerna.json');
const lernaConfig = require(lernaConfigFile);
const targetVersion = semver.inc(lernaConfig.version, 'patch');
lernaConfig.version = targetVersion;
fs.writeFileSync(lernaConfigFile, JSON.stringify(lernaConfig, null, 4), 'utf8');
119 changes: 119 additions & 0 deletions scripts/pkgcheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env node
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const child_process = require('child_process');
const colors = require('colors');
const fs = require('fs');
const path = require('path');
const semver = require('semver')

const packages = {};
const fix = (process.argv.indexOf('--fix') !== -1);

const lernaDirectory = path.resolve('.');
const lernaConfigFile = path.resolve(lernaDirectory, 'lerna.json');
const lernaConfig = require(lernaConfigFile);
const targetVersion = lernaConfig.version;
const targetDependency = `^${targetVersion}`;
packages['lerna.json'] = lernaConfig;

if (!semver.valid(targetVersion)) {
console.error(`Error: the version "${targetVersion}" in "${lernaConfigFile}" is invalid!`);
process.exit(1);
}

const masterPackageFile = path.resolve(lernaDirectory, 'package.json');
const masterPackage = require(masterPackageFile);
packages['package.json'] = masterPackage;

const packagesDirectory = path.resolve(lernaDirectory, 'packages');
const packageNames = fs.readdirSync(packagesDirectory);
packageNames.forEach((packageName) => {
const packageFile = path.resolve(packagesDirectory, packageName, 'package.json');
const thisPackage = require(packageFile);
packages[packageName] = thisPackage;
});

let mismatch = false;
const badDependencies = {};
for (const i in packages) {
const currentPackage = packages[i];
if (targetVersion !== currentPackage.version) {
mismatch = true;
break;
}
}

for (const i in packages) {
const currentPackage = packages[i];
for (const j in packages) {
const otherPackage = packages[j];
for (const dependency in currentPackage.dependencies) {
const currentValue = currentPackage.dependencies[dependency];
if (dependency === otherPackage.name) {
if (currentValue !== targetDependency) {
if (!badDependencies[i]) {
badDependencies[i] = [];
}
badDependencies[i].push({ dependency: dependency, currentValue: currentValue });
mismatch = true;
}
}
}
for (const dependency in currentPackage.devDependencies) {
const currentValue = currentPackage.devDependencies[dependency];
if (dependency === otherPackage.name) {
if (currentValue !== targetDependency) {
if (!badDependencies[i]) {
badDependencies[i] = [];
}
badDependencies[i].push({ dependency: dependency, currentValue: currentValue });
mismatch = true;
}
}
}
}
}

if (mismatch && !fix) {
console.error('Error: there is a mismatch between the versions of the packages in this repository!\n');
for (const i in packages) {
const currentPackage = packages[i];
if (targetVersion !== currentPackage.version) {
console.error(` ${i} ${currentPackage.version.red} (should be ${targetVersion})`);
} else {
console.error(` ${i} ${currentPackage.version.green}`);
}
if (badDependencies[i]) {
badDependencies[i].forEach((badDependency) => {
console.error(` ${badDependency.dependency}@${badDependency.currentValue.red} (should be ${targetDependency})`);
});
}
}
console.error('\n');
console.error(`Run "scripts/pkgcheck.js --fix" inside the directory "${lernaDirectory}" to resolve this problem and change the version to "${targetVersion}"`);
process.exit(1);
} else if (mismatch && fix) {
const command = `lerna publish --skip-git --skip-npm --yes --repo-version ${targetVersion}`;
console.warn(`Status: running command ${command} to fix problems ...`)
child_process.execSync(command);
console.warn(`Status: modifying "${masterPackageFile} to fix problems ...`);
masterPackage.version = targetVersion;
fs.writeFileSync(masterPackageFile, JSON.stringify(masterPackage, null, 4), 'utf8');
} else {
console.log('Status: no problems detected!');
}
66 changes: 66 additions & 0 deletions scripts/pkgstamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env node
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const fs = require('fs');
const moment = require('moment');
const path = require('path');

const timestamp = moment().format('YYYYMMDDHHmmss');

const lernaDirectory = path.resolve('.');
const lernaConfigFile = path.resolve(lernaDirectory, 'lerna.json');
const lernaConfig = require(lernaConfigFile);
lernaConfig.version.replace(/-.*/, '');
const targetVersion = lernaConfig.version + '-' + timestamp;
lernaConfig.version = targetVersion;
fs.writeFileSync(lernaConfigFile, JSON.stringify(lernaConfig, null, 4), 'utf8');

const masterPackageFile = path.resolve(lernaDirectory, 'package.json');
const masterPackage = require(masterPackageFile);
masterPackage.version = targetVersion;
fs.writeFileSync(masterPackageFile, JSON.stringify(masterPackage, null, 4), 'utf8');

const packagesDirectory = path.resolve(lernaDirectory, 'packages');
const packageNames = fs.readdirSync(packagesDirectory);
const packages = {};
packageNames.forEach((packageName) => {
const packageFile = path.resolve(packagesDirectory, packageName, 'package.json');
const thisPackage = require(packageFile);
thisPackage.version = targetVersion;
packages[packageName] = thisPackage;
});

for (const i in packages) {
const currentPackage = packages[i];
for (const j in packages) {
const otherPackage = packages[j];
for (const dependency in currentPackage.dependencies) {
const currentValue = currentPackage.dependencies[dependency];
if (dependency === otherPackage.name) {
currentPackage.dependencies[dependency] = targetVersion;
}
}
for (const dependency in currentPackage.devDependencies) {
const currentValue = currentPackage.devDependencies[dependency];
if (dependency === otherPackage.name) {
currentPackage.devDependencies[dependency] = targetVersion;
}
}
}
const packageFile = path.resolve(packagesDirectory, i, 'package.json');
fs.writeFileSync(packageFile, JSON.stringify(currentPackage, null, 4), 'utf8');
}

0 comments on commit a804bcb

Please sign in to comment.