-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ngrid-cypress): fix package build process
- Loading branch information
1 parent
6ba59ee
commit e064725
Showing
9 changed files
with
190 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tools/ng-packagr.transformers/ngrid-cypress.transformers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './write-package.json'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
106
tools/ng-packagr.transformers/tasks/write-package.json.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`); | ||
} | ||
} | ||
} |