Skip to content

Commit

Permalink
feat(@angular/cli): adding git builds script
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitarora authored and hansl committed Jun 2, 2017
1 parent e72693a commit 93424b1
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ before_install:
script:
- if [[ "$SCRIPT" ]]; then npm run-script $SCRIPT; fi
- if [[ "$NODE_SCRIPT" ]]; then node $NODE_SCRIPT; fi

deploy:
provider: script
script: node scripts/git-builds.js
on:
branch: master
113 changes: 113 additions & 0 deletions scripts/git-builds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env node
'use strict';

/* eslint-disable no-console */
const spawn = require( 'child_process').spawnSync;
const fs = require('fs');
const temp = require('temp');
const chalk = require('chalk');

const outputPath = temp.mkdirSync('angular-cli-builds');
const cli = 'https://github.com/angular/angular-cli.git';
const cliBuilds = 'https://github.com/angular/cli-builds.git';
const ngToolsWebpackBuilds = 'https://github.com/angular/ngtools-webpack-builds.git';

function execute(command, cwd, ...args) {
return new Promise((resolve, reject) => {
const runCommand = spawn(command, args, { cwd });
if (runCommand.status === 0) {
console.log(chalk.gray(runCommand.output.toString()));
resolve(runCommand.output.toString());
} else {
reject({ message: runCommand.error || runCommand.stdout.toString() || runCommand.stderr.toString() });
}
});
}

function printMessage(message) {
console.log(chalk.green(`${message}\r\n`));
}

function updateDependencyPath(path, commitMessage) {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf-8', (readError, data) => {
if (readError) {
reject(readError);
} else {
let packageJSON = JSON.parse(data);
packageJSON.dependencies['@ngtools/webpack'] = `${ngToolsWebpackBuilds}#${commitMessage.substr(1, 7)}`;
fs.writeFile(path, JSON.stringify(packageJSON, null, 2), (writeError, updatedFile) => {
if (writeError) {
reject(writeError);
} else {
resolve(updatedFile);
}
});
}
});
});
}

function updateVersion(path, commitMessage) {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf-8', (readError, data) => {
if (readError) {
reject(readError);
} else {
let packageJSON = JSON.parse(data);
packageJSON.version = packageJSON.version + '-' + commitMessage.substr(1, 7);
fs.writeFile(path, JSON.stringify(packageJSON, null, 2), (writeError, updatedFile) => {
if (writeError) {
reject(writeError);
} else {
resolve(updatedFile);
}
});
}
});
});
}

function getCommitMessage(path) {
return execute('git', path, 'log', '--format=%h %s', '-n', 1)
.then(data => {
return data;
});
}

Promise.resolve()
.then(() => process.chdir(outputPath))
.then(() => console.log(process.cwd()))
.then(() => printMessage('Cloning...'))
.then(() => execute('git', process.cwd(), 'clone', cli))
.then(() => execute('git', process.cwd(), 'clone', cliBuilds))
.then(() => execute('git', process.cwd(), 'clone', ngToolsWebpackBuilds))
.then(() => printMessage('Installing packages...'))
.then(() => execute('npm', './angular-cli', 'install'))
.then(() => printMessage('Creating build...'))
.then(() => execute('npm', './angular-cli', 'run', 'build'))
//---------------------------- ngtools-webpack-builds ----------------------//
.then(() => printMessage('Copying ngtools-webpack-builds dist....'))
.then(() => execute('cp', './ngtools-webpack-builds', '-a', './../angular-cli/dist/@ngtools/webpack/.', '.'))
.then(() => printMessage('Updating package.json'))
.then(() => getCommitMessage('./angular-cli'))
.then((message) => updateVersion('./ngtools-webpack-builds/package.json', message))
.then(() => execute('git', './ngtools-webpack-builds', 'add', '-A'))
.then(() => getCommitMessage('./angular-cli'))
.then((message) => execute('git', './ngtools-webpack-builds', 'commit', '-am', message.substr(1)))
.then(() => execute('git', './ngtools-webpack-builds', 'push'))
//---------------------------- cli-builds ----------------------------------//
.then(() => printMessage('Copying cli-builds dist....'))
.then(() => execute('cp', './cli-builds', '-a', './../angular-cli/dist/@angular/cli/.', '.'))
.then(() => printMessage('Updating package.json'))
.then(() => getCommitMessage('./angular-cli'))
.then((message) => updateVersion('./cli-builds/package.json', message))
.then(() => getCommitMessage('./ngtools-webpack-builds'))
.then((message) => updateDependencyPath('./cli-builds/package.json', message))
.then(() => execute('git', './cli-builds', 'add', '-A'))
.then(() => getCommitMessage('./angular-cli'))
.then((message) => execute('git', './cli-builds', 'commit', '-am', message.substr(1)))
.then(() => execute('git', './cli-builds', 'push'))
//---------------------------- done ----------------------------------------//
.then(() => console.log('Done...'))
.catch(err => console.error(`Error:\n${err.message}`));

0 comments on commit 93424b1

Please sign in to comment.