Skip to content

Commit

Permalink
chore(ngrid-cypress): fix package build process
Browse files Browse the repository at this point in the history
  • Loading branch information
shlomiassaf committed Dec 3, 2020
1 parent 6ba59ee commit e064725
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 16 deletions.
16 changes: 15 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,16 @@
"tsConfig": "libs/ngrid-cypress/tsconfig.lib.json",
"project": "libs/ngrid-cypress/ng-package.json",
"tasks": {
"config": "tools/ng-packagr.transformers/ngrid.transformers.ts",
"config": "tools/ng-packagr.transformers/ngrid-cypress.transformers.ts",
"tsConfig": "tools/tsconfig.json",
"data": {
"copyFile": {
"assets": [
{
"glob": "**/*.d.ts",
"input": "libs/ngrid-cypress/src",
"output": "dist/@pebula/ngrid-cypress/"
},
{
"glob": "README.md",
"input": ".",
Expand All @@ -343,12 +348,21 @@
"output": "dist/@pebula/ngrid-cypress"
}
]
},
"nodeLib": {
"compilerOptions": {
"sourceMap": false,
"inlineSourceMap": true,
"inlineSources": true
}
}
}
}

},
"configurations": {
"production": {
"tsConfig": "libs/ngrid-cypress/tsconfig.lib.prod.json",
"project": "libs/ngrid-cypress/ng-package.prod.json"
}
}
Expand Down
22 changes: 21 additions & 1 deletion libs/ngrid-cypress/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
{
"name": "@pebula/ngrid-cypress",
"version": "2.3.0"
"version": "2.3.0",
"license": "MIT",
"description": "Cypress Test Helpers For @pebula/ngrid",
"repository": {
"type": "git",
"url": "https://github.com/shlomiassaf/ngrid.git"
},
"keywords": [
"ngrid",
"cypress",
"testing",
"e2e",
"angular",
"grid",
"table"
],
"author": "Shlomi Assaf",
"homepage": "https://github.com/shlomiassaf/ngrid",
"bugs": {
"url": "https://github.com/shlomiassaf/ngrid/issues"
}
}
20 changes: 10 additions & 10 deletions libs/ngrid-cypress/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"extends": "../../tsconfig.lib.json",
"compilerOptions": {
"outDir": "../../out-tsc",
"target": "es2015",
"module": "es2015",
"moduleResolution": "node",
"rootDir": ".",
"paths": {
"@pebula/*": ["dist/@pebula/*"]
},
"target": "es2016",
"module": "commonjs",
"types": ["cypress", "node"],
"declaration": true,
"sourceMap": true,
"inlineSources": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"types": [],
"lib": ["dom", "es2015"]
"inlineSourceMap": false,
"inlineSources": false,
"esModuleInterop": true,
},
"exclude": ["src/test-setup.ts", "**/*.spec.ts", "karma.conf.ts"]
}
3 changes: 0 additions & 3 deletions libs/ngrid-cypress/tsconfig.lib.prod.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.lib.json",
"compilerOptions": {
"declarationMap": false
},
"angularCompilerOptions": {
"enableIvy": false
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"ng": "nx",
"start": "ng serve",
"build-grid": "ng build ngrid --prod && ng build ngrid-material --prod && ng build ngrid-cypress -- prod",
"build-grid": "ng build ngrid --prod && ng build ngrid-material --prod && ng build ngrid-cypress --prod",
"test": "ng test",
"lint": "./node_modules/.bin/nx workspace-lint && ng lint",
"e2e": "ng e2e",
Expand Down
28 changes: 28 additions & 0 deletions tools/ng-packagr.transformers/ngrid-cypress.transformers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as path from 'path';
import * as fs from 'fs';
import { NgPackagerHooksContext, HookRegistry, EntryPointTaskContext } from 'ng-cli-packagr-tasks';
import { ensureUnixPath } from 'ng-packagr/lib/utils/path';

import { CopyFile } from 'ng-cli-packagr-tasks/dist/tasks/copy-file';
import { NodeLib } from 'ng-cli-packagr-tasks/dist/tasks/node-lib';

import { writePackage } from './tasks';

async function fixDtsReference(context: EntryPointTaskContext) {
const { entryPoint } = context.epNode.data;
const relativeUnixFromDestPath = (filePath: string) => ensureUnixPath(path.relative(entryPoint.destinationPath, filePath));

const actionsDtsPath = path.join(entryPoint.destinationPath, 'lib/ngrid-harness/actions.d.ts');
const content = fs.readFileSync(actionsDtsPath, { encoding: 'utf-8'})
.replace(`/// <reference types="./extend-cypress" />`, `/// <reference types="../extend-cypress" />`);
fs.writeFileSync(actionsDtsPath, content, { encoding: 'utf-8' });
}

module.exports = function(ctx: NgPackagerHooksContext, registry: HookRegistry) {
registry
.register(NodeLib)
.register('writePackage', { before: fixDtsReference })
.register('writePackage', { before: writePackage })
.register(CopyFile);
}

1 change: 1 addition & 0 deletions tools/ng-packagr.transformers/tasks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './write-package.json';
8 changes: 8 additions & 0 deletions tools/ng-packagr.transformers/tasks/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NgEntryPoint } from 'ng-packagr/lib/ng-package/entry-point/entry-point';
import * as path from 'path';

