Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch latest NPM package version except of hardcoding those values #161

Merged
merged 5 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/schematics/library-versions.ts

This file was deleted.

10 changes: 9 additions & 1 deletion src/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,22 @@ describe('ng-add', () => {
expect(tree.files).toBeDefined();
});

test('should add single-spa dependency', async () => {
test('should add single-spa to dependencies', async () => {
const tree = await testRunner
.runSchematicAsync<NgAddOptions>('ng-add', {}, defaultAppTree)
.toPromise();
const packageJSON = JSON.parse(getFileContent(tree, '/package.json'));
expect(packageJSON.dependencies['single-spa-angular']).toBeDefined();
});

test('should add @angular-builders/custom-webpack to devDependencies', async () => {
const tree = await testRunner
.runSchematicAsync<NgAddOptions>('ng-add', {}, defaultAppTree)
.toPromise();
const packageJSON = JSON.parse(getFileContent(tree, '/package.json'));
expect(packageJSON.devDependencies['@angular-builders/custom-webpack']).toBeDefined();
});

test('should add main-single-spa.ts', async () => {
const tree = await testRunner
.runSchematicAsync<NgAddOptions>('ng-add', {}, defaultAppTree)
Expand Down
30 changes: 17 additions & 13 deletions src/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import {
applyTemplates,
SchematicsException,
} from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';

import { addPackageJsonDependency } from '@schematics/angular/utility/dependencies';
import { getWorkspace, getWorkspacePath } from '@schematics/angular/utility/config';
import { normalize, join } from 'path';
import { WorkspaceProject, Builders, BrowserBuilderOptions } from '@schematics/angular/utility/workspace-models';

import { normalize, join } from 'path';

import { Schema as NgAddOptions } from './schema';
import * as versions from '../library-versions';
import { addPackageToPackageJson } from '../utils';
import { getSingleSpaAngularDependency, getAngularBuildersCustomWebpackDependency } from './npm';

interface CustomWebpackBuilderOptions extends BrowserBuilderOptions {
customWebpackConfig: {
Expand All @@ -28,22 +29,25 @@ interface CustomWebpackBuilderOptions extends BrowserBuilderOptions {

export default function (options: NgAddOptions): Rule {
return chain([
addDependencies(options),
addDependencies(),
createMainEntry(options),
updateConfiguration(options),
addNPMScripts(options),
]);
}

export function addDependencies(options: NgAddOptions) {
return (host: Tree, context: SchematicContext) => {
addPackageToPackageJson(host, 'single-spa-angular', versions.singleSpaAngular);
if (atLeastAngular8()) {
addPackageToPackageJson(host, '@angular-builders/custom-webpack', versions.angularBuilderCustomWebpack);
export function addDependencies(): Rule {
const dependencies = [
getSingleSpaAngularDependency(),
getAngularBuildersCustomWebpackDependency(),
];

return async (tree: Tree, context: SchematicContext) => {
for await (const dependency of dependencies) {
addPackageJsonDependency(tree, dependency);
context.logger.info(`Added '${dependency.name}' as a dependency`);
}
context.addTask(new NodePackageInstallTask());
context.logger.info(`Added 'single-spa-angular' as a dependency`);
}
};
}

export function createMainEntry(options: NgAddOptions): Rule {
Expand Down
78 changes: 78 additions & 0 deletions src/schematics/ng-add/npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import * as http from 'http';
import { NodeDependencyType, NodeDependency } from '@schematics/angular/utility/dependencies';

const LATEST_COMPATIBLE_CUSTOM_WEBPACK_VERSIONS = {
// `8.4.1` is the latest version of `@angular-builders/custom-webpack`
// that is compatible with Angular 8 and will not change anymore because
// 9 version is in active development. We will need to consider choosing
// `@angular-builders/custom-webpack` based on the currently used Angular
// version.
8: '8.4.1',
};

/**
* Actually, there is the `latest-version` package that does the
* same stuff, but we'd want these schematics to be lightweight and
* less dependent on other packages.
*/
function getLatestNodeVersion(name: string) {
return new Promise<string>(resolve => {
const defaultVersion = 'latest';

const req = http.get(`http://registry.npmjs.org/${name}`, res => {
const chunks: Uint8Array[] = [];

res.on('data', chunk => {
chunks.push(chunk);
});

res.on('end', () => {
try {
const body = Buffer.concat(chunks).toString();
const json = JSON.parse(body);
const tags = json['dist-tags'];
resolve(tags.latest);
} catch {
resolve(defaultVersion);
}
});
});

req.on('error', () => resolve(defaultVersion));
req.end();
});
}

/**
* We're interested in installing the latest `single-spa-angular` version.
*/
export async function getSingleSpaAngularDependency(): Promise<NodeDependency> {
const name = 'single-spa-angular';
const version = await getLatestNodeVersion(name);
arturovt marked this conversation as resolved.
Show resolved Hide resolved

return {
name,
version,
overwrite: false,
type: NodeDependencyType.Default,
};
}

/**
* We have to install `@angular-builders/custom-webpack` version compatible with the current
* version of Angular. If Angular is 8 then `custom-webpack@8.4.1` has to be installed.
*/
export async function getAngularBuildersCustomWebpackDependency(): Promise<NodeDependency> {
const { VERSION } = require('@angular/core');
const name = '@angular-builders/custom-webpack';

const version =
LATEST_COMPATIBLE_CUSTOM_WEBPACK_VERSIONS[VERSION.major] || (await getLatestNodeVersion(name));
arturovt marked this conversation as resolved.
Show resolved Hide resolved

return {
name,
version,
overwrite: false,
type: NodeDependencyType.Dev,
};
}
31 changes: 0 additions & 31 deletions src/schematics/utils.ts

This file was deleted.