|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +/* eslint-disable no-console */ |
| 5 | +const spawn = require( 'child_process').spawnSync; |
| 6 | +const fs = require('fs'); |
| 7 | +const temp = require('temp'); |
| 8 | +const chalk = require('chalk'); |
| 9 | + |
| 10 | +const outputPath = temp.mkdirSync('angular-cli-builds'); |
| 11 | +const cli = 'https://github.com/angular/angular-cli.git'; |
| 12 | +const cliBuilds = 'https://github.com/angular/cli-builds.git'; |
| 13 | +const ngToolsWebpackBuilds = 'https://github.com/angular/ngtools-webpack-builds.git'; |
| 14 | + |
| 15 | +function execute(command, cwd, ...args) { |
| 16 | + return new Promise((resolve, reject) => { |
| 17 | + const runCommand = spawn(command, args, { cwd }); |
| 18 | + if (runCommand.status === 0) { |
| 19 | + console.log(chalk.gray(runCommand.output.toString())); |
| 20 | + resolve(runCommand.output.toString()); |
| 21 | + } else { |
| 22 | + reject({ message: runCommand.error || runCommand.stdout.toString() || runCommand.stderr.toString() }); |
| 23 | + } |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +function printMessage(message) { |
| 28 | + console.log(chalk.green(`${message}\r\n`)); |
| 29 | +} |
| 30 | + |
| 31 | +function updateDependencyPath(path, commitMessage) { |
| 32 | + return new Promise((resolve, reject) => { |
| 33 | + fs.readFile(path, 'utf-8', (readError, data) => { |
| 34 | + if (readError) { |
| 35 | + reject(readError); |
| 36 | + } else { |
| 37 | + let packageJSON = JSON.parse(data); |
| 38 | + packageJSON.dependencies['@ngtools/webpack'] = `${ngToolsWebpackBuilds}#${commitMessage.substr(1, 7)}`; |
| 39 | + fs.writeFile(path, JSON.stringify(packageJSON, null, 2), (writeError, updatedFile) => { |
| 40 | + if (writeError) { |
| 41 | + reject(writeError); |
| 42 | + } else { |
| 43 | + resolve(updatedFile); |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | + }); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +function updateVersion(path, commitMessage) { |
| 52 | + return new Promise((resolve, reject) => { |
| 53 | + fs.readFile(path, 'utf-8', (readError, data) => { |
| 54 | + if (readError) { |
| 55 | + reject(readError); |
| 56 | + } else { |
| 57 | + let packageJSON = JSON.parse(data); |
| 58 | + packageJSON.version = packageJSON.version + '-' + commitMessage.substr(1, 7); |
| 59 | + fs.writeFile(path, JSON.stringify(packageJSON, null, 2), (writeError, updatedFile) => { |
| 60 | + if (writeError) { |
| 61 | + reject(writeError); |
| 62 | + } else { |
| 63 | + resolve(updatedFile); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + }); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +function getCommitMessage(path) { |
| 72 | + return execute('git', path, 'log', '--format=%h %s', '-n', 1) |
| 73 | + .then(data => { |
| 74 | + return data; |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +Promise.resolve() |
| 79 | + .then(() => process.chdir(outputPath)) |
| 80 | + .then(() => console.log(process.cwd())) |
| 81 | + .then(() => printMessage('Cloning...')) |
| 82 | + .then(() => execute('git', process.cwd(), 'clone', cli)) |
| 83 | + .then(() => execute('git', process.cwd(), 'clone', cliBuilds)) |
| 84 | + .then(() => execute('git', process.cwd(), 'clone', ngToolsWebpackBuilds)) |
| 85 | + .then(() => printMessage('Installing packages...')) |
| 86 | + .then(() => execute('npm', './angular-cli', 'install')) |
| 87 | + .then(() => printMessage('Creating build...')) |
| 88 | + .then(() => execute('npm', './angular-cli', 'run', 'build')) |
| 89 | + //---------------------------- ngtools-webpack-builds ----------------------// |
| 90 | + .then(() => printMessage('Copying ngtools-webpack-builds dist....')) |
| 91 | + .then(() => execute('cp', './ngtools-webpack-builds', '-a', './../angular-cli/dist/@ngtools/webpack/.', '.')) |
| 92 | + .then(() => printMessage('Updating package.json')) |
| 93 | + .then(() => getCommitMessage('./angular-cli')) |
| 94 | + .then((message) => updateVersion('./ngtools-webpack-builds/package.json', message)) |
| 95 | + .then(() => execute('git', './ngtools-webpack-builds', 'add', '-A')) |
| 96 | + .then(() => getCommitMessage('./angular-cli')) |
| 97 | + .then((message) => execute('git', './ngtools-webpack-builds', 'commit', '-am', message.substr(1))) |
| 98 | + .then(() => execute('git', './ngtools-webpack-builds', 'push')) |
| 99 | + //---------------------------- cli-builds ----------------------------------// |
| 100 | + .then(() => printMessage('Copying cli-builds dist....')) |
| 101 | + .then(() => execute('cp', './cli-builds', '-a', './../angular-cli/dist/@angular/cli/.', '.')) |
| 102 | + .then(() => printMessage('Updating package.json')) |
| 103 | + .then(() => getCommitMessage('./angular-cli')) |
| 104 | + .then((message) => updateVersion('./cli-builds/package.json', message)) |
| 105 | + .then(() => getCommitMessage('./ngtools-webpack-builds')) |
| 106 | + .then((message) => updateDependencyPath('./cli-builds/package.json', message)) |
| 107 | + .then(() => execute('git', './cli-builds', 'add', '-A')) |
| 108 | + .then(() => getCommitMessage('./angular-cli')) |
| 109 | + .then((message) => execute('git', './cli-builds', 'commit', '-am', message.substr(1))) |
| 110 | + .then(() => execute('git', './cli-builds', 'push')) |
| 111 | + //---------------------------- done ----------------------------------------// |
| 112 | + .then(() => console.log('Done...')) |
| 113 | + .catch(err => console.error(`Error:\n${err.message}`)); |
0 commit comments