// TODO: This will map the entry file to it's emitted output path taking rootDir into account.
// It might not be fully accurate, consider using the compiler host to create a direct map.
export function getDistEntryFile(entryPoint: NgEntryPoint, rootDir: string) {
return path.join(entryPoint.destinationPath, path.relative(rootDir, entryPoint.entryFilePath)).replace(/\.ts$/, '.js');
}
106 changes: 106 additions & 0 deletions tools/ng-packagr.transformers/tasks/write-package.json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import { NgEntryPoint } from 'ng-packagr/lib/ng-package/entry-point/entry-point';
import { NgPackage } from 'ng-packagr/lib/ng-package/package';
import * as log from 'ng-packagr/lib/utils/log';
import { ensureUnixPath } from 'ng-packagr/lib/utils/path';
import { rimraf } from 'ng-packagr/lib/utils/rimraf';
import { EntryPointTaskContext } from 'ng-cli-packagr-tasks';
import { getDistEntryFile } from './utils';

export async function writePackage(context: EntryPointTaskContext) {
const { tsConfig, entryPoint } = context.epNode.data;
const ngPackage: NgPackage = context.graph.find(node => node.type === 'application/ng-package').data;

log.info('Writing package metadata');
const relativeUnixFromDestPath = (filePath: string) => ensureUnixPath(path.relative(entryPoint.destinationPath, filePath));

// TODO: This will map the entry file to it's emitted output path taking rootDir into account.
// It might not be fully accurate, consider using the compiler host to create a direct map.
const distEntryFile = getDistEntryFile(entryPoint, tsConfig.options.rootDir);
const distDtsEntryFile = distEntryFile.replace(/\.js$/, '.d.ts');

await writePackageJson(entryPoint, ngPackage, {
main: relativeUnixFromDestPath(distEntryFile),
typings: relativeUnixFromDestPath(distDtsEntryFile),
});

log.success(`Built ${entryPoint.moduleId}`);
}

async function writePackageJson(entryPoint: NgEntryPoint,
pkg: NgPackage,
additionalProperties: { [key: string]: string | boolean | string[] }): Promise<void> {
log.debug('Writing package.json');

// set additional properties
const packageJson = { ...entryPoint.packageJson, ...additionalProperties };

// Verify non-peerDependencies as they can easily lead to duplicate installs or version conflicts
// in the node_modules folder of an application
const whitelist = pkg.whitelistedNonPeerDependencies.map(value => new RegExp(value));
try {
checkNonPeerDependencies(packageJson, 'dependencies', whitelist);
} catch (e) {
await rimraf(entryPoint.destinationPath);
throw e;
}

// Removes scripts from package.json after build
if (packageJson.scripts) {
if (pkg.keepLifecycleScripts !== true) {
log.info(`Removing scripts section in package.json as it's considered a potential security vulnerability.`);
delete packageJson.scripts;
} else {
log.warn(
`You enabled keepLifecycleScripts explicitly. The scripts section in package.json will be published to npm.`,
);
}
}

// keep the dist package.json clean
// this will not throw if ngPackage field does not exist
delete packageJson.ngPackage;

const packageJsonPropertiesToDelete = [
'stylelint',
'prettier',
'browserslist',
'devDependencies',
'jest',
'workspaces',
'husky',
];

for (const prop of packageJsonPropertiesToDelete) {
if (prop in packageJson) {
delete packageJson[prop];
log.info(`Removing ${prop} section in package.json.`);
}
}

packageJson.name = entryPoint.moduleId;

// `outputJson()` creates intermediate directories, if they do not exist
// -- https://github.com/jprichardson/node-fs-extra/blob/master/docs/outputJson.md
await fs.outputJson(path.join(entryPoint.destinationPath, 'package.json'), packageJson, {
spaces: 2,
});
}

function checkNonPeerDependencies(packageJson: Record<string, unknown>, property: string, whitelist: RegExp[]) {
if (!packageJson[property]) {
return;
}

for (const dep of Object.keys(packageJson[property])) {
if (whitelist.find(regex => regex.test(dep))) {
log.debug(`Dependency ${dep} is whitelisted in '${property}'`);
} else {
log.warn(
`Distributing npm packages with '${property}' is not recommended. Please consider adding ${dep} to 'peerDependencies' or remove it from '${property}'.`,
);
throw new Error(`Dependency ${dep} must be explicitly whitelisted.`);
}
}
}

0 comments on commit e064725

Please sign in to comment.