build(aio): add support for using the locally built Angular packages for aio (#19600)

This commit allows building angular.io against the locally built Angular
packages. It adds two new npm scripts:

- `setup-local`: Same as `setup`, but overwrites the Angular packages for both
  angular.io and the examples boilerplate with the locally built ones.
- `build-local`: Same as `build`, but uses `setup-local` instead of `setup`
  under the hood, thus overwriting installed Angular packages with locally built
  ones.

Fixes #18611

PR Close #19600
This commit is contained in:
Georgios Kalpakas
2017-08-16 03:34:53 +03:00
committed by Chuck Jazdzewski
parent 62616f541a
commit 074a997302
8 changed files with 604 additions and 85 deletions

View File

@ -4,6 +4,8 @@ const path = require('canonical-path');
const shelljs = require('shelljs');
const yargs = require('yargs');
const ngPackagesInstaller = require('../ng-packages-installer');
const SHARED_PATH = path.resolve(__dirname, 'shared');
const SHARED_NODE_MODULES_PATH = path.resolve(SHARED_PATH, 'node_modules');
const BOILERPLATE_BASE_PATH = path.resolve(SHARED_PATH, 'boilerplate');
@ -27,27 +29,10 @@ const BOILERPLATE_TEST_PATHS = [
'karma.conf.js'
];
const ANGULAR_DIST_PATH = path.resolve(__dirname, '../../../dist');
const ANGULAR_PACKAGES_PATH = path.resolve(ANGULAR_DIST_PATH, 'packages-dist');
const ANGULAR_PACKAGES = [
'animations',
'common',
'compiler',
'compiler-cli',
'core',
'forms',
'http',
'platform-browser',
'platform-browser-dynamic',
'platform-server',
'router',
'upgrade',
];
const ANGULAR_TOOLS_PACKAGES_PATH = path.resolve(ANGULAR_DIST_PATH, 'tools', '@angular');
const ANGULAR_TOOLS_PACKAGES = [
'tsc-wrapped'
];
const EXAMPLE_CONFIG_FILENAME = 'example-config.json';
class ExampleBoilerPlate {
@ -57,14 +42,8 @@ class ExampleBoilerPlate {
* @param useLocal if true then overwrite the Angular library files with locally built ones
*/
add(useLocal) {
// first install the shared node_modules
this.installNodeModules(SHARED_PATH);
// Replace the Angular packages with those from the dist folder, if necessary
if (useLocal) {
ANGULAR_PACKAGES.forEach(packageName => this.overridePackage(ANGULAR_PACKAGES_PATH, packageName));
ANGULAR_TOOLS_PACKAGES.forEach(packageName => this.overridePackage(ANGULAR_TOOLS_PACKAGES_PATH, packageName));
}
// Install the shared `node_modules/` (if necessary overwrite Angular packages from npm with local ones).
this.installNodeModules(SHARED_PATH, useLocal);
// Get all the examples folders, indicated by those that contain a `example-config.json` file
const exampleFolders = this.getFoldersContaining(EXAMPLES_BASE_PATH, EXAMPLE_CONFIG_FILENAME, 'node_modules');
@ -103,15 +82,14 @@ class ExampleBoilerPlate {
.argv;
}
installNodeModules(basePath) {
installNodeModules(basePath, useLocal) {
shelljs.exec('yarn', {cwd: basePath});
}
overridePackage(basePath, packageName) {
const sourceFolder = path.resolve(basePath, packageName);
const destinationFolder = path.resolve(SHARED_NODE_MODULES_PATH, '@angular', packageName);
shelljs.rm('-rf', destinationFolder);
fs.copySync(sourceFolder, destinationFolder);
if (useLocal) {
ngPackagesInstaller.overwritePackages(basePath);
} else {
ngPackagesInstaller.restorePackages(basePath);
}
}
getFoldersContaining(basePath, filename, ignore) {

View File

@ -1,50 +1,54 @@
const exampleBoilerPlate = require('./example-boilerplate');
const shelljs = require('shelljs');
const path = require('canonical-path');
const fs = require('fs-extra');
const glob = require('glob');
const path = require('canonical-path');
const shelljs = require('shelljs');
const ngPackagesInstaller = require('../ng-packages-installer');
const exampleBoilerPlate = require('./example-boilerplate');
describe('example-boilerplate tool', () => {
describe('add', () => {
const sharedDir = path.resolve(__dirname, 'shared');
const sharedNodeModulesDir = path.resolve(sharedDir, 'node_modules');
const numberOfBoilerPlateFiles = 8;
const numberOfBoilerPlateTestFiles = 3;
const exampleFolders = ['a/b', 'c/d'];
beforeEach(() => {
spyOn(exampleBoilerPlate, 'installNodeModules');
spyOn(exampleBoilerPlate, 'overridePackage');
spyOn(exampleBoilerPlate, 'getFoldersContaining').and.returnValue(exampleFolders);
spyOn(fs, 'ensureSymlinkSync');
spyOn(exampleBoilerPlate, 'copyFile');
spyOn(exampleBoilerPlate, 'getFoldersContaining').and.returnValue(exampleFolders);
spyOn(exampleBoilerPlate, 'installNodeModules');
spyOn(exampleBoilerPlate, 'loadJsonFile').and.returnValue({});
});
it('should install the node modules', () => {
it('should install the npm dependencies into `sharedDir` (and pass the `useLocal` argument through)', () => {
exampleBoilerPlate.add();
expect(exampleBoilerPlate.installNodeModules).toHaveBeenCalledWith(path.resolve(__dirname, 'shared'));
});
expect(exampleBoilerPlate.installNodeModules).toHaveBeenCalledWith(sharedDir, undefined);
exampleBoilerPlate.installNodeModules.calls.reset();
it('should override the Angular node_modules with the locally built Angular packages if `useLocal` is true', () => {
const numberOfAngularPackages = 12;
const numberOfAngularToolsPackages = 1;
exampleBoilerPlate.add(true);
expect(exampleBoilerPlate.overridePackage).toHaveBeenCalledTimes(numberOfAngularPackages + numberOfAngularToolsPackages);
// for example
expect(exampleBoilerPlate.overridePackage).toHaveBeenCalledWith(path.resolve(__dirname, '../../../dist/packages-dist'), 'common');
expect(exampleBoilerPlate.overridePackage).toHaveBeenCalledWith(path.resolve(__dirname, '../../../dist/packages-dist'), 'core');
expect(exampleBoilerPlate.overridePackage).toHaveBeenCalledWith(path.resolve(__dirname, '../../../dist/tools/@angular'), 'tsc-wrapped');
expect(exampleBoilerPlate.installNodeModules).toHaveBeenCalledWith(sharedDir, true);
exampleBoilerPlate.installNodeModules.calls.reset();
exampleBoilerPlate.add(false);
expect(exampleBoilerPlate.installNodeModules).toHaveBeenCalledWith(sharedDir, false);
});
it('should process all the example folders', () => {
const examplesDir = path.resolve(__dirname, '../../content/examples');
exampleBoilerPlate.add();
expect(exampleBoilerPlate.getFoldersContaining).toHaveBeenCalledWith(path.resolve(__dirname, '../../content/examples'), 'example-config.json', 'node_modules');
expect(exampleBoilerPlate.getFoldersContaining)
.toHaveBeenCalledWith(examplesDir, 'example-config.json', 'node_modules');
});
it('should symlink the node_modules', () => {
exampleBoilerPlate.add();
expect(fs.ensureSymlinkSync).toHaveBeenCalledTimes(exampleFolders.length);
expect(fs.ensureSymlinkSync).toHaveBeenCalledWith(path.resolve(__dirname, 'shared/node_modules'), path.resolve('a/b/node_modules'));
expect(fs.ensureSymlinkSync).toHaveBeenCalledWith(path.resolve(__dirname, 'shared/node_modules'), path.resolve('c/d/node_modules'));
expect(fs.ensureSymlinkSync).toHaveBeenCalledWith(sharedNodeModulesDir, path.resolve('a/b/node_modules'));
expect(fs.ensureSymlinkSync).toHaveBeenCalledWith(sharedNodeModulesDir, path.resolve('c/d/node_modules'));
});
it('should copy all the source boilerplate files', () => {
@ -81,27 +85,33 @@ describe('example-boilerplate tool', () => {
});
describe('installNodeModules', () => {
it('should run `yarn` in the base path', () => {
beforeEach(() => {
spyOn(shelljs, 'exec');
spyOn(ngPackagesInstaller, 'overwritePackages');
spyOn(ngPackagesInstaller, 'restorePackages');
});
it('should run `yarn` in the base path', () => {
exampleBoilerPlate.installNodeModules('some/base/path');
expect(shelljs.exec).toHaveBeenCalledWith('yarn', { cwd: 'some/base/path' });
});
});
describe('overridePackage', () => {
beforeEach(() => {
spyOn(shelljs, 'rm');
spyOn(fs, 'copySync');
it('should overwrite the Angular packages if `useLocal` is true', () => {
ngPackagesInstaller.overwritePackages.and.callFake(() => expect(shelljs.exec).toHaveBeenCalled());
exampleBoilerPlate.installNodeModules('some/base/path', true);
expect(ngPackagesInstaller.overwritePackages).toHaveBeenCalledWith('some/base/path');
expect(ngPackagesInstaller.restorePackages).not.toHaveBeenCalled();
});
it('should remove the original package from the shared node_modules folder', () => {
exampleBoilerPlate.overridePackage('base/path', 'somePackage');
expect(shelljs.rm).toHaveBeenCalledWith('-rf', path.resolve(__dirname, 'shared/node_modules/@angular/somePackage'));
});
it('should restore the Angular packages if `useLocal` is not true', () => {
exampleBoilerPlate.installNodeModules('some/base/path1');
expect(ngPackagesInstaller.restorePackages).toHaveBeenCalledWith('some/base/path1');
it('should copy the source folder to the shared node_modules folder', () => {
exampleBoilerPlate.overridePackage('base/path', 'somePackage');
expect(fs.copySync).toHaveBeenCalledWith(path.resolve('base/path/somePackage'), path.resolve(__dirname, 'shared/node_modules/@angular/somePackage'));
exampleBoilerPlate.installNodeModules('some/base/path2', false);
expect(ngPackagesInstaller.restorePackages).toHaveBeenCalledWith('some/base/path2');
expect(ngPackagesInstaller.overwritePackages).not.toHaveBeenCalled();
});
});